Confusion about scan_code in (in python 2.6)

Gabriel Genellina gagsl-py2 at
Wed Jan 13 09:52:21 CET 2010

En Tue, 12 Jan 2010 23:15:17 -0300, Brock Pytlik <bpytlik at>  

> I've been working with the code recently and I've come  
> across a bit of code I'm not grasping. In the scan_code function, there  
> are the following lines:
>                 if sys.version_info >= (2, 5):
>                         scanner = self.scan_opcodes_25
>                 else:
>                         scanner = self.scan_opcodes
> I don't understand their purpose. Why would I be running a version of  
> python less than 2.6 and using the 2.6 module? Should this be looking at  
> the version of python that was used to compile 'co'?

According to the comment at the top of, for some reason  
(unknown to me) the module must remain compatible with Python 2.2. So the  
code from the 2.6 version should work fine when run under Python 2.2 (and  
indeed it does).

> In my use, I'm subclassing the modulefinder class and adapting the  
> scan_code function to do what I need, but I've found I need separate  
> classes for (2.4, 2.5) and 2.6 because when running 2.4 or 2.5, the  
> above code traces back. (In 2.4 because self.scan_opcodes isn't defined,  
> in 2.5 because self.scan_opcodes_25 isn't.)

You could use a private copy of the last released version of modulefinder.

> In the end, I've gotten everything working to my satisfaction, but I'm  
> curious about how this module (and those lines in particular) were  
> designed to be used. Any insight would be appreciated.

The bit I don't know is *why* modulefinder must remain 2.2 compatible;  
PEP291 says nothing. Maybe it is required by distutils.

Gabriel Genellina

More information about the Python-list mailing list