5

ドキュメントには次のように書かれています。

ast.parse(source, filename='<unknown>', mode='exec')

    Equivalent to compile(source, filename, mode, ast.PyCF_ONLY_AST).


compile(source, filename, mode[, flags[, dont_inherit]])

    The filename argument should give the file from which the code was read;
    pass some recognizable value if it wasn’t read from a file
    ('<string>' is commonly used).

しかし、このファイル名を AST ノードから取得する方法はあまりわかりません。または、このファイル名パラメーターはどのように使用されますか。スタブだけですか?

4

1 に答える 1

3

コード オブジェクトに属性を設定しco_filenameます。これは、トレースバックでファイル名を表示するために使用されます。それに加えて、渡す値はそれほど重要ではありません。

>>> c = compile('raise Exception("spam")', 'eggs', 'exec')
>>> eval(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "eggs", line 1, in <module>
Exception: spam
于 2013-07-10T13:27:20.297 に答える