Skip to content

Surface SyntaxError from input transformation instead of crashing the…#293

Open
JimWallace wants to merge 1 commit into
jupyterlite:mainfrom
JimWallace:fix-transform-syntaxerror
Open

Surface SyntaxError from input transformation instead of crashing the…#293
JimWallace wants to merge 1 commit into
jupyterlite:mainfrom
JimWallace:fix-transform-syntaxerror

Conversation

@JimWallace

Copy link
Copy Markdown

… kernel

PyodideKernel.run() called transform_cell() outside any try/except, so a SyntaxError raised during input transformation — most commonly TabError on Python 3.12+, which the tokenizer raises eagerly — escaped run() unhandled and surfaced as a raw kernel traceback instead of a normal error cell.

Guard the transform like InteractiveShell.run_cell does and route failures through showtraceback() (which dispatches SyntaxError to showsyntaxerror), reusing the existing _last_traceback reporting.

https://claude.ai/code/session_017qs8Cz1XgFG4oBPqNPqLom

… kernel

PyodideKernel.run() called transform_cell() outside any try/except, so a
SyntaxError raised during input transformation — most commonly TabError on
Python 3.12+, which the tokenizer raises eagerly — escaped run() unhandled and
surfaced as a raw kernel traceback instead of a normal error cell.

Guard the transform like InteractiveShell.run_cell does and route failures
through showtraceback() (which dispatches SyntaxError to showsyntaxerror),
reusing the existing _last_traceback reporting.

https://claude.ai/code/session_017qs8Cz1XgFG4oBPqNPqLom
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

lite-badge 👈 Try it on ReadTheDocs

@JimWallace

Copy link
Copy Markdown
Author

Closes #292

Summary

On Python 3.12+, a SyntaxError that CPython's tokenizer raises during tokenization — most commonly TabError: inconsistent use of tabs and spaces in indentation — escaped PyodideKernel.run() unhandled, so the user saw a raw internal traceback through pyodide_kernel/litetransform.pyIPython/core/inputtransformer2.py:make_tokens_by_line instead of a normal error cell.

Root cause

PyodideKernel.run() called the input transforms outside any try/except. On Python 3.12+ the C tokenizer raises TabError/IndentationError (a SyntaxError subclass) eagerly during tokenization (python/cpython#105238). IPython's generate_tokens_catch_errors only does except tokenize.TokenError, so it cannot catch a SyntaxError; both make_tokens_by_line and InteractiveShell.transform_cell propagate it. IPython's own InteractiveShell.run_cell guards transform_cell in a try/except; run() reimplemented the cell loop and omitted that guard. Not fixable by upgrading IPython (confirmed against 9.14.0).

Fix

Wrap the two transform_cell calls in a try/except that routes failures through showtraceback() (which dispatches SyntaxErrorshowsyntaxerror), nesting the existing _load_packages_from_imports block under else:. Reuses the method's existing _last_tracebackresults["status"] = "error" reporting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants