问题
I am using python 3.4.3 and I am working with the python abstract syntax tree(ast) module. I looked at the source of the ast.py module and I found out that all the ast node classes are implemented in the _ast module, which the ast.py module imports.
from _ast import *
Now, I wanted to look at the elements of the ast module, so I got the name of all the classes implemented in this module :
import _ast
import inspect
for element in inspect.getmembers(_ast) :
print(element)
I got the names of all elements of this module :
('AST', <class '_ast.AST'>)
('Add', <class '_ast.Add'>)
('And', <class '_ast.And'>)
('Assert', <class '_ast.Assert'>)
('Assign', <class '_ast.Assign'>)
('Attribute', <class '_ast.Attribute'>)
('AugAssign', <class '_ast.AugAssign'>)
('AugLoad', <class '_ast.AugLoad'>)
('AugStore', <class '_ast.AugStore'>)
('BinOp', <class '_ast.BinOp'>)
('BitAnd', <class '_ast.BitAnd'>)
('BitOr', <class '_ast.BitOr'>)
('BitXor', <class '_ast.BitXor'>)
('BoolOp', <class '_ast.BoolOp'>)
('Break', <class '_ast.Break'>)
('Bytes', <class '_ast.Bytes'>)
('Call', <class '_ast.Call'>)
('ClassDef', <class '_ast.ClassDef'>)
('Compare', <class '_ast.Compare'>)
('Continue', <class '_ast.Continue'>)
('Del', <class '_ast.Del'>)
('Delete', <class '_ast.Delete'>)
('Dict', <class '_ast.Dict'>)
('DictComp', <class '_ast.DictComp'>)
('Div', <class '_ast.Div'>)
('Ellipsis', <class '_ast.Ellipsis'>)
('Eq', <class '_ast.Eq'>)
('ExceptHandler', <class '_ast.ExceptHandler'>)
('Expr', <class '_ast.Expr'>)
('Expression', <class '_ast.Expression'>)
('ExtSlice', <class '_ast.ExtSlice'>)
('FloorDiv', <class '_ast.FloorDiv'>)
('For', <class '_ast.For'>)
('FunctionDef', <class '_ast.FunctionDef'>)
('GeneratorExp', <class '_ast.GeneratorExp'>)
('Global', <class '_ast.Global'>)
('Gt', <class '_ast.Gt'>)
('GtE', <class '_ast.GtE'>)
('If', <class '_ast.If'>)
('IfExp', <class '_ast.IfExp'>)
('Import', <class '_ast.Import'>)
('ImportFrom', <class '_ast.ImportFrom'>)
('In', <class '_ast.In'>)
('Index', <class '_ast.Index'>)
('Interactive', <class '_ast.Interactive'>)
('Invert', <class '_ast.Invert'>)
('Is', <class '_ast.Is'>)
('IsNot', <class '_ast.IsNot'>)
('LShift', <class '_ast.LShift'>)
('Lambda', <class '_ast.Lambda'>)
('List', <class '_ast.List'>)
('ListComp', <class '_ast.ListComp'>)
('Load', <class '_ast.Load'>)
('Lt', <class '_ast.Lt'>)
('LtE', <class '_ast.LtE'>)
('Mod', <class '_ast.Mod'>)
('Module', <class '_ast.Module'>)
('Mult', <class '_ast.Mult'>)
('Name', <class '_ast.Name'>)
('NameConstant', <class '_ast.NameConstant'>)
('Nonlocal', <class '_ast.Nonlocal'>)
('Not', <class '_ast.Not'>)
('NotEq', <class '_ast.NotEq'>)
('NotIn', <class '_ast.NotIn'>)
('Num', <class '_ast.Num'>)
('Or', <class '_ast.Or'>)
('Param', <class '_ast.Param'>)
('Pass', <class '_ast.Pass'>)
('Pow', <class '_ast.Pow'>)
('PyCF_ONLY_AST', 1024)
('RShift', <class '_ast.RShift'>)
('Raise', <class '_ast.Raise'>)
('Return', <class '_ast.Return'>)
('Set', <class '_ast.Set'>)
('SetComp', <class '_ast.SetComp'>)
('Slice', <class '_ast.Slice'>)
('Starred', <class '_ast.Starred'>)
('Store', <class '_ast.Store'>)
('Str', <class '_ast.Str'>)
('Sub', <class '_ast.Sub'>)
('Subscript', <class '_ast.Subscript'>)
('Suite', <class '_ast.Suite'>)
('Try', <class '_ast.Try'>)
('Tuple', <class '_ast.Tuple'>)
('UAdd', <class '_ast.UAdd'>)
('USub', <class '_ast.USub'>)
('UnaryOp', <class '_ast.UnaryOp'>)
('While', <class '_ast.While'>)
('With', <class '_ast.With'>)
('Yield', <class '_ast.Yield'>)
('YieldFrom', <class '_ast.YieldFrom'>)
('__doc__', None)
('__loader__', <class '_frozen_importlib.BuiltinImporter'>)
('__name__', '_ast')
('__package__', '')
('__spec__', ModuleSpec(name='_ast', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in'))
('alias', <class '_ast.alias'>)
('arg', <class '_ast.arg'>)
('arguments', <class '_ast.arguments'>)
('boolop', <class '_ast.boolop'>)
('cmpop', <class '_ast.cmpop'>)
('comprehension', <class '_ast.comprehension'>)
('excepthandler', <class '_ast.excepthandler'>)
('expr', <class '_ast.expr'>)
('expr_context', <class '_ast.expr_context'>)
('keyword', <class '_ast.keyword'>)
('mod', <class '_ast.mod'>)
('operator', <class '_ast.operator'>)
('slice', <class '_ast.slice'>)
('stmt', <class '_ast.stmt'>)
('unaryop', <class '_ast.unaryop'>)
('withitem', <class '_ast.withitem'>)
Now, I want to look at how each of these classes is implemented in the _ast module. But when I try to get the source file using the inspect.getsource method, I get an error :
>>inspect.getsource(_ast)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
inspect.getsource(_ast)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 836, in getsource
lines, lnum = getsourcelines(object)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 825, in getsourcelines
lines, lnum = findsource(object)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 655, in findsource
file = getsourcefile(object)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 571, in getsourcefile
filename = getfile(object)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/inspect.py", line 518, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '_ast' (built-in)> is a built-in module
why do i get this error. And how do i get the source of all these classes ?
Thanks in advance
回答1:
You cannot do that (not for _ast
module , which is a built-in
module). As clearly stated in the documentations -
inspect.getsourcefile(object)
Return the name of the Python source file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.
(Emphasis mine)
And inspect.getsource()
internally uses inspect.getsourcefile()
, as evident from the Traceback.
Since _ast
is a built-in module, Example -
>>> import _ast
>>> _ast
<module '_ast' (built-in)>
You cannot get its source through that method. If you really want to take a look at the source code, the source code for Python is open source and available here . I think the module you are trying to get source for is written completely in C
.
来源:https://stackoverflow.com/questions/32218509/find-out-elements-of-ast-module-which-is-imported-in-ast-py