Open
Description
$ python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ast
>>> import codegen
>>> a = ast.parse('import stuf')
>>> print ast.dump(a)
Module(body=[Import(names=[alias(name='stuf', asname=None)])])
>>> print codegen.to_source(a)
import stuf
>>> # so far, so good!
...
>>> b = ast.parse('from stuf import orderedstuf')
>>> print ast.dump(b)
Module(body=[ImportFrom(module='stuf', names=[alias(name='orderedstuf', asname=None)], level=0)])
>>> print codegen.to_source(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/codegen.py", line 70, in to_source
return ''.join(generator.result)
TypeError: sequence item 2: expected string, alias found
>>> # uh... something broke
...
>>>
In looking this over, codegen is based on a 2008 code release. I have not explored them yet, but have found several other forks / descendants of the same code: this refactoring and then an evolved derivative of that: astor. At least one, astor
successfully handles this from x import y
construction.
Metadata
Metadata
Assignees
Labels
No labels