Jake
Jake

Reputation: 23

How to get function names from compiled Python module without importing it?

I'm creating an intellisense type module, where you input python code and output a dictionary of function and variable names, etc. Using import would execute any top-level statements in the code so I would rather not use that. Instead I'm using the ast module. It works for .py modules but not .pyc or .so modules because ast.parse() actually compiles the code and the .so is already compiled. So is there a way to grab the function and variable names and docstrings from a compiled module without using import?

[Edited for clarity]

# re module is .py
import ast, imp
file_object, module_path, description = imp.find_module('re')
src = file_object.read()
tree = ast.parse(source=src, filename=module_path, mode='exec')
for node in tree.body:
    print node

# datetime module is .so
file_object, module_path, description = imp.find_module('datetime')
src = file_object.read()
tree = ast.parse(source=src, filename=module_path, mode='exec')
for node in tree.body:
    print node


 File "test_ast.py", line 12, in <module>
   tree = ast.parse(source=src, filename=module_path, mode='exec')
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.py", line 37, in parse
   return compile(source, filename, mode, PyCF_ONLY_AST)
TypeError: compile() expected string without null bytes

Upvotes: 2

Views: 616

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121764

For C extensions, you have no other option but to import them and use introspection.

Komodo's CodeIntel used separate datafiles generated from a module (using introspection) or otherwise written manually to provide it with autocompletion metadata for C extensions, for example. Their autocompleter then uses this static data instead.

The CodeIntel project is Open Source code; see http://community.activestate.com/faq/codeintel-cix-schema and http://community.activestate.com/faq/generate-python-api-catalog for inspiration, and / or study the source code of the toolset.

Upvotes: 1

Related Questions