python / python/cpython

Incorrect SyntaxError attributes when raised by `compile`

オープン
#127,927 コメント 11 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

This is probably a problem with compile, and I will demonstrate it through ast.parse. Consider the following code in a file f.py.

import ast

code = "x y"
ast.parse(code, filename='f.py')

This correctly raises a SyntaxError with lineno = 1 and offset = 3 since the error is at the beginning of y in the code. However, the traceback points into the file f.py itself, instead of the string code. Notice that it points to the third column of the first line of f.py.

  File "f.py", line 1
    import ast
      ^
SyntaxError: invalid syntax

This does not happen for any other string given as filename to ast.parse. In general, the traceback seems to point with the "correct" lineno and offset to the code in the file itself instead of code given to ast.parse. However, the problem is slightly more nuanced. If the offset in the string given to ast.parse is more than the length of the corresponding line in f.py, the offset attribute of the SyntaxError raised is incorrectly set to the length of the corresponding line in f.py plus two. Consider the following code in f.py.

import ast

code = "                                     x y"
ast.parse(code, filename='f.py')

This raises the following exception.

  File "f.py", line 1
    import ast
              ^
IndentationError: unexpected indent

On closer inspection, it turns out that the offset of the exception raised is 12 (note that the length of "import ast" is 10) instead of the correct 37 which is observed when filename is set to anything other than f.py. However, something more interesting happens when the number of lines in the code string is more than the number of lines in the file f.py.

import ast

code = "\n\n\n\nx y"
ast.parse(code, filename='f.py')

The problem goes away!

  File "f.py", line 5
    x y
      ^
SyntaxError: invalid syntax

We even see the correct offset.

CPython versions tested on:

3.11, 3.12, 3.13

Operating systems tested on:

Linux

(Edit) Incorrect text attribute

There is something more going on here. The following issue can be seen in 3.11 and 3.12 but not in 3.13. Consider the following f.py.

import ast

with open('temp.py') as f:
    code = f.read()
ast.parse(code, filename='temp.py')

temp.py is as follows.

"""                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             """; x """                                                                                                                                                         """; foobar

This leads to the following traceback.

  File "temp.py", line 1
    foobar
          ^
SyntaxError: invalid syntax

Besides the fact that the offset is incorrectly set to 8 (instead of 840), the text is incorrectly set to foobar (on 3.13, the entire file contents of temp.py are present in text). This problem goes away if filename is set to anything else. The problem also goes away if the contents of temp.py are shorter!

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、ast.parse と compile を使用して例を再現し、filename がソースファイルと一致する場合と一致しない場合で SyntaxError 属性を比較します。一致するファイル名に対して traceback、offset、text がどのように設定されるかを追跡します。示されたケース全体で lineno、offset、text、traceback が解析された文字列を一貫して記述し、例に対するリグレッションカバレッジがあることを完了条件とします。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。