[Python-Dev] maximum recursion depth exceeded in lib\pstats.py

Adam Bielański ab at rdprojekt.pl
Thu Oct 21 13:20:12 CEST 2010


  Hi all,

There's a bug in Python 2.6, module lib\pstats.py, line 150. I'm pretty sure, 
however that it also exists in other versions, as I don't think that profiler 
differs very much between them.

Let me paste a little piece of surrounding code:

     class Stats:
       (....)
         def add(self, *arg_list):
             if not arg_list: return self
             if len(arg_list) > 1: self.add(*arg_list[1:])  #Line 150, the 
problem is right here!
             other = arg_list[0]
             (... do some processing with first item from arg_list ...)

This is the code for adding profiling stats from multiple files (names are on 
arg_list) so that they can be displayed in cumulated and readable form.

As you can see it chops off the first item from the list and then uses recursion 
to process the rest of it. It's no wonder then that when you profiled a little 
too many function calls, you're bound to see RuntimeError with 'maximum 
recursion depth exceeded' message when you try using this. IMO this should be 
done with iteration instead, like:

         def add(self, *arg_list):
             if not arg_list: return self
             for other in arg_list: #Preserved variable name only for sake of 
comparison with previous snippet
                 (... do some processing with each item from arg_list, merging 
results in some rightful manner ...)


Kind regards,
     Adam Bielanski.


More information about the Python-Dev mailing list