FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implement the sentinel builtin (PEP 661) by youknowdot · Pull Request #8945 · RustPython/RustPython · GitHub

Repository navigation

Implement the sentinel builtin (PEP 661) - #8945

Merged
youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:cpython-315-sentinel
Oct 5, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
youknowdot:cpython-315-sentinel

Conversation

youknowdot commented Oct 2, 2026 •
edited by coderabbitai Bot
Loading

Copy link
Copy Markdown
Contributor

Summary

Implement the native sentinel(name, /, *, repr=None) builtin described by PEP 661, as an independent prerequisite for the Python 3.15 standard-library migration.

  • Add the immutable, non-subclassable builtin with identity/hash/truthiness semantics, custom representation, read-only __name__, writable/deletable __module__, copy/deepcopy identity, and name-based pickle reduction.
  • Support sentinel operands in unions through the existing union implementation.
  • Traverse and clear owned references so string-subclass and mutable-module reference cycles can be collected.
  • Preserve the caller function's module snapshot, including module/eval/exec contexts.
  • Add six CPython-derived tests and six focused API, module, and GC regression tests. The snippet runs unconditionally on RustPython; the CPython comparison skips versions older than 3.15 before the unavailable builtin is imported.

This branch is based directly on RustPython ada43c4f82b9df4e260f1368038c31cd55c4a61e. It contains only the native sentinel implementation and its snippet. It does not change the advertised Python version, standard-library sources, import machinery, compile/module behavior, or generated workflows.

Reference and attribution

Behavior was compared with CPython v3.15.0rc2, commit 435c9e5a798c99653e3ab64ce29baed0e4f3dfee:

The six test_sentinel* methods are copied from that CPython test source; their assertions and AST semantics are preserved. Additional edge tests cover module capture, self/mutual/repr GC cycles, string subclasses, Unicode, signature/documentation, and identity/protocol behavior.

Validation

All commands below use the independent branch and its unchanged 3.14 Lib:

  • cargo build --locked --release --features sqlite: passed
  • Native sentinel snippet: all 12 tests passed on RustPython, despite its retained 3.14 version
  • CPython v3.15.0rc2 comparison: all 12 tests passed; CPython 3.14.7 cleanly takes the explicit version guard
  • Six upstream sentinel test methods: AST-equivalent to the exact rc2 source
  • rustpython -m test test_builtin test_types test_gc test_pickle: all four modules passed; 1,341 tests run, 104 skipped
  • Source isolation assertions: version 3.14, cpython-314, and site, importlib, and test.support all loaded from this branch's Lib
  • Configured pre-commit hooks and git diff --check: passed

The full snippet aggregate (pytest -v --timeout=60, CPython 3.14.7 plus the isolated RustPython binary) reports 469 passed, 13 failed. It is not a green aggregate result:

  • 10 failures stem from sandbox-denied AF_UNIX sockets: four manager tests and three multiprocessing snippets on both interpreters
  • 2 failures are the unchanged ownership-change test expecting PermissionError where this sandbox returns EINVAL
  • 1 is the unchanged CPython GC/import stress case reaching the 60-second limit; RustPython passes that case

Both sentinel harness cases and both SQLite cases pass. The CPython 3.14 sentinel harness case validates the version guard, while the separate 3.15rc2 run executes all 12 tests. All failing test source files are unchanged.

The remaining isolated-branch Rust checks also pass:

  • Workspace Rust tests: 1,345 passed, 18 ignored
  • Separate C-API tests: 115 passed, 4 ignored
  • Workspace and separate C-API all-targets Clippy: both passed without warnings

GitHub CI, CodSpeed, and pyperformance currently require maintainer approval for this fork; they have not run. This remains a draft for review.

Limitations

This change adds the runtime builtin and union support; it does not claim complete Python 3.15 compatibility or add third-party static type-checker support. Validation is on Linux x86_64; other platforms have not been exercised here.

AI assistance

OpenAI Codex assisted with implementation, CPython comparison, tests, review, and this PR description under maintainer direction. The runtime did not expose the exact model version. Reported checks are automated Linux x86_64 verification; no additional human-use or untested-platform verification is claimed.

