Mercurial > cpython-lru_cache-weakref
annotate Lib/inspect.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | f42e07ab326e |
| children | b36cb4602e21 |
| rev | line source |
|---|---|
|
24537
28a5c8afa056
Add encoding declaration.
Martin v. Löwis <martin@v.loewis.de>
parents:
23634
diff
changeset
|
1 # -*- coding: iso-8859-1 -*- |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
2 """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
|
3 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
4 This module encapsulates the interface provided by the internal special |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
5 attributes (func_*, co_*, im_*, tb_*, etc.) in a friendlier fashion. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
6 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
|
7 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
8 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
|
9 |
|
45386
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
10 ismodule(), isclass(), ismethod(), isfunction(), isgeneratorfunction(), |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
11 isgenerator(), istraceback(), isframe(), iscode(), isbuiltin(), |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
12 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
|
13 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
|
14 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 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
|
19 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
20 getargspec(), getargvalues() - get info about function arguments |
|
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 |
|
45386
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
32 import sys |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
33 import os |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
34 import types |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
35 import string |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
36 import re |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
37 import dis |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
38 import imp |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
39 import tokenize |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
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 |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
42 from collections import namedtuple |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
43 |
|
48121
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
44 # These constants are from Include/code.h. |
|
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
45 CO_OPTIMIZED, CO_NEWLOCALS, CO_VARARGS, CO_VARKEYWORDS = 0x1, 0x2, 0x4, 0x8 |
|
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
46 CO_NESTED, CO_GENERATOR, CO_NOFREE = 0x10, 0x20, 0x40 |
|
45634
6282dfd5dc49
Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents:
45386
diff
changeset
|
47 # See Include/object.h |
|
6282dfd5dc49
Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents:
45386
diff
changeset
|
48 TPFLAGS_IS_ABSTRACT = 1 << 20 |
|
6282dfd5dc49
Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents:
45386
diff
changeset
|
49 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
50 # ----------------------------------------------------------- type-checking |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
51 def ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
52 """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
|
53 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
54 Module 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
|
55 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
56 __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
|
57 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
|
58 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
59 def isclass(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
60 """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
|
61 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
62 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
|
63 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
64 __module__ name of module in which this class was defined""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
65 return isinstance(object, types.ClassType) or hasattr(object, '__bases__') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
66 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
67 def ismethod(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
68 """Return true if the object is an instance method. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
69 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
70 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
|
71 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
72 __name__ name with which this method was defined |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
73 im_class class object in which this method belongs |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
74 im_func function object containing implementation of method |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
75 im_self instance to which this 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
|
76 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
|
77 |
|
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
|
78 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
|
79 """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
|
80 |
|
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
81 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
|
82 |
|
cec9e25af121
After much thrashing, I believe this is a truly minimal patch to teach
Tim Peters <tim.peters@gmail.com>
parents:
19930
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 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
|
87 |
|
19992
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
88 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
|
89 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
|
90 the other tests promise more -- you can, e.g., count on having the |
|
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
91 im_func attribute (etc) when an object passes ismethod().""" |
|
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
|
92 return (hasattr(object, "__get__") |
|
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 and not hasattr(object, "__set__") # else it's a data descriptor |
|
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 and not ismethod(object) # mutual exclusion |
|
19992
9d7ca5914d1c
Ensure that isfunction(obj) and (the new) ismethoddescriptor(obj) never
Tim Peters <tim.peters@gmail.com>
parents:
19989
diff
changeset
|
95 and not isfunction(object) |
|
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
|
96 and not isclass(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
|
97 |
|
28948
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
98 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
|
99 """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
|
100 |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
101 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
|
102 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
|
103 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
|
104 (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
|
105 is not guaranteed.""" |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
106 return (hasattr(object, "__set__") and hasattr(object, "__get__")) |
|
33598e8e49b2
Patch #711902: Cause pydoc to show data descriptor __doc__ strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
28419
diff
changeset
|
107 |
|
39226
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
108 if hasattr(types, 'MemberDescriptorType'): |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
109 # CPython and equivalent |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
110 def ismemberdescriptor(object): |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
111 """Return true if the object is a member descriptor. |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
112 |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
113 Member descriptors are specialized descriptors defined in extension |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
114 modules.""" |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
115 return isinstance(object, types.MemberDescriptorType) |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
116 else: |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
117 # Other implementations |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
118 def ismemberdescriptor(object): |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
119 """Return true if the object is a member descriptor. |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
120 |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
121 Member descriptors are specialized descriptors defined in extension |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
122 modules.""" |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
123 return False |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
124 |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
125 if hasattr(types, 'GetSetDescriptorType'): |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
126 # CPython and equivalent |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
127 def isgetsetdescriptor(object): |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
128 """Return true if the object is a getset descriptor. |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
129 |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
130 getset descriptors are specialized descriptors defined in extension |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
131 modules.""" |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
132 return isinstance(object, types.GetSetDescriptorType) |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
133 else: |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
134 # Other implementations |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
135 def isgetsetdescriptor(object): |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
136 """Return true if the object is a getset descriptor. |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
137 |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
138 getset descriptors are specialized descriptors defined in extension |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
139 modules.""" |
|
28cdd43e0569
Patch #1520294: Support for getset and member descriptors in types.py,
Barry Warsaw <barry@python.org>
parents:
39121
diff
changeset
|
140 return False |
|
39228
0b3fd04cdfb4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
39226
diff
changeset
|
141 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
142 def isfunction(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
143 """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
|
144 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
145 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
|
146 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
147 __name__ name with which this function was defined |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
148 func_code code object containing compiled function bytecode |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
149 func_defaults tuple of any default values for arguments |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
150 func_doc (same as __doc__) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
151 func_globals global namespace in which this function was defined |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
152 func_name (same as __name__)""" |
|
19930
36c07a1718e9
In a world with a growing number of subclassable types, replace
Tim Peters <tim.peters@gmail.com>
parents:
19694
diff
changeset
|
153 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
|
154 |
|
45386
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
155 def isgeneratorfunction(object): |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
156 """Return true if the object is a user-defined generator function. |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
157 |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
158 Generator function objects provides same attributes as functions. |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
159 |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
160 See isfunction.__doc__ for attributes listing.""" |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49568
diff
changeset
|
161 return bool((isfunction(object) or ismethod(object)) and |
|
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49568
diff
changeset
|
162 object.func_code.co_flags & CO_GENERATOR) |
|
45386
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
163 |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
164 def isgenerator(object): |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
165 """Return true if the object is a generator. |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
166 |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
167 Generator objects provide these attributes: |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
168 __iter__ defined to support interation over container |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
169 close raises a new GeneratorExit exception inside the |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
170 generator to terminate the iteration |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
171 gi_code code object |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
172 gi_frame frame object or possibly None once the generator has |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
173 been exhausted |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
174 gi_running set to 1 when generator is executing, 0 otherwise |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
175 next return the next item from the container |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
176 send resumes the generator and "sends" a value that becomes |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
177 the result of the current yield-expression |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
178 throw used to raise an exception inside the generator""" |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
179 return isinstance(object, types.GeneratorType) |
|
1bc923a107d9
Issue #1916. Added isgenerator() and isgeneratorfunction() to
Facundo Batista <facundobatista@gmail.com>
parents:
44632
diff
changeset
|
180 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
181 def istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
182 """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
|
183 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
184 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
|
185 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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
191 def isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
192 """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
|
193 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
194 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
|
195 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
|
196 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
|
197 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
|
198 f_exc_traceback traceback if raised in this frame, or None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
199 f_exc_type exception type if raised in this frame, or None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
200 f_exc_value exception value if raised in this frame, or None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
201 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
|
202 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
|
203 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
|
204 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
|
205 f_restricted 0 or 1 if frame is in restricted execution mode |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
206 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
|
207 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
|
208 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
209 def iscode(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
210 """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
|
211 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
212 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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
227 def isbuiltin(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
228 """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
|
229 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
230 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
|
231 __doc__ documentation string |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
232 __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
|
233 __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
|
234 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
|
235 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
236 def isroutine(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
237 """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
|
238 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
|
239 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
|
240 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
|
241 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
|
242 |
|
45634
6282dfd5dc49
Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents:
45386
diff
changeset
|
243 def isabstract(object): |
|
6282dfd5dc49
Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents:
45386
diff
changeset
|
244 """Return true if the object is an abstract base class (ABC).""" |
|
46359
8543f8c81da0
Prevent an error when inspect.isabstract() is called with something else than a new-style class.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
45634
diff
changeset
|
245 return isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT |
|
45634
6282dfd5dc49
Since abc._Abstract was replaces by a new type flags the regression test suite fails. I've added a new function inspect.isabstract(). Is the mmethod fine or should I check if object is a instance of type or subclass of object, too?
Christian Heimes <christian@cheimes.de>
parents:
45386
diff
changeset
|
246 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
247 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
|
248 """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
|
249 Optionally, only return members that satisfy a given predicate.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
250 results = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
251 for key in dir(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
252 value = getattr(object, key) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
253 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
|
254 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
|
255 results.sort() |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
256 return results |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
257 |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
258 Attribute = namedtuple('Attribute', 'name kind defining_class object') |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
259 |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
260 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
|
261 """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
|
262 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
263 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
|
264 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
|
265 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
266 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
|
267 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
268 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
|
269 '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
|
270 '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
|
271 '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
|
272 '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
|
273 '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
|
274 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
275 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
|
276 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
277 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
|
278 __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
|
279 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
|
280 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
|
281 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
|
282 """ |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
283 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
284 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
|
285 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
|
286 result = [] |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
287 for name in names: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
288 # Get the object associated with the name. |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
289 # 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
|
290 # using getattr. Static and class methods are dramatic examples. |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
291 if name in cls.__dict__: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
292 obj = cls.__dict__[name] |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
293 else: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
294 obj = getattr(cls, name) |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
295 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
296 # Figure out where it was defined. |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
297 homecls = getattr(obj, "__objclass__", None) |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
298 if homecls is None: |
|
20445
27ab0327b2fc
Get rid of __defined__ and tp_defined -- there's no need to
Guido van Rossum <guido@python.org>
parents:
20040
diff
changeset
|
299 # search the dicts. |
|
20040
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
300 for base in mro: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
301 if name in base.__dict__: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
302 homecls = base |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
303 break |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
304 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
305 # Get the object again, in order to get it from the defining |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
306 # __dict__ instead of via getattr (if possible). |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
307 if homecls is not None and name in homecls.__dict__: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
308 obj = homecls.__dict__[name] |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
309 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
310 # Also get the object via getattr. |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
311 obj_via_getattr = getattr(cls, name) |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
312 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
313 # 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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 kind = "property" |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
320 elif (ismethod(obj_via_getattr) or |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
321 ismethoddescriptor(obj_via_getattr)): |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
322 kind = "method" |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
323 else: |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
324 kind = "data" |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
325 |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
326 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
|
327 |
|
59882e7aab89
New function classify_class_attrs(). As a number of SF bug reports
Tim Peters <tim.peters@gmail.com>
parents:
20036
diff
changeset
|
328 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
|
329 |
|
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
|
330 # ----------------------------------------------------------- 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
|
331 def _searchbases(cls, accum): |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
332 # Simulate the "classic class" search order. |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
333 if cls in accum: |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
334 return |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
335 accum.append(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
|
336 for base in cls.__bases__: |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
337 _searchbases(base, accum) |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
338 |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
339 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
|
340 "Return tuple of base classes (including cls) in method resolution order." |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
341 if hasattr(cls, "__mro__"): |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
342 return cls.__mro__ |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
343 else: |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
344 result = [] |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
345 _searchbases(cls, result) |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
346 return tuple(result) |
|
ab62d147ba90
Add a function to compute a class's method resolution order. This is
Tim Peters <tim.peters@gmail.com>
parents:
19992
diff
changeset
|
347 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
348 # -------------------------------------------------- source code extraction |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
349 def indentsize(line): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
350 """Return the indent size, in spaces, at the start of a line of text.""" |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
351 expline = string.expandtabs(line) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
352 return len(expline) - len(string.lstrip(expline)) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
353 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
354 def getdoc(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
355 """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
|
356 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
357 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
|
358 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
|
359 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
|
360 try: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
361 doc = object.__doc__ |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
362 except AttributeError: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
363 return None |
|
23486
bb5790c6cdaa
Use types.StringTypes instead of explicit (str, unicode) list
Michael W. Hudson <mwh@python.net>
parents:
23281
diff
changeset
|
364 if not isinstance(doc, types.StringTypes): |
|
22348
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
365 return None |
|
47506
ebf6f639e117
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents:
46359
diff
changeset
|
366 return cleandoc(doc) |
|
ebf6f639e117
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents:
46359
diff
changeset
|
367 |
|
ebf6f639e117
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents:
46359
diff
changeset
|
368 def cleandoc(doc): |
|
ebf6f639e117
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents:
46359
diff
changeset
|
369 """Clean up indentation from docstrings. |
|
ebf6f639e117
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents:
46359
diff
changeset
|
370 |
|
ebf6f639e117
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents:
46359
diff
changeset
|
371 Any whitespace that can be uniformly removed from the second line |
|
ebf6f639e117
Factor out docstring dedenting from inspect.getdoc() into inspect.cleandoc()
Georg Brandl <georg@python.org>
parents:
46359
diff
changeset
|
372 onwards is removed.""" |
|
22348
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
373 try: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
374 lines = string.split(string.expandtabs(doc), '\n') |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
375 except UnicodeError: |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
376 return None |
|
51aa9eb24d81
SF patch 530070: pydoc regression, from Martin and Guido.
Tim Peters <tim.peters@gmail.com>
parents:
22264
diff
changeset
|
377 else: |
| 26467 | 378 # Find minimum indentation of any non-blank lines after first line. |
| 379 margin = sys.maxint | |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
380 for line in lines[1:]: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
381 content = len(string.lstrip(line)) |
| 26467 | 382 if content: |
| 383 indent = len(line) - content | |
| 384 margin = min(margin, indent) | |
| 385 # Remove indentation. | |
| 386 if lines: | |
| 387 lines[0] = lines[0].lstrip() | |
| 388 if margin < sys.maxint: | |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
389 for i in range(1, len(lines)): lines[i] = lines[i][margin:] |
| 26467 | 390 # Remove any trailing or leading blank lines. |
| 391 while lines and not lines[-1]: | |
| 392 lines.pop() | |
| 393 while lines and not lines[0]: | |
| 394 lines.pop(0) | |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
395 return string.join(lines, '\n') |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
396 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
397 def getfile(object): |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
398 """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
|
399 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
400 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
|
401 return object.__file__ |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
402 raise TypeError('arg is a built-in module') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
403 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
|
404 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
|
405 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
|
406 return object.__file__ |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
407 raise TypeError('arg is a built-in class') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
408 if ismethod(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
409 object = object.im_func |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
410 if isfunction(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
411 object = object.func_code |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
412 if istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
413 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
|
414 if isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
415 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
|
416 if iscode(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
417 return object.co_filename |
|
29668
36a17904a3ed
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29617
diff
changeset
|
418 raise TypeError('arg is not a module, class, method, ' |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
419 'function, traceback, frame, or code object') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
420 |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
421 ModuleInfo = namedtuple('ModuleInfo', 'name suffix mode module_type') |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
422 |
|
17370
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
423 def getmoduleinfo(path): |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
424 """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
|
425 filename = os.path.basename(path) |
|
48121
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
426 suffixes = map(lambda info: |
|
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
427 (-len(info[0]), info[0], info[1], info[2]), |
|
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
428 imp.get_suffixes()) |
|
17370
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
429 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
|
430 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
|
431 if filename[neglen:] == suffix: |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
432 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
|
433 |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
434 def getmodulename(path): |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
435 """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
|
436 info = getmoduleinfo(path) |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
437 if info: return info[0] |
|
65f363236ecf
Add getmodulename() and getmoduleinfo() routines to inspect filenames.
Ka-Ping Yee <ping@zesty.ca>
parents:
17190
diff
changeset
|
438 |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
439 def getsourcefile(object): |
|
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
440 """Return the Python source file an object was defined in, if it exists.""" |
|
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
441 filename = getfile(object) |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
34152
diff
changeset
|
442 if string.lower(filename[-4:]) in ('.pyc', '.pyo'): |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
443 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
|
444 for suffix, mode, kind in imp.get_suffixes(): |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
445 if 'b' in mode and string.lower(filename[-len(suffix):]) == suffix: |
|
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
446 # 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
|
447 return None |
|
38056
69f0457c17bb
Fix infinite regress when inspecting <string> or <stdin> frames.
Phillip J. Eby <pje@telecommunity.com>
parents:
37593
diff
changeset
|
448 if os.path.exists(filename): |
|
69f0457c17bb
Fix infinite regress when inspecting <string> or <stdin> frames.
Phillip J. Eby <pje@telecommunity.com>
parents:
37593
diff
changeset
|
449 return filename |
|
39053
b978afe7cab6
Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
38057
diff
changeset
|
450 # only return a non-existent filename if the module has a PEP 302 loader |
|
b978afe7cab6
Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
38057
diff
changeset
|
451 if hasattr(getmodule(object, filename), '__loader__'): |
|
16927
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
452 return filename |
|
2548d1a70180
Clarify the purpose of getsourcefile().
Ka-Ping Yee <ping@zesty.ca>
parents:
16924
diff
changeset
|
453 |
|
39053
b978afe7cab6
Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
38057
diff
changeset
|
454 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
|
455 """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
|
456 |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
457 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
|
458 normalizes the result as much as possible.""" |
|
39121
5c903f650719
Fix SF#1516184 (again) and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
39053
diff
changeset
|
459 if _filename is None: |
|
5c903f650719
Fix SF#1516184 (again) and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
39053
diff
changeset
|
460 _filename = getsourcefile(object) or getfile(object) |
|
5c903f650719
Fix SF#1516184 (again) and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
39053
diff
changeset
|
461 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
|
462 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
463 modulesbyfile = {} |
|
39796
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
464 _filesbymodname = {} |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
465 |
|
39053
b978afe7cab6
Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
38057
diff
changeset
|
466 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
|
467 """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
|
468 if ismodule(object): |
|
926b64fba5e7
Remove duplicate type objects from isroutine() and isbuiltin().
Ka-Ping Yee <ping@zesty.ca>
parents:
17446
diff
changeset
|
469 return object |
|
33455
9e3c52ca3a04
Use __module__ attribute when available instead of using isclass() predicate
Johannes Gijsbers <jlg@dds.nl>
parents:
33172
diff
changeset
|
470 if hasattr(object, '__module__'): |
| 16898 | 471 return sys.modules.get(object.__module__) |
|
39796
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
472 # Try the filename to modulename cache |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
473 if _filename is not None and _filename in modulesbyfile: |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
474 return sys.modules.get(modulesbyfile[_filename]) |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
475 # 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
|
476 try: |
|
39053
b978afe7cab6
Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
38057
diff
changeset
|
477 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
|
478 except TypeError: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
479 return None |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
480 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
|
481 return sys.modules.get(modulesbyfile[file]) |
|
39796
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
482 # Update the filename to module name cache and check yet again |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
483 # Copy sys.modules in order to cope with changes while iterating |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
484 for modname, module in sys.modules.items(): |
|
37576
d11366f7e1e7
Updated the warnings, linecache, inspect, traceback, site, and doctest modules
Phillip J. Eby <pje@telecommunity.com>
parents:
35640
diff
changeset
|
485 if ismodule(module) and hasattr(module, '__file__'): |
|
39796
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
486 f = module.__file__ |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
487 if f == _filesbymodname.get(modname, None): |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
488 # Have already mapped this module, so skip it |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
489 continue |
|
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
490 _filesbymodname[modname] = f |
|
39053
b978afe7cab6
Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
38057
diff
changeset
|
491 f = getabsfile(module) |
|
39796
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
492 # Always map to the name the module knows itself by |
|
39053
b978afe7cab6
Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
38057
diff
changeset
|
493 modulesbyfile[f] = modulesbyfile[ |
|
b978afe7cab6
Fix SF#1516184 and add a test to prevent regression.
Phillip J. Eby <pje@telecommunity.com>
parents:
38057
diff
changeset
|
494 os.path.realpath(f)] = module.__name__ |
|
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]) |
|
39796
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
497 # 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
|
498 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
|
499 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
|
500 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
|
501 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
|
502 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
|
503 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
|
504 return main |
|
39796
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
505 # Check builtins |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
506 builtin = sys.modules['__builtin__'] |
|
16938
5d7e0ea31818
Make getsourcefile() succeed even if the filename doesn't end in '.py' --
Ka-Ping Yee <ping@zesty.ca>
parents:
16927
diff
changeset
|
507 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
|
508 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
|
509 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
|
510 return builtin |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
511 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
512 def findsource(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
513 """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
|
514 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
515 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
|
516 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
|
517 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
|
518 is raised if the source code cannot be retrieved.""" |
|
22486
8ecd1bad2408
Use linecache for loading source code. Closes SF patch 490374.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22348
diff
changeset
|
519 file = getsourcefile(object) or getfile(object) |
|
39796
38ba9524783a
Fix the speed regression in inspect.py by adding another cache to speed up getmodule(). Patch #1553314
Nick Coghlan <ncoghlan@gmail.com>
parents:
39228
diff
changeset
|
520 module = getmodule(object, file) |
|
38057
8f7abc2ba7d0
Fix another problem in inspect: if the module for an object cannot be found, don't try to give its __dict__ to linecache.
Georg Brandl <georg@python.org>
parents:
38056
diff
changeset
|
521 if module: |
|
8f7abc2ba7d0
Fix another problem in inspect: if the module for an object cannot be found, don't try to give its __dict__ to linecache.
Georg Brandl <georg@python.org>
parents:
38056
diff
changeset
|
522 lines = linecache.getlines(file, module.__dict__) |
|
8f7abc2ba7d0
Fix another problem in inspect: if the module for an object cannot be found, don't try to give its __dict__ to linecache.
Georg Brandl <georg@python.org>
parents:
38056
diff
changeset
|
523 else: |
|
8f7abc2ba7d0
Fix another problem in inspect: if the module for an object cannot be found, don't try to give its __dict__ to linecache.
Georg Brandl <georg@python.org>
parents:
38056
diff
changeset
|
524 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
|
525 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
|
526 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
|
527 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
528 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
529 return lines, 0 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
530 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
531 if isclass(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
532 name = object.__name__ |
|
40097
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
533 pat = re.compile(r'^(\s*)class\s*' + name + r'\b') |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
534 # make some effort to find the best matching class definition: |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
535 # use the one with the least indentation, which is the one |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
536 # that's most probably not inside a function definition. |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
537 candidates = [] |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
538 for i in range(len(lines)): |
|
40097
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
539 match = pat.match(lines[i]) |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
540 if match: |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
541 # if it's at toplevel, it's already the best one |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
542 if lines[i][0] == 'c': |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
543 return lines, i |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
544 # else add whitespace to candidate list |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
545 candidates.append((match.group(1), i)) |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
546 if candidates: |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
547 # this will sort by whitespace, and by line number, |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
548 # less whitespace first |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
549 candidates.sort() |
|
c0208195a558
Bug #1550524: better heuristics to find correct class definition
Georg Brandl <georg@python.org>
parents:
39796
diff
changeset
|
550 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
|
551 else: |
|
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
552 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
|
553 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
554 if ismethod(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
555 object = object.im_func |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
556 if isfunction(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
557 object = object.func_code |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
558 if istraceback(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
559 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
|
560 if isframe(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
561 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
|
562 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
|
563 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
|
564 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
|
565 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
|
566 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
|
567 while lnum > 0: |
|
17020
953c50f12184
Fix findsource() to work for derived classes.
Ka-Ping Yee <ping@zesty.ca>
parents:
16938
diff
changeset
|
568 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
|
569 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
|
570 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
|
571 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
|
572 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
573 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
|
574 """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
|
575 |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
576 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
|
577 """ |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
578 try: |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
579 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
|
580 except (IOError, TypeError): |
|
9c2ca37bdeec
Fix getcomments() so that it doesn't fail with TypeErrors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
22563
diff
changeset
|
581 return None |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
582 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
583 if ismodule(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
584 # 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
|
585 start = 0 |
|
17446
0184c50a8751
Robustify getcomments() so it doesn't crash on empty files.
Ka-Ping Yee <ping@zesty.ca>
parents:
17370
diff
changeset
|
586 if lines and lines[0][:2] == '#!': start = 1 |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
34152
diff
changeset
|
587 while start < len(lines) and string.strip(lines[start]) in ('', '#'): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
588 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
|
589 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
|
590 comments = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
591 end = start |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
592 while end < len(lines) and lines[end][:1] == '#': |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
593 comments.append(string.expandtabs(lines[end])) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
594 end = end + 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
595 return string.join(comments, '') |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
596 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
597 # 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
|
598 elif lnum > 0: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
599 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
|
600 end = lnum - 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
601 if end >= 0 and string.lstrip(lines[end])[:1] == '#' and \ |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
602 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
|
603 comments = [string.lstrip(string.expandtabs(lines[end]))] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
604 if end > 0: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
605 end = end - 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
606 comment = string.lstrip(string.expandtabs(lines[end])) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
607 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
|
608 comments[:0] = [comment] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
609 end = end - 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
610 if end < 0: break |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
611 comment = string.lstrip(string.expandtabs(lines[end])) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
612 while comments and string.strip(comments[0]) == '#': |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
613 comments[:1] = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
614 while comments and string.strip(comments[-1]) == '#': |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
615 comments[-1:] = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
616 return string.join(comments, '') |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
617 |
|
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
|
618 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
|
619 |
|
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
|
620 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
|
621 """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
|
622 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
|
623 self.indent = 0 |
|
34740
3bb759498767
Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents:
34734
diff
changeset
|
624 self.islambda = False |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
625 self.started = False |
|
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
626 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
|
627 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
|
628 |
|
48121
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
629 def tokeneater(self, type, token, srow_scol, erow_ecol, line): |
|
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
630 srow, scol = srow_scol |
|
1140774f69a5
Silence (Syntax|Deprecation)Warning for 'inspect'. Had to remove tuple
Brett Cannon <bcannon@gmail.com>
parents:
47506
diff
changeset
|
631 erow, ecol = erow_ecol |
|
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
|
632 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
|
633 # 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
|
634 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
|
635 if token == "lambda": |
|
3bb759498767
Patch #1159931/bug #1143895: inspect.getsource failed when functions,
Johannes Gijsbers <jlg@dds.nl>
parents:
34734
diff
changeset
|
636 self.islambda = True |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
637 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
|
638 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
|
639 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
|
640 self.passline = False # stop skipping when a NEWLINE is seen |
|
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
|
641 self.last = srow |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
642 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
|
643 raise EndOfBlock |
|
34152
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
644 elif self.passline: |
|
bbdaacc44f6f
Patch #1011890: fix inspect.getsource breaking with line-continuation &
Johannes Gijsbers <jlg@dds.nl>
parents:
33528
diff
changeset
|
645 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
|
646 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
|
647 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
|
648 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
|
649 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
|
650 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
|
651 # 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
|
652 # (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
|
653 # 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
|
654 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
|
655 raise EndOfBlock |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
656 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
|
657 # 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
|
658 # 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
|
659 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
|
660 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
661 def getblock(lines): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
662 """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
|
663 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
|
664 try: |
|
35640
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
665 tokenize.tokenize(iter(lines).next, blockfinder.tokeneater) |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
666 except (EndOfBlock, IndentationError): |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
667 pass |
|
1e7b755194a4
some more fixes and tests for inspect.getsource(), triggered by crashes
Armin Rigo <arigo@tunes.org>
parents:
34740
diff
changeset
|
668 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
|
669 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
670 def getsourcelines(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
671 """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
|
672 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
673 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
|
674 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
680 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
|
681 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
|
682 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
683 def getsource(object): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
684 """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
|
685 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
686 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
|
687 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
|
688 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
|
689 lines, lnum = getsourcelines(object) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
690 return string.join(lines, '') |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
691 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
692 # --------------------------------------------------- class tree extraction |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
693 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
|
694 """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
|
695 results = [] |
|
34734
d355da1d7c8f
Apply itemgetter() instead of lambda.
Raymond Hettinger <python@rcn.com>
parents:
34545
diff
changeset
|
696 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
|
697 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
|
698 results.append((c, c.__bases__)) |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
699 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
|
700 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
|
701 return results |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
702 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
703 def getclasstree(classes, unique=0): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
704 """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
|
705 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
706 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
|
707 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
|
708 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
|
709 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
|
710 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
|
711 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
|
712 children = {} |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
713 roots = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
714 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
|
715 if c.__bases__: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
716 for parent in c.__bases__: |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23613
diff
changeset
|
717 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
|
718 children[parent] = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
719 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
|
720 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
|
721 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
|
722 roots.append(c) |
|
23634
dbee2f628a90
Replaced .keys() with dictionary iterators
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
723 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
|
724 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
|
725 roots.append(parent) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
726 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
|
727 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
728 # ------------------------------------------------ argument list extraction |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
729 Arguments = namedtuple('Arguments', 'args varargs keywords') |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
730 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
731 def getargs(co): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
732 """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
|
733 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
734 Three things are returned: (args, varargs, varkw), where 'args' is |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
735 a list of argument names (possibly containing nested lists), and |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
736 '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
|
737 |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
738 if not iscode(co): |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
739 raise TypeError('arg is not a code object') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
740 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
741 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
|
742 names = co.co_varnames |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
743 args = list(names[:nargs]) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
744 step = 0 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
745 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
746 # The following acrobatics are for anonymous (tuple) arguments. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
747 for i in range(nargs): |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
34152
diff
changeset
|
748 if args[i][:1] in ('', '.'): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
749 stack, remain, count = [], [], [] |
|
42214
6319adb0041d
Patch #1739696: use code.co_code only if really necessary
Georg Brandl <georg@python.org>
parents:
40097
diff
changeset
|
750 while step < len(co.co_code): |
|
6319adb0041d
Patch #1739696: use code.co_code only if really necessary
Georg Brandl <georg@python.org>
parents:
40097
diff
changeset
|
751 op = ord(co.co_code[step]) |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
752 step = step + 1 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
753 if op >= dis.HAVE_ARGUMENT: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
754 opname = dis.opname[op] |
|
42214
6319adb0041d
Patch #1739696: use code.co_code only if really necessary
Georg Brandl <georg@python.org>
parents:
40097
diff
changeset
|
755 value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
756 step = step + 2 |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
34152
diff
changeset
|
757 if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
758 remain.append(value) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
759 count.append(value) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
760 elif opname == 'STORE_FAST': |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
761 stack.append(names[value]) |
|
33137
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
762 |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
763 # Special case for sublists of length 1: def foo((bar)) |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
764 # doesn't generate the UNPACK_TUPLE bytecode, so if |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
765 # `remain` is empty here, we have such a sublist. |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
766 if not remain: |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
767 stack[0] = [stack[0]] |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
768 break |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
769 else: |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
770 remain[-1] = remain[-1] - 1 |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
771 while remain[-1] == 0: |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
772 remain.pop() |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
773 size = count.pop() |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
774 stack[-size:] = [stack[-size:]] |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
775 if not remain: break |
|
ad268a8408f4
- Bug #891637, patch #1005466: fix inspect.getargs() crash on def foo((bar)).
Matthias Klose <doko@ubuntu.com>
parents:
33119
diff
changeset
|
776 remain[-1] = remain[-1] - 1 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
777 if not remain: break |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
778 args[i] = stack[0] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
779 |
|
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] |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
787 return Arguments(args, varargs, varkw) |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
788 |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
789 ArgSpec = namedtuple('ArgSpec', 'args varargs keywords defaults') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
790 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
791 def getargspec(func): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
792 """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
|
793 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
794 A tuple of four things is returned: (args, varargs, varkw, defaults). |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
795 'args' is a list of the argument names (it may contain nested lists). |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
796 '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
|
797 'defaults' is an n-tuple of the default values of the last n arguments. |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
798 """ |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
799 |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
800 if ismethod(func): |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
801 func = func.im_func |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
802 if not isfunction(func): |
|
0bdb4aced6e2
Fix for SF bug 620190: getargspec() doesn't work with methods.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29484
diff
changeset
|
803 raise TypeError('arg is not a Python function') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
804 args, varargs, varkw = getargs(func.func_code) |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
805 return ArgSpec(args, varargs, varkw, func.func_defaults) |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
806 |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
807 ArgInfo = namedtuple('ArgInfo', 'args varargs keywords locals') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
808 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
809 def getargvalues(frame): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
810 """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
|
811 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
812 A tuple of four things is returned: (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
|
813 'args' is a list of the argument names (it may contain nested lists). |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
814 'varargs' and 'varkw' are the names of the * and ** arguments or None. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
815 '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
|
816 args, varargs, varkw = getargs(frame.f_code) |
|
49568
df7422460660
Merged revisions 66995-66996 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48277
diff
changeset
|
817 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
|
818 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
819 def joinseq(seq): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
820 if len(seq) == 1: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
821 return '(' + seq[0] + ',)' |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
822 else: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
823 return '(' + string.join(seq, ', ') + ')' |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
824 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
825 def strseq(object, convert, join=joinseq): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
826 """Recursively walk a sequence, stringifying each element.""" |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
34152
diff
changeset
|
827 if type(object) in (list, tuple): |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
828 return join(map(lambda o, c=convert, j=join: strseq(o, c, j), object)) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
829 else: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
830 return convert(object) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
831 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
832 def formatargspec(args, varargs=None, varkw=None, defaults=None, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
833 formatarg=str, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
834 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
|
835 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
|
836 formatvalue=lambda value: '=' + repr(value), |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
837 join=joinseq): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
838 """Format an argument spec from the 4 values returned by getargspec. |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
839 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
840 The first four arguments are (args, varargs, varkw, defaults). The |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
841 other 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
|
842 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
|
843 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
|
844 specs = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
845 if defaults: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
846 firstdefault = len(args) - len(defaults) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
847 for i in range(len(args)): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
848 spec = strseq(args[i], formatarg, join) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
849 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
|
850 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
|
851 specs.append(spec) |
|
23613
4a1f5bd486ff
Replaced boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23486
diff
changeset
|
852 if varargs is not None: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
853 specs.append(formatvarargs(varargs)) |
|
23613
4a1f5bd486ff
Replaced boolean test with is None
Raymond Hettinger <python@rcn.com>
parents:
23486
diff
changeset
|
854 if varkw is not None: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
855 specs.append(formatvarkw(varkw)) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
856 return '(' + string.join(specs, ', ') + ')' |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
857 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
858 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
|
859 formatarg=str, |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
860 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
|
861 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
|
862 formatvalue=lambda value: '=' + repr(value), |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
863 join=joinseq): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
864 """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
|
865 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
866 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
|
867 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
|
868 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
|
869 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
|
870 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
|
871 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
|
872 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
|
873 specs = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
874 for i in range(len(args)): |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
875 specs.append(strseq(args[i], convert, join)) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
876 if varargs: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
877 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
|
878 if varkw: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
879 specs.append(formatvarkw(varkw) + formatvalue(locals[varkw])) |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
880 return '(' + string.join(specs, ', ') + ')' |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
881 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
882 # -------------------------------------------------- stack frame extraction |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
883 |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
884 Traceback = namedtuple('Traceback', 'filename lineno function code_context index') |
|
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
885 |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
886 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
|
887 """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
|
888 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
889 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
|
890 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
|
891 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
|
892 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
|
893 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
|
894 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
|
895 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
|
896 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
|
897 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
|
898 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
|
899 if not isframe(frame): |
|
29617
badcd158d81c
A bit o' reformatting and removal of non-_getframe currentframe().
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29613
diff
changeset
|
900 raise TypeError('arg is not a frame or traceback object') |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
901 |
|
22486
8ecd1bad2408
Use linecache for loading source code. Closes SF patch 490374.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22348
diff
changeset
|
902 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
|
903 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
|
904 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
|
905 try: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
906 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
|
907 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
|
908 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
|
909 else: |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
910 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
|
911 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
|
912 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
|
913 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
|
914 else: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
915 lines = index = None |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
916 |
|
44632
785237fa5f40
Let most inspect functions return named tuples
Raymond Hettinger <python@rcn.com>
parents:
42214
diff
changeset
|
917 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
|
918 |
|
14dbde1ecdea
Add getlineno() routine to account for LINENO optimization.
Ka-Ping Yee <ping@zesty.ca>
parents:
16833
diff
changeset
|
919 def getlineno(frame): |
|
14dbde1ecdea
Add getlineno() routine to account for LINENO optimization.
Ka-Ping Yee <ping@zesty.ca>
parents:
16833
diff
changeset
|
920 """Get the line number from a frame object, allowing for optimization.""" |
| 24844 | 921 # FrameType.f_lineno is now a descriptor that grovels co_lnotab |
| 922 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
|
923 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
924 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
|
925 """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
|
926 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
927 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
|
928 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
|
929 framelist = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
930 while frame: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
931 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
|
932 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
|
933 return framelist |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
934 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
935 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
|
936 """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
|
937 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
938 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
|
939 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
|
940 framelist = [] |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
941 while tb: |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
942 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
|
943 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
|
944 return framelist |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
945 |
|
55455
c10773bbafa0
Issue 6581. Michael Foord
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
50424
diff
changeset
|
946 if hasattr(sys, '_getframe'): |
|
c10773bbafa0
Issue 6581. Michael Foord
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
50424
diff
changeset
|
947 currentframe = sys._getframe |
|
c10773bbafa0
Issue 6581. Michael Foord
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
50424
diff
changeset
|
948 else: |
|
c10773bbafa0
Issue 6581. Michael Foord
Michael Foord <fuzzyman@voidspace.org.uk>
parents:
50424
diff
changeset
|
949 currentframe = lambda _=None: None |
|
16790
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
950 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
951 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
|
952 """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
|
953 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
|
954 |
|
67bad9f20b29
inspect: a module for getting information out of live Python objects
Ka-Ping Yee <ping@zesty.ca>
parents:
diff
changeset
|
955 def trace(context=1): |
|
16833
bbdeb6d4dea1
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16790
diff
changeset
|
956 """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
|
957 return getinnerframes(sys.exc_info()[2], context) |
