Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise SyntaxError('hello', 'abcdef') crashes the new Python 3.13 REPL #128894

Open
PierreQuentel opened this issue Jan 15, 2025 · 2 comments
Open
Assignees
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@PierreQuentel
Copy link
Contributor

PierreQuentel commented Jan 15, 2025

Bug report

Bug description:

In we enter this in the new interactive interpreter in Python 3.13

raise SyntaxError('hello', 'abcdef')

the interpreter window closes without any error message.

This happens because the second argument of SyntaxError is interpreted as an iterator that produces (filename, lineno, offset, text, end_lineno, end_offset). The constructor doesn't control if offset is an integer; in the example above it is set to the character 'c'.

When an exception is caught by the REPL it is handled by the traceback module. This line

if self.text and offset > len(self.text):

compares the offset and an integer. This raises an exception which is not caught and makes the program crash.

There are 2 solutions to this issue:

  • add more controls on the second argument to SyntaxError to make sure that the types are those expected (str for filename, int for lineno, etc.)
  • test the type of SyntaxError instances arguments in traceback.py

This issue doesn't happen with previous versions of the REPL

Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> raise SyntaxError('hello', 'abcdef')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SyntaxError: hello (a)
>>>

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

@PierreQuentel PierreQuentel added the type-bug An unexpected behavior, bug, or error label Jan 15, 2025
@brettcannon brettcannon changed the title "raise SyntaxError('hello', 'abcdef')" crashes the new Python 3.13 REPL raise SyntaxError('hello', 'abcdef') crashes the new Python 3.13 REPL Jan 15, 2025
@brettcannon brettcannon added the topic-repl Related to the interactive shell label Jan 15, 2025
@ZeroIntensity ZeroIntensity added 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Jan 16, 2025
@sobolevn
Copy link
Member

New REPL is not directly the cause of this problem. It also happens with an old REPL:

» PYTHON_BASIC_REPL=1 ./python.exe
Python 3.14.0a4+ (heads/main:211f41316b7, Jan 16 2025, 23:07:30) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> raise SyntaxError('hello', 'abcdef')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    raise SyntaxError('hello', 'abcdef')
Exception ignored in the internal traceback machinery:
Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 139, in _print_exception_bltin
    return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 130, in print_exception
    te.print(file=file, chain=chain, colorize=colorize)
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 1454, in print
    for line in self.format(chain=chain, colorize=colorize):
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 1391, in format
    yield from _ctx.emit(exc.format_exception_only(colorize=colorize))
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 985, in emit
    for text in text_gen:
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 1248, in format_exception_only
    yield from [indent + l for l in self._format_syntax_error(stype, colorize=colorize)]
  File "/Users/sobolev/Desktop/cpython2/Lib/traceback.py", line 1302, in _format_syntax_error
    if self.text and offset > len(self.text):
TypeError: '>' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "<stdin>-0", line 1, in <module>
  File "a", line 0
SyntaxError: hello (a)

This is related to e733136

Bisected to that commit. Commits before that - work correctly.

I am working on the fix 👍

CC @pablogsal as the commit author

@sobolevn sobolevn self-assigned this Jan 16, 2025
@sobolevn
Copy link
Member

diff --git Lib/traceback.py Lib/traceback.py
index 6367c00e4d4..4f945630c8b 100644
--- Lib/traceback.py
+++ Lib/traceback.py
@@ -1283,7 +1283,7 @@ def _format_syntax_error(self, stype, **kwargs):
             filename_suffix = ' ({})'.format(self.filename)
 
         text = self.text
-        if text is not None:
+        if isinstance(text, str):
             # text  = "   foo\n"
             # rtext = "   foo"
             # ltext =    "foo"
@@ -1292,10 +1292,17 @@ def _format_syntax_error(self, stype, **kwargs):
             spaces = len(rtext) - len(ltext)
             if self.offset is None:
                 yield '    {}\n'.format(ltext)
-            else:
+            elif isinstance(self.offset, int):
                 offset = self.offset
                 if self.lineno == self.end_lineno:
-                    end_offset = self.end_offset if self.end_offset not in {None, 0} else offset
+                    end_offset = (
+                        self.end_offset
+                        if (
+                            isinstance(self.end_offset, int)
+                            and self.end_offset != 0
+                        )
+                        else offset
+                    )
                 else:
                     end_offset = len(rtext) + 1
 

@pablogsal what do you think about this solution?

All tests pass, including a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants