inspect.py 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049
  1. """Get useful information from live Python objects.
  2. This module encapsulates the interface provided by the internal special
  3. attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion.
  4. It also provides some help for examining source code and class layout.
  5. Here are some of the useful functions provided by this module:
  6. ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(),
  7. isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(),
  8. isroutine() - check object types
  9. getmembers() - get members of an object that satisfy a given condition
  10. getfile(), getsourcefile(), getsource() - find an object's source code
  11. getdoc(), getcomments() - get documentation on an object
  12. getmodule() - determine the module that an object came from
  13. getclasstree() - arrange classes so as to represent their hierarchy
  14. getargspec(), getargvalues(), getcallargs() - get info about function arguments
  15. getfullargspec() - same, with support for Python 3 features
  16. formatargspec(), formatargvalues() - format an argument spec
  17. getouterframes(), getinnerframes() - get info about frames
  18. currentframe() - get the current stack frame
  19. stack(), trace() - get info about frames on the stack or in a traceback
  20. signature() - get a Signature object for the callable
  21. """
  22. # This module is in the public domain. No warranties.
  23. __author__ = ('Ka-Ping Yee <ping@lfw.org>',
  24. 'Yury Selivanov <yselivanov@sprymix.com>')
  25. import ast
  26. import dis
  27. import collections.abc
  28. import enum
  29. import importlib.machinery
  30. import itertools
  31. import linecache
  32. import os
  33. import re
  34. import sys
  35. import tokenize
  36. import token
  37. import types
  38. import warnings
  39. import functools
  40. import builtins
  41. from operator import attrgetter
  42. from collections import namedtuple, OrderedDict
  43. # Create constants for the compiler flags in Include/code.h
  44. # We try to get them from dis to avoid duplication
  45. mod_dict = globals()
  46. for k, v in dis.COMPILER_FLAG_NAMES.items():
  47. mod_dict["CO_" + v] = k
  48. # See Include/object.h
  49. TPFLAGS_IS_ABSTRACT = 1 << 20
  50. # ----------------------------------------------------------- type-checking
  51. def ismodule(object):
  52. """Return true if the object is a module.
  53. Module objects provide these attributes:
  54. __cached__ pathname to byte compiled file
  55. __doc__ documentation string
  56. __file__ filename (missing for built-in modules)"""
  57. return isinstance(object, types.ModuleType)
  58. def isclass(object):
  59. """Return true if the object is a class.
  60. Class objects provide these attributes:
  61. __doc__ documentation string
  62. __module__ name of module in which this class was defined"""
  63. return isinstance(object, type)
  64. def ismethod(object):
  65. """Return true if the object is an instance method.
  66. Instance method objects provide these attributes:
  67. __doc__ documentation string
  68. __name__ name with which this method was defined
  69. __func__ function object containing implementation of method
  70. __self__ instance to which this method is bound"""
  71. return isinstance(object, types.MethodType)
  72. def ismethoddescriptor(object):
  73. """Return true if the object is a method descriptor.
  74. But not if ismethod() or isclass() or isfunction() are true.
  75. This is new in Python 2.2, and, for example, is true of int.__add__.
  76. An object passing this test has a __get__ attribute but not a __set__
  77. attribute, but beyond that the set of attributes varies. __name__ is
  78. usually sensible, and __doc__ often is.
  79. Methods implemented via descriptors that also pass one of the other
  80. tests return false from the ismethoddescriptor() test, simply because
  81. the other tests promise more -- you can, e.g., count on having the
  82. __func__ attribute (etc) when an object passes ismethod()."""
  83. if isclass(object) or ismethod(object) or isfunction(object):
  84. # mutual exclusion
  85. return False
  86. tp = type(object)
  87. return hasattr(tp, "__get__") and not hasattr(tp, "__set__")
  88. def isdatadescriptor(object):
  89. """Return true if the object is a data descriptor.
  90. Data descriptors have both a __get__ and a __set__ attribute. Examples are
  91. properties (defined in Python) and getsets and members (defined in C).
  92. Typically, data descriptors will also have __name__ and __doc__ attributes
  93. (properties, getsets, and members have both of these attributes), but this
  94. is not guaranteed."""
  95. if isclass(object) or ismethod(object) or isfunction(object):
  96. # mutual exclusion
  97. return False
  98. tp = type(object)
  99. return hasattr(tp, "__set__") and hasattr(tp, "__get__")
  100. if hasattr(types, 'MemberDescriptorType'):
  101. # CPython and equivalent
  102. def ismemberdescriptor(object):
  103. """Return true if the object is a member descriptor.
  104. Member descriptors are specialized descriptors defined in extension
  105. modules."""
  106. return isinstance(object, types.MemberDescriptorType)
  107. else:
  108. # Other implementations
  109. def ismemberdescriptor(object):
  110. """Return true if the object is a member descriptor.
  111. Member descriptors are specialized descriptors defined in extension
  112. modules."""
  113. return False
  114. if hasattr(types, 'GetSetDescriptorType'):
  115. # CPython and equivalent
  116. def isgetsetdescriptor(object):
  117. """Return true if the object is a getset descriptor.
  118. getset descriptors are specialized descriptors defined in extension
  119. modules."""
  120. return isinstance(object, types.GetSetDescriptorType)
  121. else:
  122. # Other implementations
  123. def isgetsetdescriptor(object):
  124. """Return true if the object is a getset descriptor.
  125. getset descriptors are specialized descriptors defined in extension
  126. modules."""
  127. return False
  128. def isfunction(object):
  129. """Return true if the object is a user-defined function.
  130. Function objects provide these attributes:
  131. __doc__ documentation string
  132. __name__ name with which this function was defined
  133. __code__ code object containing compiled function bytecode
  134. __defaults__ tuple of any default values for arguments
  135. __globals__ global namespace in which this function was defined
  136. __annotations__ dict of parameter annotations
  137. __kwdefaults__ dict of keyword only parameters with defaults"""
  138. return isinstance(object, types.FunctionType)
  139. def isgeneratorfunction(object):
  140. """Return true if the object is a user-defined generator function.
  141. Generator function objects provides same attributes as functions.
  142. See help(isfunction) for attributes listing."""
  143. return bool((isfunction(object) or ismethod(object)) and
  144. object.__code__.co_flags & CO_GENERATOR)
  145. def iscoroutinefunction(object):
  146. """Return true if the object is a coroutine function.
  147. Coroutine functions are defined with "async def" syntax,
  148. or generators decorated with "types.coroutine".
  149. """
  150. return bool((isfunction(object) or ismethod(object)) and
  151. object.__code__.co_flags & CO_COROUTINE)
  152. def isgenerator(object):
  153. """Return true if the object is a generator.
  154. Generator objects provide these attributes:
  155. __iter__ defined to support iteration over container
  156. close raises a new GeneratorExit exception inside the
  157. generator to terminate the iteration
  158. gi_code code object
  159. gi_frame frame object or possibly None once the generator has
  160. been exhausted
  161. gi_running set to 1 when generator is executing, 0 otherwise
  162. next return the next item from the container
  163. send resumes the generator and "sends" a value that becomes
  164. the result of the current yield-expression
  165. throw used to raise an exception inside the generator"""
  166. return isinstance(object, types.GeneratorType)
  167. def iscoroutine(object):
  168. """Return true if the object is a coroutine."""
  169. return isinstance(object, types.CoroutineType)
  170. def isawaitable(object):
  171. """Return true is object can be passed to an ``await`` expression."""
  172. return (isinstance(object, types.CoroutineType) or
  173. isinstance(object, types.GeneratorType) and
  174. object.gi_code.co_flags & CO_ITERABLE_COROUTINE or
  175. isinstance(object, collections.abc.Awaitable))
  176. def istraceback(object):
  177. """Return true if the object is a traceback.
  178. Traceback objects provide these attributes:
  179. tb_frame frame object at this level
  180. tb_lasti index of last attempted instruction in bytecode
  181. tb_lineno current line number in Python source code
  182. tb_next next inner traceback object (called by this level)"""
  183. return isinstance(object, types.TracebackType)
  184. def isframe(object):
  185. """Return true if the object is a frame object.
  186. Frame objects provide these attributes:
  187. f_back next outer frame object (this frame's caller)
  188. f_builtins built-in namespace seen by this frame
  189. f_code code object being executed in this frame
  190. f_globals global namespace seen by this frame
  191. f_lasti index of last attempted instruction in bytecode
  192. f_lineno current line number in Python source code
  193. f_locals local namespace seen by this frame
  194. f_trace tracing function for this frame, or None"""
  195. return isinstance(object, types.FrameType)
  196. def iscode(object):
  197. """Return true if the object is a code object.
  198. Code objects provide these attributes:
  199. co_argcount number of arguments (not including * or ** args)
  200. co_code string of raw compiled bytecode
  201. co_consts tuple of constants used in the bytecode
  202. co_filename name of file in which this code object was created
  203. co_firstlineno number of first line in Python source code
  204. co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg
  205. co_lnotab encoded mapping of line numbers to bytecode indices
  206. co_name name with which this code object was defined
  207. co_names tuple of names of local variables
  208. co_nlocals number of local variables
  209. co_stacksize virtual machine stack space required
  210. co_varnames tuple of names of arguments and local variables"""
  211. return isinstance(object, types.CodeType)
  212. def isbuiltin(object):
  213. """Return true if the object is a built-in function or method.
  214. Built-in functions and methods provide these attributes:
  215. __doc__ documentation string
  216. __name__ original name of this function or method
  217. __self__ instance to which a method is bound, or None"""
  218. return isinstance(object, types.BuiltinFunctionType)
  219. def isroutine(object):
  220. """Return true if the object is any kind of function or method."""
  221. return (isbuiltin(object)
  222. or isfunction(object)
  223. or ismethod(object)
  224. or ismethoddescriptor(object))
  225. def isabstract(object):
  226. """Return true if the object is an abstract base class (ABC)."""
  227. return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
  228. def getmembers(object, predicate=None):
  229. """Return all members of an object as (name, value) pairs sorted by name.
  230. Optionally, only return members that satisfy a given predicate."""
  231. if isclass(object):
  232. mro = (object,) + getmro(object)
  233. else:
  234. mro = ()
  235. results = []
  236. processed = set()
  237. names = dir(object)
  238. # :dd any DynamicClassAttributes to the list of names if object is a class;
  239. # this may result in duplicate entries if, for example, a virtual
  240. # attribute with the same name as a DynamicClassAttribute exists
  241. try:
  242. for base in object.__bases__:
  243. for k, v in base.__dict__.items():
  244. if isinstance(v, types.DynamicClassAttribute):
  245. names.append(k)
  246. except AttributeError:
  247. pass
  248. for key in names:
  249. # First try to get the value via getattr. Some descriptors don't
  250. # like calling their __get__ (see bug #1785), so fall back to
  251. # looking in the __dict__.
  252. try:
  253. value = getattr(object, key)
  254. # handle the duplicate key
  255. if key in processed:
  256. raise AttributeError
  257. except AttributeError:
  258. for base in mro:
  259. if key in base.__dict__:
  260. value = base.__dict__[key]
  261. break
  262. else:
  263. # could be a (currently) missing slot member, or a buggy
  264. # __dir__; discard and move on
  265. continue
  266. if not predicate or predicate(value):
  267. results.append((key, value))
  268. processed.add(key)
  269. results.sort(key=lambda pair: pair[0])
  270. return results
  271. Attribute = namedtuple('Attribute', 'name kind defining_class object')
  272. def classify_class_attrs(cls):
  273. """Return list of attribute-descriptor tuples.
  274. For each name in dir(cls), the return list contains a 4-tuple
  275. with these elements:
  276. 0. The name (a string).
  277. 1. The kind of attribute this is, one of these strings:
  278. 'class method' created via classmethod()
  279. 'static method' created via staticmethod()
  280. 'property' created via property()
  281. 'method' any other flavor of method or descriptor
  282. 'data' not a method
  283. 2. The class which defined this attribute (a class).
  284. 3. The object as obtained by calling getattr; if this fails, or if the
  285. resulting object does not live anywhere in the class' mro (including
  286. metaclasses) then the object is looked up in the defining class's
  287. dict (found by walking the mro).
  288. If one of the items in dir(cls) is stored in the metaclass it will now
  289. be discovered and not have None be listed as the class in which it was
  290. defined. Any items whose home class cannot be discovered are skipped.
  291. """
  292. mro = getmro(cls)
  293. metamro = getmro(type(cls)) # for attributes stored in the metaclass
  294. metamro = tuple([cls for cls in metamro if cls not in (type, object)])
  295. class_bases = (cls,) + mro
  296. all_bases = class_bases + metamro
  297. names = dir(cls)
  298. # :dd any DynamicClassAttributes to the list of names;
  299. # this may result in duplicate entries if, for example, a virtual
  300. # attribute with the same name as a DynamicClassAttribute exists.
  301. for base in mro:
  302. for k, v in base.__dict__.items():
  303. if isinstance(v, types.DynamicClassAttribute):
  304. names.append(k)
  305. result = []
  306. processed = set()
  307. for name in names:
  308. # Get the object associated with the name, and where it was defined.
  309. # Normal objects will be looked up with both getattr and directly in
  310. # its class' dict (in case getattr fails [bug #1785], and also to look
  311. # for a docstring).
  312. # For DynamicClassAttributes on the second pass we only look in the
  313. # class's dict.
  314. #
  315. # Getting an obj from the __dict__ sometimes reveals more than
  316. # using getattr. Static and class methods are dramatic examples.
  317. homecls = None
  318. get_obj = None
  319. dict_obj = None
  320. if name not in processed:
  321. try:
  322. if name == '__dict__':
  323. raise Exception("__dict__ is special, don't want the proxy")
  324. get_obj = getattr(cls, name)
  325. except Exception as exc:
  326. pass
  327. else:
  328. homecls = getattr(get_obj, "__objclass__", homecls)
  329. if homecls not in class_bases:
  330. # if the resulting object does not live somewhere in the
  331. # mro, drop it and search the mro manually
  332. homecls = None
  333. last_cls = None
  334. # first look in the classes
  335. for srch_cls in class_bases:
  336. srch_obj = getattr(srch_cls, name, None)
  337. if srch_obj is get_obj:
  338. last_cls = srch_cls
  339. # then check the metaclasses
  340. for srch_cls in metamro:
  341. try:
  342. srch_obj = srch_cls.__getattr__(cls, name)
  343. except AttributeError:
  344. continue
  345. if srch_obj is get_obj:
  346. last_cls = srch_cls
  347. if last_cls is not None:
  348. homecls = last_cls
  349. for base in all_bases:
  350. if name in base.__dict__:
  351. dict_obj = base.__dict__[name]
  352. if homecls not in metamro:
  353. homecls = base
  354. break
  355. if homecls is None:
  356. # unable to locate the attribute anywhere, most likely due to
  357. # buggy custom __dir__; discard and move on
  358. continue
  359. obj = get_obj if get_obj is not None else dict_obj
  360. # Classify the object or its descriptor.
  361. if isinstance(dict_obj, staticmethod):
  362. kind = "static method"
  363. obj = dict_obj
  364. elif isinstance(dict_obj, classmethod):
  365. kind = "class method"
  366. obj = dict_obj
  367. elif isinstance(dict_obj, property):
  368. kind = "property"
  369. obj = dict_obj
  370. elif isroutine(obj):
  371. kind = "method"
  372. else:
  373. kind = "data"
  374. result.append(Attribute(name, kind, homecls, obj))
  375. processed.add(name)
  376. return result
  377. # ----------------------------------------------------------- class helpers
  378. def getmro(cls):
  379. "Return tuple of base classes (including cls) in method resolution order."
  380. return cls.__mro__
  381. # -------------------------------------------------------- function helpers
  382. def unwrap(func, *, stop=None):
  383. """Get the object wrapped by *func*.
  384. Follows the chain of :attr:`__wrapped__` attributes returning the last
  385. object in the chain.
  386. *stop* is an optional callback accepting an object in the wrapper chain
  387. as its sole argument that allows the unwrapping to be terminated early if
  388. the callback returns a true value. If the callback never returns a true
  389. value, the last object in the chain is returned as usual. For example,
  390. :func:`signature` uses this to stop unwrapping if any object in the
  391. chain has a ``__signature__`` attribute defined.
  392. :exc:`ValueError` is raised if a cycle is encountered.
  393. """
  394. if stop is None:
  395. def _is_wrapper(f):
  396. return hasattr(f, '__wrapped__')
  397. else:
  398. def _is_wrapper(f):
  399. return hasattr(f, '__wrapped__') and not stop(f)
  400. f = func # remember the original func for error reporting
  401. memo = {id(f)} # Memoise by id to tolerate non-hashable objects
  402. while _is_wrapper(func):
  403. func = func.__wrapped__
  404. id_func = id(func)
  405. if id_func in memo:
  406. raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
  407. memo.add(id_func)
  408. return func
  409. # -------------------------------------------------- source code extraction
  410. def indentsize(line):
  411. """Return the indent size, in spaces, at the start of a line of text."""
  412. expline = line.expandtabs()
  413. return len(expline) - len(expline.lstrip())
  414. def _findclass(func):
  415. cls = sys.modules.get(func.__module__)
  416. if cls is None:
  417. return None
  418. for name in func.__qualname__.split('.')[:-1]:
  419. cls = getattr(cls, name)
  420. if not isclass(cls):
  421. return None
  422. return cls
  423. def _finddoc(obj):
  424. if isclass(obj):
  425. for base in obj.__mro__:
  426. if base is not object:
  427. try:
  428. doc = base.__doc__
  429. except AttributeError:
  430. continue
  431. if doc is not None:
  432. return doc
  433. return None
  434. if ismethod(obj):
  435. name = obj.__func__.__name__
  436. self = obj.__self__
  437. if (isclass(self) and
  438. getattr(getattr(self, name, None), '__func__') is obj.__func__):
  439. # classmethod
  440. cls = self
  441. else:
  442. cls = self.__class__
  443. elif isfunction(obj):
  444. name = obj.__name__
  445. cls = _findclass(obj)
  446. if cls is None or getattr(cls, name) is not obj:
  447. return None
  448. elif isbuiltin(obj):
  449. name = obj.__name__
  450. self = obj.__self__
  451. if (isclass(self) and
  452. self.__qualname__ + '.' + name == obj.__qualname__):
  453. # classmethod
  454. cls = self
  455. else:
  456. cls = self.__class__
  457. # Should be tested before isdatadescriptor().
  458. elif isinstance(obj, property):
  459. func = obj.fget
  460. name = func.__name__
  461. cls = _findclass(func)
  462. if cls is None or getattr(cls, name) is not obj:
  463. return None
  464. elif ismethoddescriptor(obj) or isdatadescriptor(obj):
  465. name = obj.__name__
  466. cls = obj.__objclass__
  467. if getattr(cls, name) is not obj:
  468. return None
  469. else:
  470. return None
  471. for base in cls.__mro__:
  472. try:
  473. doc = getattr(base, name).__doc__
  474. except AttributeError:
  475. continue
  476. if doc is not None:
  477. return doc
  478. return None
  479. def getdoc(object):
  480. """Get the documentation string for an object.
  481. All tabs are expanded to spaces. To clean up docstrings that are
  482. indented to line up with blocks of code, any whitespace than can be
  483. uniformly removed from the second line onwards is removed."""
  484. try:
  485. doc = object.__doc__
  486. except AttributeError:
  487. return None
  488. if doc is None:
  489. try:
  490. doc = _finddoc(object)
  491. except (AttributeError, TypeError):
  492. return None
  493. if not isinstance(doc, str):
  494. return None
  495. return cleandoc(doc)
  496. def cleandoc(doc):
  497. """Clean up indentation from docstrings.
  498. Any whitespace that can be uniformly removed from the second line
  499. onwards is removed."""
  500. try:
  501. lines = doc.expandtabs().split('\n')
  502. except UnicodeError:
  503. return None
  504. else:
  505. # Find minimum indentation of any non-blank lines after first line.
  506. margin = sys.maxsize
  507. for line in lines[1:]:
  508. content = len(line.lstrip())
  509. if content:
  510. indent = len(line) - content
  511. margin = min(margin, indent)
  512. # Remove indentation.
  513. if lines:
  514. lines[0] = lines[0].lstrip()
  515. if margin < sys.maxsize:
  516. for i in range(1, len(lines)): lines[i] = lines[i][margin:]
  517. # Remove any trailing or leading blank lines.
  518. while lines and not lines[-1]:
  519. lines.pop()
  520. while lines and not lines[0]:
  521. lines.pop(0)
  522. return '\n'.join(lines)
  523. def getfile(object):
  524. """Work out which source or compiled file an object was defined in."""
  525. if ismodule(object):
  526. if hasattr(object, '__file__'):
  527. return object.__file__
  528. raise TypeError('{!r} is a built-in module'.format(object))
  529. if isclass(object):
  530. if hasattr(object, '__module__'):
  531. object = sys.modules.get(object.__module__)
  532. if hasattr(object, '__file__'):
  533. return object.__file__
  534. raise TypeError('{!r} is a built-in class'.format(object))
  535. if ismethod(object):
  536. object = object.__func__
  537. if isfunction(object):
  538. object = object.__code__
  539. if istraceback(object):
  540. object = object.tb_frame
  541. if isframe(object):
  542. object = object.f_code
  543. if iscode(object):
  544. return object.co_filename
  545. raise TypeError('{!r} is not a module, class, method, '
  546. 'function, traceback, frame, or code object'.format(object))
  547. ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type')
  548. def getmoduleinfo(path):
  549. """Get the module name, suffix, mode, and module type for a given file."""
  550. warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning,
  551. 2)
  552. with warnings.catch_warnings():
  553. warnings.simplefilter('ignore', PendingDeprecationWarning)
  554. import imp
  555. filename = os.path.basename(path)
  556. suffixes = [(-len(suffix), suffix, mode, mtype)
  557. for suffix, mode, mtype in imp.get_suffixes()]
  558. suffixes.sort() # try longest suffixes first, in case they overlap
  559. for neglen, suffix, mode, mtype in suffixes:
  560. if filename[neglen:] == suffix:
  561. return ModuleInfo(filename[:neglen], suffix, mode, mtype)
  562. def getmodulename(path):
  563. """Return the module name for a given file, or None."""
  564. fname = os.path.basename(path)
  565. # Check for paths that look like an actual module file
  566. suffixes = [(-len(suffix), suffix)
  567. for suffix in importlib.machinery.all_suffixes()]
  568. suffixes.sort() # try longest suffixes first, in case they overlap
  569. for neglen, suffix in suffixes:
  570. if fname.endswith(suffix):
  571. return fname[:neglen]
  572. return None
  573. def getsourcefile(object):
  574. """Return the filename that can be used to locate an object's source.
  575. Return None if no way can be identified to get the source.
  576. """
  577. filename = getfile(object)
  578. all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
  579. all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
  580. if any(filename.endswith(s) for s in all_bytecode_suffixes):
  581. filename = (os.path.splitext(filename)[0] +
  582. importlib.machinery.SOURCE_SUFFIXES[0])
  583. elif any(filename.endswith(s) for s in
  584. importlib.machinery.EXTENSION_SUFFIXES):
  585. return None
  586. if os.path.exists(filename):
  587. return filename
  588. # only return a non-existent filename if the module has a PEP 302 loader
  589. if getattr(getmodule(object, filename), '__loader__', None) is not None:
  590. return filename
  591. # or it is in the linecache
  592. if filename in linecache.cache:
  593. return filename
  594. def getabsfile(object, _filename=None):
  595. """Return an absolute path to the source or compiled file for an object.
  596. The idea is for each object to have a unique origin, so this routine
  597. normalizes the result as much as possible."""
  598. if _filename is None:
  599. _filename = getsourcefile(object) or getfile(object)
  600. return os.path.normcase(os.path.abspath(_filename))
  601. modulesbyfile = {}
  602. _filesbymodname = {}
  603. def getmodule(object, _filename=None):
  604. """Return the module an object was defined in, or None if not found."""
  605. if ismodule(object):
  606. return object
  607. if hasattr(object, '__module__'):
  608. return sys.modules.get(object.__module__)
  609. # Try the filename to modulename cache
  610. if _filename is not None and _filename in modulesbyfile:
  611. return sys.modules.get(modulesbyfile[_filename])
  612. # Try the cache again with the absolute file name
  613. try:
  614. file = getabsfile(object, _filename)
  615. except TypeError:
  616. return None
  617. if file in modulesbyfile:
  618. return sys.modules.get(modulesbyfile[file])
  619. # Update the filename to module name cache and check yet again
  620. # Copy sys.modules in order to cope with changes while iterating
  621. for modname, module in list(sys.modules.items()):
  622. if ismodule(module) and hasattr(module, '__file__'):
  623. f = module.__file__
  624. if f == _filesbymodname.get(modname, None):
  625. # Have already mapped this module, so skip it
  626. continue
  627. _filesbymodname[modname] = f
  628. f = getabsfile(module)
  629. # Always map to the name the module knows itself by
  630. modulesbyfile[f] = modulesbyfile[
  631. os.path.realpath(f)] = module.__name__
  632. if file in modulesbyfile:
  633. return sys.modules.get(modulesbyfile[file])
  634. # Check the main module
  635. main = sys.modules['__main__']
  636. if not hasattr(object, '__name__'):
  637. return None
  638. if hasattr(main, object.__name__):
  639. mainobject = getattr(main, object.__name__)
  640. if mainobject is object:
  641. return main
  642. # Check builtins
  643. builtin = sys.modules['builtins']
  644. if hasattr(builtin, object.__name__):
  645. builtinobject = getattr(builtin, object.__name__)
  646. if builtinobject is object:
  647. return builtin
  648. def findsource(object):
  649. """Return the entire source file and starting line number for an object.
  650. The argument may be a module, class, method, function, traceback, frame,
  651. or code object. The source code is returned as a list of all the lines
  652. in the file and the line number indexes a line in that list. An OSError
  653. is raised if the source code cannot be retrieved."""
  654. file = getsourcefile(object)
  655. if file:
  656. # Invalidate cache if needed.
  657. linecache.checkcache(file)
  658. else:
  659. file = getfile(object)
  660. # Allow filenames in form of "<something>" to pass through.
  661. # `doctest` monkeypatches `linecache` module to enable
  662. # inspection, so let `linecache.getlines` to be called.
  663. if not (file.startswith('<') and file.endswith('>')):
  664. raise OSError('source code not available')
  665. module = getmodule(object, file)
  666. if module:
  667. lines = linecache.getlines(file, module.__dict__)
  668. else:
  669. lines = linecache.getlines(file)
  670. if not lines:
  671. raise OSError('could not get source code')
  672. if ismodule(object):
  673. return lines, 0
  674. if isclass(object):
  675. name = object.__name__
  676. pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
  677. # make some effort to find the best matching class definition:
  678. # use the one with the least indentation, which is the one
  679. # that's most probably not inside a function definition.
  680. candidates = []
  681. for i in range(len(lines)):
  682. match = pat.match(lines[i])
  683. if match:
  684. # if it's at toplevel, it's already the best one
  685. if lines[i][0] == 'c':
  686. return lines, i
  687. # else add whitespace to candidate list
  688. candidates.append((match.group(1), i))
  689. if candidates:
  690. # this will sort by whitespace, and by line number,
  691. # less whitespace first
  692. candidates.sort()
  693. return lines, candidates[0][1]
  694. else:
  695. raise OSError('could not find class definition')
  696. if ismethod(object):
  697. object = object.__func__
  698. if isfunction(object):
  699. object = object.__code__
  700. if istraceback(object):
  701. object = object.tb_frame
  702. if isframe(object):
  703. object = object.f_code
  704. if iscode(object):
  705. if not hasattr(object, 'co_firstlineno'):
  706. raise OSError('could not find function definition')
  707. lnum = object.co_firstlineno - 1
  708. pat = re.compile(r'^(\s*def\s)|(\s*async\s+def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
  709. while lnum > 0:
  710. if pat.match(lines[lnum]): break
  711. lnum = lnum - 1
  712. return lines, lnum
  713. raise OSError('could not find code object')
  714. def getcomments(object):
  715. """Get lines of comments immediately preceding an object's source code.
  716. Returns None when source can't be found.
  717. """
  718. try:
  719. lines, lnum = findsource(object)
  720. except (OSError, TypeError):
  721. return None
  722. if ismodule(object):
  723. # Look for a comment block at the top of the file.
  724. start = 0
  725. if lines and lines[0][:2] == '#!': start = 1
  726. while start < len(lines) and lines[start].strip() in ('', '#'):
  727. start = start + 1
  728. if start < len(lines) and lines[start][:1] == '#':
  729. comments = []
  730. end = start
  731. while end < len(lines) and lines[end][:1] == '#':
  732. comments.append(lines[end].expandtabs())
  733. end = end + 1
  734. return ''.join(comments)
  735. # Look for a preceding block of comments at the same indentation.
  736. elif lnum > 0:
  737. indent = indentsize(lines[lnum])
  738. end = lnum - 1
  739. if end >= 0 and lines[end].lstrip()[:1] == '#' and \
  740. indentsize(lines[end]) == indent:
  741. comments = [lines[end].expandtabs().lstrip()]
  742. if end > 0:
  743. end = end - 1
  744. comment = lines[end].expandtabs().lstrip()
  745. while comment[:1] == '#' and indentsize(lines[end]) == indent:
  746. comments[:0] = [comment]
  747. end = end - 1
  748. if end < 0: break
  749. comment = lines[end].expandtabs().lstrip()
  750. while comments and comments[0].strip() == '#':
  751. comments[:1] = []
  752. while comments and comments[-1].strip() == '#':
  753. comments[-1:] = []
  754. return ''.join(comments)
  755. class EndOfBlock(Exception): pass
  756. class BlockFinder:
  757. """Provide a tokeneater() method to detect the end of a code block."""
  758. def __init__(self):
  759. self.indent = 0
  760. self.islambda = False
  761. self.started = False
  762. self.passline = False
  763. self.indecorator = False
  764. self.decoratorhasargs = False
  765. self.last = 1
  766. def tokeneater(self, type, token, srowcol, erowcol, line):
  767. if not self.started and not self.indecorator:
  768. # skip any decorators
  769. if token == "@":
  770. self.indecorator = True
  771. # look for the first "def", "class" or "lambda"
  772. elif token in ("def", "class", "lambda"):
  773. if token == "lambda":
  774. self.islambda = True
  775. self.started = True
  776. self.passline = True # skip to the end of the line
  777. elif token == "(":
  778. if self.indecorator:
  779. self.decoratorhasargs = True
  780. elif token == ")":
  781. if self.indecorator:
  782. self.indecorator = False
  783. self.decoratorhasargs = False
  784. elif type == tokenize.NEWLINE:
  785. self.passline = False # stop skipping when a NEWLINE is seen
  786. self.last = srowcol[0]
  787. if self.islambda: # lambdas always end at the first NEWLINE
  788. raise EndOfBlock
  789. # hitting a NEWLINE when in a decorator without args
  790. # ends the decorator
  791. if self.indecorator and not self.decoratorhasargs:
  792. self.indecorator = False
  793. elif self.passline:
  794. pass
  795. elif type == tokenize.INDENT:
  796. self.indent = self.indent + 1
  797. self.passline = True
  798. elif type == tokenize.DEDENT:
  799. self.indent = self.indent - 1
  800. # the end of matching indent/dedent pairs end a block
  801. # (note that this only works for "def"/"class" blocks,
  802. # not e.g. for "if: else:" or "try: finally:" blocks)
  803. if self.indent <= 0:
  804. raise EndOfBlock
  805. elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL):
  806. # any other token on the same indentation level end the previous
  807. # block as well, except the pseudo-tokens COMMENT and NL.
  808. raise EndOfBlock
  809. def getblock(lines):
  810. """Extract the block of code at the top of the given list of lines."""
  811. blockfinder = BlockFinder()
  812. try:
  813. tokens = tokenize.generate_tokens(iter(lines).__next__)
  814. for _token in tokens:
  815. blockfinder.tokeneater(*_token)
  816. except (EndOfBlock, IndentationError):
  817. pass
  818. return lines[:blockfinder.last]
  819. def getsourcelines(object):
  820. """Return a list of source lines and starting line number for an object.
  821. The argument may be a module, class, method, function, traceback, frame,
  822. or code object. The source code is returned as a list of the lines
  823. corresponding to the object and the line number indicates where in the
  824. original source file the first line of code was found. An OSError is
  825. raised if the source code cannot be retrieved."""
  826. object = unwrap(object)
  827. lines, lnum = findsource(object)
  828. if ismodule(object):
  829. return lines, 0
  830. else:
  831. return getblock(lines[lnum:]), lnum + 1
  832. def getsource(object):
  833. """Return the text of the source code for an object.
  834. The argument may be a module, class, method, function, traceback, frame,
  835. or code object. The source code is returned as a single string. An
  836. OSError is raised if the source code cannot be retrieved."""
  837. lines, lnum = getsourcelines(object)
  838. return ''.join(lines)
  839. # --------------------------------------------------- class tree extraction
  840. def walktree(classes, children, parent):
  841. """Recursive helper function for getclasstree()."""
  842. results = []
  843. classes.sort(key=attrgetter('__module__', '__name__'))
  844. for c in classes:
  845. results.append((c, c.__bases__))
  846. if c in children:
  847. results.append(walktree(children[c], children, c))
  848. return results
  849. def getclasstree(classes, unique=False):
  850. """Arrange the given list of classes into a hierarchy of nested lists.
  851. Where a nested list appears, it contains classes derived from the class
  852. whose entry immediately precedes the list. Each entry is a 2-tuple
  853. containing a class and a tuple of its base classes. If the 'unique'
  854. argument is true, exactly one entry appears in the returned structure
  855. for each class in the given list. Otherwise, classes using multiple
  856. inheritance and their descendants will appear multiple times."""
  857. children = {}
  858. roots = []
  859. for c in classes:
  860. if c.__bases__:
  861. for parent in c.__bases__:
  862. if not parent in children:
  863. children[parent] = []
  864. if c not in children[parent]:
  865. children[parent].append(c)
  866. if unique and parent in classes: break
  867. elif c not in roots:
  868. roots.append(c)
  869. for parent in children:
  870. if parent not in classes:
  871. roots.append(parent)
  872. return walktree(roots, children, None)
  873. # ------------------------------------------------ argument list extraction
  874. Arguments = namedtuple('Arguments', 'args, varargs, varkw')
  875. def getargs(co):
  876. """Get information about the arguments accepted by a code object.
  877. Three things are returned: (args, varargs, varkw), where
  878. 'args' is the list of argument names. Keyword-only arguments are
  879. appended. 'varargs' and 'varkw' are the names of the * and **
  880. arguments or None."""
  881. args, varargs, kwonlyargs, varkw = _getfullargs(co)
  882. return Arguments(args + kwonlyargs, varargs, varkw)
  883. def _getfullargs(co):
  884. """Get information about the arguments accepted by a code object.
  885. Four things are returned: (args, varargs, kwonlyargs, varkw), where
  886. 'args' and 'kwonlyargs' are lists of argument names, and 'varargs'
  887. and 'varkw' are the names of the * and ** arguments or None."""
  888. if not iscode(co):
  889. raise TypeError('{!r} is not a code object'.format(co))
  890. nargs = co.co_argcount
  891. names = co.co_varnames
  892. nkwargs = co.co_kwonlyargcount
  893. args = list(names[:nargs])
  894. kwonlyargs = list(names[nargs:nargs+nkwargs])
  895. step = 0
  896. nargs += nkwargs
  897. varargs = None
  898. if co.co_flags & CO_VARARGS:
  899. varargs = co.co_varnames[nargs]
  900. nargs = nargs + 1
  901. varkw = None
  902. if co.co_flags & CO_VARKEYWORDS:
  903. varkw = co.co_varnames[nargs]
  904. return args, varargs, kwonlyargs, varkw
  905. ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults')
  906. def getargspec(func):
  907. """Get the names and default values of a function's arguments.
  908. A tuple of four things is returned: (args, varargs, keywords, defaults).
  909. 'args' is a list of the argument names, including keyword-only argument names.
  910. 'varargs' and 'keywords' are the names of the * and ** arguments or None.
  911. 'defaults' is an n-tuple of the default values of the last n arguments.
  912. Use the getfullargspec() API for Python 3 code, as annotations
  913. and keyword arguments are supported. getargspec() will raise ValueError
  914. if the func has either annotations or keyword arguments.
  915. """
  916. warnings.warn("inspect.getargspec() is deprecated, "
  917. "use inspect.signature() instead", DeprecationWarning,
  918. stacklevel=2)
  919. args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \
  920. getfullargspec(func)
  921. if kwonlyargs or ann:
  922. raise ValueError("Function has keyword-only arguments or annotations"
  923. ", use getfullargspec() API which can support them")
  924. return ArgSpec(args, varargs, varkw, defaults)
  925. FullArgSpec = namedtuple('FullArgSpec',
  926. 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations')
  927. def getfullargspec(func):
  928. """Get the names and default values of a callable object's arguments.
  929. A tuple of seven things is returned:
  930. (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations).
  931. 'args' is a list of the argument names.
  932. 'varargs' and 'varkw' are the names of the * and ** arguments or None.
  933. 'defaults' is an n-tuple of the default values of the last n arguments.
  934. 'kwonlyargs' is a list of keyword-only argument names.
  935. 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults.
  936. 'annotations' is a dictionary mapping argument names to annotations.
  937. The first four items in the tuple correspond to getargspec().
  938. This function is deprecated, use inspect.signature() instead.
  939. """
  940. try:
  941. # Re: `skip_bound_arg=False`
  942. #
  943. # There is a notable difference in behaviour between getfullargspec
  944. # and Signature: the former always returns 'self' parameter for bound
  945. # methods, whereas the Signature always shows the actual calling
  946. # signature of the passed object.
  947. #
  948. # To simulate this behaviour, we "unbind" bound methods, to trick
  949. # inspect.signature to always return their first parameter ("self",
  950. # usually)
  951. # Re: `follow_wrapper_chains=False`
  952. #
  953. # getfullargspec() historically ignored __wrapped__ attributes,
  954. # so we ensure that remains the case in 3.3+
  955. sig = _signature_from_callable(func,
  956. follow_wrapper_chains=False,
  957. skip_bound_arg=False,
  958. sigcls=Signature)
  959. except Exception as ex:
  960. # Most of the times 'signature' will raise ValueError.
  961. # But, it can also raise AttributeError, and, maybe something
  962. # else. So to be fully backwards compatible, we catch all
  963. # possible exceptions here, and reraise a TypeError.
  964. raise TypeError('unsupported callable') from ex
  965. args = []
  966. varargs = None
  967. varkw = None
  968. kwonlyargs = []
  969. defaults = ()
  970. annotations = {}
  971. defaults = ()
  972. kwdefaults = {}
  973. if sig.return_annotation is not sig.empty:
  974. annotations['return'] = sig.return_annotation
  975. for param in sig.parameters.values():
  976. kind = param.kind
  977. name = param.name
  978. if kind is _POSITIONAL_ONLY:
  979. args.append(name)
  980. elif kind is _POSITIONAL_OR_KEYWORD:
  981. args.append(name)
  982. if param.default is not param.empty:
  983. defaults += (param.default,)
  984. elif kind is _VAR_POSITIONAL:
  985. varargs = name
  986. elif kind is _KEYWORD_ONLY:
  987. kwonlyargs.append(name)
  988. if param.default is not param.empty:
  989. kwdefaults[name] = param.default
  990. elif kind is _VAR_KEYWORD:
  991. varkw = name
  992. if param.annotation is not param.empty:
  993. annotations[name] = param.annotation
  994. if not kwdefaults:
  995. # compatibility with 'func.__kwdefaults__'
  996. kwdefaults = None
  997. if not defaults:
  998. # compatibility with 'func.__defaults__'
  999. defaults = None
  1000. return FullArgSpec(args, varargs, varkw, defaults,
  1001. kwonlyargs, kwdefaults, annotations)
  1002. ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals')
  1003. def getargvalues(frame):
  1004. """Get information about arguments passed into a particular frame.
  1005. A tuple of four things is returned: (args, varargs, varkw, locals).
  1006. 'args' is a list of the argument names.
  1007. 'varargs' and 'varkw' are the names of the * and ** arguments or None.
  1008. 'locals' is the locals dictionary of the given frame."""
  1009. args, varargs, varkw = getargs(frame.f_code)
  1010. return ArgInfo(args, varargs, varkw, frame.f_locals)
  1011. def formatannotation(annotation, base_module=None):
  1012. if isinstance(annotation, type):
  1013. if annotation.__module__ in ('builtins', base_module):
  1014. return annotation.__qualname__
  1015. return annotation.__module__+'.'+annotation.__qualname__
  1016. return repr(annotation)
  1017. def formatannotationrelativeto(object):
  1018. module = getattr(object, '__module__', None)
  1019. def _formatannotation(annotation):
  1020. return formatannotation(annotation, module)
  1021. return _formatannotation
  1022. def formatargspec(args, varargs=None, varkw=None, defaults=None,
  1023. kwonlyargs=(), kwonlydefaults={}, annotations={},
  1024. formatarg=str,
  1025. formatvarargs=lambda name: '*' + name,
  1026. formatvarkw=lambda name: '**' + name,
  1027. formatvalue=lambda value: '=' + repr(value),
  1028. formatreturns=lambda text: ' -> ' + text,
  1029. formatannotation=formatannotation):
  1030. """Format an argument spec from the values returned by getargspec
  1031. or getfullargspec.
  1032. The first seven arguments are (args, varargs, varkw, defaults,
  1033. kwonlyargs, kwonlydefaults, annotations). The other five arguments
  1034. are the corresponding optional formatting functions that are called to
  1035. turn names and values into strings. The last argument is an optional
  1036. function to format the sequence of arguments."""
  1037. def formatargandannotation(arg):
  1038. result = formatarg(arg)
  1039. if arg in annotations:
  1040. result += ': ' + formatannotation(annotations[arg])
  1041. return result
  1042. specs = []
  1043. if defaults:
  1044. firstdefault = len(args) - len(defaults)
  1045. for i, arg in enumerate(args):
  1046. spec = formatargandannotation(arg)
  1047. if defaults and i >= firstdefault:
  1048. spec = spec + formatvalue(defaults[i - firstdefault])
  1049. specs.append(spec)
  1050. if varargs is not None:
  1051. specs.append(formatvarargs(formatargandannotation(varargs)))
  1052. else:
  1053. if kwonlyargs:
  1054. specs.append('*')
  1055. if kwonlyargs:
  1056. for kwonlyarg in kwonlyargs:
  1057. spec = formatargandannotation(kwonlyarg)
  1058. if kwonlydefaults and kwonlyarg in kwonlydefaults:
  1059. spec += formatvalue(kwonlydefaults[kwonlyarg])
  1060. specs.append(spec)
  1061. if varkw is not None:
  1062. specs.append(formatvarkw(formatargandannotation(varkw)))
  1063. result = '(' + ', '.join(specs) + ')'
  1064. if 'return' in annotations:
  1065. result += formatreturns(formatannotation(annotations['return']))
  1066. return result
  1067. def formatargvalues(args, varargs, varkw, locals,
  1068. formatarg=str,
  1069. formatvarargs=lambda name: '*' + name,
  1070. formatvarkw=lambda name: '**' + name,
  1071. formatvalue=lambda value: '=' + repr(value)):
  1072. """Format an argument spec from the 4 values returned by getargvalues.
  1073. The first four arguments are (args, varargs, varkw, locals). The
  1074. next four arguments are the corresponding optional formatting functions
  1075. that are called to turn names and values into strings. The ninth
  1076. argument is an optional function to format the sequence of arguments."""
  1077. def convert(name, locals=locals,
  1078. formatarg=formatarg, formatvalue=formatvalue):
  1079. return formatarg(name) + formatvalue(locals[name])
  1080. specs = []
  1081. for i in range(len(args)):
  1082. specs.append(convert(args[i]))
  1083. if varargs:
  1084. specs.append(formatvarargs(varargs) + formatvalue(locals[varargs]))
  1085. if varkw:
  1086. specs.append(formatvarkw(varkw) + formatvalue(locals[varkw]))
  1087. return '(' + ', '.join(specs) + ')'
  1088. def _missing_arguments(f_name, argnames, pos, values):
  1089. names = [repr(name) for name in argnames if name not in values]
  1090. missing = len(names)
  1091. if missing == 1:
  1092. s = names[0]
  1093. elif missing == 2:
  1094. s = "{} and {}".format(*names)
  1095. else:
  1096. tail = ", {} and {}".format(*names[-2:])
  1097. del names[-2:]
  1098. s = ", ".join(names) + tail
  1099. raise TypeError("%s() missing %i required %s argument%s: %s" %
  1100. (f_name, missing,
  1101. "positional" if pos else "keyword-only",
  1102. "" if missing == 1 else "s", s))
  1103. def _too_many(f_name, args, kwonly, varargs, defcount, given, values):
  1104. atleast = len(args) - defcount
  1105. kwonly_given = len([arg for arg in kwonly if arg in values])
  1106. if varargs:
  1107. plural = atleast != 1
  1108. sig = "at least %d" % (atleast,)
  1109. elif defcount:
  1110. plural = True
  1111. sig = "from %d to %d" % (atleast, len(args))
  1112. else:
  1113. plural = len(args) != 1
  1114. sig = str(len(args))
  1115. kwonly_sig = ""
  1116. if kwonly_given:
  1117. msg = " positional argument%s (and %d keyword-only argument%s)"
  1118. kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given,
  1119. "s" if kwonly_given != 1 else ""))
  1120. raise TypeError("%s() takes %s positional argument%s but %d%s %s given" %
  1121. (f_name, sig, "s" if plural else "", given, kwonly_sig,
  1122. "was" if given == 1 and not kwonly_given else "were"))
  1123. def getcallargs(*func_and_positional, **named):
  1124. """Get the mapping of arguments to values.
  1125. A dict is returned, with keys the function argument names (including the
  1126. names of the * and ** arguments, if any), and values the respective bound
  1127. values from 'positional' and 'named'."""
  1128. func = func_and_positional[0]
  1129. positional = func_and_positional[1:]
  1130. spec = getfullargspec(func)
  1131. args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec
  1132. f_name = func.__name__
  1133. arg2value = {}
  1134. if ismethod(func) and func.__self__ is not None:
  1135. # implicit 'self' (or 'cls' for classmethods) argument
  1136. positional = (func.__self__,) + positional
  1137. num_pos = len(positional)
  1138. num_args = len(args)
  1139. num_defaults = len(defaults) if defaults else 0
  1140. n = min(num_pos, num_args)
  1141. for i in range(n):
  1142. arg2value[args[i]] = positional[i]
  1143. if varargs:
  1144. arg2value[varargs] = tuple(positional[n:])
  1145. possible_kwargs = set(args + kwonlyargs)
  1146. if varkw:
  1147. arg2value[varkw] = {}
  1148. for kw, value in named.items():
  1149. if kw not in possible_kwargs:
  1150. if not varkw:
  1151. raise TypeError("%s() got an unexpected keyword argument %r" %
  1152. (f_name, kw))
  1153. arg2value[varkw][kw] = value
  1154. continue
  1155. if kw in arg2value:
  1156. raise TypeError("%s() got multiple values for argument %r" %
  1157. (f_name, kw))
  1158. arg2value[kw] = value
  1159. if num_pos > num_args and not varargs:
  1160. _too_many(f_name, args, kwonlyargs, varargs, num_defaults,
  1161. num_pos, arg2value)
  1162. if num_pos < num_args:
  1163. req = args[:num_args - num_defaults]
  1164. for arg in req:
  1165. if arg not in arg2value:
  1166. _missing_arguments(f_name, req, True, arg2value)
  1167. for i, arg in enumerate(args[num_args - num_defaults:]):
  1168. if arg not in arg2value:
  1169. arg2value[arg] = defaults[i]
  1170. missing = 0
  1171. for kwarg in kwonlyargs:
  1172. if kwarg not in arg2value:
  1173. if kwonlydefaults and kwarg in kwonlydefaults:
  1174. arg2value[kwarg] = kwonlydefaults[kwarg]
  1175. else:
  1176. missing += 1
  1177. if missing:
  1178. _missing_arguments(f_name, kwonlyargs, False, arg2value)
  1179. return arg2value
  1180. ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound')
  1181. def getclosurevars(func):
  1182. """
  1183. Get the mapping of free variables to their current values.
  1184. Returns a named tuple of dicts mapping the current nonlocal, global
  1185. and builtin references as seen by the body of the function. A final
  1186. set of unbound names that could not be resolved is also provided.
  1187. """
  1188. if ismethod(func):
  1189. func = func.__func__
  1190. if not isfunction(func):
  1191. raise TypeError("'{!r}' is not a Python function".format(func))
  1192. code = func.__code__
  1193. # Nonlocal references are named in co_freevars and resolved
  1194. # by looking them up in __closure__ by positional index
  1195. if func.__closure__ is None:
  1196. nonlocal_vars = {}
  1197. else:
  1198. nonlocal_vars = {
  1199. var : cell.cell_contents
  1200. for var, cell in zip(code.co_freevars, func.__closure__)
  1201. }
  1202. # Global and builtin references are named in co_names and resolved
  1203. # by looking them up in __globals__ or __builtins__
  1204. global_ns = func.__globals__
  1205. builtin_ns = global_ns.get("__builtins__", builtins.__dict__)
  1206. if ismodule(builtin_ns):
  1207. builtin_ns = builtin_ns.__dict__
  1208. global_vars = {}
  1209. builtin_vars = {}
  1210. unbound_names = set()
  1211. for name in code.co_names:
  1212. if name in ("None", "True", "False"):
  1213. # Because these used to be builtins instead of keywords, they
  1214. # may still show up as name references. We ignore them.
  1215. continue
  1216. try:
  1217. global_vars[name] = global_ns[name]
  1218. except KeyError:
  1219. try:
  1220. builtin_vars[name] = builtin_ns[name]
  1221. except KeyError:
  1222. unbound_names.add(name)
  1223. return ClosureVars(nonlocal_vars, global_vars,
  1224. builtin_vars, unbound_names)
  1225. # -------------------------------------------------- stack frame extraction
  1226. Traceback = namedtuple('Traceback', 'filename lineno function code_context index')
  1227. def getframeinfo(frame, context=1):
  1228. """Get information about a frame or traceback object.
  1229. A tuple of five things is returned: the filename, the line number of
  1230. the current line, the function name, a list of lines of context from
  1231. the source code, and the index of the current line within that list.
  1232. The optional second argument specifies the number of lines of context
  1233. to return, which are centered around the current line."""
  1234. if istraceback(frame):
  1235. lineno = frame.tb_lineno
  1236. frame = frame.tb_frame
  1237. else:
  1238. lineno = frame.f_lineno
  1239. if not isframe(frame):
  1240. raise TypeError('{!r} is not a frame or traceback object'.format(frame))
  1241. filename = getsourcefile(frame) or getfile(frame)
  1242. if context > 0:
  1243. start = lineno - 1 - context//2
  1244. try:
  1245. lines, lnum = findsource(frame)
  1246. except OSError:
  1247. lines = index = None
  1248. else:
  1249. start = max(start, 1)
  1250. start = max(0, min(start, len(lines) - context))
  1251. lines = lines[start:start+context]
  1252. index = lineno - 1 - start
  1253. else:
  1254. lines = index = None
  1255. return Traceback(filename, lineno, frame.f_code.co_name, lines, index)
  1256. def getlineno(frame):
  1257. """Get the line number from a frame object, allowing for optimization."""
  1258. # FrameType.f_lineno is now a descriptor that grovels co_lnotab
  1259. return frame.f_lineno
  1260. FrameInfo = namedtuple('FrameInfo', ('frame',) + Traceback._fields)
  1261. def getouterframes(frame, context=1):
  1262. """Get a list of records for a frame and all higher (calling) frames.
  1263. Each record contains a frame object, filename, line number, function
  1264. name, a list of lines of context, and index within the context."""
  1265. framelist = []
  1266. while frame:
  1267. frameinfo = (frame,) + getframeinfo(frame, context)
  1268. framelist.append(FrameInfo(*frameinfo))
  1269. frame = frame.f_back
  1270. return framelist
  1271. def getinnerframes(tb, context=1):
  1272. """Get a list of records for a traceback's frame and all lower frames.
  1273. Each record contains a frame object, filename, line number, function
  1274. name, a list of lines of context, and index within the context."""
  1275. framelist = []
  1276. while tb:
  1277. frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
  1278. framelist.append(FrameInfo(*frameinfo))
  1279. tb = tb.tb_next
  1280. return framelist
  1281. def currentframe():
  1282. """Return the frame of the caller or None if this is not possible."""
  1283. return sys._getframe(1) if hasattr(sys, "_getframe") else None
  1284. def stack(context=1):
  1285. """Return a list of records for the stack above the caller's frame."""
  1286. return getouterframes(sys._getframe(1), context)
  1287. def trace(context=1):
  1288. """Return a list of records for the stack below the current exception."""
  1289. return getinnerframes(sys.exc_info()[2], context)
  1290. # ------------------------------------------------ static version of getattr
  1291. _sentinel = object()
  1292. def _static_getmro(klass):
  1293. return type.__dict__['__mro__'].__get__(klass)
  1294. def _check_instance(obj, attr):
  1295. instance_dict = {}
  1296. try:
  1297. instance_dict = object.__getattribute__(obj, "__dict__")
  1298. except AttributeError:
  1299. pass
  1300. return dict.get(instance_dict, attr, _sentinel)
  1301. def _check_class(klass, attr):
  1302. for entry in _static_getmro(klass):
  1303. if _shadowed_dict(type(entry)) is _sentinel:
  1304. try:
  1305. return entry.__dict__[attr]
  1306. except KeyError:
  1307. pass
  1308. return _sentinel
  1309. def _is_type(obj):
  1310. try:
  1311. _static_getmro(obj)
  1312. except TypeError:
  1313. return False
  1314. return True
  1315. def _shadowed_dict(klass):
  1316. dict_attr = type.__dict__["__dict__"]
  1317. for entry in _static_getmro(klass):
  1318. try:
  1319. class_dict = dict_attr.__get__(entry)["__dict__"]
  1320. except KeyError:
  1321. pass
  1322. else:
  1323. if not (type(class_dict) is types.GetSetDescriptorType and
  1324. class_dict.__name__ == "__dict__" and
  1325. class_dict.__objclass__ is entry):
  1326. return class_dict
  1327. return _sentinel
  1328. def getattr_static(obj, attr, default=_sentinel):
  1329. """Retrieve attributes without triggering dynamic lookup via the
  1330. descriptor protocol, __getattr__ or __getattribute__.
  1331. Note: this function may not be able to retrieve all attributes
  1332. that getattr can fetch (like dynamically created attributes)
  1333. and may find attributes that getattr can't (like descriptors
  1334. that raise AttributeError). It can also return descriptor objects
  1335. instead of instance members in some cases. See the
  1336. documentation for details.
  1337. """
  1338. instance_result = _sentinel
  1339. if not _is_type(obj):
  1340. klass = type(obj)
  1341. dict_attr = _shadowed_dict(klass)
  1342. if (dict_attr is _sentinel or
  1343. type(dict_attr) is types.MemberDescriptorType):
  1344. instance_result = _check_instance(obj, attr)
  1345. else:
  1346. klass = obj
  1347. klass_result = _check_class(klass, attr)
  1348. if instance_result is not _sentinel and klass_result is not _sentinel:
  1349. if (_check_class(type(klass_result), '__get__') is not _sentinel and
  1350. _check_class(type(klass_result), '__set__') is not _sentinel):
  1351. return klass_result
  1352. if instance_result is not _sentinel:
  1353. return instance_result
  1354. if klass_result is not _sentinel:
  1355. return klass_result
  1356. if obj is klass:
  1357. # for types we check the metaclass too
  1358. for entry in _static_getmro(type(klass)):
  1359. if _shadowed_dict(type(entry)) is _sentinel:
  1360. try:
  1361. return entry.__dict__[attr]
  1362. except KeyError:
  1363. pass
  1364. if default is not _sentinel:
  1365. return default
  1366. raise AttributeError(attr)
  1367. # ------------------------------------------------ generator introspection
  1368. GEN_CREATED = 'GEN_CREATED'
  1369. GEN_RUNNING = 'GEN_RUNNING'
  1370. GEN_SUSPENDED = 'GEN_SUSPENDED'
  1371. GEN_CLOSED = 'GEN_CLOSED'
  1372. def getgeneratorstate(generator):
  1373. """Get current state of a generator-iterator.
  1374. Possible states are:
  1375. GEN_CREATED: Waiting to start execution.
  1376. GEN_RUNNING: Currently being executed by the interpreter.
  1377. GEN_SUSPENDED: Currently suspended at a yield expression.
  1378. GEN_CLOSED: Execution has completed.
  1379. """
  1380. if generator.gi_running:
  1381. return GEN_RUNNING
  1382. if generator.gi_frame is None:
  1383. return GEN_CLOSED
  1384. if generator.gi_frame.f_lasti == -1:
  1385. return GEN_CREATED
  1386. return GEN_SUSPENDED
  1387. def getgeneratorlocals(generator):
  1388. """
  1389. Get the mapping of generator local variables to their current values.
  1390. A dict is returned, with the keys the local variable names and values the
  1391. bound values."""
  1392. if not isgenerator(generator):
  1393. raise TypeError("'{!r}' is not a Python generator".format(generator))
  1394. frame = getattr(generator, "gi_frame", None)
  1395. if frame is not None:
  1396. return generator.gi_frame.f_locals
  1397. else:
  1398. return {}
  1399. # ------------------------------------------------ coroutine introspection
  1400. CORO_CREATED = 'CORO_CREATED'
  1401. CORO_RUNNING = 'CORO_RUNNING'
  1402. CORO_SUSPENDED = 'CORO_SUSPENDED'
  1403. CORO_CLOSED = 'CORO_CLOSED'
  1404. def getcoroutinestate(coroutine):
  1405. """Get current state of a coroutine object.
  1406. Possible states are:
  1407. CORO_CREATED: Waiting to start execution.
  1408. CORO_RUNNING: Currently being executed by the interpreter.
  1409. CORO_SUSPENDED: Currently suspended at an await expression.
  1410. CORO_CLOSED: Execution has completed.
  1411. """
  1412. if coroutine.cr_running:
  1413. return CORO_RUNNING
  1414. if coroutine.cr_frame is None:
  1415. return CORO_CLOSED
  1416. if coroutine.cr_frame.f_lasti == -1:
  1417. return CORO_CREATED
  1418. return CORO_SUSPENDED
  1419. def getcoroutinelocals(coroutine):
  1420. """
  1421. Get the mapping of coroutine local variables to their current values.
  1422. A dict is returned, with the keys the local variable names and values the
  1423. bound values."""
  1424. frame = getattr(coroutine, "cr_frame", None)
  1425. if frame is not None:
  1426. return frame.f_locals
  1427. else:
  1428. return {}
  1429. ###############################################################################
  1430. ### Function Signature Object (PEP 362)
  1431. ###############################################################################
  1432. _WrapperDescriptor = type(type.__call__)
  1433. _MethodWrapper = type(all.__call__)
  1434. _ClassMethodWrapper = type(int.__dict__['from_bytes'])
  1435. _NonUserDefinedCallables = (_WrapperDescriptor,
  1436. _MethodWrapper,
  1437. _ClassMethodWrapper,
  1438. types.BuiltinFunctionType)
  1439. def _signature_get_user_defined_method(cls, method_name):
  1440. """Private helper. Checks if ``cls`` has an attribute
  1441. named ``method_name`` and returns it only if it is a
  1442. pure python function.
  1443. """
  1444. try:
  1445. meth = getattr(cls, method_name)
  1446. except AttributeError:
  1447. return
  1448. else:
  1449. if not isinstance(meth, _NonUserDefinedCallables):
  1450. # Once '__signature__' will be added to 'C'-level
  1451. # callables, this check won't be necessary
  1452. return meth
  1453. def _signature_get_partial(wrapped_sig, partial, extra_args=()):
  1454. """Private helper to calculate how 'wrapped_sig' signature will
  1455. look like after applying a 'functools.partial' object (or alike)
  1456. on it.
  1457. """
  1458. old_params = wrapped_sig.parameters
  1459. new_params = OrderedDict(old_params.items())
  1460. partial_args = partial.args or ()
  1461. partial_keywords = partial.keywords or {}
  1462. if extra_args:
  1463. partial_args = extra_args + partial_args
  1464. try:
  1465. ba = wrapped_sig.bind_partial(*partial_args, **partial_keywords)
  1466. except TypeError as ex:
  1467. msg = 'partial object {!r} has incorrect arguments'.format(partial)
  1468. raise ValueError(msg) from ex
  1469. transform_to_kwonly = False
  1470. for param_name, param in old_params.items():
  1471. try:
  1472. arg_value = ba.arguments[param_name]
  1473. except KeyError:
  1474. pass
  1475. else:
  1476. if param.kind is _POSITIONAL_ONLY:
  1477. # If positional-only parameter is bound by partial,
  1478. # it effectively disappears from the signature
  1479. new_params.pop(param_name)
  1480. continue
  1481. if param.kind is _POSITIONAL_OR_KEYWORD:
  1482. if param_name in partial_keywords:
  1483. # This means that this parameter, and all parameters
  1484. # after it should be keyword-only (and var-positional
  1485. # should be removed). Here's why. Consider the following
  1486. # function:
  1487. # foo(a, b, *args, c):
  1488. # pass
  1489. #
  1490. # "partial(foo, a='spam')" will have the following
  1491. # signature: "(*, a='spam', b, c)". Because attempting
  1492. # to call that partial with "(10, 20)" arguments will
  1493. # raise a TypeError, saying that "a" argument received
  1494. # multiple values.
  1495. transform_to_kwonly = True
  1496. # Set the new default value
  1497. new_params[param_name] = param.replace(default=arg_value)
  1498. else:
  1499. # was passed as a positional argument
  1500. new_params.pop(param.name)
  1501. continue
  1502. if param.kind is _KEYWORD_ONLY:
  1503. # Set the new default value
  1504. new_params[param_name] = param.replace(default=arg_value)
  1505. if transform_to_kwonly:
  1506. assert param.kind is not _POSITIONAL_ONLY
  1507. if param.kind is _POSITIONAL_OR_KEYWORD:
  1508. new_param = new_params[param_name].replace(kind=_KEYWORD_ONLY)
  1509. new_params[param_name] = new_param
  1510. new_params.move_to_end(param_name)
  1511. elif param.kind in (_KEYWORD_ONLY, _VAR_KEYWORD):
  1512. new_params.move_to_end(param_name)
  1513. elif param.kind is _VAR_POSITIONAL:
  1514. new_params.pop(param.name)
  1515. return wrapped_sig.replace(parameters=new_params.values())
  1516. def _signature_bound_method(sig):
  1517. """Private helper to transform signatures for unbound
  1518. functions to bound methods.
  1519. """
  1520. params = tuple(sig.parameters.values())
  1521. if not params or params[0].kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
  1522. raise ValueError('invalid method signature')
  1523. kind = params[0].kind
  1524. if kind in (_POSITIONAL_OR_KEYWORD, _POSITIONAL_ONLY):
  1525. # Drop first parameter:
  1526. # '(p1, p2[, ...])' -> '(p2[, ...])'
  1527. params = params[1:]
  1528. else:
  1529. if kind is not _VAR_POSITIONAL:
  1530. # Unless we add a new parameter type we never
  1531. # get here
  1532. raise ValueError('invalid argument type')
  1533. # It's a var-positional parameter.
  1534. # Do nothing. '(*args[, ...])' -> '(*args[, ...])'
  1535. return sig.replace(parameters=params)
  1536. def _signature_is_builtin(obj):
  1537. """Private helper to test if `obj` is a callable that might
  1538. support Argument Clinic's __text_signature__ protocol.
  1539. """
  1540. return (isbuiltin(obj) or
  1541. ismethoddescriptor(obj) or
  1542. isinstance(obj, _NonUserDefinedCallables) or
  1543. # Can't test 'isinstance(type)' here, as it would
  1544. # also be True for regular python classes
  1545. obj in (type, object))
  1546. def _signature_is_functionlike(obj):
  1547. """Private helper to test if `obj` is a duck type of FunctionType.
  1548. A good example of such objects are functions compiled with
  1549. Cython, which have all attributes that a pure Python function
  1550. would have, but have their code statically compiled.
  1551. """
  1552. if not callable(obj) or isclass(obj):
  1553. # All function-like objects are obviously callables,
  1554. # and not classes.
  1555. return False
  1556. name = getattr(obj, '__name__', None)
  1557. code = getattr(obj, '__code__', None)
  1558. defaults = getattr(obj, '__defaults__', _void) # Important to use _void ...
  1559. kwdefaults = getattr(obj, '__kwdefaults__', _void) # ... and not None here
  1560. annotations = getattr(obj, '__annotations__', None)
  1561. return (isinstance(code, types.CodeType) and
  1562. isinstance(name, str) and
  1563. (defaults is None or isinstance(defaults, tuple)) and
  1564. (kwdefaults is None or isinstance(kwdefaults, dict)) and
  1565. isinstance(annotations, dict))
  1566. def _signature_get_bound_param(spec):
  1567. """ Private helper to get first parameter name from a
  1568. __text_signature__ of a builtin method, which should
  1569. be in the following format: '($param1, ...)'.
  1570. Assumptions are that the first argument won't have
  1571. a default value or an annotation.
  1572. """
  1573. assert spec.startswith('($')
  1574. pos = spec.find(',')
  1575. if pos == -1:
  1576. pos = spec.find(')')
  1577. cpos = spec.find(':')
  1578. assert cpos == -1 or cpos > pos
  1579. cpos = spec.find('=')
  1580. assert cpos == -1 or cpos > pos
  1581. return spec[2:pos]
  1582. def _signature_strip_non_python_syntax(signature):
  1583. """
  1584. Private helper function. Takes a signature in Argument Clinic's
  1585. extended signature format.
  1586. Returns a tuple of three things:
  1587. * that signature re-rendered in standard Python syntax,
  1588. * the index of the "self" parameter (generally 0), or None if
  1589. the function does not have a "self" parameter, and
  1590. * the index of the last "positional only" parameter,
  1591. or None if the signature has no positional-only parameters.
  1592. """
  1593. if not signature:
  1594. return signature, None, None
  1595. self_parameter = None
  1596. last_positional_only = None
  1597. lines = [l.encode('ascii') for l in signature.split('\n')]
  1598. generator = iter(lines).__next__
  1599. token_stream = tokenize.tokenize(generator)
  1600. delayed_comma = False
  1601. skip_next_comma = False
  1602. text = []
  1603. add = text.append
  1604. current_parameter = 0
  1605. OP = token.OP
  1606. ERRORTOKEN = token.ERRORTOKEN
  1607. # token stream always starts with ENCODING token, skip it
  1608. t = next(token_stream)
  1609. assert t.type == tokenize.ENCODING
  1610. for t in token_stream:
  1611. type, string = t.type, t.string
  1612. if type == OP:
  1613. if string == ',':
  1614. if skip_next_comma:
  1615. skip_next_comma = False
  1616. else:
  1617. assert not delayed_comma
  1618. delayed_comma = True
  1619. current_parameter += 1
  1620. continue
  1621. if string == '/':
  1622. assert not skip_next_comma
  1623. assert last_positional_only is None
  1624. skip_next_comma = True
  1625. last_positional_only = current_parameter - 1
  1626. continue
  1627. if (type == ERRORTOKEN) and (string == '$'):
  1628. assert self_parameter is None
  1629. self_parameter = current_parameter
  1630. continue
  1631. if delayed_comma:
  1632. delayed_comma = False
  1633. if not ((type == OP) and (string == ')')):
  1634. add(', ')
  1635. add(string)
  1636. if (string == ','):
  1637. add(' ')
  1638. clean_signature = ''.join(text)
  1639. return clean_signature, self_parameter, last_positional_only
  1640. def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
  1641. """Private helper to parse content of '__text_signature__'
  1642. and return a Signature based on it.
  1643. """
  1644. Parameter = cls._parameter_cls
  1645. clean_signature, self_parameter, last_positional_only = \
  1646. _signature_strip_non_python_syntax(s)
  1647. program = "def foo" + clean_signature + ": pass"
  1648. try:
  1649. module = ast.parse(program)
  1650. except SyntaxError:
  1651. module = None
  1652. if not isinstance(module, ast.Module):
  1653. raise ValueError("{!r} builtin has invalid signature".format(obj))
  1654. f = module.body[0]
  1655. parameters = []
  1656. empty = Parameter.empty
  1657. invalid = object()
  1658. module = None
  1659. module_dict = {}
  1660. module_name = getattr(obj, '__module__', None)
  1661. if module_name:
  1662. module = sys.modules.get(module_name, None)
  1663. if module:
  1664. module_dict = module.__dict__
  1665. sys_module_dict = sys.modules
  1666. def parse_name(node):
  1667. assert isinstance(node, ast.arg)
  1668. if node.annotation != None:
  1669. raise ValueError("Annotations are not currently supported")
  1670. return node.arg
  1671. def wrap_value(s):
  1672. try:
  1673. value = eval(s, module_dict)
  1674. except NameError:
  1675. try:
  1676. value = eval(s, sys_module_dict)
  1677. except NameError:
  1678. raise RuntimeError()
  1679. if isinstance(value, str):
  1680. return ast.Str(value)
  1681. if isinstance(value, (int, float)):
  1682. return ast.Num(value)
  1683. if isinstance(value, bytes):
  1684. return ast.Bytes(value)
  1685. if value in (True, False, None):
  1686. return ast.NameConstant(value)
  1687. raise RuntimeError()
  1688. class RewriteSymbolics(ast.NodeTransformer):
  1689. def visit_Attribute(self, node):
  1690. a = []
  1691. n = node
  1692. while isinstance(n, ast.Attribute):
  1693. a.append(n.attr)
  1694. n = n.value
  1695. if not isinstance(n, ast.Name):
  1696. raise RuntimeError()
  1697. a.append(n.id)
  1698. value = ".".join(reversed(a))
  1699. return wrap_value(value)
  1700. def visit_Name(self, node):
  1701. if not isinstance(node.ctx, ast.Load):
  1702. raise ValueError()
  1703. return wrap_value(node.id)
  1704. def p(name_node, default_node, default=empty):
  1705. name = parse_name(name_node)
  1706. if name is invalid:
  1707. return None
  1708. if default_node and default_node is not _empty:
  1709. try:
  1710. default_node = RewriteSymbolics().visit(default_node)
  1711. o = ast.literal_eval(default_node)
  1712. except ValueError:
  1713. o = invalid
  1714. if o is invalid:
  1715. return None
  1716. default = o if o is not invalid else default
  1717. parameters.append(Parameter(name, kind, default=default, annotation=empty))
  1718. # non-keyword-only parameters
  1719. args = reversed(f.args.args)
  1720. defaults = reversed(f.args.defaults)
  1721. iter = itertools.zip_longest(args, defaults, fillvalue=None)
  1722. if last_positional_only is not None:
  1723. kind = Parameter.POSITIONAL_ONLY
  1724. else:
  1725. kind = Parameter.POSITIONAL_OR_KEYWORD
  1726. for i, (name, default) in enumerate(reversed(list(iter))):
  1727. p(name, default)
  1728. if i == last_positional_only:
  1729. kind = Parameter.POSITIONAL_OR_KEYWORD
  1730. # *args
  1731. if f.args.vararg:
  1732. kind = Parameter.VAR_POSITIONAL
  1733. p(f.args.vararg, empty)
  1734. # keyword-only arguments
  1735. kind = Parameter.KEYWORD_ONLY
  1736. for name, default in zip(f.args.kwonlyargs, f.args.kw_defaults):
  1737. p(name, default)
  1738. # **kwargs
  1739. if f.args.kwarg:
  1740. kind = Parameter.VAR_KEYWORD
  1741. p(f.args.kwarg, empty)
  1742. if self_parameter is not None:
  1743. # Possibly strip the bound argument:
  1744. # - We *always* strip first bound argument if
  1745. # it is a module.
  1746. # - We don't strip first bound argument if
  1747. # skip_bound_arg is False.
  1748. assert parameters
  1749. _self = getattr(obj, '__self__', None)
  1750. self_isbound = _self is not None
  1751. self_ismodule = ismodule(_self)
  1752. if self_isbound and (self_ismodule or skip_bound_arg):
  1753. parameters.pop(0)
  1754. else:
  1755. # for builtins, self parameter is always positional-only!
  1756. p = parameters[0].replace(kind=Parameter.POSITIONAL_ONLY)
  1757. parameters[0] = p
  1758. return cls(parameters, return_annotation=cls.empty)
  1759. def _signature_from_builtin(cls, func, skip_bound_arg=True):
  1760. """Private helper function to get signature for
  1761. builtin callables.
  1762. """
  1763. if not _signature_is_builtin(func):
  1764. raise TypeError("{!r} is not a Python builtin "
  1765. "function".format(func))
  1766. s = getattr(func, "__text_signature__", None)
  1767. if not s:
  1768. raise ValueError("no signature found for builtin {!r}".format(func))
  1769. return _signature_fromstr(cls, func, s, skip_bound_arg)
  1770. def _signature_from_function(cls, func):
  1771. """Private helper: constructs Signature for the given python function."""
  1772. is_duck_function = False
  1773. if not isfunction(func):
  1774. if _signature_is_functionlike(func):
  1775. is_duck_function = True
  1776. else:
  1777. # If it's not a pure Python function, and not a duck type
  1778. # of pure function:
  1779. raise TypeError('{!r} is not a Python function'.format(func))
  1780. Parameter = cls._parameter_cls
  1781. # Parameter information.
  1782. func_code = func.__code__
  1783. pos_count = func_code.co_argcount
  1784. arg_names = func_code.co_varnames
  1785. positional = tuple(arg_names[:pos_count])
  1786. keyword_only_count = func_code.co_kwonlyargcount
  1787. keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)]
  1788. annotations = func.__annotations__
  1789. defaults = func.__defaults__
  1790. kwdefaults = func.__kwdefaults__
  1791. if defaults:
  1792. pos_default_count = len(defaults)
  1793. else:
  1794. pos_default_count = 0
  1795. parameters = []
  1796. # Non-keyword-only parameters w/o defaults.
  1797. non_default_count = pos_count - pos_default_count
  1798. for name in positional[:non_default_count]:
  1799. annotation = annotations.get(name, _empty)
  1800. parameters.append(Parameter(name, annotation=annotation,
  1801. kind=_POSITIONAL_OR_KEYWORD))
  1802. # ... w/ defaults.
  1803. for offset, name in enumerate(positional[non_default_count:]):
  1804. annotation = annotations.get(name, _empty)
  1805. parameters.append(Parameter(name, annotation=annotation,
  1806. kind=_POSITIONAL_OR_KEYWORD,
  1807. default=defaults[offset]))
  1808. # *args
  1809. if func_code.co_flags & CO_VARARGS:
  1810. name = arg_names[pos_count + keyword_only_count]
  1811. annotation = annotations.get(name, _empty)
  1812. parameters.append(Parameter(name, annotation=annotation,
  1813. kind=_VAR_POSITIONAL))
  1814. # Keyword-only parameters.
  1815. for name in keyword_only:
  1816. default = _empty
  1817. if kwdefaults is not None:
  1818. default = kwdefaults.get(name, _empty)
  1819. annotation = annotations.get(name, _empty)
  1820. parameters.append(Parameter(name, annotation=annotation,
  1821. kind=_KEYWORD_ONLY,
  1822. default=default))
  1823. # **kwargs
  1824. if func_code.co_flags & CO_VARKEYWORDS:
  1825. index = pos_count + keyword_only_count
  1826. if func_code.co_flags & CO_VARARGS:
  1827. index += 1
  1828. name = arg_names[index]
  1829. annotation = annotations.get(name, _empty)
  1830. parameters.append(Parameter(name, annotation=annotation,
  1831. kind=_VAR_KEYWORD))
  1832. # Is 'func' is a pure Python function - don't validate the
  1833. # parameters list (for correct order and defaults), it should be OK.
  1834. return cls(parameters,
  1835. return_annotation=annotations.get('return', _empty),
  1836. __validate_parameters__=is_duck_function)
  1837. def _signature_from_callable(obj, *,
  1838. follow_wrapper_chains=True,
  1839. skip_bound_arg=True,
  1840. sigcls):
  1841. """Private helper function to get signature for arbitrary
  1842. callable objects.
  1843. """
  1844. if not callable(obj):
  1845. raise TypeError('{!r} is not a callable object'.format(obj))
  1846. if isinstance(obj, types.MethodType):
  1847. # In this case we skip the first parameter of the underlying
  1848. # function (usually `self` or `cls`).
  1849. sig = _signature_from_callable(
  1850. obj.__func__,
  1851. follow_wrapper_chains=follow_wrapper_chains,
  1852. skip_bound_arg=skip_bound_arg,
  1853. sigcls=sigcls)
  1854. if skip_bound_arg:
  1855. return _signature_bound_method(sig)
  1856. else:
  1857. return sig
  1858. # Was this function wrapped by a decorator?
  1859. if follow_wrapper_chains:
  1860. obj = unwrap(obj, stop=(lambda f: hasattr(f, "__signature__")))
  1861. if isinstance(obj, types.MethodType):
  1862. # If the unwrapped object is a *method*, we might want to
  1863. # skip its first parameter (self).
  1864. # See test_signature_wrapped_bound_method for details.
  1865. return _signature_from_callable(
  1866. obj,
  1867. follow_wrapper_chains=follow_wrapper_chains,
  1868. skip_bound_arg=skip_bound_arg,
  1869. sigcls=sigcls)
  1870. try:
  1871. sig = obj.__signature__
  1872. except AttributeError:
  1873. pass
  1874. else:
  1875. if sig is not None:
  1876. if not isinstance(sig, Signature):
  1877. raise TypeError(
  1878. 'unexpected object {!r} in __signature__ '
  1879. 'attribute'.format(sig))
  1880. return sig
  1881. try:
  1882. partialmethod = obj._partialmethod
  1883. except AttributeError:
  1884. pass
  1885. else:
  1886. if isinstance(partialmethod, functools.partialmethod):
  1887. # Unbound partialmethod (see functools.partialmethod)
  1888. # This means, that we need to calculate the signature
  1889. # as if it's a regular partial object, but taking into
  1890. # account that the first positional argument
  1891. # (usually `self`, or `cls`) will not be passed
  1892. # automatically (as for boundmethods)
  1893. wrapped_sig = _signature_from_callable(
  1894. partialmethod.func,
  1895. follow_wrapper_chains=follow_wrapper_chains,
  1896. skip_bound_arg=skip_bound_arg,
  1897. sigcls=sigcls)
  1898. sig = _signature_get_partial(wrapped_sig, partialmethod, (None,))
  1899. first_wrapped_param = tuple(wrapped_sig.parameters.values())[0]
  1900. new_params = (first_wrapped_param,) + tuple(sig.parameters.values())
  1901. return sig.replace(parameters=new_params)
  1902. if isfunction(obj) or _signature_is_functionlike(obj):
  1903. # If it's a pure Python function, or an object that is duck type
  1904. # of a Python function (Cython functions, for instance), then:
  1905. return _signature_from_function(sigcls, obj)
  1906. if _signature_is_builtin(obj):
  1907. return _signature_from_builtin(sigcls, obj,
  1908. skip_bound_arg=skip_bound_arg)
  1909. if isinstance(obj, functools.partial):
  1910. wrapped_sig = _signature_from_callable(
  1911. obj.func,
  1912. follow_wrapper_chains=follow_wrapper_chains,
  1913. skip_bound_arg=skip_bound_arg,
  1914. sigcls=sigcls)
  1915. return _signature_get_partial(wrapped_sig, obj)
  1916. sig = None
  1917. if isinstance(obj, type):
  1918. # obj is a class or a metaclass
  1919. # First, let's see if it has an overloaded __call__ defined
  1920. # in its metaclass
  1921. call = _signature_get_user_defined_method(type(obj), '__call__')
  1922. if call is not None:
  1923. sig = _signature_from_callable(
  1924. call,
  1925. follow_wrapper_chains=follow_wrapper_chains,
  1926. skip_bound_arg=skip_bound_arg,
  1927. sigcls=sigcls)
  1928. else:
  1929. # Now we check if the 'obj' class has a '__new__' method
  1930. new = _signature_get_user_defined_method(obj, '__new__')
  1931. if new is not None:
  1932. sig = _signature_from_callable(
  1933. new,
  1934. follow_wrapper_chains=follow_wrapper_chains,
  1935. skip_bound_arg=skip_bound_arg,
  1936. sigcls=sigcls)
  1937. else:
  1938. # Finally, we should have at least __init__ implemented
  1939. init = _signature_get_user_defined_method(obj, '__init__')
  1940. if init is not None:
  1941. sig = _signature_from_callable(
  1942. init,
  1943. follow_wrapper_chains=follow_wrapper_chains,
  1944. skip_bound_arg=skip_bound_arg,
  1945. sigcls=sigcls)
  1946. if sig is None:
  1947. # At this point we know, that `obj` is a class, with no user-
  1948. # defined '__init__', '__new__', or class-level '__call__'
  1949. for base in obj.__mro__[:-1]:
  1950. # Since '__text_signature__' is implemented as a
  1951. # descriptor that extracts text signature from the
  1952. # class docstring, if 'obj' is derived from a builtin
  1953. # class, its own '__text_signature__' may be 'None'.
  1954. # Therefore, we go through the MRO (except the last
  1955. # class in there, which is 'object') to find the first
  1956. # class with non-empty text signature.
  1957. try:
  1958. text_sig = base.__text_signature__
  1959. except AttributeError:
  1960. pass
  1961. else:
  1962. if text_sig:
  1963. # If 'obj' class has a __text_signature__ attribute:
  1964. # return a signature based on it
  1965. return _signature_fromstr(sigcls, obj, text_sig)
  1966. # No '__text_signature__' was found for the 'obj' class.
  1967. # Last option is to check if its '__init__' is
  1968. # object.__init__ or type.__init__.
  1969. if type not in obj.__mro__:
  1970. # We have a class (not metaclass), but no user-defined
  1971. # __init__ or __new__ for it
  1972. if (obj.__init__ is object.__init__ and
  1973. obj.__new__ is object.__new__):
  1974. # Return a signature of 'object' builtin.
  1975. return signature(object)
  1976. else:
  1977. raise ValueError(
  1978. 'no signature found for builtin type {!r}'.format(obj))
  1979. elif not isinstance(obj, _NonUserDefinedCallables):
  1980. # An object with __call__
  1981. # We also check that the 'obj' is not an instance of
  1982. # _WrapperDescriptor or _MethodWrapper to avoid
  1983. # infinite recursion (and even potential segfault)
  1984. call = _signature_get_user_defined_method(type(obj), '__call__')
  1985. if call is not None:
  1986. try:
  1987. sig = _signature_from_callable(
  1988. call,
  1989. follow_wrapper_chains=follow_wrapper_chains,
  1990. skip_bound_arg=skip_bound_arg,
  1991. sigcls=sigcls)
  1992. except ValueError as ex:
  1993. msg = 'no signature found for {!r}'.format(obj)
  1994. raise ValueError(msg) from ex
  1995. if sig is not None:
  1996. # For classes and objects we skip the first parameter of their
  1997. # __call__, __new__, or __init__ methods
  1998. if skip_bound_arg:
  1999. return _signature_bound_method(sig)
  2000. else:
  2001. return sig
  2002. if isinstance(obj, types.BuiltinFunctionType):
  2003. # Raise a nicer error message for builtins
  2004. msg = 'no signature found for builtin function {!r}'.format(obj)
  2005. raise ValueError(msg)
  2006. raise ValueError('callable {!r} is not supported by signature'.format(obj))
  2007. class _void:
  2008. """A private marker - used in Parameter & Signature."""
  2009. class _empty:
  2010. """Marker object for Signature.empty and Parameter.empty."""
  2011. class _ParameterKind(enum.IntEnum):
  2012. POSITIONAL_ONLY = 0
  2013. POSITIONAL_OR_KEYWORD = 1
  2014. VAR_POSITIONAL = 2
  2015. KEYWORD_ONLY = 3
  2016. VAR_KEYWORD = 4
  2017. def __str__(self):
  2018. return self._name_
  2019. _POSITIONAL_ONLY = _ParameterKind.POSITIONAL_ONLY
  2020. _POSITIONAL_OR_KEYWORD = _ParameterKind.POSITIONAL_OR_KEYWORD
  2021. _VAR_POSITIONAL = _ParameterKind.VAR_POSITIONAL
  2022. _KEYWORD_ONLY = _ParameterKind.KEYWORD_ONLY
  2023. _VAR_KEYWORD = _ParameterKind.VAR_KEYWORD
  2024. class Parameter:
  2025. """Represents a parameter in a function signature.
  2026. Has the following public attributes:
  2027. * name : str
  2028. The name of the parameter as a string.
  2029. * default : object
  2030. The default value for the parameter if specified. If the
  2031. parameter has no default value, this attribute is set to
  2032. `Parameter.empty`.
  2033. * annotation
  2034. The annotation for the parameter if specified. If the
  2035. parameter has no annotation, this attribute is set to
  2036. `Parameter.empty`.
  2037. * kind : str
  2038. Describes how argument values are bound to the parameter.
  2039. Possible values: `Parameter.POSITIONAL_ONLY`,
  2040. `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`,
  2041. `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`.
  2042. """
  2043. __slots__ = ('_name', '_kind', '_default', '_annotation')
  2044. POSITIONAL_ONLY = _POSITIONAL_ONLY
  2045. POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD
  2046. VAR_POSITIONAL = _VAR_POSITIONAL
  2047. KEYWORD_ONLY = _KEYWORD_ONLY
  2048. VAR_KEYWORD = _VAR_KEYWORD
  2049. empty = _empty
  2050. def __init__(self, name, kind, *, default=_empty, annotation=_empty):
  2051. if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD,
  2052. _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD):
  2053. raise ValueError("invalid value for 'Parameter.kind' attribute")
  2054. self._kind = kind
  2055. if default is not _empty:
  2056. if kind in (_VAR_POSITIONAL, _VAR_KEYWORD):
  2057. msg = '{} parameters cannot have default values'.format(kind)
  2058. raise ValueError(msg)
  2059. self._default = default
  2060. self._annotation = annotation
  2061. if name is _empty:
  2062. raise ValueError('name is a required attribute for Parameter')
  2063. if not isinstance(name, str):
  2064. raise TypeError("name must be a str, not a {!r}".format(name))
  2065. if not name.isidentifier():
  2066. raise ValueError('{!r} is not a valid parameter name'.format(name))
  2067. self._name = name
  2068. def __reduce__(self):
  2069. return (type(self),
  2070. (self._name, self._kind),
  2071. {'_default': self._default,
  2072. '_annotation': self._annotation})
  2073. def __setstate__(self, state):
  2074. self._default = state['_default']
  2075. self._annotation = state['_annotation']
  2076. @property
  2077. def name(self):
  2078. return self._name
  2079. @property
  2080. def default(self):
  2081. return self._default
  2082. @property
  2083. def annotation(self):
  2084. return self._annotation
  2085. @property
  2086. def kind(self):
  2087. return self._kind
  2088. def replace(self, *, name=_void, kind=_void,
  2089. annotation=_void, default=_void):
  2090. """Creates a customized copy of the Parameter."""
  2091. if name is _void:
  2092. name = self._name
  2093. if kind is _void:
  2094. kind = self._kind
  2095. if annotation is _void:
  2096. annotation = self._annotation
  2097. if default is _void:
  2098. default = self._default
  2099. return type(self)(name, kind, default=default, annotation=annotation)
  2100. def __str__(self):
  2101. kind = self.kind
  2102. formatted = self._name
  2103. # Add annotation and default value
  2104. if self._annotation is not _empty:
  2105. formatted = '{}:{}'.format(formatted,
  2106. formatannotation(self._annotation))
  2107. if self._default is not _empty:
  2108. formatted = '{}={}'.format(formatted, repr(self._default))
  2109. if kind == _VAR_POSITIONAL:
  2110. formatted = '*' + formatted
  2111. elif kind == _VAR_KEYWORD:
  2112. formatted = '**' + formatted
  2113. return formatted
  2114. def __repr__(self):
  2115. return '<{} "{}">'.format(self.__class__.__name__, self)
  2116. def __hash__(self):
  2117. return hash((self.name, self.kind, self.annotation, self.default))
  2118. def __eq__(self, other):
  2119. if self is other:
  2120. return True
  2121. if not isinstance(other, Parameter):
  2122. return NotImplemented
  2123. return (self._name == other._name and
  2124. self._kind == other._kind and
  2125. self._default == other._default and
  2126. self._annotation == other._annotation)
  2127. class BoundArguments:
  2128. """Result of `Signature.bind` call. Holds the mapping of arguments
  2129. to the function's parameters.
  2130. Has the following public attributes:
  2131. * arguments : OrderedDict
  2132. An ordered mutable mapping of parameters' names to arguments' values.
  2133. Does not contain arguments' default values.
  2134. * signature : Signature
  2135. The Signature object that created this instance.
  2136. * args : tuple
  2137. Tuple of positional arguments values.
  2138. * kwargs : dict
  2139. Dict of keyword arguments values.
  2140. """
  2141. __slots__ = ('arguments', '_signature', '__weakref__')
  2142. def __init__(self, signature, arguments):
  2143. self.arguments = arguments
  2144. self._signature = signature
  2145. @property
  2146. def signature(self):
  2147. return self._signature
  2148. @property
  2149. def args(self):
  2150. args = []
  2151. for param_name, param in self._signature.parameters.items():
  2152. if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
  2153. break
  2154. try:
  2155. arg = self.arguments[param_name]
  2156. except KeyError:
  2157. # We're done here. Other arguments
  2158. # will be mapped in 'BoundArguments.kwargs'
  2159. break
  2160. else:
  2161. if param.kind == _VAR_POSITIONAL:
  2162. # *args
  2163. args.extend(arg)
  2164. else:
  2165. # plain argument
  2166. args.append(arg)
  2167. return tuple(args)
  2168. @property
  2169. def kwargs(self):
  2170. kwargs = {}
  2171. kwargs_started = False
  2172. for param_name, param in self._signature.parameters.items():
  2173. if not kwargs_started:
  2174. if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
  2175. kwargs_started = True
  2176. else:
  2177. if param_name not in self.arguments:
  2178. kwargs_started = True
  2179. continue
  2180. if not kwargs_started:
  2181. continue
  2182. try:
  2183. arg = self.arguments[param_name]
  2184. except KeyError:
  2185. pass
  2186. else:
  2187. if param.kind == _VAR_KEYWORD:
  2188. # **kwargs
  2189. kwargs.update(arg)
  2190. else:
  2191. # plain keyword argument
  2192. kwargs[param_name] = arg
  2193. return kwargs
  2194. def apply_defaults(self):
  2195. """Set default values for missing arguments.
  2196. For variable-positional arguments (*args) the default is an
  2197. empty tuple.
  2198. For variable-keyword arguments (**kwargs) the default is an
  2199. empty dict.
  2200. """
  2201. arguments = self.arguments
  2202. new_arguments = []
  2203. for name, param in self._signature.parameters.items():
  2204. try:
  2205. new_arguments.append((name, arguments[name]))
  2206. except KeyError:
  2207. if param.default is not _empty:
  2208. val = param.default
  2209. elif param.kind is _VAR_POSITIONAL:
  2210. val = ()
  2211. elif param.kind is _VAR_KEYWORD:
  2212. val = {}
  2213. else:
  2214. # This BoundArguments was likely produced by
  2215. # Signature.bind_partial().
  2216. continue
  2217. new_arguments.append((name, val))
  2218. self.arguments = OrderedDict(new_arguments)
  2219. def __eq__(self, other):
  2220. if self is other:
  2221. return True
  2222. if not isinstance(other, BoundArguments):
  2223. return NotImplemented
  2224. return (self.signature == other.signature and
  2225. self.arguments == other.arguments)
  2226. def __setstate__(self, state):
  2227. self._signature = state['_signature']
  2228. self.arguments = state['arguments']
  2229. def __getstate__(self):
  2230. return {'_signature': self._signature, 'arguments': self.arguments}
  2231. def __repr__(self):
  2232. args = []
  2233. for arg, value in self.arguments.items():
  2234. args.append('{}={!r}'.format(arg, value))
  2235. return '<{} ({})>'.format(self.__class__.__name__, ', '.join(args))
  2236. class Signature:
  2237. """A Signature object represents the overall signature of a function.
  2238. It stores a Parameter object for each parameter accepted by the
  2239. function, as well as information specific to the function itself.
  2240. A Signature object has the following public attributes and methods:
  2241. * parameters : OrderedDict
  2242. An ordered mapping of parameters' names to the corresponding
  2243. Parameter objects (keyword-only arguments are in the same order
  2244. as listed in `code.co_varnames`).
  2245. * return_annotation : object
  2246. The annotation for the return type of the function if specified.
  2247. If the function has no annotation for its return type, this
  2248. attribute is set to `Signature.empty`.
  2249. * bind(*args, **kwargs) -> BoundArguments
  2250. Creates a mapping from positional and keyword arguments to
  2251. parameters.
  2252. * bind_partial(*args, **kwargs) -> BoundArguments
  2253. Creates a partial mapping from positional and keyword arguments
  2254. to parameters (simulating 'functools.partial' behavior.)
  2255. """
  2256. __slots__ = ('_return_annotation', '_parameters')
  2257. _parameter_cls = Parameter
  2258. _bound_arguments_cls = BoundArguments
  2259. empty = _empty
  2260. def __init__(self, parameters=None, *, return_annotation=_empty,
  2261. __validate_parameters__=True):
  2262. """Constructs Signature from the given list of Parameter
  2263. objects and 'return_annotation'. All arguments are optional.
  2264. """
  2265. if parameters is None:
  2266. params = OrderedDict()
  2267. else:
  2268. if __validate_parameters__:
  2269. params = OrderedDict()
  2270. top_kind = _POSITIONAL_ONLY
  2271. kind_defaults = False
  2272. for idx, param in enumerate(parameters):
  2273. kind = param.kind
  2274. name = param.name
  2275. if kind < top_kind:
  2276. msg = 'wrong parameter order: {!r} before {!r}'
  2277. msg = msg.format(top_kind, kind)
  2278. raise ValueError(msg)
  2279. elif kind > top_kind:
  2280. kind_defaults = False
  2281. top_kind = kind
  2282. if kind in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD):
  2283. if param.default is _empty:
  2284. if kind_defaults:
  2285. # No default for this parameter, but the
  2286. # previous parameter of the same kind had
  2287. # a default
  2288. msg = 'non-default argument follows default ' \
  2289. 'argument'
  2290. raise ValueError(msg)
  2291. else:
  2292. # There is a default for this parameter.
  2293. kind_defaults = True
  2294. if name in params:
  2295. msg = 'duplicate parameter name: {!r}'.format(name)
  2296. raise ValueError(msg)
  2297. params[name] = param
  2298. else:
  2299. params = OrderedDict(((param.name, param)
  2300. for param in parameters))
  2301. self._parameters = types.MappingProxyType(params)
  2302. self._return_annotation = return_annotation
  2303. @classmethod
  2304. def from_function(cls, func):
  2305. """Constructs Signature for the given python function."""
  2306. warnings.warn("inspect.Signature.from_function() is deprecated, "
  2307. "use Signature.from_callable()",
  2308. DeprecationWarning, stacklevel=2)
  2309. return _signature_from_function(cls, func)
  2310. @classmethod
  2311. def from_builtin(cls, func):
  2312. """Constructs Signature for the given builtin function."""
  2313. warnings.warn("inspect.Signature.from_builtin() is deprecated, "
  2314. "use Signature.from_callable()",
  2315. DeprecationWarning, stacklevel=2)
  2316. return _signature_from_builtin(cls, func)
  2317. @classmethod
  2318. def from_callable(cls, obj, *, follow_wrapped=True):
  2319. """Constructs Signature for the given callable object."""
  2320. return _signature_from_callable(obj, sigcls=cls,
  2321. follow_wrapper_chains=follow_wrapped)
  2322. @property
  2323. def parameters(self):
  2324. return self._parameters
  2325. @property
  2326. def return_annotation(self):
  2327. return self._return_annotation
  2328. def replace(self, *, parameters=_void, return_annotation=_void):
  2329. """Creates a customized copy of the Signature.
  2330. Pass 'parameters' and/or 'return_annotation' arguments
  2331. to override them in the new copy.
  2332. """
  2333. if parameters is _void:
  2334. parameters = self.parameters.values()
  2335. if return_annotation is _void:
  2336. return_annotation = self._return_annotation
  2337. return type(self)(parameters,
  2338. return_annotation=return_annotation)
  2339. def _hash_basis(self):
  2340. params = tuple(param for param in self.parameters.values()
  2341. if param.kind != _KEYWORD_ONLY)
  2342. kwo_params = {param.name: param for param in self.parameters.values()
  2343. if param.kind == _KEYWORD_ONLY}
  2344. return params, kwo_params, self.return_annotation
  2345. def __hash__(self):
  2346. params, kwo_params, return_annotation = self._hash_basis()
  2347. kwo_params = frozenset(kwo_params.values())
  2348. return hash((params, kwo_params, return_annotation))
  2349. def __eq__(self, other):
  2350. if self is other:
  2351. return True
  2352. if not isinstance(other, Signature):
  2353. return NotImplemented
  2354. return self._hash_basis() == other._hash_basis()
  2355. def _bind(self, args, kwargs, *, partial=False):
  2356. """Private method. Don't use directly."""
  2357. arguments = OrderedDict()
  2358. parameters = iter(self.parameters.values())
  2359. parameters_ex = ()
  2360. arg_vals = iter(args)
  2361. while True:
  2362. # Let's iterate through the positional arguments and corresponding
  2363. # parameters
  2364. try:
  2365. arg_val = next(arg_vals)
  2366. except StopIteration:
  2367. # No more positional arguments
  2368. try:
  2369. param = next(parameters)
  2370. except StopIteration:
  2371. # No more parameters. That's it. Just need to check that
  2372. # we have no `kwargs` after this while loop
  2373. break
  2374. else:
  2375. if param.kind == _VAR_POSITIONAL:
  2376. # That's OK, just empty *args. Let's start parsing
  2377. # kwargs
  2378. break
  2379. elif param.name in kwargs:
  2380. if param.kind == _POSITIONAL_ONLY:
  2381. msg = '{arg!r} parameter is positional only, ' \
  2382. 'but was passed as a keyword'
  2383. msg = msg.format(arg=param.name)
  2384. raise TypeError(msg) from None
  2385. parameters_ex = (param,)
  2386. break
  2387. elif (param.kind == _VAR_KEYWORD or
  2388. param.default is not _empty):
  2389. # That's fine too - we have a default value for this
  2390. # parameter. So, lets start parsing `kwargs`, starting
  2391. # with the current parameter
  2392. parameters_ex = (param,)
  2393. break
  2394. else:
  2395. # No default, not VAR_KEYWORD, not VAR_POSITIONAL,
  2396. # not in `kwargs`
  2397. if partial:
  2398. parameters_ex = (param,)
  2399. break
  2400. else:
  2401. msg = 'missing a required argument: {arg!r}'
  2402. msg = msg.format(arg=param.name)
  2403. raise TypeError(msg) from None
  2404. else:
  2405. # We have a positional argument to process
  2406. try:
  2407. param = next(parameters)
  2408. except StopIteration:
  2409. raise TypeError('too many positional arguments') from None
  2410. else:
  2411. if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY):
  2412. # Looks like we have no parameter for this positional
  2413. # argument
  2414. raise TypeError(
  2415. 'too many positional arguments') from None
  2416. if param.kind == _VAR_POSITIONAL:
  2417. # We have an '*args'-like argument, let's fill it with
  2418. # all positional arguments we have left and move on to
  2419. # the next phase
  2420. values = [arg_val]
  2421. values.extend(arg_vals)
  2422. arguments[param.name] = tuple(values)
  2423. break
  2424. if param.name in kwargs:
  2425. raise TypeError(
  2426. 'multiple values for argument {arg!r}'.format(
  2427. arg=param.name)) from None
  2428. arguments[param.name] = arg_val
  2429. # Now, we iterate through the remaining parameters to process
  2430. # keyword arguments
  2431. kwargs_param = None
  2432. for param in itertools.chain(parameters_ex, parameters):
  2433. if param.kind == _VAR_KEYWORD:
  2434. # Memorize that we have a '**kwargs'-like parameter
  2435. kwargs_param = param
  2436. continue
  2437. if param.kind == _VAR_POSITIONAL:
  2438. # Named arguments don't refer to '*args'-like parameters.
  2439. # We only arrive here if the positional arguments ended
  2440. # before reaching the last parameter before *args.
  2441. continue
  2442. param_name = param.name
  2443. try:
  2444. arg_val = kwargs.pop(param_name)
  2445. except KeyError:
  2446. # We have no value for this parameter. It's fine though,
  2447. # if it has a default value, or it is an '*args'-like
  2448. # parameter, left alone by the processing of positional
  2449. # arguments.
  2450. if (not partial and param.kind != _VAR_POSITIONAL and
  2451. param.default is _empty):
  2452. raise TypeError('missing a required argument: {arg!r}'. \
  2453. format(arg=param_name)) from None
  2454. else:
  2455. if param.kind == _POSITIONAL_ONLY:
  2456. # This should never happen in case of a properly built
  2457. # Signature object (but let's have this check here
  2458. # to ensure correct behaviour just in case)
  2459. raise TypeError('{arg!r} parameter is positional only, '
  2460. 'but was passed as a keyword'. \
  2461. format(arg=param.name))
  2462. arguments[param_name] = arg_val
  2463. if kwargs:
  2464. if kwargs_param is not None:
  2465. # Process our '**kwargs'-like parameter
  2466. arguments[kwargs_param.name] = kwargs
  2467. else:
  2468. raise TypeError(
  2469. 'got an unexpected keyword argument {arg!r}'.format(
  2470. arg=next(iter(kwargs))))
  2471. return self._bound_arguments_cls(self, arguments)
  2472. def bind(*args, **kwargs):
  2473. """Get a BoundArguments object, that maps the passed `args`
  2474. and `kwargs` to the function's signature. Raises `TypeError`
  2475. if the passed arguments can not be bound.
  2476. """
  2477. return args[0]._bind(args[1:], kwargs)
  2478. def bind_partial(*args, **kwargs):
  2479. """Get a BoundArguments object, that partially maps the
  2480. passed `args` and `kwargs` to the function's signature.
  2481. Raises `TypeError` if the passed arguments can not be bound.
  2482. """
  2483. return args[0]._bind(args[1:], kwargs, partial=True)
  2484. def __reduce__(self):
  2485. return (type(self),
  2486. (tuple(self._parameters.values()),),
  2487. {'_return_annotation': self._return_annotation})
  2488. def __setstate__(self, state):
  2489. self._return_annotation = state['_return_annotation']
  2490. def __repr__(self):
  2491. return '<{} {}>'.format(self.__class__.__name__, self)
  2492. def __str__(self):
  2493. result = []
  2494. render_pos_only_separator = False
  2495. render_kw_only_separator = True
  2496. for param in self.parameters.values():
  2497. formatted = str(param)
  2498. kind = param.kind
  2499. if kind == _POSITIONAL_ONLY:
  2500. render_pos_only_separator = True
  2501. elif render_pos_only_separator:
  2502. # It's not a positional-only parameter, and the flag
  2503. # is set to 'True' (there were pos-only params before.)
  2504. result.append('/')
  2505. render_pos_only_separator = False
  2506. if kind == _VAR_POSITIONAL:
  2507. # OK, we have an '*args'-like parameter, so we won't need
  2508. # a '*' to separate keyword-only arguments
  2509. render_kw_only_separator = False
  2510. elif kind == _KEYWORD_ONLY and render_kw_only_separator:
  2511. # We have a keyword-only parameter to render and we haven't
  2512. # rendered an '*args'-like parameter before, so add a '*'
  2513. # separator to the parameters list ("foo(arg1, *, arg2)" case)
  2514. result.append('*')
  2515. # This condition should be only triggered once, so
  2516. # reset the flag
  2517. render_kw_only_separator = False
  2518. result.append(formatted)
  2519. if render_pos_only_separator:
  2520. # There were only positional-only parameters, hence the
  2521. # flag was not reset to 'False'
  2522. result.append('/')
  2523. rendered = '({})'.format(', '.join(result))
  2524. if self.return_annotation is not _empty:
  2525. anno = formatannotation(self.return_annotation)
  2526. rendered += ' -> {}'.format(anno)
  2527. return rendered
  2528. def signature(obj, *, follow_wrapped=True):
  2529. """Get a signature object for the passed callable."""
  2530. return Signature.from_callable(obj, follow_wrapped=follow_wrapped)
  2531. def _main():
  2532. """ Logic for inspecting an object given at command line """
  2533. import argparse
  2534. import importlib
  2535. parser = argparse.ArgumentParser()
  2536. parser.add_argument(
  2537. 'object',
  2538. help="The object to be analysed. "
  2539. "It supports the 'module:qualname' syntax")
  2540. parser.add_argument(
  2541. '-d', '--details', action='store_true',
  2542. help='Display info about the module rather than its source code')
  2543. args = parser.parse_args()
  2544. target = args.object
  2545. mod_name, has_attrs, attrs = target.partition(":")
  2546. try:
  2547. obj = module = importlib.import_module(mod_name)
  2548. except Exception as exc:
  2549. msg = "Failed to import {} ({}: {})".format(mod_name,
  2550. type(exc).__name__,
  2551. exc)
  2552. print(msg, file=sys.stderr)
  2553. exit(2)
  2554. if has_attrs:
  2555. parts = attrs.split(".")
  2556. obj = module
  2557. for part in parts:
  2558. obj = getattr(obj, part)
  2559. if module.__name__ in sys.builtin_module_names:
  2560. print("Can't get info for builtin modules.", file=sys.stderr)
  2561. exit(1)
  2562. if args.details:
  2563. print('Target: {}'.format(target))
  2564. print('Origin: {}'.format(getsourcefile(module)))
  2565. print('Cached: {}'.format(module.__cached__))
  2566. if obj is module:
  2567. print('Loader: {}'.format(repr(module.__loader__)))
  2568. if hasattr(module, '__path__'):
  2569. print('Submodule search path: {}'.format(module.__path__))
  2570. else:
  2571. try:
  2572. __, lineno = findsource(obj)
  2573. except Exception:
  2574. pass
  2575. else:
  2576. print('Line: {}'.format(lineno))
  2577. print('\n')
  2578. else:
  2579. print(getsource(obj))
  2580. if __name__ == "__main__":
  2581. _main()