Summary by CodeRabbit

  • New Features
    • Added a built-in sentinel type for representing named sentinel values. Sentinels can provide a custom representation, retain their identity when copied, and participate in union operations.
    • Sentinel values are available through the built-ins module.

Add unique sentinel values with copy/pickle identity, caller module capture, union support, and cycle collection. Compare constructor behavior and six upstream tests with CPython v3.15.0rc2; retain the existing Python target and bundled libraries.

Assisted-by: Codex:model-version-unavailable

coderabbitai Bot commented Oct 2, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

🧰 Additional context used 📚 Code guidelines (1)
AGENTS.md — auto-discovered

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info ⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c40a4381-e24c-4684-8360-d5d1629881f1
📥 Commits

Reviewing files that changed from the base of the PR and between ada43c4 and 5715732.

📒 Files selected for processing (6)
  • crates/vm/src/builtins/mod.rs
  • crates/vm/src/builtins/sentinel.rs
  • crates/vm/src/builtins/union.rs
  • crates/vm/src/stdlib/builtins.rs
  • crates/vm/src/types/slot.rs
  • crates/vm/src/types/zoo.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The VM adds a sentinel type with construction, representation, copy, reduction, and union operations. It registers the type in the builtins module and recognizes sentinel instances as union operands.

Changes

Sentinel builtin

Layer / File(s) Summary
Define sentinel type
crates/vm/src/builtins/sentinel.rs, crates/vm/src/builtins/mod.rs, crates/vm/src/types/slot.rs
PySentinel stores its name, module, and optional representation. It implements construction, garbage-collection traversal, copy, reduction, representation, and union operations.
Register sentinel builtin
crates/vm/src/types/zoo.rs, crates/vm/src/stdlib/builtins.rs, crates/vm/src/builtins/union.rs
TypeZoo initializes and extends the sentinel type. The builtins module exposes the type, and union operand validation accepts sentinel instances.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Merge Risk: ⚪ Minimal · up to 57157

No actionable merge-blocking risk remains in the reviewed sentinel implementation; it is ready to merge after normal checks.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 57157

The change stays within existing Python object, union, and serialization behavior. No new privilege grant or security-boundary bypass was established. Residual uncertainty concerns native object cleanup and concurrent lifecycle behavior, which were assessed from source rather than exercised at runtime.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — New reachability is available to Python code with access to the builtin: allocating sentinels, retaining object graphs through mutable module metadata, and constructing unions. The demonstrated exposure is interpreter object state and native reference cleanup, not a new service endpoint or privileged capability.

Trust Boundaries and Controls

  • observed — Name-based sentinel reduction uses the pre-existing pickle global-resolution path. Serialization requires string module metadata and a matching object binding; unpickling imports the recorded module and resolves its name. These import semantics predate the PR and do not make untrusted pickle data safe.

Resilience and Maintainability Implications

  • observed — Sentinel traversal visits all three owned reference fields. Clear extracts those references without invoking Python callbacks. Existing GC ordering runs finalizers first, preserves transitively resurrected objects, skips clearing saved garbage, re-tracks late-resurrected objects, and defers reference release until cycle-clearing operations finish. These are source-level controls protecting externally reachable state and cleanup ordering.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 6 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: implementing the sentinel builtin described by PEP 661.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

codspeed Bot commented Oct 2, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing youknowdot:cpython-315-sentinel (5715732) with main (f46ba48)

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

fanninpm requested a review from youknowone October 2, 2026 14:17
Comment on lines +1 to +2
# Sentinel cases copied unchanged from CPython v3.15.0rc2 Lib/test/test_builtin.py.
# https://github.com/python/cpython/blob/v3.15.0rc2/Lib/test/test_builtin.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

When RustPython decides to target CPython 3.15 (rather than CPython 3.14), the file in question will be updated by a semi-automated process.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

this can be simply replaced by test_builtin. so removing will be fine

Rely on the canonical Lib/test/test_builtin.py sentinel cases when the standard library is updated, as requested in PR RustPython#8945 review. Remove the custom snippet without changing the runtime implementation or existing standard-library tests.

Assisted-by: Codex:model-version-unavailable
youknowone marked this pull request as ready for review October 5, 2026 06:51

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

youknowone merged commit f1ebf16 into RustPython:main Oct 5, 2026
30 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

3 participants


Back | FazBrowse Home | New Git URL