GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — extend types and exceptions only for genesis by youknowone · Pull Request #7316 · RustPython/RustPython Conversation
|
📝 WalkthroughWalkthroughMoved TypeZoo and ExceptionZoo extension into Context::genesis and changed many builtin init/extend APIs to accept a &'static Context; class construction now derives a &'static Context via an unsafe cast and uses that context's slot_new_wrapper when building bound new methods, with added safety comments. Changes
Sequence Diagram(s)sequenceDiagram
participant Genesis as Context::genesis
participant TypeZoo as TypeZoo
participant ExceptionZoo as ExceptionZoo
participant MakeClass as make_class / extend_class
participant Builtin as builtin::init
Genesis->>TypeZoo: TypeZoo::extend(&ctx)
Genesis->>ExceptionZoo: ExceptionZoo::extend(&ctx)
Note right of Genesis: genesis returns Context (now populated)
Builtin->>MakeClass: call make_class(ctx)
MakeClass->>MakeClass: unsafe cast ctx -> &'static Context
MakeClass->>MakeClass: use ctx.slot_new_wrapper to build bound __new__
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
f77c40a to
a0692d3
Compare
|
Code has been automatically formatted The code in this PR has been formatted using:
git pull origin genesis-extend |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/vm/src/class.rs`:
- Around line 191-196: The unsafe cast to &'static PyMethodDef (using
ctx.slot_new_wrapper) is unsound because extend_class takes ctx: &Context;
change the trait signature to require static lifetimes (e.g., fn
extend_class(ctx: &'static Context, class: &'static Py<PyType>)) so the compiler
encodes the invariant, update all impls/call sites accordingly, then remove the
unsafe &'static cast and use wrapper = &ctx.slot_new_wrapper directly (or keep
wrapper as &'static if callers now supply &'static Context); reference symbols:
extend_class, Context, ctx.slot_new_wrapper, PyMethodDef, build_bound_method,
and class.set_attr to locate and modify the code.
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
crates/vm/src/class.rscrates/vm/src/vm/context.rscrates/vm/src/vm/interpreter.rs
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
050ca62 to
a0692d3
Compare
8df31f9 to
072c495
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
crates/vm/src/class.rs (1)
209-217:⚠️ Potential issue | 🔴 Critical
make_classstill allows non-static input before forcing'staticvia unsafe cast.Line 217 casts
&Contextto&'static Context, but Line 209 still accepts any borrowed context. If a non-genesis context reaches this path, this is undefined behavior.Suggested fix
- fn make_class(ctx: &Context) -> PyTypeRef + fn make_class(ctx: &'static Context) -> PyTypeRef where Self: StaticType + Sized, { (*Self::static_cell().get_or_init(|| { let typ = Self::create_static_type(); - // SAFETY: Context is heap-allocated via PyRc and stored in a static cell - // (Context::genesis), so it lives for 'static. - let ctx: &'static Context = unsafe { &*(ctx as *const Context) }; Self::extend_class(ctx, unsafe { // typ will be saved in static_cell let r: &Py<PyType> = &typ;#!/bin/bash set -euo pipefail echo "== Check make_class signature and unsafe cast ==" rg -nP --type=rust -C2 'fn\s+make_class\s*\(\s*ctx:\s*&Context\s*\)|unsafe\s*\{\s*&\*\(ctx as \*const Context\)\s*\}' crates/vm/src/class.rs echo echo "== List make_class call sites ==" rg -nP --type=rust -C2 '\bmake_class\s*\(' echo echo "== Inspect Context construction/entrypoints ==" rg -nP --type=rust -C3 'impl\s+Context|fn\s+genesis\s*\(|fn\s+new\s*\(' crates/vm/src/vm/context.rsAs per coding guidelines "Follow Rust best practices for error handling and memory management".
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
crates/derive-impl/src/pyclass.rscrates/vm/src/builtins/asyncgenerator.rscrates/vm/src/builtins/bool.rscrates/vm/src/builtins/builtin_func.rscrates/vm/src/builtins/bytearray.rscrates/vm/src/builtins/bytes.rscrates/vm/src/builtins/capsule.rscrates/vm/src/builtins/classmethod.rscrates/vm/src/builtins/code.rscrates/vm/src/builtins/complex.rscrates/vm/src/builtins/coroutine.rscrates/vm/src/builtins/descriptor.rscrates/vm/src/builtins/dict.rscrates/vm/src/builtins/enumerate.rscrates/vm/src/builtins/filter.rscrates/vm/src/builtins/float.rscrates/vm/src/builtins/frame.rscrates/vm/src/builtins/function.rscrates/vm/src/builtins/generator.rscrates/vm/src/builtins/genericalias.rscrates/vm/src/builtins/getset.rscrates/vm/src/builtins/int.rscrates/vm/src/builtins/interpolation.rscrates/vm/src/builtins/iter.rscrates/vm/src/builtins/list.rscrates/vm/src/builtins/map.rscrates/vm/src/builtins/mappingproxy.rscrates/vm/src/builtins/memory.rscrates/vm/src/builtins/module.rscrates/vm/src/builtins/namespace.rscrates/vm/src/builtins/object.rscrates/vm/src/builtins/property.rscrates/vm/src/builtins/range.rscrates/vm/src/builtins/set.rscrates/vm/src/builtins/singletons.rscrates/vm/src/builtins/slice.rscrates/vm/src/builtins/staticmethod.rscrates/vm/src/builtins/str.rscrates/vm/src/builtins/super.rscrates/vm/src/builtins/template.rscrates/vm/src/builtins/traceback.rscrates/vm/src/builtins/tuple.rscrates/vm/src/builtins/type.rscrates/vm/src/builtins/union.rscrates/vm/src/builtins/weakproxy.rscrates/vm/src/builtins/weakref.rscrates/vm/src/builtins/zip.rscrates/vm/src/class.rscrates/vm/src/exceptions.rscrates/vm/src/stdlib/typing.rscrates/vm/src/types/zoo.rscrates/vm/src/vm/context.rscrates/vm/src/vm/interpreter.rs
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.