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