gak / pycallgraph

pycallgraph is a Python module that creates call graphs for Python programs.
GNU General Public License v2.0
1.83k stars 335 forks source link

Doesn't work with Django's SimpleLazyObject #92

Closed valtron closed 11 years ago

valtron commented 12 years ago

I'm using pycallgraph 0.5.1, Django 1.3.1, Python 2.7.2 32bit.

Line 235 of pycallgraph.py, which gets the "class" of "self" in the current call being traced, causes an infinite loop. The method being traced is SimpleLazyObject's "init", which among other things sets "self._wrapped". ".class" triggers the "get_class" method on the object, which checks if "_wrapped" is None, but since the constructor hasn't run yet (it was intercepted by the tracer), this gets translated into a "getattr__" call on the object, which again checks if "_wrapped" is None... etc.

Here is the relevant part the stack trace. I used custom middleware to integrate pycallgraph in Django.

C:\Python27\lib\site-packages\django\contrib\auth\context_processors.py in auth   57. 'user': SimpleLazyObject(get_user), C:\Python27\lib\site-packages\django\utils\functional.py in init   316. def init(self, func): C:\Python27\lib\site-packages\pycallgraph.py in tracer   235. class_name = frame.f_locals['self'].class.name C:\Python27\lib\site-packages\django\utils\functional.py in get_class   354. if self._wrapped is None: self._setup() C:\Python27\lib\site-packages\django\utils\functional.py in getattr   275. if self.wrapped is None: ... (this is the infinite loop) ..._ C:\Python27\lib\site-packages\django\utils\functional.py in getattr__   275. if self._wrapped is None:

blaze33 commented 11 years ago

Same issue on django 1.4.2 For anyone interested in using pycallgraph to profile a django app, I did some debugging and solved this. Add the following:

_wrapped = None

there: https://github.com/django/django/blob/1.4.2/django/utils/functional.py#L195

I'll make a pull request upstream once I get some time to write tests, explain this, etc.

blaze33 commented 11 years ago

Bug report there: https://code.djangoproject.com/ticket/19456

aaugustin commented 11 years ago

This bug will be fixed in Django 1.6.

gak commented 11 years ago

OK great. Closing.