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
__doc__and__name__attributes for that. See docs.python.org/2/library/inspect.html#types-and-members as well.