Mercurial > cpython-lru_cache-weakref
annotate Lib/inspect.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | abc26b51fbfc |
| children | 49fd1c8aeca5 7d69d04522e3 |
| rev | line source |
|---|---|
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1 """Get useful information from live Python objects. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
2 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
3 This module encapsulates the interface provided by the internal special |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
4 attributes (co_*, im_*, tb_*, etc.) in a friendlier fashion. |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
5 It also provides some help for examining source code and class layout. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
6 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
7 Here are some of the useful functions provided by this module: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
8 |
|
45399
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
9 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(), |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
10 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(), |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
11 isroutine() - check object types |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
12 getmembers() - get members of an object that satisfy a given condition |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
13 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
14 getfile(), getsourcefile(), getsource() - find an object's source code |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
15 getdoc(), getcomments() - get documentation on an object |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
16 getmodule() - determine the module that an object came from |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
17 getclasstree() - arrange classes so as to represent their hierarchy |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
18 |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
19 getargspec(), getargvalues(), getcallargs() - get info about function arguments |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
20 getfullargspec() - same, with support for Python-3000 features |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
21 formatargspec(), formatargvalues() - format an argument spec |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
22 getouterframes(), getinnerframes() - get info about frames |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
23 currentframe() - get the current stack frame |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
24 stack(), trace() - get info about frames on the stack or in a traceback |
|
77594
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
25 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
26 signature() - get a Signature object for the callable |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
27 """ |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
28 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
29 # This module is in the public domain. No warranties. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
30 |
|
77594
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
31 __author__ = ('Ka-Ping Yee <ping@lfw.org>', |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
32 'Yury Selivanov <yselivanov@sprymix.com>') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
33 |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
34 import imp |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
35 import importlib.machinery |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
36 import itertools |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
37 import linecache |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
38 import os |
|
45399
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
39 import re |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
40 import sys |
|
45399
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
41 import tokenize |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
42 import types |
|
77312
3de5b053d924
Issue #14987: Add a missing import statement
Brett Cannon <brett@python.org>
parents:
76874
diff
changeset
|
43 import warnings |
|
77594
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
44 import functools |
|
77616
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
45 import builtins |
|
34734
d355da1d7c8f
Apply itemgetter() instead of lambda.
Raymond Hettinger <python@rcn.com>
parents:
34545
diff
changeset
|
46 from operator import attrgetter |
|
77594
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
47 from collections import namedtuple, OrderedDict |
|
64108
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
48 |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
49 # Create constants for the compiler flags in Include/code.h |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
50 # We try to get them from dis to avoid duplication, but fall |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
51 # back to hardcording so the dependency is optional |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
52 try: |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
53 from dis import COMPILER_FLAG_NAMES as _flag_names |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
54 except ImportError: |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
55 CO_OPTIMIZED, CO_NEWLOCALS = 0x1, 0x2 |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
56 CO_VARARGS, CO_VARKEYWORDS = 0x4, 0x8 |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
57 CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40 |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
58 else: |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
59 mod_dict = globals() |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
60 for k, v in _flag_names.items(): |
|
5fce01a51aa5
Address XXX comment in dis.py: inspect.py now attempts to reuse the dis.py compiler flag values before resorting to defining its own
Nick Coghlan <ncoghlan@gmail.com>
parents:
64076
diff
changeset
|
61 mod_dict["CO_" + v] = k |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
62 |
|
45636
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
63 # See Include/object.h |
|
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
64 TPFLAGS_IS_ABSTRACT = 1 << 20 |
|
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
65 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
66 # ----------------------------------------------------------- type-checking |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
67 def ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
68 """Return true if the object is a module. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
69 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
70 Module objects provide these attributes: |
| 60648 | 71 __cached__ pathname to byte compiled file |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
72 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
73 __file__ filename (missing for built-in modules)""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
74 return isinstance(object, types.ModuleType) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
75 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
76 def isclass(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
77 """Return true if the object is a class. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
78 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
79 Class objects provide these attributes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
80 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
81 __module__ name of module in which this class was defined""" |
|
50835
db21d121ba53
Merged revisions 68676 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50817
diff
changeset
|
82 return isinstance(object, type) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
83 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
84 def ismethod(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
85 """Return true if the object is an instance method. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
86 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
87 Instance method objects provide these attributes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
88 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
89 __name__ name with which this method was defined |
|
44026
48af6375207e
Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents:
44021
diff
changeset
|
90 __func__ function object containing implementation of method |
|
48af6375207e
Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents:
44021
diff
changeset
|
91 __self__ instance to which this method is bound""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
92 return isinstance(object, types.MethodType) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
93 |
|
19989
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
94 def ismethoddescriptor(object): |
|
19992
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
95 """Return true if the object is a method descriptor. |
|
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
96 |
|
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
97 But not if ismethod() or isclass() or isfunction() are true. |
|
19989
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
98 |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
99 This is new in Python 2.2, and, for example, is true of int.__add__. |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
100 An object passing this test has a __get__ attribute but not a __set__ |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
101 attribute, but beyond that the set of attributes varies. __name__ is |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
102 usually sensible, and __doc__ often is. |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
103 |
|
19992
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
104 Methods implemented via descriptors that also pass one of the other |
|
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
105 tests return false from the ismethoddescriptor() test, simply because |
|
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
106 the other tests promise more -- you can, e.g., count on having the |
|
44026
48af6375207e
Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents:
44021
diff
changeset
|
107 __func__ attribute (etc) when an object passes ismethod().""" |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
108 if isclass(object) or ismethod(object) or isfunction(object): |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
109 # mutual exclusion |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
110 return False |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
111 tp = type(object) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
112 return hasattr(tp, "__get__") and not hasattr(tp, "__set__") |
|
19989
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
113 |
|
28948
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
114 def isdatadescriptor(object): |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
115 """Return true if the object is a data descriptor. |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
116 |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
117 Data descriptors have both a __get__ and a __set__ attribute. Examples are |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
118 properties (defined in Python) and getsets and members (defined in C). |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
119 Typically, data descriptors will also have __name__ and __doc__ attributes |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
120 (properties, getsets, and members have both of these attributes), but this |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
121 is not guaranteed.""" |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
122 if isclass(object) or ismethod(object) or isfunction(object): |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
123 # mutual exclusion |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
124 return False |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
125 tp = type(object) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
126 return hasattr(tp, "__set__") and hasattr(tp, "__get__") |
|
28948
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
127 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
128 if hasattr(types, 'MemberDescriptorType'): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
129 # CPython and equivalent |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
130 def ismemberdescriptor(object): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
131 """Return true if the object is a member descriptor. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
132 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
133 Member descriptors are specialized descriptors defined in extension |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
134 modules.""" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
135 return isinstance(object, types.MemberDescriptorType) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
136 else: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
137 # Other implementations |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
138 def ismemberdescriptor(object): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
139 """Return true if the object is a member descriptor. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
140 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
141 Member descriptors are specialized descriptors defined in extension |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
142 modules.""" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
143 return False |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
144 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
145 if hasattr(types, 'GetSetDescriptorType'): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
146 # CPython and equivalent |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
147 def isgetsetdescriptor(object): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
148 """Return true if the object is a getset descriptor. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
149 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
150 getset descriptors are specialized descriptors defined in extension |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
151 modules.""" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
152 return isinstance(object, types.GetSetDescriptorType) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
153 else: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
154 # Other implementations |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
155 def isgetsetdescriptor(object): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
156 """Return true if the object is a getset descriptor. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
157 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
158 getset descriptors are specialized descriptors defined in extension |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
159 modules.""" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
160 return False |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
161 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
162 def isfunction(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
163 """Return true if the object is a user-defined function. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
164 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
165 Function objects provide these attributes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
166 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
167 __name__ name with which this function was defined |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
168 __code__ code object containing compiled function bytecode |
|
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
169 __defaults__ tuple of any default values for arguments |
|
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
170 __globals__ global namespace in which this function was defined |
|
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
171 __annotations__ dict of parameter annotations |
|
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
172 __kwdefaults__ dict of keyword only parameters with defaults""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
173 return isinstance(object, types.FunctionType) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
174 |
|
45399
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
175 def isgeneratorfunction(object): |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
176 """Return true if the object is a user-defined generator function. |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
177 |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
178 Generator function objects provides same attributes as functions. |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
179 |
|
64077
4085cc560e9e
Merged revisions 84106 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
62372
diff
changeset
|
180 See help(isfunction) for attributes listing.""" |
|
50568
fb905890d049
Merged revisions 68245 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49720
diff
changeset
|
181 return bool((isfunction(object) or ismethod(object)) and |
|
fb905890d049
Merged revisions 68245 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49720
diff
changeset
|
182 object.__code__.co_flags & CO_GENERATOR) |
|
45399
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
183 |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
184 def isgenerator(object): |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
185 """Return true if the object is a generator. |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
186 |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
187 Generator objects provide these attributes: |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
188 __iter__ defined to support interation over container |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
189 close raises a new GeneratorExit exception inside the |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
190 generator to terminate the iteration |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
191 gi_code code object |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
192 gi_frame frame object or possibly None once the generator has |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
193 been exhausted |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
194 gi_running set to 1 when generator is executing, 0 otherwise |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
195 next return the next item from the container |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
196 send resumes the generator and "sends" a value that becomes |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
197 the result of the current yield-expression |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
198 throw used to raise an exception inside the generator""" |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
199 return isinstance(object, types.GeneratorType) |
|
15d5776f2703
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60878,60880-60892,60894-60898 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44643
diff
changeset
|
200 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
201 def istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
202 """Return true if the object is a traceback. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
203 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
204 Traceback objects provide these attributes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
205 tb_frame frame object at this level |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
206 tb_lasti index of last attempted instruction in bytecode |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
207 tb_lineno current line number in Python source code |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
208 tb_next next inner traceback object (called by this level)""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
209 return isinstance(object, types.TracebackType) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
210 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
211 def isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
212 """Return true if the object is a frame object. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
213 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
214 Frame objects provide these attributes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
215 f_back next outer frame object (this frame's caller) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
216 f_builtins built-in namespace seen by this frame |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
217 f_code code object being executed in this frame |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
218 f_globals global namespace seen by this frame |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
219 f_lasti index of last attempted instruction in bytecode |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
220 f_lineno current line number in Python source code |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
221 f_locals local namespace seen by this frame |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
222 f_trace tracing function for this frame, or None""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
223 return isinstance(object, types.FrameType) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
224 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
225 def iscode(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
226 """Return true if the object is a code object. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
227 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
228 Code objects provide these attributes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
229 co_argcount number of arguments (not including * or ** args) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
230 co_code string of raw compiled bytecode |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
231 co_consts tuple of constants used in the bytecode |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
232 co_filename name of file in which this code object was created |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
233 co_firstlineno number of first line in Python source code |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
234 co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
235 co_lnotab encoded mapping of line numbers to bytecode indices |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
236 co_name name with which this code object was defined |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
237 co_names tuple of names of local variables |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
238 co_nlocals number of local variables |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
239 co_stacksize virtual machine stack space required |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
240 co_varnames tuple of names of arguments and local variables""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
241 return isinstance(object, types.CodeType) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
242 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
243 def isbuiltin(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
244 """Return true if the object is a built-in function or method. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
245 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
246 Built-in functions and methods provide these attributes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
247 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
248 __name__ original name of this function or method |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
249 __self__ instance to which a method is bound, or None""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
250 return isinstance(object, types.BuiltinFunctionType) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
251 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
252 def isroutine(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
253 """Return true if the object is any kind of function or method.""" |
|
19989
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
254 return (isbuiltin(object) |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
255 or isfunction(object) |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
256 or ismethod(object) |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
257 or ismethoddescriptor(object)) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
258 |
|
45636
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
259 def isabstract(object): |
|
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
260 """Return true if the object is an abstract base class (ABC).""" |
|
57210
a0951f11be3d
Merged revisions 76235 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56368
diff
changeset
|
261 return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT) |
|
45636
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
262 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
263 def getmembers(object, predicate=None): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
264 """Return all members of an object as (name, value) pairs sorted by name. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
265 Optionally, only return members that satisfy a given predicate.""" |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
266 if isclass(object): |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
267 mro = (object,) + getmro(object) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
268 else: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
269 mro = () |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
270 results = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
271 for key in dir(object): |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
272 # First try to get the value via __dict__. Some descriptors don't |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
273 # like calling their __get__ (see bug #1785). |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
274 for base in mro: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
275 if key in base.__dict__: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
276 value = base.__dict__[key] |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
277 break |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
278 else: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
279 try: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
280 value = getattr(object, key) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
281 except AttributeError: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
282 continue |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
283 if not predicate or predicate(value): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
284 results.append((key, value)) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
285 results.sort() |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
286 return results |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
287 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
288 Attribute = namedtuple('Attribute', 'name kind defining_class object') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
289 |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
290 def classify_class_attrs(cls): |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
291 """Return list of attribute-descriptor tuples. |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
292 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
293 For each name in dir(cls), the return list contains a 4-tuple |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
294 with these elements: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
295 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
296 0. The name (a string). |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
297 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
298 1. The kind of attribute this is, one of these strings: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
299 'class method' created via classmethod() |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
300 'static method' created via staticmethod() |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
301 'property' created via property() |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
302 'method' any other flavor of method |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
303 'data' not a method |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
304 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
305 2. The class which defined this attribute (a class). |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
306 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
307 3. The object as obtained directly from the defining class's |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
308 __dict__, not via getattr. This is especially important for |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
309 data attributes: C.data is just a data object, but |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
310 C.__dict__['data'] may be a data descriptor with additional |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
311 info, like a __doc__ string. |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
312 """ |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
313 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
314 mro = getmro(cls) |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
315 names = dir(cls) |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
316 result = [] |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
317 for name in names: |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
318 # Get the object associated with the name, and where it was defined. |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
319 # Getting an obj from the __dict__ sometimes reveals more than |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
320 # using getattr. Static and class methods are dramatic examples. |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
321 # Furthermore, some objects may raise an Exception when fetched with |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
322 # getattr(). This is the case with some descriptors (bug #1785). |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
323 # Thus, we only use getattr() as a last resort. |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
324 homecls = None |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
325 for base in (cls,) + mro: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
326 if name in base.__dict__: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
327 obj = base.__dict__[name] |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
328 homecls = base |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
329 break |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
330 else: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
331 obj = getattr(cls, name) |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
332 homecls = getattr(obj, "__objclass__", homecls) |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
333 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
334 # Classify the object. |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
335 if isinstance(obj, staticmethod): |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
336 kind = "static method" |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
337 elif isinstance(obj, classmethod): |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
338 kind = "class method" |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
339 elif isinstance(obj, property): |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
340 kind = "property" |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
341 elif ismethoddescriptor(obj): |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
342 kind = "method" |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
343 elif isdatadescriptor(obj): |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
344 kind = "data" |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
345 else: |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
346 obj_via_getattr = getattr(cls, name) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
347 if (isfunction(obj_via_getattr) or |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
348 ismethoddescriptor(obj_via_getattr)): |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
349 kind = "method" |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
350 else: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
351 kind = "data" |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
352 obj = obj_via_getattr |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
353 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
354 result.append(Attribute(name, kind, homecls, obj)) |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
355 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
356 return result |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
357 |
|
20036
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
358 # ----------------------------------------------------------- class helpers |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
359 |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
360 def getmro(cls): |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
361 "Return tuple of base classes (including cls) in method resolution order." |
|
65922
dc30382ba89a
classic classes are gone; whoppie!
Benjamin Peterson <benjamin@python.org>
parents:
65368
diff
changeset
|
362 return cls.__mro__ |
|
20036
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
363 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
364 # -------------------------------------------------- source code extraction |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
365 def indentsize(line): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
366 """Return the indent size, in spaces, at the start of a line of text.""" |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
367 expline = line.expandtabs() |
|
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
368 return len(expline) - len(expline.lstrip()) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
369 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
370 def getdoc(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
371 """Get the documentation string for an object. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
372 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
373 All tabs are expanded to spaces. To clean up docstrings that are |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
374 indented to line up with blocks of code, any whitespace than can be |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
375 uniformly removed from the second line onwards is removed.""" |
|
22348
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
376 try: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
377 doc = object.__doc__ |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
378 except AttributeError: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
379 return None |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
43274
diff
changeset
|
380 if not isinstance(doc, str): |
|
22348
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
381 return None |
|
47555
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46370
diff
changeset
|
382 return cleandoc(doc) |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46370
diff
changeset
|
383 |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46370
diff
changeset
|
384 def cleandoc(doc): |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46370
diff
changeset
|
385 """Clean up indentation from docstrings. |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46370
diff
changeset
|
386 |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46370
diff
changeset
|
387 Any whitespace that can be uniformly removed from the second line |
|
7d824fd131ad
Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents:
46370
diff
changeset
|
388 onwards is removed.""" |
|
22348
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
389 try: |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
390 lines = doc.expandtabs().split('\n') |
|
22348
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
391 except UnicodeError: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
392 return None |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
393 else: |
| 26467 | 394 # Find minimum indentation of any non-blank lines after first line. |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44098
diff
changeset
|
395 margin = sys.maxsize |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
396 for line in lines[1:]: |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
397 content = len(line.lstrip()) |
| 26467 | 398 if content: |
| 399 indent = len(line) - content | |
| 400 margin = min(margin, indent) | |
| 401 # Remove indentation. | |
| 402 if lines: | |
| 403 lines[0] = lines[0].lstrip() | |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44098
diff
changeset
|
404 if margin < sys.maxsize: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
405 for i in range(1, len(lines)): lines[i] = lines[i][margin:] |
| 26467 | 406 # Remove any trailing or leading blank lines. |
| 407 while lines and not lines[-1]: | |
| 408 lines.pop() | |
| 409 while lines and not lines[0]: | |
| 410 lines.pop(0) | |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
411 return '\n'.join(lines) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
412 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
413 def getfile(object): |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
414 """Work out which source or compiled file an object was defined in.""" |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
415 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
416 if hasattr(object, '__file__'): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
417 return object.__file__ |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
418 raise TypeError('{!r} is a built-in module'.format(object)) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
419 if isclass(object): |
|
17473
339a93b0084c
Robustify getfile() against classes that lie about their __module__s
Ka-Ping Yee <ping@zesty.ca>
parents:
17469
diff
changeset
|
420 object = sys.modules.get(object.__module__) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
421 if hasattr(object, '__file__'): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
422 return object.__file__ |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
423 raise TypeError('{!r} is a built-in class'.format(object)) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
424 if ismethod(object): |
|
44026
48af6375207e
Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents:
44021
diff
changeset
|
425 object = object.__func__ |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
426 if isfunction(object): |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
427 object = object.__code__ |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
428 if istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
429 object = object.tb_frame |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
430 if isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
431 object = object.f_code |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
432 if iscode(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
433 return object.co_filename |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
434 raise TypeError('{!r} is not a module, class, method, ' |
|
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
435 'function, traceback, frame, or code object'.format(object)) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
436 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
437 ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
438 |
|
17370
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
439 def getmoduleinfo(path): |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
440 """Get the module name, suffix, mode, and module type for a given file.""" |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
441 warnings.warn('inspect.getmoduleinfo() is deprecated', DeprecationWarning, |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
442 2) |
|
17370
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
443 filename = os.path.basename(path) |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41715
diff
changeset
|
444 suffixes = [(-len(suffix), suffix, mode, mtype) |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41715
diff
changeset
|
445 for suffix, mode, mtype in imp.get_suffixes()] |
|
17370
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
446 suffixes.sort() # try longest suffixes first, in case they overlap |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
447 for neglen, suffix, mode, mtype in suffixes: |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
448 if filename[neglen:] == suffix: |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
449 return ModuleInfo(filename[:neglen], suffix, mode, mtype) |
|
17370
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
450 |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
451 def getmodulename(path): |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
452 """Return the module name for a given file, or None.""" |
|
78161
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
453 fname = os.path.basename(path) |
|
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
454 # Check for paths that look like an actual module file |
|
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
455 suffixes = [(-len(suffix), suffix) |
|
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
456 for suffix in importlib.machinery.all_suffixes()] |
|
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
457 suffixes.sort() # try longest suffixes first, in case they overlap |
|
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
458 for neglen, suffix in suffixes: |
|
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
459 if fname.endswith(suffix): |
|
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
460 return fname[:neglen] |
|
af7961e1c362
Close #15387: inspect.getmodulename() now uses a new importlib.machinery.all_suffixes() API rather than the deprecated inspect.getmoduleinfo()
Nick Coghlan <ncoghlan@gmail.com>
parents:
77619
diff
changeset
|
461 return None |
|
17370
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
462 |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
463 def getsourcefile(object): |
|
62372
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
464 """Return the filename that can be used to locate an object's source. |
|
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
465 Return None if no way can be identified to get the source. |
|
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
466 """ |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
467 filename = getfile(object) |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
468 all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:] |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
469 all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:] |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
470 if any(filename.endswith(s) for s in all_bytecode_suffixes): |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
471 filename = (os.path.splitext(filename)[0] + |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
472 importlib.machinery.SOURCE_SUFFIXES[0]) |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
473 elif any(filename.endswith(s) for s in |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
474 importlib.machinery.EXTENSION_SUFFIXES): |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
74123
diff
changeset
|
475 return None |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
476 if os.path.exists(filename): |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
477 return filename |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
478 # only return a non-existent filename if the module has a PEP 302 loader |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
479 if hasattr(getmodule(object, filename), '__loader__'): |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
480 return filename |
|
62372
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
481 # or it is in the linecache |
|
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
482 if filename in linecache.cache: |
|
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
483 return filename |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
484 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
485 def getabsfile(object, _filename=None): |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
486 """Return an absolute path to the source or compiled file for an object. |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
487 |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
488 The idea is for each object to have a unique origin, so this routine |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
489 normalizes the result as much as possible.""" |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
490 if _filename is None: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
491 _filename = getsourcefile(object) or getfile(object) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
492 return os.path.normcase(os.path.abspath(_filename)) |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
493 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
494 modulesbyfile = {} |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
495 _filesbymodname = {} |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
496 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
497 def getmodule(object, _filename=None): |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
498 """Return the module an object was defined in, or None if not found.""" |
|
17469
926b64fba5e7
Remove duplicate type objects from isroutine() and isbuiltin().
Ka-Ping Yee <ping@zesty.ca>
parents:
17446
diff
changeset
|
499 if ismodule(object): |
|
926b64fba5e7
Remove duplicate type objects from isroutine() and isbuiltin().
Ka-Ping Yee <ping@zesty.ca>
parents:
17446
diff
changeset
|
500 return object |
|
33455
9e3c52ca3a04
Use __module__ attribute when available instead of using isclass() predicate
Johannes Gijsbers <jlg@dds.nl>
parents:
33172
diff
changeset
|
501 if hasattr(object, '__module__'): |
| 16898 | 502 return sys.modules.get(object.__module__) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
503 # Try the filename to modulename cache |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
504 if _filename is not None and _filename in modulesbyfile: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
505 return sys.modules.get(modulesbyfile[_filename]) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
506 # Try the cache again with the absolute file name |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
507 try: |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
508 file = getabsfile(object, _filename) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
509 except TypeError: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
510 return None |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
511 if file in modulesbyfile: |
|
28419
a9351270385a
Make module lookup a little more robust (certain kinds of fiddling to
Ka-Ping Yee <ping@zesty.ca>
parents:
27297
diff
changeset
|
512 return sys.modules.get(modulesbyfile[file]) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
513 # Update the filename to module name cache and check yet again |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
514 # Copy sys.modules in order to cope with changes while iterating |
|
73789
2ef359d7a2e9
Fix inspect.getmodule to use a copy of sys.modules for iteration (#13487).
Éric Araujo <merwok@netwok.org>
parents:
70799
diff
changeset
|
515 for modname, module in list(sys.modules.items()): |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
35640
diff
changeset
|
516 if ismodule(module) and hasattr(module, '__file__'): |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
517 f = module.__file__ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
518 if f == _filesbymodname.get(modname, None): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
519 # Have already mapped this module, so skip it |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
520 continue |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
521 _filesbymodname[modname] = f |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
522 f = getabsfile(module) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
523 # Always map to the name the module knows itself by |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
524 modulesbyfile[f] = modulesbyfile[ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
525 os.path.realpath(f)] = module.__name__ |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
526 if file in modulesbyfile: |
|
28419
a9351270385a
Make module lookup a little more robust (certain kinds of fiddling to
Ka-Ping Yee <ping@zesty.ca>
parents:
27297
diff
changeset
|
527 return sys.modules.get(modulesbyfile[file]) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
528 # Check the main module |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
529 main = sys.modules['__main__'] |
|
29484
3133b50e99f3
Return None to signal that the module the object was defined in was not found when object has no __name__ attr but is needed to figure out location of object.
Brett Cannon <bcannon@gmail.com>
parents:
28948
diff
changeset
|
530 if not hasattr(object, '__name__'): |
|
3133b50e99f3
Return None to signal that the module the object was defined in was not found when object has no __name__ attr but is needed to figure out location of object.
Brett Cannon <bcannon@gmail.com>
parents:
28948
diff
changeset
|
531 return None |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
532 if hasattr(main, object.__name__): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
533 mainobject = getattr(main, object.__name__) |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
534 if mainobject is object: |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
535 return main |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
536 # Check builtins |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
44026
diff
changeset
|
537 builtin = sys.modules['builtins'] |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
538 if hasattr(builtin, object.__name__): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
539 builtinobject = getattr(builtin, object.__name__) |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
540 if builtinobject is object: |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
541 return builtin |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
542 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
543 def findsource(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
544 """Return the entire source file and starting line number for an object. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
545 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
546 The argument may be a module, class, method, function, traceback, frame, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
547 or code object. The source code is returned as a list of all the lines |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
548 in the file and the line number indexes a line in that list. An IOError |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
549 is raised if the source code cannot be retrieved.""" |
|
70799
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
550 |
|
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
551 file = getfile(object) |
|
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
552 sourcefile = getsourcefile(object) |
|
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
553 if not sourcefile and file[0] + file[-1] != '<>': |
|
54043
e9a7aee8e800
This fix makes, eg, 'pydoc time' work again.
R. David Murray <rdmurray@bitdance.com>
parents:
50885
diff
changeset
|
554 raise IOError('source code not available') |
|
70799
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
555 file = sourcefile if sourcefile else file |
|
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
556 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
557 module = getmodule(object, file) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
558 if module: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
559 lines = linecache.getlines(file, module.__dict__) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
560 else: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
561 lines = linecache.getlines(file) |
|
22486
8ecd1bad2408
Use linecache for loading source code. Closes SF patch 490374.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22348
diff
changeset
|
562 if not lines: |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
563 raise IOError('could not get source code') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
564 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
565 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
566 return lines, 0 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
567 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
568 if isclass(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
569 name = object.__name__ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
570 pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
571 # make some effort to find the best matching class definition: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
572 # use the one with the least indentation, which is the one |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
573 # that's most probably not inside a function definition. |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
574 candidates = [] |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
575 for i in range(len(lines)): |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
576 match = pat.match(lines[i]) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
577 if match: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
578 # if it's at toplevel, it's already the best one |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
579 if lines[i][0] == 'c': |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
580 return lines, i |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
581 # else add whitespace to candidate list |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
582 candidates.append((match.group(1), i)) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
583 if candidates: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
584 # this will sort by whitespace, and by line number, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
585 # less whitespace first |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
586 candidates.sort() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
587 return lines, candidates[0][1] |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
588 else: |
|
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
589 raise IOError('could not find class definition') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
590 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
591 if ismethod(object): |
|
44026
48af6375207e
Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents:
44021
diff
changeset
|
592 object = object.__func__ |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
593 if isfunction(object): |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
594 object = object.__code__ |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
595 if istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
596 object = object.tb_frame |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
597 if isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
598 object = object.f_code |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
599 if iscode(object): |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
600 if not hasattr(object, 'co_firstlineno'): |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
601 raise IOError('could not find function definition') |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
602 lnum = object.co_firstlineno - 1 |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
603 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)') |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
604 while lnum > 0: |
|
17020
953c50f12184
Fix findsource() to work for derived classes.
Ka-Ping Yee <ping@zesty.ca>
parents:
16938
diff
changeset
|
605 if pat.match(lines[lnum]): break |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
606 lnum = lnum - 1 |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
607 return lines, lnum |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
608 raise IOError('could not find code object') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
609 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
610 def getcomments(object): |
|
22639
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
611 """Get lines of comments immediately preceding an object's source code. |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
612 |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
613 Returns None when source can't be found. |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
614 """ |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
615 try: |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
616 lines, lnum = findsource(object) |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
617 except (IOError, TypeError): |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
618 return None |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
619 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
620 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
621 # Look for a comment block at the top of the file. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
622 start = 0 |
|
17446
0184c50a8751
Robustify getcomments() so it doesn't crash on empty files.
Ka-Ping Yee <ping@zesty.ca>
parents:
17370
diff
changeset
|
623 if lines and lines[0][:2] == '#!': start = 1 |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
624 while start < len(lines) and lines[start].strip() in ('', '#'): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
625 start = start + 1 |
|
17446
0184c50a8751
Robustify getcomments() so it doesn't crash on empty files.
Ka-Ping Yee <ping@zesty.ca>
parents:
17370
diff
changeset
|
626 if start < len(lines) and lines[start][:1] == '#': |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
627 comments = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
628 end = start |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
629 while end < len(lines) and lines[end][:1] == '#': |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
630 comments.append(lines[end].expandtabs()) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
631 end = end + 1 |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
632 return ''.join(comments) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
633 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
634 # Look for a preceding block of comments at the same indentation. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
635 elif lnum > 0: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
636 indent = indentsize(lines[lnum]) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
637 end = lnum - 1 |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
638 if end >= 0 and lines[end].lstrip()[:1] == '#' and \ |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
639 indentsize(lines[end]) == indent: |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
640 comments = [lines[end].expandtabs().lstrip()] |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
641 if end > 0: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
642 end = end - 1 |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
643 comment = lines[end].expandtabs().lstrip() |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
644 while comment[:1] == '#' and indentsize(lines[end]) == indent: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
645 comments[:0] = [comment] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
646 end = end - 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
647 if end < 0: break |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
648 comment = lines[end].expandtabs().lstrip() |
|
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
649 while comments and comments[0].strip() == '#': |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
650 comments[:1] = [] |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
651 while comments and comments[-1].strip() == '#': |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
652 comments[-1:] = [] |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
653 return ''.join(comments) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
654 |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
655 class EndOfBlock(Exception): pass |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
656 |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
657 class BlockFinder: |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
658 """Provide a tokeneater() method to detect the end of a code block.""" |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
659 def __init__(self): |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
660 self.indent = 0 |
|
34740
3bb759498767
Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents:
34734
diff
changeset
|
661 self.islambda = False |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
662 self.started = False |
|
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
663 self.passline = False |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
664 self.last = 1 |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
665 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41715
diff
changeset
|
666 def tokeneater(self, type, token, srowcol, erowcol, line): |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
667 if not self.started: |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
668 # look for the first "def", "class" or "lambda" |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
669 if token in ("def", "class", "lambda"): |
|
34740
3bb759498767
Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents:
34734
diff
changeset
|
670 if token == "lambda": |
|
3bb759498767
Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents:
34734
diff
changeset
|
671 self.islambda = True |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
672 self.started = True |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
673 self.passline = True # skip to the end of the line |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
674 elif type == tokenize.NEWLINE: |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
675 self.passline = False # stop skipping when a NEWLINE is seen |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41715
diff
changeset
|
676 self.last = srowcol[0] |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
677 if self.islambda: # lambdas always end at the first NEWLINE |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
678 raise EndOfBlock |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
679 elif self.passline: |
|
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
680 pass |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
681 elif type == tokenize.INDENT: |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
682 self.indent = self.indent + 1 |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
683 self.passline = True |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
684 elif type == tokenize.DEDENT: |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
685 self.indent = self.indent - 1 |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
686 # the end of matching indent/dedent pairs end a block |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
687 # (note that this only works for "def"/"class" blocks, |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
688 # not e.g. for "if: else:" or "try: finally:" blocks) |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
689 if self.indent <= 0: |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
690 raise EndOfBlock |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
691 elif self.indent == 0 and type not in (tokenize.COMMENT, tokenize.NL): |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
692 # any other token on the same indentation level end the previous |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
693 # block as well, except the pseudo-tokens COMMENT and NL. |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
694 raise EndOfBlock |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
695 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
696 def getblock(lines): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
697 """Extract the block of code at the top of the given list of lines.""" |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
698 blockfinder = BlockFinder() |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
699 try: |
|
45888
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
45636
diff
changeset
|
700 tokens = tokenize.generate_tokens(iter(lines).__next__) |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
45636
diff
changeset
|
701 for _token in tokens: |
|
51e24512e305
- Issue #719888: Updated tokenize to use a bytes API. generate_tokens has been
Trent Nelson <trent.nelson@snakebite.org>
parents:
45636
diff
changeset
|
702 blockfinder.tokeneater(*_token) |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
703 except (EndOfBlock, IndentationError): |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
704 pass |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
705 return lines[:blockfinder.last] |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
706 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
707 def getsourcelines(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
708 """Return a list of source lines and starting line number for an object. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
709 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
710 The argument may be a module, class, method, function, traceback, frame, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
711 or code object. The source code is returned as a list of the lines |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
712 corresponding to the object and the line number indicates where in the |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
713 original source file the first line of code was found. An IOError is |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
714 raised if the source code cannot be retrieved.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
715 lines, lnum = findsource(object) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
716 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
717 if ismodule(object): return lines, 0 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
718 else: return getblock(lines[lnum:]), lnum + 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
719 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
720 def getsource(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
721 """Return the text of the source code for an object. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
722 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
723 The argument may be a module, class, method, function, traceback, frame, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
724 or code object. The source code is returned as a single string. An |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
725 IOError is raised if the source code cannot be retrieved.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
726 lines, lnum = getsourcelines(object) |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
727 return ''.join(lines) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
728 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
729 # --------------------------------------------------- class tree extraction |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
730 def walktree(classes, children, parent): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
731 """Recursive helper function for getclasstree().""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
732 results = [] |
|
34734
d355da1d7c8f
Apply itemgetter() instead of lambda.
Raymond Hettinger <python@rcn.com>
parents:
34545
diff
changeset
|
733 classes.sort(key=attrgetter('__module__', '__name__')) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
734 for c in classes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
735 results.append((c, c.__bases__)) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
736 if c in children: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
737 results.append(walktree(children[c], children, c)) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
738 return results |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
739 |
|
54485
18b3d5b37d83
Use true boolean for flag argument.
Georg Brandl <georg@python.org>
parents:
54043
diff
changeset
|
740 def getclasstree(classes, unique=False): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
741 """Arrange the given list of classes into a hierarchy of nested lists. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
742 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
743 Where a nested list appears, it contains classes derived from the class |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
744 whose entry immediately precedes the list. Each entry is a 2-tuple |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
745 containing a class and a tuple of its base classes. If the 'unique' |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
746 argument is true, exactly one entry appears in the returned structure |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
747 for each class in the given list. Otherwise, classes using multiple |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
748 inheritance and their descendants will appear multiple times.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
749 children = {} |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
750 roots = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
751 for c in classes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
752 if c.__bases__: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
753 for parent in c.__bases__: |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
754 if not parent in children: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
755 children[parent] = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
756 children[parent].append(c) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
757 if unique and parent in classes: break |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
758 elif c not in roots: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
759 roots.append(c) |
|
23634
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
760 for parent in children: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
761 if parent not in classes: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
762 roots.append(parent) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
763 return walktree(roots, children, None) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
764 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
765 # ------------------------------------------------ argument list extraction |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
766 Arguments = namedtuple('Arguments', 'args, varargs, varkw') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
767 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
768 def getargs(co): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
769 """Get information about the arguments accepted by a code object. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
770 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
771 Three things are returned: (args, varargs, varkw), where |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
772 'args' is the list of argument names. Keyword-only arguments are |
|
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
773 appended. 'varargs' and 'varkw' are the names of the * and ** |
|
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
774 arguments or None.""" |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
775 args, varargs, kwonlyargs, varkw = _getfullargs(co) |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
776 return Arguments(args + kwonlyargs, varargs, varkw) |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
777 |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
778 def _getfullargs(co): |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
779 """Get information about the arguments accepted by a code object. |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
780 |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
781 Four things are returned: (args, varargs, kwonlyargs, varkw), where |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
782 'args' and 'kwonlyargs' are lists of argument names, and 'varargs' |
|
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
783 and 'varkw' are the names of the * and ** arguments or None.""" |
|
29613
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
784 |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
785 if not iscode(co): |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
786 raise TypeError('{!r} is not a code object'.format(co)) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
787 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
788 nargs = co.co_argcount |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
789 names = co.co_varnames |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
790 nkwargs = co.co_kwonlyargcount |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
791 args = list(names[:nargs]) |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
792 kwonlyargs = list(names[nargs:nargs+nkwargs]) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
793 step = 0 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
794 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
795 nargs += nkwargs |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
796 varargs = None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
797 if co.co_flags & CO_VARARGS: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
798 varargs = co.co_varnames[nargs] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
799 nargs = nargs + 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
800 varkw = None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
801 if co.co_flags & CO_VARKEYWORDS: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
802 varkw = co.co_varnames[nargs] |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
803 return args, varargs, kwonlyargs, varkw |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
804 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
805 |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
806 ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
807 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
808 def getargspec(func): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
809 """Get the names and default values of a function's arguments. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
810 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
811 A tuple of four things is returned: (args, varargs, varkw, defaults). |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
812 'args' is a list of the argument names. |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
813 'args' will include keyword-only argument names. |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
814 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
|
29613
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
815 'defaults' is an n-tuple of the default values of the last n arguments. |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
816 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
817 Use the getfullargspec() API for Python-3000 code, as annotations |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
818 and keyword arguments are supported. getargspec() will raise ValueError |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
819 if the func has either annotations or keyword arguments. |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
820 """ |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
821 |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
822 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = \ |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
823 getfullargspec(func) |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
824 if kwonlyargs or ann: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42309
diff
changeset
|
825 raise ValueError("Function has keyword-only arguments or annotations" |
|
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42309
diff
changeset
|
826 ", use getfullargspec() API which can support them") |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
827 return ArgSpec(args, varargs, varkw, defaults) |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
828 |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
829 FullArgSpec = namedtuple('FullArgSpec', |
|
49720
cd08b00acd6a
change the named tuple returned by inspect.getfullargspec to have a 'kwonlydefaults' (as claimed by the docs) attribute instead of 'kwdefaults'
Benjamin Peterson <benjamin@python.org>
parents:
49587
diff
changeset
|
830 'args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations') |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
831 |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
832 def getfullargspec(func): |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
833 """Get the names and default values of a function's arguments. |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
834 |
|
43274
7052593574bf
Fix docstring for getfullargspec().
Brett Cannon <bcannon@gmail.com>
parents:
43020
diff
changeset
|
835 A tuple of seven things is returned: |
|
7052593574bf
Fix docstring for getfullargspec().
Brett Cannon <bcannon@gmail.com>
parents:
43020
diff
changeset
|
836 (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations). |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
837 'args' is a list of the argument names. |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
838 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
839 'defaults' is an n-tuple of the default values of the last n arguments. |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
840 'kwonlyargs' is a list of keyword-only argument names. |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
841 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults. |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
842 'annotations' is a dictionary mapping argument names to annotations. |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
843 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
844 The first four items in the tuple correspond to getargspec(). |
|
29613
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
845 """ |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
846 |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
847 if ismethod(func): |
|
44026
48af6375207e
Removed the API to create unbound methods and simplified the API for bound methods. The signature is PyMethod_New(func, instance).
Christian Heimes <christian@cheimes.de>
parents:
44021
diff
changeset
|
848 func = func.__func__ |
|
29613
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
849 if not isfunction(func): |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
850 raise TypeError('{!r} is not a Python function'.format(func)) |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
851 args, varargs, kwonlyargs, varkw = _getfullargs(func.__code__) |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
852 return FullArgSpec(args, varargs, varkw, func.__defaults__, |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
853 kwonlyargs, func.__kwdefaults__, func.__annotations__) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
854 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
855 ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
856 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
857 def getargvalues(frame): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
858 """Get information about arguments passed into a particular frame. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
859 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
860 A tuple of four things is returned: (args, varargs, varkw, locals). |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
861 'args' is a list of the argument names. |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
862 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
863 'locals' is the locals dictionary of the given frame.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
864 args, varargs, varkw = getargs(frame.f_code) |
|
49587
f66688e69f66
Merged revisions 66974,66977,66984,66989,66992,66994-66996,66998-67000,67007,67015 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48732
diff
changeset
|
865 return ArgInfo(args, varargs, varkw, frame.f_locals) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
866 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
867 def formatannotation(annotation, base_module=None): |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
868 if isinstance(annotation, type): |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
44026
diff
changeset
|
869 if annotation.__module__ in ('builtins', base_module): |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
870 return annotation.__name__ |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
871 return annotation.__module__+'.'+annotation.__name__ |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
872 return repr(annotation) |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
873 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
874 def formatannotationrelativeto(object): |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
875 module = getattr(object, '__module__', None) |
|
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
876 def _formatannotation(annotation): |
|
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
877 return formatannotation(annotation, module) |
|
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
878 return _formatannotation |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
879 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
880 def formatargspec(args, varargs=None, varkw=None, defaults=None, |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
881 kwonlyargs=(), kwonlydefaults={}, annotations={}, |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
882 formatarg=str, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
883 formatvarargs=lambda name: '*' + name, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
884 formatvarkw=lambda name: '**' + name, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
885 formatvalue=lambda value: '=' + repr(value), |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
886 formatreturns=lambda text: ' -> ' + text, |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
887 formatannotation=formatannotation): |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
888 """Format an argument spec from the values returned by getargspec |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
889 or getfullargspec. |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
890 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
891 The first seven arguments are (args, varargs, varkw, defaults, |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
892 kwonlyargs, kwonlydefaults, annotations). The other five arguments |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
893 are the corresponding optional formatting functions that are called to |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
894 turn names and values into strings. The last argument is an optional |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
895 function to format the sequence of arguments.""" |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
896 def formatargandannotation(arg): |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
897 result = formatarg(arg) |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
898 if arg in annotations: |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
899 result += ': ' + formatannotation(annotations[arg]) |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
900 return result |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
901 specs = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
902 if defaults: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
903 firstdefault = len(args) - len(defaults) |
|
50885
9f45fa27b838
Merged revisions 68633,68648,68667,68706,68718,68720-68721,68724-68727,68739 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50835
diff
changeset
|
904 for i, arg in enumerate(args): |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
905 spec = formatargandannotation(arg) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
906 if defaults and i >= firstdefault: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
907 spec = spec + formatvalue(defaults[i - firstdefault]) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
908 specs.append(spec) |
|
23613
4a1f5bd486ff
Replaced boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23486
diff
changeset
|
909 if varargs is not None: |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
910 specs.append(formatvarargs(formatargandannotation(varargs))) |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
911 else: |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
912 if kwonlyargs: |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
913 specs.append('*') |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
914 if kwonlyargs: |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
915 for kwonlyarg in kwonlyargs: |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
916 spec = formatargandannotation(kwonlyarg) |
|
50817
0b1a8f56117a
fix inspect.formatargspec on functions with keyword-only arguments without defaults #4959
Benjamin Peterson <benjamin@python.org>
parents:
50812
diff
changeset
|
917 if kwonlydefaults and kwonlyarg in kwonlydefaults: |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
918 spec += formatvalue(kwonlydefaults[kwonlyarg]) |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
919 specs.append(spec) |
|
23613
4a1f5bd486ff
Replaced boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23486
diff
changeset
|
920 if varkw is not None: |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
921 specs.append(formatvarkw(formatargandannotation(varkw))) |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
922 result = '(' + ', '.join(specs) + ')' |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
923 if 'return' in annotations: |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
924 result += formatreturns(formatannotation(annotations['return'])) |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
925 return result |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
926 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
927 def formatargvalues(args, varargs, varkw, locals, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
928 formatarg=str, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
929 formatvarargs=lambda name: '*' + name, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
930 formatvarkw=lambda name: '**' + name, |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
931 formatvalue=lambda value: '=' + repr(value)): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
932 """Format an argument spec from the 4 values returned by getargvalues. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
933 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
934 The first four arguments are (args, varargs, varkw, locals). The |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
935 next four arguments are the corresponding optional formatting functions |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
936 that are called to turn names and values into strings. The ninth |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
937 argument is an optional function to format the sequence of arguments.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
938 def convert(name, locals=locals, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
939 formatarg=formatarg, formatvalue=formatvalue): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
940 return formatarg(name) + formatvalue(locals[name]) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
941 specs = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
942 for i in range(len(args)): |
|
66470
45e49269e0d8
Merged revisions 85530,85532-85534,85538-85543,85546-85548 via svnmerge from
Georg Brandl <georg@python.org>
parents:
64077
diff
changeset
|
943 specs.append(convert(args[i])) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
944 if varargs: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
945 specs.append(formatvarargs(varargs) + formatvalue(locals[varargs])) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
946 if varkw: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
947 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw])) |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
948 return '(' + ', '.join(specs) + ')' |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
949 |
|
70940
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
950 def _missing_arguments(f_name, argnames, pos, values): |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
951 names = [repr(name) for name in argnames if name not in values] |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
952 missing = len(names) |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
953 if missing == 1: |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
954 s = names[0] |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
955 elif missing == 2: |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
956 s = "{} and {}".format(*names) |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
957 else: |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
958 tail = ", {} and {}".format(names[-2:]) |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
959 del names[-2:] |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
960 s = ", ".join(names) + tail |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
961 raise TypeError("%s() missing %i required %s argument%s: %s" % |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
962 (f_name, missing, |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
963 "positional" if pos else "keyword-only", |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
964 "" if missing == 1 else "s", s)) |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
965 |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
966 def _too_many(f_name, args, kwonly, varargs, defcount, given, values): |
|
70652
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
967 atleast = len(args) - defcount |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
968 kwonly_given = len([arg for arg in kwonly if arg in values]) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
969 if varargs: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
970 plural = atleast != 1 |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
971 sig = "at least %d" % (atleast,) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
972 elif defcount: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
973 plural = True |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
974 sig = "from %d to %d" % (atleast, len(args)) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
975 else: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
976 plural = len(args) != 1 |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
977 sig = str(len(args)) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
978 kwonly_sig = "" |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
979 if kwonly_given: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
980 msg = " positional argument%s (and %d keyword-only argument%s)" |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
981 kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given, |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
982 "s" if kwonly_given != 1 else "")) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
983 raise TypeError("%s() takes %s positional argument%s but %d%s %s given" % |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
984 (f_name, sig, "s" if plural else "", given, kwonly_sig, |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
985 "was" if given == 1 and not kwonly_given else "were")) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
986 |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
987 def getcallargs(func, *positional, **named): |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
988 """Get the mapping of arguments to values. |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
989 |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
990 A dict is returned, with keys the function argument names (including the |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
991 names of the * and ** arguments, if any), and values the respective bound |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
992 values from 'positional' and 'named'.""" |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
993 spec = getfullargspec(func) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
994 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
995 f_name = func.__name__ |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
996 arg2value = {} |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
997 |
|
70652
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
998 |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
999 if ismethod(func) and func.__self__ is not None: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1000 # implicit 'self' (or 'cls' for classmethods) argument |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1001 positional = (func.__self__,) + positional |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1002 num_pos = len(positional) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1003 num_args = len(args) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1004 num_defaults = len(defaults) if defaults else 0 |
|
70652
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1005 |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1006 n = min(num_pos, num_args) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1007 for i in range(n): |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1008 arg2value[args[i]] = positional[i] |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1009 if varargs: |
|
70652
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1010 arg2value[varargs] = tuple(positional[n:]) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1011 possible_kwargs = set(args + kwonlyargs) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1012 if varkw: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1013 arg2value[varkw] = {} |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1014 for kw, value in named.items(): |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1015 if kw not in possible_kwargs: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1016 if not varkw: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1017 raise TypeError("%s() got an unexpected keyword argument %r" % |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1018 (f_name, kw)) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1019 arg2value[varkw][kw] = value |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1020 continue |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1021 if kw in arg2value: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1022 raise TypeError("%s() got multiple values for argument %r" % |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1023 (f_name, kw)) |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1024 arg2value[kw] = value |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1025 if num_pos > num_args and not varargs: |
|
70940
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1026 _too_many(f_name, args, kwonlyargs, varargs, num_defaults, |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1027 num_pos, arg2value) |
|
70652
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1028 if num_pos < num_args: |
|
70940
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1029 req = args[:num_args - num_defaults] |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1030 for arg in req: |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1031 if arg not in arg2value: |
|
70940
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1032 _missing_arguments(f_name, req, True, arg2value) |
|
70652
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1033 for i, arg in enumerate(args[num_args - num_defaults:]): |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1034 if arg not in arg2value: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1035 arg2value[arg] = defaults[i] |
|
70940
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1036 missing = 0 |
|
70652
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1037 for kwarg in kwonlyargs: |
|
44d46d74ef4f
greatly improve argument parsing error messages (closes #12265)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
1038 if kwarg not in arg2value: |
|
70940
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1039 if kwarg in kwonlydefaults: |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1040 arg2value[kwarg] = kwonlydefaults[kwarg] |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1041 else: |
|
70940
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1042 missing += 1 |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1043 if missing: |
|
52744a5a9260
give the names of missing positional or keyword-only arguments (closes #12356)
Benjamin Peterson <benjamin@python.org>
parents:
70800
diff
changeset
|
1044 _missing_arguments(f_name, kwonlyargs, False, arg2value) |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1045 return arg2value |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1046 |
|
77616
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1047 ClosureVars = namedtuple('ClosureVars', 'nonlocals globals builtins unbound') |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1048 |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1049 def getclosurevars(func): |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1050 """ |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1051 Get the mapping of free variables to their current values. |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1052 |
|
78186
abc26b51fbfc
Issue #15401: Fix typo in inspect.getclosurevars docstring.
Meador Inge <meadori@gmail.com>
parents:
78161
diff
changeset
|
1053 Returns a named tuple of dicts mapping the current nonlocal, global |
|
77616
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1054 and builtin references as seen by the body of the function. A final |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1055 set of unbound names that could not be resolved is also provided. |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1056 """ |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1057 |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1058 if ismethod(func): |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1059 func = func.__func__ |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1060 |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1061 if not isfunction(func): |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1062 raise TypeError("'{!r}' is not a Python function".format(func)) |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1063 |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1064 code = func.__code__ |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1065 # Nonlocal references are named in co_freevars and resolved |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1066 # by looking them up in __closure__ by positional index |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1067 if func.__closure__ is None: |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1068 nonlocal_vars = {} |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1069 else: |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1070 nonlocal_vars = { |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1071 var : cell.cell_contents |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1072 for var, cell in zip(code.co_freevars, func.__closure__) |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1073 } |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1074 |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1075 # Global and builtin references are named in co_names and resolved |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1076 # by looking them up in __globals__ or __builtins__ |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1077 global_ns = func.__globals__ |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1078 builtin_ns = global_ns.get("__builtins__", builtins.__dict__) |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1079 if ismodule(builtin_ns): |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1080 builtin_ns = builtin_ns.__dict__ |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1081 global_vars = {} |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1082 builtin_vars = {} |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1083 unbound_names = set() |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1084 for name in code.co_names: |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1085 if name in ("None", "True", "False"): |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1086 # Because these used to be builtins instead of keywords, they |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1087 # may still show up as name references. We ignore them. |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1088 continue |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1089 try: |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1090 global_vars[name] = global_ns[name] |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1091 except KeyError: |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1092 try: |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1093 builtin_vars[name] = builtin_ns[name] |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1094 except KeyError: |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1095 unbound_names.add(name) |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1096 |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1097 return ClosureVars(nonlocal_vars, global_vars, |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1098 builtin_vars, unbound_names) |
|
487fe648de56
Close #13062: Add inspect.getclosurevars to simplify testing stateful closures
Nick Coghlan <ncoghlan@gmail.com>
parents:
77594
diff
changeset
|
1099 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1100 # -------------------------------------------------- stack frame extraction |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
1101 |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
1102 Traceback = namedtuple('Traceback', 'filename lineno function code_context index') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
1103 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1104 def getframeinfo(frame, context=1): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1105 """Get information about a frame or traceback object. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1106 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1107 A tuple of five things is returned: the filename, the line number of |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1108 the current line, the function name, a list of lines of context from |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1109 the source code, and the index of the current line within that list. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1110 The optional second argument specifies the number of lines of context |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1111 to return, which are centered around the current line.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1112 if istraceback(frame): |
|
32253
ec6f9a190831
[Bug #954364] inspect.getframeinfo() sometimes produces incorrect traceback line #s; fix is to look at tb.tb_lineno, not tb.frame.f_lineno. Patch from Robin Becker and me.
Andrew M. Kuchling <amk@amk.ca>
parents:
31054
diff
changeset
|
1113 lineno = frame.tb_lineno |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1114 frame = frame.tb_frame |
|
32253
ec6f9a190831
[Bug #954364] inspect.getframeinfo() sometimes produces incorrect traceback line #s; fix is to look at tb.tb_lineno, not tb.frame.f_lineno. Patch from Robin Becker and me.
Andrew M. Kuchling <amk@amk.ca>
parents:
31054
diff
changeset
|
1115 else: |
|
ec6f9a190831
[Bug #954364] inspect.getframeinfo() sometimes produces incorrect traceback line #s; fix is to look at tb.tb_lineno, not tb.frame.f_lineno. Patch from Robin Becker and me.
Andrew M. Kuchling <amk@amk.ca>
parents:
31054
diff
changeset
|
1116 lineno = frame.f_lineno |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1117 if not isframe(frame): |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
1118 raise TypeError('{!r} is not a frame or traceback object'.format(frame)) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1119 |
|
22486
8ecd1bad2408
Use linecache for loading source code. Closes SF patch 490374.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22348
diff
changeset
|
1120 filename = getsourcefile(frame) or getfile(frame) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1121 if context > 0: |
|
19694
3716b116fb31
The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents:
18609
diff
changeset
|
1122 start = lineno - 1 - context//2 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1123 try: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1124 lines, lnum = findsource(frame) |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
1125 except IOError: |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
1126 lines = index = None |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
1127 else: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1128 start = max(start, 1) |
|
32352
348db77a07e9
SF bug #973092: inspect.getframeinfo bug if 'context' is to big
Raymond Hettinger <python@rcn.com>
parents:
32253
diff
changeset
|
1129 start = max(0, min(start, len(lines) - context)) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1130 lines = lines[start:start+context] |
|
16887
14dbde1ecdea
Add getlineno() routine to account for LINENO optimization.
Ka-Ping Yee <ping@zesty.ca>
parents:
16833
diff
changeset
|
1131 index = lineno - 1 - start |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1132 else: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1133 lines = index = None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1134 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
1135 return Traceback(filename, lineno, frame.f_code.co_name, lines, index) |
|
16887
14dbde1ecdea
Add getlineno() routine to account for LINENO optimization.
Ka-Ping Yee <ping@zesty.ca>
parents:
16833
diff
changeset
|
1136 |
|
14dbde1ecdea
Add getlineno() routine to account for LINENO optimization.
Ka-Ping Yee <ping@zesty.ca>
parents:
16833
diff
changeset
|
1137 def getlineno(frame): |
|
14dbde1ecdea
Add getlineno() routine to account for LINENO optimization.
Ka-Ping Yee <ping@zesty.ca>
parents:
16833
diff
changeset
|
1138 """Get the line number from a frame object, allowing for optimization.""" |
| 24844 | 1139 # FrameType.f_lineno is now a descriptor that grovels co_lnotab |
| 1140 return frame.f_lineno | |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1141 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1142 def getouterframes(frame, context=1): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1143 """Get a list of records for a frame and all higher (calling) frames. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1144 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1145 Each record contains a frame object, filename, line number, function |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1146 name, a list of lines of context, and index within the context.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1147 framelist = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1148 while frame: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1149 framelist.append((frame,) + getframeinfo(frame, context)) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1150 frame = frame.f_back |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1151 return framelist |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1152 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1153 def getinnerframes(tb, context=1): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1154 """Get a list of records for a traceback's frame and all lower frames. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1155 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1156 Each record contains a frame object, filename, line number, function |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1157 name, a list of lines of context, and index within the context.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1158 framelist = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1159 while tb: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1160 framelist.append((tb.tb_frame,) + getframeinfo(tb, context)) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1161 tb = tb.tb_next |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1162 return framelist |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1163 |
|
63876
795eb12403b9
don't alias directly, so that extra arguments don't appear #6678
Benjamin Peterson <benjamin@python.org>
parents:
62492
diff
changeset
|
1164 def currentframe(): |
| 63877 | 1165 """Return the frame of the caller or None if this is not possible.""" |
|
63876
795eb12403b9
don't alias directly, so that extra arguments don't appear #6678
Benjamin Peterson <benjamin@python.org>
parents:
62492
diff
changeset
|
1166 return sys._getframe(1) if hasattr(sys, "_getframe") else None |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1167 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1168 def stack(context=1): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1169 """Return a list of records for the stack above the caller's frame.""" |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
1170 return getouterframes(sys._getframe(1), context) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1171 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1172 def trace(context=1): |
|
16833
bbdeb6d4dea1
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16790
diff
changeset
|
1173 """Return a list of records for the stack below the current exception.""" |
|
23281
f5fd7e295897
Clean up uses of some deprecated features.
Fred Drake <fdrake@acm.org>
parents:
22639
diff
changeset
|
1174 return getinnerframes(sys.exc_info()[2], context) |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1175 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1176 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1177 # ------------------------------------------------ static version of getattr |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1178 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1179 _sentinel = object() |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1180 |
|
66292
d57181acaf8c
Issue 9732: fetch the method resolution order from the type metaclass directly in getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66289
diff
changeset
|
1181 def _static_getmro(klass): |
|
d57181acaf8c
Issue 9732: fetch the method resolution order from the type metaclass directly in getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66289
diff
changeset
|
1182 return type.__dict__['__mro__'].__get__(klass) |
|
d57181acaf8c
Issue 9732: fetch the method resolution order from the type metaclass directly in getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66289
diff
changeset
|
1183 |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1184 def _check_instance(obj, attr): |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1185 instance_dict = {} |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1186 try: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1187 instance_dict = object.__getattribute__(obj, "__dict__") |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1188 except AttributeError: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1189 pass |
|
68575
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1190 return dict.get(instance_dict, attr, _sentinel) |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1191 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1192 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1193 def _check_class(klass, attr): |
|
66292
d57181acaf8c
Issue 9732: fetch the method resolution order from the type metaclass directly in getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66289
diff
changeset
|
1194 for entry in _static_getmro(klass): |
|
74062
56731ccf2e86
Fix inspect.getattr_static to work on modules (again).
Michael Foord <michael@voidspace.org.uk>
parents:
73789
diff
changeset
|
1195 if _shadowed_dict(type(entry)) is _sentinel: |
|
68575
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1196 try: |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1197 return entry.__dict__[attr] |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1198 except KeyError: |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1199 pass |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1200 return _sentinel |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1201 |
|
66293
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1202 def _is_type(obj): |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1203 try: |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1204 _static_getmro(obj) |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1205 except TypeError: |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1206 return False |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1207 return True |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1208 |
|
68575
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1209 def _shadowed_dict(klass): |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1210 dict_attr = type.__dict__["__dict__"] |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1211 for entry in _static_getmro(klass): |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1212 try: |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1213 class_dict = dict_attr.__get__(entry)["__dict__"] |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1214 except KeyError: |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1215 pass |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1216 else: |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1217 if not (type(class_dict) is types.GetSetDescriptorType and |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1218 class_dict.__name__ == "__dict__" and |
|
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1219 class_dict.__objclass__ is entry): |
|
74062
56731ccf2e86
Fix inspect.getattr_static to work on modules (again).
Michael Foord <michael@voidspace.org.uk>
parents:
73789
diff
changeset
|
1220 return class_dict |
|
56731ccf2e86
Fix inspect.getattr_static to work on modules (again).
Michael Foord <michael@voidspace.org.uk>
parents:
73789
diff
changeset
|
1221 return _sentinel |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1222 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1223 def getattr_static(obj, attr, default=_sentinel): |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1224 """Retrieve attributes without triggering dynamic lookup via the |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1225 descriptor protocol, __getattr__ or __getattribute__. |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1226 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1227 Note: this function may not be able to retrieve all attributes |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1228 that getattr can fetch (like dynamically created attributes) |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1229 and may find attributes that getattr can't (like descriptors |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1230 that raise AttributeError). It can also return descriptor objects |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1231 instead of instance members in some cases. See the |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1232 documentation for details. |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1233 """ |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1234 instance_result = _sentinel |
|
66293
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1235 if not _is_type(obj): |
|
66289
838f6948deb8
Issue 9732: remove use of __class__ in inspect.getattr_static and note the mro exception to code execution
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66280
diff
changeset
|
1236 klass = type(obj) |
|
74062
56731ccf2e86
Fix inspect.getattr_static to work on modules (again).
Michael Foord <michael@voidspace.org.uk>
parents:
73789
diff
changeset
|
1237 dict_attr = _shadowed_dict(klass) |
|
56731ccf2e86
Fix inspect.getattr_static to work on modules (again).
Michael Foord <michael@voidspace.org.uk>
parents:
73789
diff
changeset
|
1238 if (dict_attr is _sentinel or |
|
56731ccf2e86
Fix inspect.getattr_static to work on modules (again).
Michael Foord <michael@voidspace.org.uk>
parents:
73789
diff
changeset
|
1239 type(dict_attr) is types.MemberDescriptorType): |
|
68575
8c7eac34f7bf
Closes issue 11133. Fixes two cases where inspect.getattr_static could trigger code execution
Michael Foord <michael@python.org>
parents:
68257
diff
changeset
|
1240 instance_result = _check_instance(obj, attr) |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1241 else: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1242 klass = obj |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1243 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1244 klass_result = _check_class(klass, attr) |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1245 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1246 if instance_result is not _sentinel and klass_result is not _sentinel: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1247 if (_check_class(type(klass_result), '__get__') is not _sentinel and |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1248 _check_class(type(klass_result), '__set__') is not _sentinel): |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1249 return klass_result |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1250 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1251 if instance_result is not _sentinel: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1252 return instance_result |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1253 if klass_result is not _sentinel: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1254 return klass_result |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1255 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1256 if obj is klass: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1257 # for types we check the metaclass too |
|
66292
d57181acaf8c
Issue 9732: fetch the method resolution order from the type metaclass directly in getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66289
diff
changeset
|
1258 for entry in _static_getmro(type(klass)): |
|
74122
8f33758df19a
Metaclasses with metaclasses with a __dict__ descriptor can no longer trigger code execution with inspect.getattr_static.
Michael Foord <michael@voidspace.org.uk>
parents:
74104
diff
changeset
|
1259 if _shadowed_dict(type(entry)) is _sentinel: |
|
8f33758df19a
Metaclasses with metaclasses with a __dict__ descriptor can no longer trigger code execution with inspect.getattr_static.
Michael Foord <michael@voidspace.org.uk>
parents:
74104
diff
changeset
|
1260 try: |
|
8f33758df19a
Metaclasses with metaclasses with a __dict__ descriptor can no longer trigger code execution with inspect.getattr_static.
Michael Foord <michael@voidspace.org.uk>
parents:
74104
diff
changeset
|
1261 return entry.__dict__[attr] |
|
8f33758df19a
Metaclasses with metaclasses with a __dict__ descriptor can no longer trigger code execution with inspect.getattr_static.
Michael Foord <michael@voidspace.org.uk>
parents:
74104
diff
changeset
|
1262 except KeyError: |
|
8f33758df19a
Metaclasses with metaclasses with a __dict__ descriptor can no longer trigger code execution with inspect.getattr_static.
Michael Foord <michael@voidspace.org.uk>
parents:
74104
diff
changeset
|
1263 pass |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1264 if default is not _sentinel: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1265 return default |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1266 raise AttributeError(attr) |
|
66344
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1267 |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1268 |
|
77619
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1269 # ------------------------------------------------ generator introspection |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1270 |
|
66551
32e675daa0e5
Issue 10220: switch to using string constants rather than integers for inspect.getgeneratorstate() return values and make debugging friendly str() and repr() for generator states a requirement in the test suite
Nick Coghlan <ncoghlan@gmail.com>
parents:
66344
diff
changeset
|
1271 GEN_CREATED = 'GEN_CREATED' |
|
32e675daa0e5
Issue 10220: switch to using string constants rather than integers for inspect.getgeneratorstate() return values and make debugging friendly str() and repr() for generator states a requirement in the test suite
Nick Coghlan <ncoghlan@gmail.com>
parents:
66344
diff
changeset
|
1272 GEN_RUNNING = 'GEN_RUNNING' |
|
32e675daa0e5
Issue 10220: switch to using string constants rather than integers for inspect.getgeneratorstate() return values and make debugging friendly str() and repr() for generator states a requirement in the test suite
Nick Coghlan <ncoghlan@gmail.com>
parents:
66344
diff
changeset
|
1273 GEN_SUSPENDED = 'GEN_SUSPENDED' |
|
32e675daa0e5
Issue 10220: switch to using string constants rather than integers for inspect.getgeneratorstate() return values and make debugging friendly str() and repr() for generator states a requirement in the test suite
Nick Coghlan <ncoghlan@gmail.com>
parents:
66344
diff
changeset
|
1274 GEN_CLOSED = 'GEN_CLOSED' |
|
66344
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1275 |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1276 def getgeneratorstate(generator): |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1277 """Get current state of a generator-iterator. |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1278 |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1279 Possible states are: |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1280 GEN_CREATED: Waiting to start execution. |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1281 GEN_RUNNING: Currently being executed by the interpreter. |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1282 GEN_SUSPENDED: Currently suspended at a yield expression. |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1283 GEN_CLOSED: Execution has completed. |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1284 """ |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1285 if generator.gi_running: |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1286 return GEN_RUNNING |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1287 if generator.gi_frame is None: |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1288 return GEN_CLOSED |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1289 if generator.gi_frame.f_lasti == -1: |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1290 return GEN_CREATED |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1291 return GEN_SUSPENDED |
|
77594
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1292 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1293 |
|
77619
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1294 def getgeneratorlocals(generator): |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1295 """ |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1296 Get the mapping of generator local variables to their current values. |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1297 |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1298 A dict is returned, with the keys the local variable names and values the |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1299 bound values.""" |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1300 |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1301 if not isgenerator(generator): |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1302 raise TypeError("'{!r}' is not a Python generator".format(generator)) |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1303 |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1304 frame = getattr(generator, "gi_frame", None) |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1305 if frame is not None: |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1306 return generator.gi_frame.f_locals |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1307 else: |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1308 return {} |
|
dd82a910eb07
Close #15153: Added inspect.getgeneratorlocals to simplify whitebox testing of generator state updates
Nick Coghlan <ncoghlan@gmail.com>
parents:
77616
diff
changeset
|
1309 |
|
77594
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1310 ############################################################################### |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1311 ### Function Signature Object (PEP 362) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1312 ############################################################################### |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1313 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1314 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1315 _WrapperDescriptor = type(type.__call__) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1316 _MethodWrapper = type(all.__call__) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1317 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1318 _NonUserDefinedCallables = (_WrapperDescriptor, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1319 _MethodWrapper, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1320 types.BuiltinFunctionType) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1321 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1322 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1323 def _get_user_defined_method(cls, method_name): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1324 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1325 meth = getattr(cls, method_name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1326 except AttributeError: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1327 return |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1328 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1329 if not isinstance(meth, _NonUserDefinedCallables): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1330 # Once '__signature__' will be added to 'C'-level |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1331 # callables, this check won't be necessary |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1332 return meth |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1333 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1334 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1335 def signature(obj): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1336 '''Get a signature object for the passed callable.''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1337 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1338 if not callable(obj): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1339 raise TypeError('{!r} is not a callable object'.format(obj)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1340 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1341 if isinstance(obj, types.MethodType): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1342 # In this case we skip the first parameter of the underlying |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1343 # function (usually `self` or `cls`). |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1344 sig = signature(obj.__func__) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1345 return sig.replace(parameters=tuple(sig.parameters.values())[1:]) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1346 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1347 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1348 sig = obj.__signature__ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1349 except AttributeError: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1350 pass |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1351 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1352 if sig is not None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1353 return sig |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1354 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1355 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1356 # Was this function wrapped by a decorator? |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1357 wrapped = obj.__wrapped__ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1358 except AttributeError: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1359 pass |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1360 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1361 return signature(wrapped) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1362 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1363 if isinstance(obj, types.FunctionType): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1364 return Signature.from_function(obj) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1365 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1366 if isinstance(obj, functools.partial): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1367 sig = signature(obj.func) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1368 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1369 new_params = OrderedDict(sig.parameters.items()) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1370 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1371 partial_args = obj.args or () |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1372 partial_keywords = obj.keywords or {} |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1373 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1374 ba = sig.bind_partial(*partial_args, **partial_keywords) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1375 except TypeError as ex: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1376 msg = 'partial object {!r} has incorrect arguments'.format(obj) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1377 raise ValueError(msg) from ex |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1378 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1379 for arg_name, arg_value in ba.arguments.items(): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1380 param = new_params[arg_name] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1381 if arg_name in partial_keywords: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1382 # We set a new default value, because the following code |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1383 # is correct: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1384 # |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1385 # >>> def foo(a): print(a) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1386 # >>> print(partial(partial(foo, a=10), a=20)()) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1387 # 20 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1388 # >>> print(partial(partial(foo, a=10), a=20)(a=30)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1389 # 30 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1390 # |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1391 # So, with 'partial' objects, passing a keyword argument is |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1392 # like setting a new default value for the corresponding |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1393 # parameter |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1394 # |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1395 # We also mark this parameter with '_partial_kwarg' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1396 # flag. Later, in '_bind', the 'default' value of this |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1397 # parameter will be added to 'kwargs', to simulate |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1398 # the 'functools.partial' real call. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1399 new_params[arg_name] = param.replace(default=arg_value, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1400 _partial_kwarg=True) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1401 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1402 elif (param.kind not in (_VAR_KEYWORD, _VAR_POSITIONAL) and |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1403 not param._partial_kwarg): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1404 new_params.pop(arg_name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1405 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1406 return sig.replace(parameters=new_params.values()) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1407 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1408 sig = None |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1409 if isinstance(obj, type): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1410 # obj is a class or a metaclass |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1411 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1412 # First, let's see if it has an overloaded __call__ defined |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1413 # in its metaclass |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1414 call = _get_user_defined_method(type(obj), '__call__') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1415 if call is not None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1416 sig = signature(call) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1417 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1418 # Now we check if the 'obj' class has a '__new__' method |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1419 new = _get_user_defined_method(obj, '__new__') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1420 if new is not None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1421 sig = signature(new) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1422 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1423 # Finally, we should have at least __init__ implemented |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1424 init = _get_user_defined_method(obj, '__init__') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1425 if init is not None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1426 sig = signature(init) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1427 elif not isinstance(obj, _NonUserDefinedCallables): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1428 # An object with __call__ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1429 # We also check that the 'obj' is not an instance of |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1430 # _WrapperDescriptor or _MethodWrapper to avoid |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1431 # infinite recursion (and even potential segfault) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1432 call = _get_user_defined_method(type(obj), '__call__') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1433 if call is not None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1434 sig = signature(call) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1435 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1436 if sig is not None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1437 # For classes and objects we skip the first parameter of their |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1438 # __call__, __new__, or __init__ methods |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1439 return sig.replace(parameters=tuple(sig.parameters.values())[1:]) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1440 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1441 if isinstance(obj, types.BuiltinFunctionType): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1442 # Raise a nicer error message for builtins |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1443 msg = 'no signature found for builtin function {!r}'.format(obj) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1444 raise ValueError(msg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1445 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1446 raise ValueError('callable {!r} is not supported by signature'.format(obj)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1447 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1448 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1449 class _void: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1450 '''A private marker - used in Parameter & Signature''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1451 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1452 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1453 class _empty: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1454 pass |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1455 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1456 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1457 class _ParameterKind(int): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1458 def __new__(self, *args, name): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1459 obj = int.__new__(self, *args) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1460 obj._name = name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1461 return obj |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1462 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1463 def __str__(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1464 return self._name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1465 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1466 def __repr__(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1467 return '<_ParameterKind: {!r}>'.format(self._name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1468 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1469 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1470 _POSITIONAL_ONLY = _ParameterKind(0, name='POSITIONAL_ONLY') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1471 _POSITIONAL_OR_KEYWORD = _ParameterKind(1, name='POSITIONAL_OR_KEYWORD') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1472 _VAR_POSITIONAL = _ParameterKind(2, name='VAR_POSITIONAL') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1473 _KEYWORD_ONLY = _ParameterKind(3, name='KEYWORD_ONLY') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1474 _VAR_KEYWORD = _ParameterKind(4, name='VAR_KEYWORD') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1475 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1476 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1477 class Parameter: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1478 '''Represents a parameter in a function signature. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1479 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1480 Has the following public attributes: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1481 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1482 * name : str |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1483 The name of the parameter as a string. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1484 * default : object |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1485 The default value for the parameter if specified. If the |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1486 parameter has no default value, this attribute is not set. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1487 * annotation |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1488 The annotation for the parameter if specified. If the |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1489 parameter has no annotation, this attribute is not set. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1490 * kind : str |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1491 Describes how argument values are bound to the parameter. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1492 Possible values: `Parameter.POSITIONAL_ONLY`, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1493 `Parameter.POSITIONAL_OR_KEYWORD`, `Parameter.VAR_POSITIONAL`, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1494 `Parameter.KEYWORD_ONLY`, `Parameter.VAR_KEYWORD`. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1495 ''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1496 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1497 __slots__ = ('_name', '_kind', '_default', '_annotation', '_partial_kwarg') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1498 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1499 POSITIONAL_ONLY = _POSITIONAL_ONLY |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1500 POSITIONAL_OR_KEYWORD = _POSITIONAL_OR_KEYWORD |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1501 VAR_POSITIONAL = _VAR_POSITIONAL |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1502 KEYWORD_ONLY = _KEYWORD_ONLY |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1503 VAR_KEYWORD = _VAR_KEYWORD |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1504 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1505 empty = _empty |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1506 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1507 def __init__(self, name, kind, *, default=_empty, annotation=_empty, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1508 _partial_kwarg=False): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1509 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1510 if kind not in (_POSITIONAL_ONLY, _POSITIONAL_OR_KEYWORD, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1511 _VAR_POSITIONAL, _KEYWORD_ONLY, _VAR_KEYWORD): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1512 raise ValueError("invalid value for 'Parameter.kind' attribute") |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1513 self._kind = kind |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1514 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1515 if default is not _empty: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1516 if kind in (_VAR_POSITIONAL, _VAR_KEYWORD): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1517 msg = '{} parameters cannot have default values'.format(kind) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1518 raise ValueError(msg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1519 self._default = default |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1520 self._annotation = annotation |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1521 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1522 if name is None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1523 if kind != _POSITIONAL_ONLY: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1524 raise ValueError("None is not a valid name for a " |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1525 "non-positional-only parameter") |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1526 self._name = name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1527 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1528 name = str(name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1529 if kind != _POSITIONAL_ONLY and not name.isidentifier(): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1530 msg = '{!r} is not a valid parameter name'.format(name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1531 raise ValueError(msg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1532 self._name = name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1533 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1534 self._partial_kwarg = _partial_kwarg |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1535 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1536 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1537 def name(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1538 return self._name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1539 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1540 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1541 def default(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1542 return self._default |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1543 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1544 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1545 def annotation(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1546 return self._annotation |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1547 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1548 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1549 def kind(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1550 return self._kind |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1551 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1552 def replace(self, *, name=_void, kind=_void, annotation=_void, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1553 default=_void, _partial_kwarg=_void): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1554 '''Creates a customized copy of the Parameter.''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1555 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1556 if name is _void: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1557 name = self._name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1558 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1559 if kind is _void: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1560 kind = self._kind |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1561 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1562 if annotation is _void: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1563 annotation = self._annotation |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1564 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1565 if default is _void: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1566 default = self._default |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1567 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1568 if _partial_kwarg is _void: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1569 _partial_kwarg = self._partial_kwarg |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1570 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1571 return type(self)(name, kind, default=default, annotation=annotation, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1572 _partial_kwarg=_partial_kwarg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1573 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1574 def __str__(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1575 kind = self.kind |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1576 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1577 formatted = self._name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1578 if kind == _POSITIONAL_ONLY: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1579 if formatted is None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1580 formatted = '' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1581 formatted = '<{}>'.format(formatted) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1582 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1583 # Add annotation and default value |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1584 if self._annotation is not _empty: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1585 formatted = '{}:{}'.format(formatted, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1586 formatannotation(self._annotation)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1587 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1588 if self._default is not _empty: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1589 formatted = '{}={}'.format(formatted, repr(self._default)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1590 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1591 if kind == _VAR_POSITIONAL: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1592 formatted = '*' + formatted |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1593 elif kind == _VAR_KEYWORD: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1594 formatted = '**' + formatted |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1595 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1596 return formatted |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1597 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1598 def __repr__(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1599 return '<{} at {:#x} {!r}>'.format(self.__class__.__name__, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1600 id(self), self.name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1601 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1602 def __eq__(self, other): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1603 return (issubclass(other.__class__, Parameter) and |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1604 self._name == other._name and |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1605 self._kind == other._kind and |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1606 self._default == other._default and |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1607 self._annotation == other._annotation) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1608 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1609 def __ne__(self, other): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1610 return not self.__eq__(other) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1611 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1612 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1613 class BoundArguments: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1614 '''Result of `Signature.bind` call. Holds the mapping of arguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1615 to the function's parameters. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1616 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1617 Has the following public attributes: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1618 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1619 * arguments : OrderedDict |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1620 An ordered mutable mapping of parameters' names to arguments' values. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1621 Does not contain arguments' default values. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1622 * signature : Signature |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1623 The Signature object that created this instance. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1624 * args : tuple |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1625 Tuple of positional arguments values. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1626 * kwargs : dict |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1627 Dict of keyword arguments values. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1628 ''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1629 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1630 def __init__(self, signature, arguments): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1631 self.arguments = arguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1632 self._signature = signature |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1633 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1634 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1635 def signature(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1636 return self._signature |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1637 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1638 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1639 def args(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1640 args = [] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1641 for param_name, param in self._signature.parameters.items(): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1642 if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1643 param._partial_kwarg): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1644 # Keyword arguments mapped by 'functools.partial' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1645 # (Parameter._partial_kwarg is True) are mapped |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1646 # in 'BoundArguments.kwargs', along with VAR_KEYWORD & |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1647 # KEYWORD_ONLY |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1648 break |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1649 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1650 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1651 arg = self.arguments[param_name] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1652 except KeyError: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1653 # We're done here. Other arguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1654 # will be mapped in 'BoundArguments.kwargs' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1655 break |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1656 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1657 if param.kind == _VAR_POSITIONAL: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1658 # *args |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1659 args.extend(arg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1660 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1661 # plain argument |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1662 args.append(arg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1663 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1664 return tuple(args) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1665 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1666 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1667 def kwargs(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1668 kwargs = {} |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1669 kwargs_started = False |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1670 for param_name, param in self._signature.parameters.items(): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1671 if not kwargs_started: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1672 if (param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY) or |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1673 param._partial_kwarg): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1674 kwargs_started = True |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1675 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1676 if param_name not in self.arguments: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1677 kwargs_started = True |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1678 continue |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1679 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1680 if not kwargs_started: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1681 continue |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1682 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1683 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1684 arg = self.arguments[param_name] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1685 except KeyError: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1686 pass |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1687 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1688 if param.kind == _VAR_KEYWORD: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1689 # **kwargs |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1690 kwargs.update(arg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1691 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1692 # plain keyword argument |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1693 kwargs[param_name] = arg |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1694 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1695 return kwargs |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1696 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1697 def __eq__(self, other): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1698 return (issubclass(other.__class__, BoundArguments) and |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1699 self.signature == other.signature and |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1700 self.arguments == other.arguments) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1701 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1702 def __ne__(self, other): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1703 return not self.__eq__(other) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1704 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1705 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1706 class Signature: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1707 '''A Signature object represents the overall signature of a function. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1708 It stores a Parameter object for each parameter accepted by the |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1709 function, as well as information specific to the function itself. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1710 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1711 A Signature object has the following public attributes and methods: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1712 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1713 * parameters : OrderedDict |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1714 An ordered mapping of parameters' names to the corresponding |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1715 Parameter objects (keyword-only arguments are in the same order |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1716 as listed in `code.co_varnames`). |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1717 * return_annotation : object |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1718 The annotation for the return type of the function if specified. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1719 If the function has no annotation for its return type, this |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1720 attribute is not set. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1721 * bind(*args, **kwargs) -> BoundArguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1722 Creates a mapping from positional and keyword arguments to |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1723 parameters. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1724 * bind_partial(*args, **kwargs) -> BoundArguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1725 Creates a partial mapping from positional and keyword arguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1726 to parameters (simulating 'functools.partial' behavior.) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1727 ''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1728 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1729 __slots__ = ('_return_annotation', '_parameters') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1730 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1731 _parameter_cls = Parameter |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1732 _bound_arguments_cls = BoundArguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1733 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1734 empty = _empty |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1735 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1736 def __init__(self, parameters=None, *, return_annotation=_empty, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1737 __validate_parameters__=True): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1738 '''Constructs Signature from the given list of Parameter |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1739 objects and 'return_annotation'. All arguments are optional. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1740 ''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1741 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1742 if parameters is None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1743 params = OrderedDict() |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1744 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1745 if __validate_parameters__: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1746 params = OrderedDict() |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1747 top_kind = _POSITIONAL_ONLY |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1748 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1749 for idx, param in enumerate(parameters): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1750 kind = param.kind |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1751 if kind < top_kind: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1752 msg = 'wrong parameter order: {} before {}' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1753 msg = msg.format(top_kind, param.kind) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1754 raise ValueError(msg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1755 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1756 top_kind = kind |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1757 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1758 name = param.name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1759 if name is None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1760 name = str(idx) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1761 param = param.replace(name=name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1762 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1763 if name in params: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1764 msg = 'duplicate parameter name: {!r}'.format(name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1765 raise ValueError(msg) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1766 params[name] = param |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1767 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1768 params = OrderedDict(((param.name, param) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1769 for param in parameters)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1770 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1771 self._parameters = types.MappingProxyType(params) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1772 self._return_annotation = return_annotation |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1773 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1774 @classmethod |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1775 def from_function(cls, func): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1776 '''Constructs Signature for the given python function''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1777 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1778 if not isinstance(func, types.FunctionType): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1779 raise TypeError('{!r} is not a Python function'.format(func)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1780 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1781 Parameter = cls._parameter_cls |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1782 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1783 # Parameter information. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1784 func_code = func.__code__ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1785 pos_count = func_code.co_argcount |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1786 arg_names = func_code.co_varnames |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1787 positional = tuple(arg_names[:pos_count]) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1788 keyword_only_count = func_code.co_kwonlyargcount |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1789 keyword_only = arg_names[pos_count:(pos_count + keyword_only_count)] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1790 annotations = func.__annotations__ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1791 defaults = func.__defaults__ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1792 kwdefaults = func.__kwdefaults__ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1793 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1794 if defaults: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1795 pos_default_count = len(defaults) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1796 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1797 pos_default_count = 0 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1798 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1799 parameters = [] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1800 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1801 # Non-keyword-only parameters w/o defaults. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1802 non_default_count = pos_count - pos_default_count |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1803 for name in positional[:non_default_count]: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1804 annotation = annotations.get(name, _empty) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1805 parameters.append(Parameter(name, annotation=annotation, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1806 kind=_POSITIONAL_OR_KEYWORD)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1807 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1808 # ... w/ defaults. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1809 for offset, name in enumerate(positional[non_default_count:]): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1810 annotation = annotations.get(name, _empty) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1811 parameters.append(Parameter(name, annotation=annotation, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1812 kind=_POSITIONAL_OR_KEYWORD, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1813 default=defaults[offset])) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1814 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1815 # *args |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1816 if func_code.co_flags & 0x04: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1817 name = arg_names[pos_count + keyword_only_count] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1818 annotation = annotations.get(name, _empty) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1819 parameters.append(Parameter(name, annotation=annotation, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1820 kind=_VAR_POSITIONAL)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1821 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1822 # Keyword-only parameters. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1823 for name in keyword_only: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1824 default = _empty |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1825 if kwdefaults is not None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1826 default = kwdefaults.get(name, _empty) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1827 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1828 annotation = annotations.get(name, _empty) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1829 parameters.append(Parameter(name, annotation=annotation, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1830 kind=_KEYWORD_ONLY, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1831 default=default)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1832 # **kwargs |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1833 if func_code.co_flags & 0x08: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1834 index = pos_count + keyword_only_count |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1835 if func_code.co_flags & 0x04: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1836 index += 1 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1837 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1838 name = arg_names[index] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1839 annotation = annotations.get(name, _empty) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1840 parameters.append(Parameter(name, annotation=annotation, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1841 kind=_VAR_KEYWORD)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1842 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1843 return cls(parameters, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1844 return_annotation=annotations.get('return', _empty), |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1845 __validate_parameters__=False) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1846 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1847 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1848 def parameters(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1849 return self._parameters |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1850 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1851 @property |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1852 def return_annotation(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1853 return self._return_annotation |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1854 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1855 def replace(self, *, parameters=_void, return_annotation=_void): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1856 '''Creates a customized copy of the Signature. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1857 Pass 'parameters' and/or 'return_annotation' arguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1858 to override them in the new copy. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1859 ''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1860 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1861 if parameters is _void: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1862 parameters = self.parameters.values() |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1863 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1864 if return_annotation is _void: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1865 return_annotation = self._return_annotation |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1866 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1867 return type(self)(parameters, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1868 return_annotation=return_annotation) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1869 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1870 def __eq__(self, other): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1871 if (not issubclass(type(other), Signature) or |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1872 self.return_annotation != other.return_annotation or |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1873 len(self.parameters) != len(other.parameters)): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1874 return False |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1875 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1876 other_positions = {param: idx |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1877 for idx, param in enumerate(other.parameters.keys())} |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1878 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1879 for idx, (param_name, param) in enumerate(self.parameters.items()): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1880 if param.kind == _KEYWORD_ONLY: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1881 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1882 other_param = other.parameters[param_name] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1883 except KeyError: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1884 return False |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1885 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1886 if param != other_param: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1887 return False |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1888 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1889 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1890 other_idx = other_positions[param_name] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1891 except KeyError: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1892 return False |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1893 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1894 if (idx != other_idx or |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1895 param != other.parameters[param_name]): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1896 return False |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1897 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1898 return True |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1899 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1900 def __ne__(self, other): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1901 return not self.__eq__(other) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1902 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1903 def _bind(self, args, kwargs, *, partial=False): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1904 '''Private method. Don't use directly.''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1905 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1906 arguments = OrderedDict() |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1907 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1908 parameters = iter(self.parameters.values()) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1909 parameters_ex = () |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1910 arg_vals = iter(args) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1911 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1912 if partial: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1913 # Support for binding arguments to 'functools.partial' objects. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1914 # See 'functools.partial' case in 'signature()' implementation |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1915 # for details. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1916 for param_name, param in self.parameters.items(): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1917 if (param._partial_kwarg and param_name not in kwargs): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1918 # Simulating 'functools.partial' behavior |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1919 kwargs[param_name] = param.default |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1920 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1921 while True: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1922 # Let's iterate through the positional arguments and corresponding |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1923 # parameters |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1924 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1925 arg_val = next(arg_vals) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1926 except StopIteration: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1927 # No more positional arguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1928 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1929 param = next(parameters) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1930 except StopIteration: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1931 # No more parameters. That's it. Just need to check that |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1932 # we have no `kwargs` after this while loop |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1933 break |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1934 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1935 if param.kind == _VAR_POSITIONAL: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1936 # That's OK, just empty *args. Let's start parsing |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1937 # kwargs |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1938 break |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1939 elif param.name in kwargs: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1940 if param.kind == _POSITIONAL_ONLY: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1941 msg = '{arg!r} parameter is positional only, ' \ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1942 'but was passed as a keyword' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1943 msg = msg.format(arg=param.name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1944 raise TypeError(msg) from None |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1945 parameters_ex = (param,) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1946 break |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1947 elif (param.kind == _VAR_KEYWORD or |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1948 param.default is not _empty): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1949 # That's fine too - we have a default value for this |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1950 # parameter. So, lets start parsing `kwargs`, starting |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1951 # with the current parameter |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1952 parameters_ex = (param,) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1953 break |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1954 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1955 if partial: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1956 parameters_ex = (param,) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1957 break |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1958 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1959 msg = '{arg!r} parameter lacking default value' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1960 msg = msg.format(arg=param.name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1961 raise TypeError(msg) from None |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1962 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1963 # We have a positional argument to process |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1964 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1965 param = next(parameters) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1966 except StopIteration: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1967 raise TypeError('too many positional arguments') from None |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1968 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1969 if param.kind in (_VAR_KEYWORD, _KEYWORD_ONLY): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1970 # Looks like we have no parameter for this positional |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1971 # argument |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1972 raise TypeError('too many positional arguments') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1973 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1974 if param.kind == _VAR_POSITIONAL: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1975 # We have an '*args'-like argument, let's fill it with |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1976 # all positional arguments we have left and move on to |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1977 # the next phase |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1978 values = [arg_val] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1979 values.extend(arg_vals) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1980 arguments[param.name] = tuple(values) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1981 break |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1982 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1983 if param.name in kwargs: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1984 raise TypeError('multiple values for argument ' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1985 '{arg!r}'.format(arg=param.name)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1986 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1987 arguments[param.name] = arg_val |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1988 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1989 # Now, we iterate through the remaining parameters to process |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1990 # keyword arguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1991 kwargs_param = None |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1992 for param in itertools.chain(parameters_ex, parameters): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1993 if param.kind == _POSITIONAL_ONLY: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1994 # This should never happen in case of a properly built |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1995 # Signature object (but let's have this check here |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1996 # to ensure correct behaviour just in case) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1997 raise TypeError('{arg!r} parameter is positional only, ' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1998 'but was passed as a keyword'. \ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
1999 format(arg=param.name)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2000 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2001 if param.kind == _VAR_KEYWORD: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2002 # Memorize that we have a '**kwargs'-like parameter |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2003 kwargs_param = param |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2004 continue |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2005 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2006 param_name = param.name |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2007 try: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2008 arg_val = kwargs.pop(param_name) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2009 except KeyError: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2010 # We have no value for this parameter. It's fine though, |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2011 # if it has a default value, or it is an '*args'-like |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2012 # parameter, left alone by the processing of positional |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2013 # arguments. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2014 if (not partial and param.kind != _VAR_POSITIONAL and |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2015 param.default is _empty): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2016 raise TypeError('{arg!r} parameter lacking default value'. \ |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2017 format(arg=param_name)) from None |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2018 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2019 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2020 arguments[param_name] = arg_val |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2021 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2022 if kwargs: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2023 if kwargs_param is not None: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2024 # Process our '**kwargs'-like parameter |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2025 arguments[kwargs_param.name] = kwargs |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2026 else: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2027 raise TypeError('too many keyword arguments') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2028 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2029 return self._bound_arguments_cls(self, arguments) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2030 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2031 def bind(self, *args, **kwargs): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2032 '''Get a BoundArguments object, that maps the passed `args` |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2033 and `kwargs` to the function's signature. Raises `TypeError` |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2034 if the passed arguments can not be bound. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2035 ''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2036 return self._bind(args, kwargs) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2037 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2038 def bind_partial(self, *args, **kwargs): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2039 '''Get a BoundArguments object, that partially maps the |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2040 passed `args` and `kwargs` to the function's signature. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2041 Raises `TypeError` if the passed arguments can not be bound. |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2042 ''' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2043 return self._bind(args, kwargs, partial=True) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2044 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2045 def __str__(self): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2046 result = [] |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2047 render_kw_only_separator = True |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2048 for idx, param in enumerate(self.parameters.values()): |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2049 formatted = str(param) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2050 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2051 kind = param.kind |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2052 if kind == _VAR_POSITIONAL: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2053 # OK, we have an '*args'-like parameter, so we won't need |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2054 # a '*' to separate keyword-only arguments |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2055 render_kw_only_separator = False |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2056 elif kind == _KEYWORD_ONLY and render_kw_only_separator: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2057 # We have a keyword-only parameter to render and we haven't |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2058 # rendered an '*args'-like parameter before, so add a '*' |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2059 # separator to the parameters list ("foo(arg1, *, arg2)" case) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2060 result.append('*') |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2061 # This condition should be only triggered once, so |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2062 # reset the flag |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2063 render_kw_only_separator = False |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2064 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2065 result.append(formatted) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2066 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2067 rendered = '({})'.format(', '.join(result)) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2068 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2069 if self.return_annotation is not _empty: |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2070 anno = formatannotation(self.return_annotation) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2071 rendered += ' -> {}'.format(anno) |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2072 |
|
d892bf410478
Issue #15008: Implement PEP 362 "Signature Objects".
Larry Hastings <larry@hastings.org>
parents:
77312
diff
changeset
|
2073 return rendered |
