2

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
6
  • Functions have __doc__ and __name__ attributes for that. See docs.python.org/2/library/inspect.html#types-and-members as well. Commented Jun 2, 2014 at 21:05
  • @MartijnPieters: The function objects don't exist unless you run the code, though, and running the code is something we want to avoid. Commented Jun 2, 2014 at 21:07
  • 1
    @user2357112: For C extensions, you have no other option. Komodo's CodeIntel used separate datafiles generated from a module to provide it with autocompletion metadata for C extensions, for example. Commented Jun 2, 2014 at 21:09
  • 1
    @user2357112: see community.activestate.com/faq/codeintel-cix-schema and community.activestate.com/faq/generate-python-api-catalog for inspiration. Commented Jun 2, 2014 at 21:10
  • @MartijnPieters: if you want to turn your second comment into an answer, I'll accept it as the correct answer. Commented Jun 4, 2014 at 23:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.