Since Python 2.2 there is a handy function in the Garbage Collection Module called get_objects(). It gives back a list of all objects that are under control of the Garbeage Collector. This way you can extract informations of your application in runtime.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import gc
import inspect
exclude = [
"function",
"type",
"list",
"dict",
"tuple",
"wrapper_descriptor",
"module",
"method_descriptor",
"member_descriptor",
"instancemethod",
"builtin_function_or_method",
"frame",
"classmethod",
"classmethod_descriptor",
"_Environ",
"MemoryError",
"_Printer",
"_Helper",
"getset_descriptor",
]
def dumpObjects():
gc.collect()
oo = gc.get_objects()
for o in oo:
if getattr(o, "__class__", None):
name = o.__class__.__name__
if name not in exclude:
filename = inspect.getabsfile(o.__class__)
print "Object of class:", name, "...",
print "defined in file:", filename
if __name__=="__main__":
class TestClass:
pass
testObject1 = TestClass()
testObject2 = TestClass()
dumpObjects()
|
The example dumps a list of all higher level objects. At first it gets the list of all objects, than tests if they are higher level and in the end it it tests if it is not in the list of objects that are more or less always there. Then we can get all informations of the object we want. In this case we show the name of the class and where this class is defined.
A nice tool to write would be a runtime debugger in an own thread that serves as a little HTTPDServer, so that you could look into the running program no mater it is command line or windows based.
Test on Python 2.4.1. On Python 2.4.1, the code should be modified a little: -- Add "weakref" to the exclude list, such as:
And the result: