Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to get started tracing Python features and behavior through the source code

I want to be able to investigate and trace through the Python source code to see how things work under the hood, and to resolve doubts about features that are not explicitly documented in the reference document.

I would like to have some starting information on how to go about this, because it's pretty overwhelming. I can read C, so I have that going on for me. Besides that, the task seems a little intimidating without at least a slight amount of guidance.

For example, let's say I wanted to document how attribute reference is implemented in Python. How would I go about tracing what happens when an attribute reference expression is present in a Python program?

Perhaps an overview of how the source code is organized and what each part does would be helpful, along with some "walk=thru" examples, such as the "attribute reference" case.

I searched for information on this but there doesn't seem to be much.

like image 237
Jeff Doering Avatar asked Oct 24 '12 16:10

Jeff Doering


2 Answers

Perhaps a good Python debugger would help? I would try using the PyDev plugin for Eclipse. That would at least help you trace what special python methods (such as __getattr__() or __setattr__()) are called when you reference an attribute of a class. If you need to go deeper, you can look at the Python C API or even the Python C Source Code.

like image 194
Josh Avatar answered Nov 05 '22 11:11

Josh


There is a trace module in the python standard library. It has several modes, and can be used to print every line of python code as it is executed like this:

python -m trace -t myscript.py

See http://docs.python.org/library/trace.html

like image 23
Dave Kirby Avatar answered Nov 05 '22 11:11

Dave Kirby