inspect.py 124 KB

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