GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-153569: Add tokenizer source and cursor primitives by pablogsal · Pull Request #153587 · python/cpython

Conversation

@pablogsal

@pablogsal pablogsal commented Jul 11, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

  • Issue: Base the tokenizer API on source offsets #153569

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Jul 11, 2026
Base the tokenizer API on source offsets #153569
Open
@pablogsal
pablogsal marked this pull request as ready for review July 11, 2026 19:26
@pablogsal
pablogsal requested review from a team, AA-Turner, emmatyping, ericsnowcurrently, erlend-aasland, itamaro and lysnikolaou as code owners July 11, 2026 19:26
@bedevere-app bedevere-app Bot added the awaiting core review label Jul 11, 2026
@pablogsal
pablogsal marked this pull request as draft July 11, 2026 19:27
@bedevere-app bedevere-app Bot removed the awaiting core review label Jul 11, 2026
@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-api branch from bce3caa to 2682fe5 Compare July 11, 2026 19:38
@pablogsal pablogsal changed the title gh-153569: Base the tokenizer API on source offsets gh-153569: Add tokenizer source and cursor primitives Jul 11, 2026
@pablogsal pablogsal added the skip news label Jul 11, 2026
@pablogsal
pablogsal marked this pull request as ready for review July 11, 2026 20:37
@bedevere-app bedevere-app Bot added the awaiting core review label Jul 11, 2026
@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-api branch from 2682fe5 to 1bf43aa Compare July 11, 2026 21:15
maurycy
maurycy reviewed Jul 11, 2026
View reviewed changes
Comment thread Parser/tokenizer/source.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal enabled auto-merge (squash) July 18, 2026 12:55
@pablogsal
pablogsal disabled auto-merge July 18, 2026 12:55
@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-api branch from b19b995 to 0dea29d Compare July 18, 2026 13:56
@pablogsal

pablogsal commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

@copilot resolve the merge conflicts in this pull request

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pablogsal added 3 commits August 26, 2026 00:53
@pablogsal
pythongh-153569: Add tokenizer source and cursor primitives
697e7dd
@pablogsal
fixup! pythongh-153569: Add tokenizer source and cursor primitives
100072c
@pablogsal
fixup! pythongh-153569: Add tokenizer source and cursor primitives
4b0454f
@pablogsal
pablogsal force-pushed the gh-153569-tokenizer-offset-api branch from 0dea29d to 4b0454f Compare August 25, 2026 23:53
@pablogsal
pablogsal merged commit f54fd2a into python:main Aug 26, 2026
110 of 113 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@pablogsal
pablogsal deleted the gh-153569-tokenizer-offset-api branch August 26, 2026 11:38
@bedevere-app bedevere-app Bot removed the awaiting core review label Aug 26, 2026
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Aug 27, 2026
@pablogsal @clin1234
pythongh-153569: Add tokenizer source and cursor primitives (python#1…
ca50f8e
…53587)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner
@emmatyping emmatyping Awaiting requested review from emmatyping emmatyping is a code owner
@itamaro itamaro Awaiting requested review from itamaro itamaro is a code owner
1 more reviewer
@maurycy maurycy maurycy left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

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.

2 participants

@pablogsal @maurycy
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.