找出_ast模块的元素,它在ast.py

时间:2015-08-26 05:20:18

标签: python python-3.x abstract-syntax-tree

我正在使用python 3.4.3,我正在使用python抽象语法树(ast)模块。我查看了ast.py模块的源代码,发现所有的ast节点类都是在_ast模块中实现的,ast.py模块导入了它。

from _ast import *

现在,我想查看ast模块的元素,所以我得到了这个模块中实现的所有类的名称:

import _ast
import inspect
for element in inspect.getmembers(_ast) : 
    print(element)

我得到了这个模块的所有元素的名称:

('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'>)

现在,我想看一下如何在_ast模块中实现这些类。但是当我尝试使用inspect.getsource方法获取源文件时,出现错误:

>>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

为什么我会收到此错误。我如何获得所有这些类的来源?

提前致谢

1 个答案:

答案 0 :(得分:1)

你不能这样做(不适用于_ast模块,这是built-in模块。如清楚所述in the documentations -

  

<强> inspect.getsourcefile(对象)

     

返回定义了对象的Python源文件的名称。 如果对象是内置模块,类或函数,则会因TypeError而失败。

(强调我的)

inspect.getsource()内部使用inspect.getsourcefile(),如Traceback所示。

由于_ast是内置模块,因此示例 -

>>> import _ast
>>> _ast
<module '_ast' (built-in)>

您无法通过该方法获取其来源。如果你真的想看看源代码,那么Python的源代码是开源的,可用here。我认为您尝试获取源代码的模块完全是在C中编写的。