GeezerVison is showing github.com — gh-153569: Add tokenizer source and cursor primitives by pablogsal · Pull Request #153587 · python/cpython
Conversation
pablogsal
commented
Jul 11, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
This adds the decoded source store, offset spans, and an offset-only cursor as the foundation for moving tokenizer state away from buffer pointers.
For example:
// Own decoded bytes by line.
_PyTok_SourceText source;
_PyTok_SourceInit(&source);
static const char text[] = "answer = 42\n";
_PyTok_Off line_start = _PyTok_SourceAppendLine(
&source, text, sizeof(text) - 1, 0);
assert(line_start >= 0);
// Navigate by source offset.
_PyTok_Cursor cursor;
_PyTok_CursorInit(&cursor, &source);
assert(_PyTok_CursorSetOffset(&cursor, line_start) == 0);
_PyTok_Off name_start = cursor.pos;
while (_PyTok_CursorPeek(&cursor, 0) != ' ') {
assert(_PyTok_CursorAdvance(&cursor) != EOF);
}
// Keep token text as a half-open span.
_PyTok_Span name = _PyTok_SpanFromBounds(name_start, cursor.pos);
// Borrow a temporary view.
Py_ssize_t name_len;
const char *name_text = _PyTok_SourceSpanView(&source, name, &name_len);
assert(name_text != NULL);
assert(name_len == 6 && memcmp(name_text, "answer", name_len) == 0);
_PyTok_SourceClear(&source);
This PR only adds these primitives and their direct tests. It does not migrate the existing readers, lexer and f-string state, pegen, or _tokenize; those changes can be built on this API in separate PRs.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
pablogsal
changed the title
gh-153569: Base the tokenizer API on source offsets
gh-153569: Add tokenizer source and cursor primitives
Jul 11, 2026
Uh oh!
There was an error while loading. Please reload this page.
|
@copilot resolve the merge conflicts in this pull request
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|