[Pythonmac-SIG] Spotlight and Python

Bob Ippolito bob at redivi.com
Wed May 11 04:02:02 CEST 2005


On May 10, 2005, at 9:55 PM, David Reed wrote:

>
> On May 10, 2005, at 8:52 PM, Jonathan Wight wrote:
>
>
>> I've made a first pass at it and have a Spotlight importer that
>> calls a built-in Python function to import a file's metadata.
>>
>> I started to look at module inspect to find out how to extract
>> information from a Python module but then realised that I'd need to
>> import the file the importer is analysing. This would mean it will
>> be executing arbitrary code inside that file. That's got to be a
>> bad thing for security reasons.
>>
>> So instead I'm just going to have to use string processing to scan
>> the file instead. Are there any modules out there for extracting
>> information from Python script files?
>
> It's been a while since I looked at the inspect module - I didn't
> remember it actually executed code. I'm not aware of any code that
> processes a python file w/o executing it (I do remember that being a
> complaint about pychecker). You could rip out code from the Python
> interpreter, but I could probably write a C or C++ function that read
> a file and returned the names of the classes and functions/methods in
> less time than I could find and decipher the Python interpreter code
> if that would help you. Would you pass it a filename or a file handle
> and how would you want the data back (an array of strings or one long
> string separated by spaces, etc.). Does it matter if it's C or could
> it use C++ features (the C++ string class might be useful). I would
> just look for "def" or "class" and grab the next word up to a "(" or
> ":".

The compiler and parser modules are both standard library bits that  
parse the AST of python source without executing it.  The stuff in  
parser is fast and weird (since it's effectively a Python interface  
to the internal parser), and the stuff in compiler is (very) slow and  
less weird.

-bob



More information about the Pythonmac-SIG mailing list