Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import collections 

2import inspect 

3import sys 

4 

5py2k = sys.version_info < (3, 0) 

6py3k = sys.version_info >= (3, 0) 

7py32 = sys.version_info >= (3, 2) 

8py27 = sys.version_info >= (2, 7) 

9jython = sys.platform.startswith("java") 

10win32 = sys.platform.startswith("win") 

11 

12try: 

13 import threading 

14except ImportError: 

15 import dummy_threading as threading # noqa 

16 

17FullArgSpec = collections.namedtuple( 

18 "FullArgSpec", 

19 [ 

20 "args", 

21 "varargs", 

22 "varkw", 

23 "defaults", 

24 "kwonlyargs", 

25 "kwonlydefaults", 

26 "annotations", 

27 ], 

28) 

29 

30ArgSpec = collections.namedtuple( 

31 "ArgSpec", ["args", "varargs", "keywords", "defaults"] 

32) 

33 

34 

35def inspect_getfullargspec(func): 

36 """Fully vendored version of getfullargspec from Python 3.3.""" 

37 

38 if inspect.ismethod(func): 

39 func = func.__func__ 

40 if not inspect.isfunction(func): 

41 raise TypeError("{!r} is not a Python function".format(func)) 

42 

43 co = func.__code__ 

44 if not inspect.iscode(co): 

45 raise TypeError("{!r} is not a code object".format(co)) 

46 

47 nargs = co.co_argcount 

48 names = co.co_varnames 

49 nkwargs = co.co_kwonlyargcount if py3k else 0 

50 args = list(names[:nargs]) 

51 kwonlyargs = list(names[nargs : nargs + nkwargs]) 

52 

53 nargs += nkwargs 

54 varargs = None 

55 if co.co_flags & inspect.CO_VARARGS: 

56 varargs = co.co_varnames[nargs] 

57 nargs = nargs + 1 

58 varkw = None 

59 if co.co_flags & inspect.CO_VARKEYWORDS: 

60 varkw = co.co_varnames[nargs] 

61 

62 return FullArgSpec( 

63 args, 

64 varargs, 

65 varkw, 

66 func.__defaults__, 

67 kwonlyargs, 

68 func.__kwdefaults__ if py3k else None, 

69 func.__annotations__ if py3k else {}, 

70 ) 

71 

72 

73def inspect_getargspec(func): 

74 return ArgSpec(*inspect_getfullargspec(func)[0:4]) 

75 

76 

77if py3k: # pragma: no cover 

78 string_types = (str,) 

79 text_type = str 

80 string_type = str 

81 

82 if py32: 

83 callable = callable # noqa 

84 else: 

85 

86 def callable(fn): # noqa 

87 return hasattr(fn, "__call__") 

88 

89 def u(s): 

90 return s 

91 

92 def ue(s): 

93 return s 

94 

95 import configparser 

96 import io 

97 import _thread as thread 

98else: 

99 # Using noqa bellow due to tox -e pep8 who use 

100 # python3.7 as the default interpreter 

101 string_types = (basestring,) # noqa 

102 text_type = unicode # noqa 

103 string_type = str 

104 

105 def u(s): 

106 return unicode(s, "utf-8") # noqa 

107 

108 def ue(s): 

109 return unicode(s, "unicode_escape") # noqa 

110 

111 import ConfigParser as configparser # noqa 

112 import StringIO as io # noqa 

113 

114 callable = callable # noqa 

115 import thread # noqa 

116 

117 

118if py3k or jython: 

119 import pickle 

120else: 

121 import cPickle as pickle # noqa 

122 

123if py3k: 

124 

125 def read_config_file(config, fileobj): 

126 return config.read_file(fileobj) 

127 

128 

129else: 

130 

131 def read_config_file(config, fileobj): 

132 return config.readfp(fileobj) 

133 

134 

135def timedelta_total_seconds(td): 

136 if py27: 

137 return td.total_seconds() 

138 else: 

139 return ( 

140 td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6 

141 ) / 1e6