Skip to content

Commit 8773755

Browse files
committed
Unify PyObjectRef ownership with extensible PyRef views
Represent PyObjectRef through PyRef<PyObject> while preserving typed ownership, payload projection, and drop behavior. Use the sealed target selector for borrowed set/frozenset union views and typed tuple elements. Keep tuple storage erased behind invariant typed slice views, with checked conversions and reference-count ownership tests. Preserve exact tuple identity for normalized nonempty full slices. Integrate the ownership contract into the public object module documentation and retain focused Rust ownership/layout tests without modifying bundled Lib sources. Spell the set/frozenset union owner directly as PyRef<PyAnySet> without a second owned alias; retain the sealed internal view registration. Assisted-by: OpenAI Codex:model identifier unavailable
1 parent f39da82 commit 8773755

12 files changed

Lines changed: 762 additions & 252 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/capi/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rustpython-stdlib = {workspace = true, features = ["threading"] }
2222
rustpython-pylib = { workspace = true }
2323

2424
[dev-dependencies]
25+
static_assertions = { workspace = true }
2526
pyo3 = { workspace = true, features = ["auto-initialize", "abi3t"] }
2627

2728
[lints]

‎crates/capi/src/pyerrors.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use core::convert::Infallible;
55
use core::ffi::{c_char, c_int};
66
use core::slice;
77
use rustpython_vm::builtins::{PyBaseException, PyTraceback, PyTuple, PyType};
8-
use rustpython_vm::convert::IntoObject;
98
use rustpython_vm::exceptions::ExceptionZoo;
109
use rustpython_vm::{AsObject, PyResult};
1110

‎crates/capi/src/util.rs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,12 @@ where
4040

4141
impl<T> FfiResult<*mut Py<T>> for PyRef<T>
4242
where
43-
Self: Into<PyObjectRef>,
43+
T: rustpython_vm::object::PyRefTarget<Target = Py<T>>,
4444
{
4545
const ERR_VALUE: *mut Py<T> = core::ptr::null_mut();
4646

4747
fn into_output(self, _vm: &VirtualMachine) -> *mut Py<T> {
48-
self.into().into_raw().as_ptr().cast()
49-
}
50-
}
51-
52-
impl FfiResult<*mut PyObject> for PyObjectRef {
53-
const ERR_VALUE: *mut PyObject = core::ptr::null_mut();
54-
55-
fn into_output(self, _vm: &VirtualMachine) -> *mut PyObject {
56-
self.into_raw().as_ptr()
48+
PyObjectRef::from(self).into_raw().as_ptr().cast()
5749
}
5850
}
5951

@@ -324,7 +316,7 @@ impl<T: PyPayload> FfiPtrExt for *mut Py<T> {
324316
"Attempted to dereference NULL {}",
325317
type_name::<Self>()
326318
);
327-
unsafe { PyRef::from_raw(self.cast_const()) }
319+
unsafe { PyRef::<T>::from_raw(self.cast_const()) }
328320
}
329321

330322
#[inline]
@@ -384,6 +376,14 @@ mod tests {
384376
use core::ffi::{c_longlong, c_ulonglong};
385377
use core::fmt::Debug;
386378

379+
#[test]
380+
fn pyref_object_ffi_targets() {
381+
use rustpython_vm::builtins::PyInt;
382+
static_assertions::assert_impl_all!(PyObjectRef: FfiResult<*mut PyObject>);
383+
static_assertions::assert_impl_all!(PyRef<PyInt>: FfiResult<*mut PyObject>, FfiResult<*mut Py<PyInt>>);
384+
static_assertions::assert_not_impl_any!(PyObjectRef: FfiResult<*mut Py<PyObject>>);
385+
}
386+
387387
#[test]
388388
fn ffi_result_err_value() {
389389
fn assert_error_value<T, Output>(value: Output)

‎crates/stdlib/src/_sqlite3.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ mod _sqlite3 {
6161
PyInt, PyIntRef, PyModule, PySlice, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType,
6262
PyTypeRef, PyUtf8Str, PyUtf8StrRef,
6363
},
64-
convert::IntoObject,
6564
function::{
6665
ArgCallable, ArgIterable, FsPath, FuncArgs, KwArgs, OptionalArg, PyComparisonValue,
6766
PySetterValue, TimeoutSeconds,

‎crates/vm/src/builtins/descriptor.rs‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,6 @@ impl MemberCell for core::sync::atomic::AtomicU32 {}
541541
// One object pointer. Readonly members may be a plain pointer. Writable
542542
// members are an atomic cell: `PyAtomicRef<PyObject>` when the pointer is
543543
// never null, or `PyAtomicRef<Option<PyObject>>` when it may be.
544-
impl MemberLayout for PyObjectRef {
545-
const KIND: MemberKind = MemberKind::Object;
546-
}
547-
impl MemberLayout for Option<PyObjectRef> {
548-
const KIND: MemberKind = MemberKind::Object;
549-
}
550544
impl MemberLayout for crate::object::PyObjectCell {
551545
const KIND: MemberKind = MemberKind::Object;
552546
const ATOMIC: bool = true;

‎crates/vm/src/builtins/frame_locals_proxy.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,10 @@ impl FrameLocalsProxy {
145145
return Ok(vm.ctx.not_implemented());
146146
}
147147
let result = self.snapshot(vm)?;
148-
if other.downcast_ref::<PyDict>().is_some() {
148+
if let Some(dict) = other.downcast_ref::<PyDict>() {
149149
// PyDict_Update reads a dict subclass's stored entries directly;
150150
// it does not dispatch to overridden mapping methods.
151-
result.merge_dict(&other.downcast().unwrap(), true, vm)?;
151+
result.merge_dict(dict, true, vm)?;
152152
} else {
153153
result.merge_object(other, vm)?;
154154
}

0 commit comments

Comments
 (0)