Traceback leaks global code when `exec`:ed code raises
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
Bug report
Bug description:
Details
The traceback module's various functions for inspecting an exception leaks surrounding code when used to inspect an exception originating from an exec or eval call. I discovered this using traceback.format_exception but it applies to traceback.print_exc and the other ones that call or are called by those as well.
Specifically when compiled for "exec" mode using compile, and the exec:ed code inherits the context from which exec was called.
In this example, the comment on the first line is included in the printed traceback.
# Hi, I'm in the error message
import traceback
try:
exec(compile("tuple()[0]", "s", "exec"))
except:
traceback.print_exc()
This prints:
Traceback (most recent call last):
File "<filename>", line 4, in <module>
exec(compile("tuple()[0]", "s", "exec"))
File "s", line 1, in <module>
# Hi, I'm in the error message
IndexError: tuple index out of range
It seems that whatever line number the error is on in the exec:ed code is applied to the calling file instead of the exec:ed code.
Changing line 4 in the example above to exec("tuple()[0]") avoids the issue, and no info regarding the line is printed by traceback. This behavior is in line with what python outputs when the exception is not caught:
# Hi, I'm not in the error message
exec(compile("tuple()[0]", "s", "exec"))
results in:
Traceback (most recent call last):
File "<filename>", line 2, in <module>
exec(compile("tuple()[0]", "s", "exec"))
File "s", line 1, in <module>
IndexError: tuple index out of range
Summary
Example code
# Hi, I'm in the error message
import traceback
try:
exec(compile("tuple()[0]", "s", "exec"))
except:
traceback.print_exc()
Expected behaviour
Should print the following:
Traceback (most recent call last):
File "<filename>", line 4, in <module>
exec(compile("tuple()[0]", "s", "exec"))
File "s", line 1, in <module>
IndexError: tuple index out of range
Actual behaviour
Prints this:
Traceback (most recent call last):
File "<filename>", line 4, in <module>
exec(compile("tuple()[0]", "s", "exec"))
File "s", line 1, in <module>
# Hi, I'm in the error message
IndexError: tuple index out of range
CPython versions tested on:
3.8, 3.10, 3.11, 3.12
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-122126
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、traceback.format_exception と traceback.print_exc を使って例を再現し、コンパイルされた exec コードと直接の exec、および捕捉されない例外を比較します。traceback の表示が exec フレームに対してファイル名と行番号をどのように使用するかを追跡します。期待される出力で呼び出し元のソース行が省略されれば完了です。説明されている他の traceback 関数についても同様です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 25/100