Skip to content

Commit eebb94e

Browse files
committed
Isolate interpreter heaps and redesign cyclic garbage collection
Assisted-by: Codex:GPT-6
1 parent 112b7ef commit eebb94e

207 files changed

Lines changed: 11193 additions & 5358 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Lib/test/test_descr.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,6 @@ class D(C):
18651865
self.assertEqual(b.foo, 3)
18661866
self.assertEqual(b.__class__, D)
18671867

1868-
@unittest.expectedSuccess # TODO: RUSTPYTHON; The `expectedFailure` here is from CPython, so this test must fail
18691868
@unittest.expectedFailure
18701869
def test_bad_new(self):
18711870
self.assertRaises(TypeError, object.__new__)

‎Lib/test/test_gc.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ def test_function(self):
236236
# is 3 because it includes f's code object.
237237
self.assertIn(gc.collect(), (2, 3))
238238

239-
@unittest.expectedFailure # TODO: RUSTPYTHON; - weakref clear ordering differs from 3.15+
240239
def test_function_tp_clear_leaves_consistent_state(self):
241240
# https://github.com/python/cpython/issues/91636
242241
code = """if 1:

‎Lib/test/test_weakref.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,6 @@ class D:
751751
del c1, c2, C, D
752752
gc.collect()
753753

754-
@unittest.expectedFailure # TODO: RUSTPYTHON
755754
def test_callback_in_cycle_resurrection(self):
756755
import gc
757756

@@ -798,7 +797,6 @@ def C_went_away(ignore):
798797
gc.collect()
799798
self.assertEqual(alist, [])
800799

801-
@unittest.expectedFailure # TODO: RUSTPYTHON
802800
def test_callbacks_on_callback(self):
803801
import gc
804802

‎benches/execution.rs‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,23 @@ fn bench_rustpython_code(b: &mut Bencher<'_>, name: &str, source: &str) {
3939
settings.write_bytecode = false;
4040
settings.user_site_directory = false;
4141
let builder = Interpreter::builder(settings);
42-
let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
43-
builder.add_native_modules(&defs).build().enter(|vm| {
44-
// Note: bench_cpython is both compiling and executing the code.
45-
// As such we compile the code in the benchmark loop as well.
46-
b.iter(|| {
47-
let code = vm.compile(source, Mode::Exec, name).unwrap();
48-
let scope = vm.new_scope_with_builtins();
49-
let res: PyResult = vm.run_code_obj(code, scope);
50-
vm.unwrap_pyresult(res);
51-
})
52-
})
42+
let defs = rustpython_stdlib::stdlib_module_defs(unsafe { builder.context() });
43+
// SAFETY: compilation and execution retain no objects beyond this entry.
44+
unsafe {
45+
builder
46+
.add_native_modules(&defs)
47+
.build()
48+
.enter_unchecked(|vm| {
49+
// Note: bench_cpython is both compiling and executing the code.
50+
// As such we compile the code in the benchmark loop as well.
51+
b.iter(|| {
52+
let code = vm.compile(source, Mode::Exec, name).unwrap();
53+
let scope = vm.new_scope_with_builtins();
54+
let res: PyResult = vm.run_code_obj(code, scope);
55+
vm.unwrap_pyresult(res);
56+
})
57+
})
58+
}
5359
}
5460

5561
pub fn benchmark_file_execution(

‎benches/microbenchmarks.rs‎

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -111,58 +111,61 @@ fn bench_rustpython_code(group: &mut BenchmarkGroup<'_, WallTime>, bench: &Micro
111111
settings.user_site_directory = false;
112112

113113
let builder = Interpreter::builder(settings);
114-
let defs = rustpython_stdlib::stdlib_module_defs(&builder.ctx);
115-
let interp = builder.add_native_modules(&defs).build();
116-
interp.enter(|vm| {
117-
let setup_code = vm
118-
.compile(&bench.setup, Mode::Exec, &bench.name)
119-
.expect("Error compiling setup code");
120-
let bench_code = vm
121-
.compile(&bench.code, Mode::Exec, &bench.name)
122-
.expect("Error compiling bench code");
123-
124-
let bench_func = |scope| {
125-
let res: PyResult = vm.run_code_obj(bench_code.clone(), scope);
126-
vm.unwrap_pyresult(res);
127-
};
114+
let defs = rustpython_stdlib::stdlib_module_defs(unsafe { builder.context() });
115+
let interp = unsafe { builder.add_native_modules(&defs) }.build();
116+
// SAFETY: benchmark scopes and code objects are released before leaving.
117+
unsafe {
118+
interp.enter_unchecked(|vm| {
119+
let setup_code = vm
120+
.compile(&bench.setup, Mode::Exec, &bench.name)
121+
.expect("Error compiling setup code");
122+
let bench_code = vm
123+
.compile(&bench.code, Mode::Exec, &bench.name)
124+
.expect("Error compiling bench code");
125+
126+
let bench_func = |scope| {
127+
let res: PyResult = vm.run_code_obj(bench_code.clone(), scope);
128+
vm.unwrap_pyresult(res);
129+
};
128130

129-
let bench_setup = |iterations| {
130-
let scope = vm.new_scope_with_builtins();
131-
if let Some(idx) = iterations {
131+
let bench_setup = |iterations| {
132+
let scope = vm.new_scope_with_builtins();
133+
if let Some(idx) = iterations {
134+
scope
135+
.locals
136+
.as_ref()
137+
.expect("new_scope_with_builtins always provides locals")
138+
.as_object()
139+
.set_item("ITERATIONS", vm.new_pyobj(idx), vm)
140+
.expect("Error adding ITERATIONS local variable");
141+
}
142+
let setup_result = vm.run_code_obj(setup_code.clone(), scope.clone());
143+
vm.unwrap_pyresult(setup_result);
132144
scope
133-
.locals
134-
.as_ref()
135-
.expect("new_scope_with_builtins always provides locals")
136-
.as_object()
137-
.set_item("ITERATIONS", vm.new_pyobj(idx), vm)
138-
.expect("Error adding ITERATIONS local variable");
139-
}
140-
let setup_result = vm.run_code_obj(setup_code.clone(), scope.clone());
141-
vm.unwrap_pyresult(setup_result);
142-
scope
143-
};
145+
};
144146

145-
if bench.iterate {
146-
for idx in iteration_counts() {
147-
group.throughput(Throughput::Elements(idx as u64));
148-
group.bench_with_input(
149-
BenchmarkId::new("rustpython", &bench.name),
150-
&idx,
151-
|b, idx| {
152-
b.iter_batched(
153-
|| bench_setup(Some(*idx)),
154-
bench_func,
155-
BatchSize::LargeInput,
156-
);
157-
},
158-
);
147+
if bench.iterate {
148+
for idx in iteration_counts() {
149+
group.throughput(Throughput::Elements(idx as u64));
150+
group.bench_with_input(
151+
BenchmarkId::new("rustpython", &bench.name),
152+
&idx,
153+
|b, idx| {
154+
b.iter_batched(
155+
|| bench_setup(Some(*idx)),
156+
bench_func,
157+
BatchSize::LargeInput,
158+
);
159+
},
160+
);
161+
}
162+
} else {
163+
group.bench_function(BenchmarkId::new("rustpython", &bench.name), move |b| {
164+
b.iter_batched(|| bench_setup(None), bench_func, BatchSize::LargeInput);
165+
});
159166
}
160-
} else {
161-
group.bench_function(BenchmarkId::new("rustpython", &bench.name), move |b| {
162-
b.iter_batched(|| bench_setup(None), bench_func, BatchSize::LargeInput);
163-
});
164-
}
165-
})
167+
})
168+
}
166169
}
167170

168171
/// `true` when the benchmarks are executed by the CodSpeed runner.

‎crates/capi/src/bytearrayobject.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ pub unsafe extern "C" fn PyByteArray_Size(bytearray: *mut PyObject) -> isize {
5959
pub unsafe extern "C" fn PyByteArray_AsString(bytearray: *mut PyObject) -> *mut c_char {
6060
with_vm(|vm| {
6161
let bytearray = unsafe { bytearray.assume_borrowed_and_cast::<PyByteArray>(vm) }?;
62-
Ok(bytearray.borrow_buf_mut().as_mut_ptr())
62+
// SAFETY: the C caller owns the PyByteArray_AsString lifetime and
63+
// synchronization contract; no resizable Rust Vec escapes this API.
64+
Ok(unsafe { bytearray.as_mut_ptr_unchecked() })
6365
})
6466
}
6567

‎crates/capi/src/ceval.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ mod tests {
180180
#[pyfunction]
181181
fn legacy_locals_is_frame_mapping() -> bool {
182182
let locals = super::PyEval_GetLocals();
183-
rustpython_vm::vm::thread::with_current_vm(|vm| {
183+
crate::pystate::with_current_vm(|vm| {
184184
let frame = vm.current_frame().unwrap();
185185
core::ptr::eq(
186186
locals.cast_const(),

‎crates/capi/src/descrobject.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::pystate::with_vm;
55
use crate::util::{CStrExt, FfiPtrExt};
66
use core::ffi::{c_char, c_int, c_void};
77
use rustpython_vm::builtins::{
8-
DescriptorMemberDef, MemberAccess, MemberKind, PyDescriptorOwned, PyGetSet, PyMappingProxy,
8+
DescriptorMemberDef, MemberAccess, MemberKind, PyDescriptor, PyGetSet, PyMappingProxy,
99
PyMemberDescriptor, PyMemberFlags, PyType,
1010
};
1111
use rustpython_vm::common::lock::PyRwLock;
@@ -144,7 +144,7 @@ impl PyMemberDef {
144144
});
145145

146146
let descriptor = PyMemberDescriptor {
147-
common: PyDescriptorOwned {
147+
common: PyDescriptor {
148148
typ: ty.to_owned(),
149149
name: vm.ctx.intern_str(name),
150150
qualname: PyRwLock::new(None),

‎crates/capi/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn init_main_interpreter(interpreter: Interpreter) {
6161
let mut interp = get_main_interpreter();
6262
assert!(interp.is_none(), "Main interpreter is already set");
6363
// Safety: Interpreter was not initialized before, so we can safely assume the statics are not used
64-
unsafe { init_exception_statics(&Context::genesis().exceptions) };
64+
unsafe { init_exception_statics(&Context::genesis_unchecked().exceptions) };
6565
*interp = Some(interpreter);
6666
MAIN_INTERP_PTR.store(
6767
interp.as_ref().unwrap() as *const _ as *mut _,

‎crates/capi/src/object/pytype.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub unsafe extern "C" fn Py_IS_TYPE(op: *mut PyObject, ty: *mut PyTypeObject) ->
2727
#[unsafe(no_mangle)]
2828
pub unsafe extern "C" fn PyType_GetFlags(ptr: *mut PyTypeObject) -> c_ulong {
2929
let ty = unsafe { ptr.assume_borrowed() };
30-
ty.slots.flags.bits() as u32 as c_ulong
30+
ty.effective_flags().bits() as u32 as c_ulong
3131
}
3232

3333
#[unsafe(no_mangle)]

0 commit comments

Comments
 (0)