Mercurial > cpython-lru_cache-weakref
annotate Lib/inspect.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | 8f33758df19a |
| children | 282a2a245278 |
| 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 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
25 """ |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
26 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
27 # 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
|
28 |
| 16898 | 29 __author__ = 'Ka-Ping Yee <ping@lfw.org>' |
| 30 __date__ = '1 Jan 2001' | |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
31 |
|
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
|
32 import sys |
|
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
|
33 import os |
|
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
|
34 import types |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
35 import itertools |
|
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
|
36 import string |
|
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
|
37 import re |
|
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
|
38 import imp |
|
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 tokenize |
|
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
|
40 import linecache |
|
34734
d355da1d7c8f
Apply itemgetter() instead of lambda.
Raymond Hettinger <python@rcn.com>
parents:
34545
diff
changeset
|
41 from operator import attrgetter |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
42 from collections import namedtuple |
|
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
|
43 |
|
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
|
44 # 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
|
45 # 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
|
46 # 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 |
|
45636
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
58 # See Include/object.h |
|
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
59 TPFLAGS_IS_ABSTRACT = 1 << 20 |
|
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
60 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
61 # ----------------------------------------------------------- type-checking |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
62 def ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
63 """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
|
64 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
65 Module objects provide these attributes: |
| 60648 | 66 __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
|
67 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
68 __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
|
69 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
|
70 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
71 def isclass(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
72 """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
|
73 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
74 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
|
75 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
76 __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
|
77 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
|
78 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
79 def ismethod(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
80 """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
|
81 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
82 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
|
83 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
84 __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
|
85 __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
|
86 __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
|
87 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
|
88 |
|
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
|
89 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
|
90 """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
|
91 |
|
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
92 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
|
93 |
|
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 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
|
95 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
|
96 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
|
97 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
|
98 |
|
19992
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 __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
|
103 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
|
104 # mutual exclusion |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
105 return False |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
106 tp = type(object) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
107 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
|
108 |
|
28948
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
109 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
|
110 """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
|
111 |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
112 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
|
113 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
|
114 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
|
115 (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
|
116 is not guaranteed.""" |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
117 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
|
118 # mutual exclusion |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
119 return False |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
120 tp = type(object) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
121 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
|
122 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
123 if hasattr(types, 'MemberDescriptorType'): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
124 # CPython and equivalent |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
125 def ismemberdescriptor(object): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
126 """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
|
127 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
128 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
|
129 modules.""" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
130 return isinstance(object, types.MemberDescriptorType) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
131 else: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
132 # Other implementations |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
133 def ismemberdescriptor(object): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
134 """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
|
135 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
136 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
|
137 modules.""" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
138 return False |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
139 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
140 if hasattr(types, 'GetSetDescriptorType'): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
141 # CPython and equivalent |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
142 def isgetsetdescriptor(object): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
143 """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
|
144 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
145 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
|
146 modules.""" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
147 return isinstance(object, types.GetSetDescriptorType) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
148 else: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
149 # Other implementations |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
150 def isgetsetdescriptor(object): |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
151 """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
|
152 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
153 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
|
154 modules.""" |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
155 return False |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
156 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
157 def isfunction(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
158 """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
|
159 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
160 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
|
161 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
162 __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
|
163 __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
|
164 __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
|
165 __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
|
166 __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
|
167 __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
|
168 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
|
169 |
|
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
|
170 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
|
171 """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
|
172 |
|
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
|
173 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
|
174 |
|
64077
4085cc560e9e
Merged revisions 84106 via svnmerge from
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
62372
diff
changeset
|
175 See help(isfunction) for attributes listing.""" |
|
50568
fb905890d049
Merged revisions 68245 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49720
diff
changeset
|
176 return bool((isfunction(object) or ismethod(object)) and |
|
fb905890d049
Merged revisions 68245 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49720
diff
changeset
|
177 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
|
178 |
|
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 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
|
180 """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
|
181 |
|
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
|
182 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
|
183 __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
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
196 def istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
197 """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
|
198 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
199 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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
206 def isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
207 """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
|
208 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
209 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
|
210 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
|
211 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
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
220 def iscode(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
221 """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
|
222 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
238 def isbuiltin(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
239 """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
|
240 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
241 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
|
242 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
243 __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
|
244 __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
|
245 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
|
246 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
247 def isroutine(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
248 """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
|
249 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
|
250 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
|
251 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
|
252 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
|
253 |
|
45636
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
254 def isabstract(object): |
|
25406b3ff97a
Merged revisions 61203-61204 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45399
diff
changeset
|
255 """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
|
256 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
|
257 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
258 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
|
259 """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
|
260 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
|
261 if isclass(object): |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
262 mro = (object,) + getmro(object) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
263 else: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
264 mro = () |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
265 results = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
266 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
|
267 # 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
|
268 # 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
|
269 for base in mro: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
270 if key in base.__dict__: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
271 value = base.__dict__[key] |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
272 break |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
273 else: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
274 try: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
275 value = getattr(object, key) |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
276 except AttributeError: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
277 continue |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
278 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
|
279 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
|
280 results.sort() |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
281 return results |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
282 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
283 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
|
284 |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
285 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
|
286 """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
|
287 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
288 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
|
289 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
|
290 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
291 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
|
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 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
|
294 '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
|
295 '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
|
296 '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
|
297 '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
|
298 '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
|
299 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
300 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
|
301 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
302 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
|
303 __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
|
304 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
|
305 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
|
306 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
|
307 """ |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
308 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
309 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
|
310 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
|
311 result = [] |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
312 for name in names: |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
313 # 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
|
314 # 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
|
315 # 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
|
316 # 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
|
317 # 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
|
318 # 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
|
319 homecls = None |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
320 for base in (cls,) + mro: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
321 if name in base.__dict__: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
322 obj = base.__dict__[name] |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
323 homecls = base |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
324 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
|
325 else: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
326 obj = getattr(cls, name) |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
327 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
|
328 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
329 # 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 kind = "property" |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
336 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
|
337 kind = "method" |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
338 elif isdatadescriptor(obj): |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
339 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
|
340 else: |
|
74104
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
341 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
|
342 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
|
343 ismethoddescriptor(obj_via_getattr)): |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
344 kind = "method" |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
345 else: |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
346 kind = "data" |
|
902f694a7b0e
Issue #1785: Fix inspect and pydoc with misbehaving descriptors.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74062
diff
changeset
|
347 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
|
348 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
349 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
|
350 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
351 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
|
352 |
|
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
|
353 # ----------------------------------------------------------- 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
|
354 |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
355 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
|
356 "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
|
357 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
|
358 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
359 # -------------------------------------------------- source code extraction |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
360 def indentsize(line): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
361 """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
|
362 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
|
363 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
|
364 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
365 def getdoc(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
366 """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
|
367 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
368 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
|
369 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
|
370 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
|
371 try: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
372 doc = object.__doc__ |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
373 except AttributeError: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
374 return None |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
43274
diff
changeset
|
375 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
|
376 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
|
377 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
|
378 |
|
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
|
379 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
|
380 """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
|
381 |
|
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 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
|
383 onwards is removed.""" |
|
22348
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
384 try: |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
385 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
|
386 except UnicodeError: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
387 return None |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
388 else: |
| 26467 | 389 # 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
|
390 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
|
391 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
|
392 content = len(line.lstrip()) |
| 26467 | 393 if content: |
| 394 indent = len(line) - content | |
| 395 margin = min(margin, indent) | |
| 396 # Remove indentation. | |
| 397 if lines: | |
| 398 lines[0] = lines[0].lstrip() | |
|
44160
9d152530910a
Removed PyInt_GetMax and sys.maxint
Christian Heimes <christian@cheimes.de>
parents:
44098
diff
changeset
|
399 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
|
400 for i in range(1, len(lines)): lines[i] = lines[i][margin:] |
| 26467 | 401 # Remove any trailing or leading blank lines. |
| 402 while lines and not lines[-1]: | |
| 403 lines.pop() | |
| 404 while lines and not lines[0]: | |
| 405 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
|
406 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
|
407 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
408 def getfile(object): |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
409 """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
|
410 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
411 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
|
412 return object.__file__ |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
413 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
|
414 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
|
415 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
|
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 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
|
419 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
|
420 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
|
421 if isfunction(object): |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
422 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
|
423 if istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
424 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
|
425 if isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
426 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
|
427 if iscode(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
428 return object.co_filename |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
429 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
|
430 '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
|
431 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
432 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
|
433 |
|
17370
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
434 def getmoduleinfo(path): |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
435 """Get the module name, suffix, mode, and module type for a given file.""" |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
436 filename = os.path.basename(path) |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41715
diff
changeset
|
437 suffixes = [(-len(suffix), suffix, mode, mtype) |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41715
diff
changeset
|
438 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
|
439 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
|
440 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
|
441 if filename[neglen:] == suffix: |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
442 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
|
443 |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
444 def getmodulename(path): |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
445 """Return the module name for a given file, or None.""" |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
446 info = getmoduleinfo(path) |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
447 if info: return info[0] |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
448 |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
449 def getsourcefile(object): |
|
62372
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
450 """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
|
451 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
|
452 """ |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
453 filename = getfile(object) |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
454 if filename[-4:].lower() in ('.pyc', '.pyo'): |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
455 filename = filename[:-4] + '.py' |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
456 for suffix, mode, kind in imp.get_suffixes(): |
|
41480
99027c2b3fd2
Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents:
40991
diff
changeset
|
457 if 'b' in mode and filename[-len(suffix):].lower() == suffix: |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
458 # Looks like a binary file. We want to only return a text file. |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
459 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
|
460 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
|
461 return filename |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
462 # 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
|
463 if hasattr(getmodule(object, filename), '__loader__'): |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
464 return filename |
|
62372
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
465 # or it is in the linecache |
|
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
466 if filename in linecache.cache: |
|
8cc0e1c9ba6d
Merged revisions 82041 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents:
57210
diff
changeset
|
467 return filename |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
468 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
469 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
|
470 """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
|
471 |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
472 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
|
473 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
|
474 if _filename is None: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
475 _filename = getsourcefile(object) or getfile(object) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
476 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
|
477 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
478 modulesbyfile = {} |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
479 _filesbymodname = {} |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
480 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
481 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
|
482 """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
|
483 if ismodule(object): |
|
926b64fba5e7
Remove duplicate type objects from isroutine() and isbuiltin().
Ka-Ping Yee <ping@zesty.ca>
parents:
17446
diff
changeset
|
484 return object |
|
33455
9e3c52ca3a04
Use __module__ attribute when available instead of using isclass() predicate
Johannes Gijsbers <jlg@dds.nl>
parents:
33172
diff
changeset
|
485 if hasattr(object, '__module__'): |
| 16898 | 486 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
|
487 # 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
|
488 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
|
489 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
|
490 # 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
|
491 try: |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
492 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
|
493 except TypeError: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
494 return None |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
495 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
|
496 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
|
497 # 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
|
498 # 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
|
499 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
|
500 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
|
501 f = module.__file__ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
502 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
|
503 # 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
|
504 continue |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
505 _filesbymodname[modname] = f |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
506 f = getabsfile(module) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
507 # 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
|
508 modulesbyfile[f] = modulesbyfile[ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
509 os.path.realpath(f)] = module.__name__ |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
510 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
|
511 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
|
512 # 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
|
513 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
|
514 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
|
515 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
|
516 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
|
517 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
|
518 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
|
519 return main |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
520 # Check builtins |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
44026
diff
changeset
|
521 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
|
522 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
|
523 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
|
524 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
|
525 return builtin |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
526 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
527 def findsource(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
528 """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
|
529 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
530 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
|
531 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
|
532 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
|
533 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
|
534 |
|
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
535 file = getfile(object) |
|
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
536 sourcefile = getsourcefile(object) |
|
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
537 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
|
538 raise IOError('source code not available') |
|
70799
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
539 file = sourcefile if sourcefile else file |
|
6cc4579dca02
allow "fake" filenames in findsource (closes #9284)
Benjamin Peterson <benjamin@python.org>
parents:
69031
diff
changeset
|
540 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
541 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
|
542 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
|
543 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
|
544 else: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
545 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
|
546 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
|
547 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
|
548 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
549 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
550 return lines, 0 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
551 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
552 if isclass(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
553 name = object.__name__ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
554 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
|
555 # 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
|
556 # 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
|
557 # 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
|
558 candidates = [] |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
559 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
|
560 match = pat.match(lines[i]) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
561 if match: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
562 # 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
|
563 if lines[i][0] == 'c': |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
564 return lines, i |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
565 # 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
|
566 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
|
567 if candidates: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
568 # 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
|
569 # less whitespace first |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
570 candidates.sort() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
571 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
|
572 else: |
|
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
573 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
|
574 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
575 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
|
576 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
|
577 if isfunction(object): |
|
40910
eaaee785eb86
Change all the function attributes from func_* -> __*__. This gets rid
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
578 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
|
579 if istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
580 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
|
581 if isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
582 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
|
583 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
|
584 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
|
585 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
|
586 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
|
587 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
|
588 while lnum > 0: |
|
17020
953c50f12184
Fix findsource() to work for derived classes.
Ka-Ping Yee <ping@zesty.ca>
parents:
16938
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 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
|
593 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
594 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
|
595 """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
|
596 |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
597 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
|
598 """ |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
599 try: |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
600 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
|
601 except (IOError, TypeError): |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
602 return None |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
603 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
604 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
605 # 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
|
606 start = 0 |
|
17446
0184c50a8751
Robustify getcomments() so it doesn't crash on empty files.
Ka-Ping Yee <ping@zesty.ca>
parents:
17370
diff
changeset
|
607 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
|
608 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
|
609 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
|
610 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
|
611 comments = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
612 end = start |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
613 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
|
614 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
|
615 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
|
616 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
|
617 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
618 # 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
|
619 elif lnum > 0: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
620 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
|
621 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
|
622 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
|
623 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
|
624 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
|
625 if end > 0: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
626 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
|
627 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
|
628 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
|
629 comments[:0] = [comment] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
630 end = end - 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
631 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
|
632 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
|
633 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
|
634 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
|
635 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
|
636 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
|
637 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
|
638 |
|
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
|
639 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
|
640 |
|
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
|
641 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
|
642 """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
|
643 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
|
644 self.indent = 0 |
|
34740
3bb759498767
Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents:
34734
diff
changeset
|
645 self.islambda = False |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
646 self.started = False |
|
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
647 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
|
648 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
|
649 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41715
diff
changeset
|
650 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
|
651 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
|
652 # 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
|
653 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
|
654 if token == "lambda": |
|
3bb759498767
Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents:
34734
diff
changeset
|
655 self.islambda = True |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
656 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
|
657 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
|
658 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
|
659 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
|
660 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
|
661 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
|
662 raise EndOfBlock |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
663 elif self.passline: |
|
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
664 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
|
665 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
|
666 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
|
667 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
|
668 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
|
669 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
|
670 # 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
|
671 # (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
|
672 # 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
|
673 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
|
674 raise EndOfBlock |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
675 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
|
676 # 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
|
677 # 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
|
678 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
|
679 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
680 def getblock(lines): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
681 """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
|
682 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
|
683 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
|
684 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
|
685 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
|
686 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
|
687 except (EndOfBlock, IndentationError): |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
688 pass |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
689 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
|
690 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
691 def getsourcelines(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
692 """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
|
693 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
694 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
|
695 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
|
696 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
|
697 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
|
698 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
|
699 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
|
700 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
701 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
|
702 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
|
703 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
704 def getsource(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
705 """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
|
706 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
707 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
|
708 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
|
709 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
|
710 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
|
711 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
|
712 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
713 # --------------------------------------------------- class tree extraction |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
714 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
|
715 """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
|
716 results = [] |
|
34734
d355da1d7c8f
Apply itemgetter() instead of lambda.
Raymond Hettinger <python@rcn.com>
parents:
34545
diff
changeset
|
717 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
|
718 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
|
719 results.append((c, c.__bases__)) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
720 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
|
721 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
|
722 return results |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
723 |
|
54485
18b3d5b37d83
Use true boolean for flag argument.
Georg Brandl <georg@python.org>
parents:
54043
diff
changeset
|
724 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
|
725 """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
|
726 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
727 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
|
728 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
|
729 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
|
730 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
|
731 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
|
732 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
|
733 children = {} |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
734 roots = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
735 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
|
736 if c.__bases__: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
737 for parent in c.__bases__: |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
738 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
|
739 children[parent] = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
740 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
|
741 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
|
742 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
|
743 roots.append(c) |
|
23634
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
744 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
|
745 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
|
746 roots.append(parent) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
747 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
|
748 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
749 # ------------------------------------------------ argument list extraction |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
750 Arguments = namedtuple('Arguments', 'args, varargs, varkw') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
751 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
752 def getargs(co): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
753 """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
|
754 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
755 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
|
756 '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
|
757 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
|
758 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
|
759 args, varargs, kwonlyargs, varkw = _getfullargs(co) |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
760 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
|
761 |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
762 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
|
763 """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
|
764 |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
765 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
|
766 '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
|
767 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
|
768 |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
769 if not iscode(co): |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
770 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
|
771 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
772 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
|
773 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
|
774 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
|
775 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
|
776 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
|
777 step = 0 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
778 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
779 nargs += nkwargs |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
780 varargs = None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
781 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
|
782 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
|
783 nargs = nargs + 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
784 varkw = None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
785 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
|
786 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
|
787 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
|
788 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
789 |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
790 ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
791 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
792 def getargspec(func): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
793 """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
|
794 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
795 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
|
796 '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
|
797 '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
|
798 '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
|
799 '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
|
800 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
801 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
|
802 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
|
803 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
|
804 """ |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
805 |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
806 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
|
807 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
|
808 if kwonlyargs or ann: |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42309
diff
changeset
|
809 raise ValueError("Function has keyword-only arguments or annotations" |
|
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
42309
diff
changeset
|
810 ", 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
|
811 return ArgSpec(args, varargs, varkw, defaults) |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
812 |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
813 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
|
814 '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
|
815 |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
816 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
|
817 """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
|
818 |
|
43274
7052593574bf
Fix docstring for getfullargspec().
Brett Cannon <bcannon@gmail.com>
parents:
43020
diff
changeset
|
819 A tuple of seven things is returned: |
|
7052593574bf
Fix docstring for getfullargspec().
Brett Cannon <bcannon@gmail.com>
parents:
43020
diff
changeset
|
820 (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
|
821 '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
|
822 '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
|
823 '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
|
824 '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
|
825 '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
|
826 '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
|
827 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
828 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
|
829 """ |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
830 |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
831 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
|
832 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
|
833 if not isfunction(func): |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
834 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
|
835 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
|
836 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
|
837 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
|
838 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
839 ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals') |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
840 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
841 def getargvalues(frame): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
842 """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
|
843 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
844 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
|
845 '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
|
846 '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
|
847 '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
|
848 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
|
849 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
|
850 |
|
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 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
|
852 if isinstance(annotation, type): |
|
44098
0889daf6d0d9
#1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents:
44026
diff
changeset
|
853 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
|
854 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
|
855 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
|
856 return repr(annotation) |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
857 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
858 def formatannotationrelativeto(object): |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
859 module = getattr(object, '__module__', None) |
|
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
860 def _formatannotation(annotation): |
|
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
861 return formatannotation(annotation, module) |
|
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
862 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
|
863 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
864 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
|
865 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
|
866 formatarg=str, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
867 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
|
868 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
|
869 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
|
870 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
|
871 formatannotation=formatannotation): |
|
41715
f60c877d52c8
Merged revisions 55270-55324 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41501
diff
changeset
|
872 """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
|
873 or getfullargspec. |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
874 |
|
40991
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
875 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
|
876 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
|
877 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
|
878 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
|
879 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
|
880 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
|
881 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
|
882 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
|
883 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
|
884 return result |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
885 specs = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
886 if defaults: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
887 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
|
888 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
|
889 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
|
890 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
|
891 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
|
892 specs.append(spec) |
|
23613
4a1f5bd486ff
Replaced boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23486
diff
changeset
|
893 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
|
894 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
|
895 else: |
|
e16268c273bb
Final part of SF# 1607548 by Tony Lownds: fix pydoc and inspect.
Guido van Rossum <guido@python.org>
parents:
40910
diff
changeset
|
896 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
|
897 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
|
898 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
|
899 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
|
900 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
|
901 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
|
902 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
|
903 specs.append(spec) |
|
23613
4a1f5bd486ff
Replaced boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23486
diff
changeset
|
904 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
|
905 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
|
906 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
|
907 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
|
908 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
|
909 return result |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
910 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
911 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
|
912 formatarg=str, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
913 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
|
914 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
|
915 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
|
916 """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
|
917 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
918 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
|
919 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
|
920 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
|
921 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
|
922 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
|
923 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
|
924 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
|
925 specs = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
926 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
|
927 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
|
928 if varargs: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
929 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
|
930 if varkw: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
931 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
|
932 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
|
933 |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
934 def getcallargs(func, *positional, **named): |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
935 """Get the mapping of arguments to values. |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
936 |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
937 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
|
938 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
|
939 values from 'positional' and 'named'.""" |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
940 spec = getfullargspec(func) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
941 args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, ann = spec |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
942 f_name = func.__name__ |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
943 arg2value = {} |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
944 |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
945 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
|
946 # implicit 'self' (or 'cls' for classmethods) argument |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
947 positional = (func.__self__,) + positional |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
948 num_pos = len(positional) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
949 num_total = num_pos + len(named) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
950 num_args = len(args) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
951 num_defaults = len(defaults) if defaults else 0 |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
952 for arg, value in zip(args, positional): |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
953 arg2value[arg] = value |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
954 if varargs: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
955 if num_pos > num_args: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
956 arg2value[varargs] = positional[-(num_pos-num_args):] |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
957 else: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
958 arg2value[varargs] = () |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
959 elif 0 < num_args < num_pos: |
|
62492
69fdf68e0b5b
only take into account positional arguments count in related error messages
Benjamin Peterson <benjamin@python.org>
parents:
62371
diff
changeset
|
960 raise TypeError('%s() takes %s %d positional %s (%d given)' % ( |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
961 f_name, 'at most' if defaults else 'exactly', num_args, |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
962 'arguments' if num_args > 1 else 'argument', num_total)) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
963 elif num_args == 0 and num_total: |
|
69031
57e99f5f5e8f
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents:
68575
diff
changeset
|
964 if varkw or kwonlyargs: |
|
57e99f5f5e8f
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents:
68575
diff
changeset
|
965 if num_pos: |
|
57e99f5f5e8f
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents:
68575
diff
changeset
|
966 # XXX: We should use num_pos, but Python also uses num_total: |
|
57e99f5f5e8f
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents:
68575
diff
changeset
|
967 raise TypeError('%s() takes exactly 0 positional arguments ' |
|
57e99f5f5e8f
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents:
68575
diff
changeset
|
968 '(%d given)' % (f_name, num_total)) |
|
57e99f5f5e8f
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents:
68575
diff
changeset
|
969 else: |
|
57e99f5f5e8f
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents:
68575
diff
changeset
|
970 raise TypeError('%s() takes no arguments (%d given)' % |
|
57e99f5f5e8f
Correct handling of functions with only kwarg args in getcallargs (closes #11256)
Benjamin Peterson <benjamin@python.org>
parents:
68575
diff
changeset
|
971 (f_name, num_total)) |
|
60081
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
972 |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
973 for arg in itertools.chain(args, kwonlyargs): |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
974 if arg in named: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
975 if arg in arg2value: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
976 raise TypeError("%s() got multiple values for keyword " |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
977 "argument '%s'" % (f_name, arg)) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
978 else: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
979 arg2value[arg] = named.pop(arg) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
980 for kwonlyarg in kwonlyargs: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
981 if kwonlyarg not in arg2value: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
982 try: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
983 arg2value[kwonlyarg] = kwonlydefaults[kwonlyarg] |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
984 except KeyError: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
985 raise TypeError("%s() needs keyword-only argument %s" % |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
986 (f_name, kwonlyarg)) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
987 if defaults: # fill in any missing values with the defaults |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
988 for arg, value in zip(args[-num_defaults:], defaults): |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
989 if arg not in arg2value: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
990 arg2value[arg] = value |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
991 if varkw: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
992 arg2value[varkw] = named |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
993 elif named: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
994 unexpected = next(iter(named)) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
995 raise TypeError("%s() got an unexpected keyword argument '%s'" % |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
996 (f_name, unexpected)) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
997 unassigned = num_args - len([arg for arg in args if arg in arg2value]) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
998 if unassigned: |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
999 num_required = num_args - num_defaults |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1000 raise TypeError('%s() takes %s %d %s (%d given)' % ( |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1001 f_name, 'at least' if defaults else 'exactly', num_required, |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1002 'arguments' if num_required > 1 else 'argument', num_total)) |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1003 return arg2value |
|
bc6814c34d5f
Merged revisions 79500 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
57209
diff
changeset
|
1004 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1005 # -------------------------------------------------- stack frame extraction |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
1006 |
|
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
1007 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
|
1008 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1009 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
|
1010 """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
|
1011 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1012 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
|
1013 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
|
1014 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
|
1015 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
|
1016 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
|
1017 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
|
1018 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
|
1019 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
|
1020 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
|
1021 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
|
1022 if not isframe(frame): |
|
56368
c9bd62efe6ce
Merged revisions 75231 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
55584
diff
changeset
|
1023 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
|
1024 |
|
22486
8ecd1bad2408
Use linecache for loading source code. Closes SF patch 490374.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22348
diff
changeset
|
1025 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
|
1026 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
|
1027 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
|
1028 try: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1029 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
|
1030 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
|
1031 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
|
1032 else: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1033 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
|
1034 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
|
1035 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
|
1036 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
|
1037 else: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1038 lines = index = None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1039 |
|
44643
d8e4342c145e
Merged revisions 59883-59920 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44160
diff
changeset
|
1040 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
|
1041 |
|
14dbde1ecdea
Add getlineno() routine to account for LINENO optimization.
Ka-Ping Yee <ping@zesty.ca>
parents:
16833
diff
changeset
|
1042 def getlineno(frame): |
|
14dbde1ecdea
Add getlineno() routine to account for LINENO optimization.
Ka-Ping Yee <ping@zesty.ca>
parents:
16833
diff
changeset
|
1043 """Get the line number from a frame object, allowing for optimization.""" |
| 24844 | 1044 # FrameType.f_lineno is now a descriptor that grovels co_lnotab |
| 1045 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
|
1046 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1047 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
|
1048 """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
|
1049 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1050 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
|
1051 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
|
1052 framelist = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1053 while frame: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1054 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
|
1055 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
|
1056 return framelist |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1057 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1058 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
|
1059 """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
|
1060 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1061 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
|
1062 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
|
1063 framelist = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1064 while tb: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1065 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
|
1066 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
|
1067 return framelist |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1068 |
|
63876
795eb12403b9
don't alias directly, so that extra arguments don't appear #6678
Benjamin Peterson <benjamin@python.org>
parents:
62492
diff
changeset
|
1069 def currentframe(): |
| 63877 | 1070 """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
|
1071 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
|
1072 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1073 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
|
1074 """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
|
1075 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
|
1076 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
1077 def trace(context=1): |
|
16833
bbdeb6d4dea1
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16790
diff
changeset
|
1078 """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
|
1079 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
|
1080 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1081 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1082 # ------------------------------------------------ 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
|
1083 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1084 _sentinel = object() |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1085 |
|
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
|
1086 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
|
1087 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
|
1088 |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1089 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
|
1090 instance_dict = {} |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1091 try: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1092 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
|
1093 except AttributeError: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1094 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
|
1095 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
|
1096 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1097 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1098 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
|
1099 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
|
1100 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
|
1101 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
|
1102 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
|
1103 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
|
1104 pass |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1105 return _sentinel |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1106 |
|
66293
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1107 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
|
1108 try: |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1109 _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
|
1110 except TypeError: |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1111 return False |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1112 return True |
|
1ef4651feef8
Issue 9732: __class__ no longer checked on objects by getattr_static
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
66292
diff
changeset
|
1113 |
|
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
|
1114 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
|
1115 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
|
1116 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
|
1117 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
|
1118 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
|
1119 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
|
1120 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
|
1121 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
|
1122 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
|
1123 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
|
1124 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
|
1125 return class_dict |
|
56731ccf2e86
Fix inspect.getattr_static to work on modules (again).
Michael Foord <michael@voidspace.org.uk>
parents:
73789
diff
changeset
|
1126 return _sentinel |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1127 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1128 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
|
1129 """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
|
1130 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
|
1131 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1132 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
|
1133 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
|
1134 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
|
1135 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
|
1136 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
|
1137 documentation for details. |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1138 """ |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1139 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
|
1140 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
|
1141 klass = type(obj) |
|
74062
56731ccf2e86
Fix inspect.getattr_static to work on modules (again).
Michael Foord <michael@voidspace.org.uk>
parents:
73789
diff
changeset
|
1142 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
|
1143 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
|
1144 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
|
1145 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
|
1146 else: |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1147 klass = obj |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1148 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1149 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
|
1150 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1151 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
|
1152 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
|
1153 _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
|
1154 return klass_result |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1155 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1156 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
|
1157 return instance_result |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1158 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
|
1159 return klass_result |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1160 |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1161 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
|
1162 # 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
|
1163 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
|
1164 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
|
1165 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
|
1166 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
|
1167 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
|
1168 pass |
|
66280
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1169 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
|
1170 return default |
|
4ed6101f8ab2
Issue 9732: addition of getattr_static to the inspect module
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
65922
diff
changeset
|
1171 raise AttributeError(attr) |
|
66344
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1172 |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1173 |
|
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
|
1174 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
|
1175 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
|
1176 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
|
1177 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
|
1178 |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1179 def getgeneratorstate(generator): |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1180 """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
|
1181 |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1182 Possible states are: |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1183 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
|
1184 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
|
1185 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
|
1186 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
|
1187 """ |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1188 if generator.gi_running: |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1189 return GEN_RUNNING |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1190 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
|
1191 return GEN_CLOSED |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1192 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
|
1193 return GEN_CREATED |
|
fdfc6dca8048
Issue #10220: Add inspect.getgeneratorstate(). Initial patch by Rodolpho Eckhardt
Nick Coghlan <ncoghlan@gmail.com>
parents:
66293
diff
changeset
|
1194 return GEN_SUSPENDED |
