8000 Rollback changes in decorator.py to 4.0.10 decorator module state · draftcode/client_python@3b49279 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b49279

Browse files
committed
Rollback changes in decorator.py to 4.0.10 decorator module state
Signed-off-by: rafsaf <rafal.safin12@gmail.com>
1 parent acad5e2 commit 3b49279

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

prometheus_client/decorator.py

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,40 @@
3535

3636
import collections
3737
import inspect
38-
from inspect import getfullargspec
3938
import itertools
4039
import operator
4140
import re
4241
import sys
4342

4443
__version__ = '4.0.10'
4544

45+
if sys.version_info >= (3,):
46+
from inspect import getfullargspec
4647

47-
def get_init(cls):
48-
return cls.__init__
4948

49+
def get_init(cls):
50+
return cls.__init__
51+
else:
52+
class getfullargspec(object):
53+
"A quick and dirty replacement for getfullargspec for Python 2.X"
54+
55+
def __init__(self, f):
56+
self.args, self.varargs, self.varkw, self.defaults = \
57+
inspect.getargspec(f)
58+
self.kwonlyargs = []
59+
self.kwonlydefaults = None
60+
61+
def __iter__(self):
62+
yield self.args
63+
yield self.varargs
64+
yield self.varkw
65+
yield self.defaults
66+
67+
getargspec = inspect.getargspec
68+
69+
70+
def get_init(cls):
71+
return cls.__init__.__func__
5072

5173
# getargspec has been deprecated in Python 3.5
5274
ArgSpec = collections.namedtuple(
@@ -91,21 +113,26 @@ def __init__(self, func=None, name=None, signature=None,
91113
setattr(self, a, getattr(argspec, a))
92114
for i, arg in enumerate(self.args):
93115
setattr(self, 'arg%d' % i, arg)
94-
allargs = list(self.args)
95-
allshortargs = list(self.args)
96-
if self.varargs:
97-
allargs.append('*' + self.varargs)
98-
allshortargs.append('*' + self.varargs)
99-
elif self.kwonlyargs:
100-
allargs.append('*') # single star syntax
101-
for a in self.kwonlyargs:
102-
allargs.append('%s=None' % a)
103-
allshortargs.append('%s=%s' % (a, a))
104-
if self.varkw:
105-
allargs.append('**' + self.varkw)
106-
allshortargs.append('**' + self.varkw)
107-
self.signature = ', '.join(allargs)
108-
self.shortsignature = ', '.join(allshortargs)
116+
if sys.version_info < (3,): # easy way
117+
self.shortsignature = self.signature = (
118+
inspect.formatargspec(
119+
formatvalue=lambda val: "", *argspec)[1:-1])
120+
else: # Python 3 way
121+
allargs = list(self.args)
122+
allshortargs = list(self.args)
123+
if self.varargs:
124+
allargs.append('*' + self.varargs)
125+
allshortargs.append('*' + self.varargs)
126+
elif self.kwonlyargs:
127+
allargs.append('*') # single star syntax
128+
for a in self.kwonlyargs:
129+
allargs.append('%s=None' % a)
130+
allshortargs.append('%s=%s' % (a, a))
131+
if self.varkw:
132+
allargs.append('**' + self.varkw)
133+
allshortargs.append('**' + self.varkw)
134+
self.signature = ', '.join(allargs)
135+
self.shortsignature = ', '.join(allshortargs)
109136
self.dict = func.__dict__.copy()
110137
# func=None happens when decorating a caller
111138
if name:

0 commit comments

Comments
 (0)
0