2

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.

4
  • Not sure what you are trying to achieve here. What kind of information are you trying to gather? Commented Jan 12, 2013 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 Commented Jan 12, 2013 at 12:49
  • 1
    And 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. Commented Jan 12, 2013 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. Commented Jan 12, 2013 at 13:56

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.