diff --git a/Lib/warnings.py b/Lib/warnings.py index 391a501f7282eb..fc8c0128e3a1eb 100644 --- a/Lib/warnings.py +++ b/Lib/warnings.py @@ -36,7 +36,9 @@ def _formatwarnmsg_impl(msg): category = msg.category.__name__ s = f"{msg.filename}:{msg.lineno}: {category}: {msg.message}\n" - if msg.line is None: + # "sys" is a made up file name when we are not able to get the frame + # so do not try to get the source line + if msg.line is None and msg.filename != "sys": try: import linecache line = linecache.getline(msg.filename, msg.lineno) diff --git a/Misc/NEWS.d/next/Library/2024-04-17-23-38-06.gh-issue-117535.4Fgjlq.rst b/Misc/NEWS.d/next/Library/2024-04-17-23-38-06.gh-issue-117535.4Fgjlq.rst new file mode 100644 index 00000000000000..2e664c70baa28a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-04-17-23-38-06.gh-issue-117535.4Fgjlq.rst @@ -0,0 +1 @@ +Do not try to get the source line for made up file name "sys" in :mod:`warnings`.