Skip to content

fix: avoid uncaught TokenError when computing node positions#3075

Open
Pierre-Sassoulas wants to merge 2 commits into
mainfrom
fix-crash-2527
Open

fix: avoid uncaught TokenError when computing node positions#3075
Pierre-Sassoulas wants to merge 2 commits into
mainfrom
fix-crash-2527

Conversation

@Pierre-Sassoulas
Copy link
Copy Markdown
Member

Type of Changes

Type
🐛 Bug fix

Description

RebuildVisitor._get_position_info tokenizes a slice of the source to locate the keyword/name span of a class or function definition. On Python < 3.12, tokenize.generate_tokens can raise TokenError for malformed input (e.g. an unterminated bracket), which escaped as an uncaught non-AstroidError exception during the build.

Position computation now catches TokenError and treats such a node as having no position information, instead of crashing.

Closes #2527

``RebuildVisitor._get_position_info`` tokenizes a slice of the source to
locate the keyword/name span of a class or function. On Python < 3.12
``tokenize.generate_tokens`` can raise ``TokenError`` for malformed input
(e.g. an unterminated bracket), which escaped as an uncaught non-Astroid
exception. Such a node now simply has no position information.

Closes #2527
@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.54%. Comparing base (649ec7d) to head (911bc08).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3075      +/-   ##
==========================================
+ Coverage   93.52%   93.54%   +0.01%     
==========================================
  Files          92       92              
  Lines       11329    11332       +3     
==========================================
+ Hits        10596    10600       +4     
+ Misses        733      732       -1     
Flag Coverage Δ
linux 93.40% <100.00%> (+0.01%) ⬆️
pypy 93.54% <100.00%> (+0.01%) ⬆️
windows 93.51% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
astroid/rebuilder.py 98.83% <100.00%> (+0.20%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Pierre-Sassoulas Pierre-Sassoulas added this to the 4.2.0 milestone May 20, 2026
Adds a test for a class whose name needs NFKC normalization (the ``fi``
ligature): ``node.name`` differs from the raw token spelling, so the name
token is never matched and ``_get_position_info`` returns via its
``for``/``else`` branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uncaught TokenError while fuzzing

1 participant