Is there any way to trace life cycle of objects during python code running? I know about sys.settrace function for setting callback, I can stop on each function call or line of code, but how can I get access to "living" objects during this? I would like to trace program so that I can do random checks after each stop, as if I really added code at stop place in sources.
Asked
Active
Viewed 2,089 times
2
-
Not sure what you are trying to achieve here. What kind of information are you trying to gather? – Martijn Pieters Jan 12 '13 at 12:42
-
For example, at this step execution of program passed to func `func1` and 1 line allready runned in which object `obj1` of some type was created. I want to check this obj1, for example get results from `dir(obj1)` or check class of `obj1` – exbluesbreaker Jan 12 '13 at 12:49
-
1And you cannot do that with `import pdb; pdb.set_trace()`? That gives you an interactive debugger at that point. Object creation is itself not traceable, put traces in their `__init__` or `__new__` functions itself. – Martijn Pieters Jan 12 '13 at 12:56
-
I want to trace program without modification of source code. I realized that i must add `pdb.set_trace()` to all functions in program, which i am tracing. Also i am interesting not in interactive tracing, i want to write code which extract needed data during execution. – exbluesbreaker Jan 12 '13 at 13:56
1 Answers
2
Up to some extend can be done by gc (garbage collector) module in CPython.
Garbage Collector There are few functions using object as parameter and can provide some information regarding object existence:
gc.get_referrers(*objs)
gc.get_referents(*objs)
gc.is_tracked(obj)
To simply check if object exists use 'try' as described here
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Ganesh Sittampalam Jan 14 '14 at 17:12
-
The link was given to python documentation. Actually it would be better for me to comment on OP question but i don't have rights yet to do that. I will paste some information from link. – asf las Jan 14 '14 at 17:35