extend types and exceptions only for genesis (#7316)

This commit is contained in:
Jeong, YunWon authored and GitHub committed 2026-03-03 11:24:58 +09:00
1 parent 454f7db911
commit 76673e383e
53 files changed
+69 -63

No files matched your search

+3 -3
View File
@@ -181,7 +181,7 @@ pub(crate) fn impl_pyclass_impl(attr: PunctuatedNestedMeta, item: Item) -> Resul
},
parse_quote! {
fn __extend_py_class(
ctx: &::rustpython_vm::Context,
ctx: &'static ::rustpython_vm::Context,
class: &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType>,
) {
#getset_impl
@@ -222,7 +222,7 @@ pub(crate) fn impl_pyclass_impl(attr: PunctuatedNestedMeta, item: Item) -> Resul
const TP_FLAGS: ::rustpython_vm::types::PyTypeFlags = #flags;
fn impl_extend_class(
ctx: &::rustpython_vm::Context,
ctx: &'static ::rustpython_vm::Context,
class: &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType>,
) {
#impl_ty::__extend_py_class(ctx, class);
@@ -284,7 +284,7 @@ pub(crate) fn impl_pyclass_impl(attr: PunctuatedNestedMeta, item: Item) -> Resul
},
parse_quote! {
fn __extend_py_class(
ctx: &::rustpython_vm::Context,
ctx: &'static ::rustpython_vm::Context,
class: &'static ::rustpython_vm::Py<::rustpython_vm::builtins::PyType>,
) {
#getset_impl
+1 -1
View File
@@ -810,7 +810,7 @@ impl Drop for PyAsyncGen {
}
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
PyAsyncGen::extend_class(ctx, ctx.types.async_generator);
PyAsyncGenASend::extend_class(ctx, ctx.types.async_generator_asend);
PyAsyncGenAThrow::extend_class(ctx, ctx.types.async_generator_athrow);
+1 -1
View File
@@ -182,7 +182,7 @@ impl Representable for PyBool {
}
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyBool::extend_class(context, context.types.bool_type);
}
+1 -1
View File
@@ -224,7 +224,7 @@ impl fmt::Debug for PyNativeMethod {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyNativeFunction::extend_class(context, context.types.builtin_function_or_method_type);
}
+1 -1
View File
@@ -67,7 +67,7 @@ impl PyPayload for PyByteArray {
}
/// Fill bytearray class methods dictionary.
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyByteArray::extend_class(context, context.types.bytearray_type);
PyByteArrayIterator::extend_class(context, context.types.bytearray_iterator_type);
}
+1 -1
View File
@@ -86,7 +86,7 @@ impl PyPayload for PyBytes {
}
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyBytes::extend_class(context, context.types.bytes_type);
PyBytesIterator::extend_class(context, context.types.bytes_iterator_type);
}
+1 -1
View File
@@ -28,6 +28,6 @@ impl Representable for PyCapsule {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyCapsule::extend_class(context, context.types.capsule_type);
}
+1 -1
View File
@@ -227,6 +227,6 @@ impl Representable for PyClassMethod {
}
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyClassMethod::extend_class(context, context.types.classmethod_type);
}
+1 -1
View File
@@ -1275,6 +1275,6 @@ impl<'a> LineTableReader<'a> {
}
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
PyCode::extend_class(ctx, ctx.types.code_type);
}
+1 -1
View File
@@ -89,7 +89,7 @@ impl PyObjectRef {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyComplex::extend_class(context, context.types.complex_type);
}
+1 -1
View File
@@ -247,7 +247,7 @@ impl Drop for PyCoroutine {
}
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
PyCoroutine::extend_class(ctx, ctx.types.coroutine_type);
PyCoroutineWrapper::extend_class(ctx, ctx.types.coroutine_wrapper_type);
}
+1 -1
View File
@@ -426,7 +426,7 @@ impl GetDescriptor for PyMemberDescriptor {
}
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
PyMemberDescriptor::extend_class(ctx, ctx.types.member_descriptor_type);
PyMethodDescriptor::extend_class(ctx, ctx.types.method_descriptor_type);
PyWrapper::extend_class(ctx, ctx.types.wrapper_descriptor_type);
+1 -1
View File
@@ -1362,7 +1362,7 @@ fn set_inner_number_or(a: &PyObject, b: &PyObject, vm: &VirtualMachine) -> PyRes
set_inner_number_op(a, b, |a, b| a.union(b, vm), vm)
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyDict::extend_class(context, context.types.dict_type);
PyDictKeys::extend_class(context, context.types.dict_keys_type);
PyDictKeyIterator::extend_class(context, context.types.dict_keyiterator_type);
+1 -1
View File
@@ -144,7 +144,7 @@ impl IterNext for PyReverseSequenceIterator {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyEnumerate::extend_class(context, context.types.enumerate_type);
PyReverseSequenceIterator::extend_class(context, context.types.reverse_iter_type);
}
+1 -1
View File
@@ -71,6 +71,6 @@ impl IterNext for PyFilter {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyFilter::extend_class(context, context.types.filter_type);
}
+1 -1
View File
@@ -480,6 +480,6 @@ pub(crate) fn get_value(obj: &PyObject) -> f64 {
}
#[rustfmt::skip] // to avoid line splitting
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyFloat::extend_class(context, context.types.float_type);
}
+1 -1
View File
@@ -423,7 +423,7 @@ pub(crate) mod stack_analysis {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
Frame::extend_class(context, context.types.frame_type);
}
+1 -1
View File
@@ -1219,7 +1219,7 @@ impl PyCell {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyFunction::extend_class(context, context.types.function_type);
PyBoundMethod::extend_class(context, context.types.bound_method_type);
PyCell::extend_class(context, context.types.cell_type);
+1 -1
View File
@@ -173,6 +173,6 @@ impl Drop for PyGenerator {
}
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
PyGenerator::extend_class(ctx, ctx.types.generator_type);
}
+1 -1
View File
@@ -739,7 +739,7 @@ pub fn subscript_generic(type_params: PyObjectRef, vm: &VirtualMachine) -> PyRes
generic_alias_class.call((generic_type, args.to_pyobject(vm)), vm)
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyGenericAlias::extend_class(context, context.types.generic_alias_type);
PyGenericAliasIterator::extend_class(context, context.types.generic_alias_iterator_type);
}
+1 -1
View File
@@ -157,6 +157,6 @@ impl Representable for PyGetSet {
}
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyGetSet::extend_class(context, context.types.getset_type);
}
+1 -1
View File
@@ -744,6 +744,6 @@ pub fn try_to_float(int: &BigInt, vm: &VirtualMachine) -> PyResult<f64> {
.ok_or_else(|| vm.new_overflow_error("int too large to convert to float"))
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyInt::extend_class(context, context.types.int_type);
}
+1 -1
View File
@@ -224,6 +224,6 @@ impl Representable for PyInterpolation {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyInterpolation::extend_class(context, context.types.interpolation_type);
}
+1 -1
View File
@@ -289,7 +289,7 @@ impl IterNext for PyCallableIterator {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PySequenceIterator::extend_class(context, context.types.iter_type);
PyCallableIterator::extend_class(context, context.types.callable_iterator);
}
+1 -1
View File
@@ -696,7 +696,7 @@ impl IterNext for PyListReverseIterator {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
let list_type = &context.types.list_type;
PyList::extend_class(context, list_type);
+1 -1
View File
@@ -70,6 +70,6 @@ impl IterNext for PyMap {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyMap::extend_class(context, context.types.map_type);
}
+1 -1
View File
@@ -293,6 +293,6 @@ impl Representable for PyMappingProxy {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyMappingProxy::extend_class(context, context.types.mappingproxy_type)
}
+1 -1
View File
@@ -1127,7 +1127,7 @@ impl Representable for PyMemoryView {
}
}
pub(crate) fn init(ctx: &Context) {
pub(crate) fn init(ctx: &'static Context) {
PyMemoryView::extend_class(ctx, ctx.types.memoryview_type);
PyMemoryViewIterator::extend_class(ctx, ctx.types.memoryviewiterator_type);
}
+1 -1
View File
@@ -452,7 +452,7 @@ impl Representable for PyModule {
}
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyModule::extend_class(context, context.types.module_type);
}
+1 -1
View File
@@ -175,6 +175,6 @@ impl Representable for PyNamespace {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyNamespace::extend_class(context, context.types.namespace_type);
}
+1 -1
View File
@@ -560,7 +560,7 @@ pub fn object_set_dict(obj: PyObjectRef, dict: PyDictRef, vm: &VirtualMachine) -
.map_err(|_| vm.new_attribute_error("This object has no __dict__"))
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
// Manually set init slot - derive macro doesn't generate extend_slots
// for trait impl that overrides #[pyslot] method
ctx.types
+1 -1
View File
@@ -395,7 +395,7 @@ impl Initializer for PyProperty {
}
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyProperty::extend_class(context, context.types.property_type);
// This is a bit unfortunate, but this instance attribute overlaps with the
+1 -1
View File
@@ -167,7 +167,7 @@ impl PyRange {
// obj.downcast_ref::<PyRange>().unwrap().clone()
// }
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyRange::extend_class(context, context.types.range_type);
PyLongRangeIterator::extend_class(context, context.types.long_range_iterator_type);
PyRangeIterator::extend_class(context, context.types.range_iterator_type);
+1 -1
View File
@@ -1421,7 +1421,7 @@ impl IterNext for PySetIterator {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PySet::extend_class(context, context.types.set_type);
PyFrozenSet::extend_class(context, context.types.frozenset_type);
PySetIterator::extend_class(context, context.types.set_iterator_type);
+1 -1
View File
@@ -132,7 +132,7 @@ impl Representable for PyNotImplemented {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyNone::extend_class(context, context.types.none_type);
PyNotImplemented::extend_class(context, context.types.not_implemented_type);
}
+1 -1
View File
@@ -354,7 +354,7 @@ impl Representable for PyEllipsis {
}
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
PySlice::extend_class(ctx, ctx.types.slice_type);
PyEllipsis::extend_class(ctx, ctx.types.ellipsis_type);
}
+1 -1
View File
@@ -197,6 +197,6 @@ impl Representable for PyStaticMethod {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyStaticMethod::extend_class(context, context.types.staticmethod_type);
}
+1 -1
View File
@@ -1761,7 +1761,7 @@ struct ReplaceArgs {
count: isize,
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
PyStr::extend_class(ctx, ctx.types.str_type);
PyStrIterator::extend_class(ctx, ctx.types.str_iterator_type);
+1 -1
View File
@@ -269,7 +269,7 @@ fn super_check(ty: PyTypeRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult
)))
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
let super_type = &context.types.super_type;
PySuper::extend_class(context, super_type);
+1 -1
View File
@@ -329,7 +329,7 @@ impl IterNext for PyTemplateIter {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyTemplate::extend_class(context, context.types.template_type);
PyTemplateIter::extend_class(context, context.types.template_iter_type);
}
+1 -1
View File
@@ -119,7 +119,7 @@ impl PyTracebackRef {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyTraceback::extend_class(context, context.types.traceback_type);
}
+1 -1
View File
@@ -583,7 +583,7 @@ impl IterNext for PyTupleIterator {
}
}
pub(crate) fn init(context: &Context) {
pub(crate) fn init(context: &'static Context) {
PyTuple::extend_class(context, context.types.tuple_type);
PyTupleIterator::extend_class(context, context.types.tuple_iterator_type);
}
+1 -1
View File
@@ -2103,7 +2103,7 @@ fn subtype_set_dict(obj: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -
* The magical type type
*/
pub(crate) fn init(ctx: &Context) {
pub(crate) fn init(ctx: &'static Context) {
PyType::extend_class(ctx, ctx.types.type_type);
}
+1 -1
View File
@@ -540,7 +540,7 @@ impl Representable for PyUnion {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
let union_type = &context.types.union_type;
PyUnion::extend_class(context, union_type);
}
+1 -1
View File
@@ -238,7 +238,7 @@ impl Representable for PyWeakProxy {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyWeakProxy::extend_class(context, context.types.weakproxy_type);
}
+1 -1
View File
@@ -153,6 +153,6 @@ impl Representable for PyWeak {
}
}
pub fn init(context: &Context) {
pub fn init(context: &'static Context) {
PyWeak::extend_class(context, context.types.weakref_type);
}
+1 -1
View File
@@ -113,6 +113,6 @@ impl IterNext for PyZip {
}
}
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
PyZip::extend_class(ctx, ctx.types.zip_type);
}
+8 -7
View File
@@ -131,7 +131,7 @@ pub trait PyClassDef {
pub trait PyClassImpl: PyClassDef {
const TP_FLAGS: PyTypeFlags = PyTypeFlags::DEFAULT;
fn extend_class(ctx: &Context, class: &'static Py<PyType>)
fn extend_class(ctx: &'static Context, class: &'static Py<PyType>)
where
Self: Sized,
{
@@ -188,11 +188,9 @@ pub trait PyClassImpl: PyClassDef {
&& object_new.is_some_and(|obj_new| slot_new as usize == obj_new as usize);
if !is_inherited_from_object {
let bound_new = Context::genesis().slot_new_wrapper.build_bound_method(
ctx,
class.to_owned().into(),
class,
);
let bound_new =
ctx.slot_new_wrapper
.build_bound_method(ctx, class.to_owned().into(), class);
class.set_attr(identifier!(ctx, __new__), bound_new.into());
}
}
@@ -214,6 +212,9 @@ pub trait PyClassImpl: PyClassDef {
{
(*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;
@@ -225,7 +226,7 @@ pub trait PyClassImpl: PyClassDef {
.to_owned()
}
fn impl_extend_class(ctx: &Context, class: &'static Py<PyType>);
fn impl_extend_class(ctx: &'static Context, class: &'static Py<PyType>);
const METHOD_DEFS: &'static [PyMethodDef];
fn extend_slots(slots: &mut PyTypeSlots);
+1 -1
View File
@@ -943,7 +943,7 @@ impl ExceptionZoo {
clippy::redundant_clone,
reason = "temporary workaround until errno/winerror handling is fixed"
)]
pub fn extend(ctx: &Context) {
pub fn extend(ctx: &'static Context) {
use self::types::*;
let excs = &ctx.exceptions;
+1 -1
View File
@@ -12,7 +12,7 @@ pub(crate) use decl::module_def;
pub use decl::*;
/// Initialize typing types (call extend_class)
pub fn init(ctx: &Context) {
pub fn init(ctx: &'static Context) {
NoDefault::extend_class(ctx, ctx.types.typing_no_default_type);
}
+1 -1
View File
@@ -216,7 +216,7 @@ impl TypeZoo {
/// Fill attributes of builtin types.
#[cold]
pub(crate) fn extend(context: &Context) {
pub(crate) fn extend(context: &'static Context) {
// object must be initialized before type to set object.slots.init,
// which type will inherit via inherit_slots()
object::init(context);
+9 -1
View File
@@ -285,7 +285,15 @@ impl Context {
rustpython_common::static_cell! {
static CONTEXT: PyRc<Context>;
}
CONTEXT.get_or_init(|| PyRc::new(Self::init_genesis()))
CONTEXT.get_or_init(|| {
let ctx = PyRc::new(Self::init_genesis());
// SAFETY: ctx is heap-allocated via PyRc and will be stored in
// the CONTEXT static cell, so the Context lives for 'static.
let ctx_ref: &'static Context = unsafe { &*PyRc::as_ptr(&ctx) };
crate::types::TypeZoo::extend(ctx_ref);
crate::exceptions::ExceptionZoo::extend(ctx_ref);
ctx
})
}
fn init_genesis() -> Self {
-3
View File
@@ -52,9 +52,6 @@ where
let paths = getpath::init_path_config(&settings);
let config = PyConfig::new(settings, paths);
crate::types::TypeZoo::extend(&ctx);
crate::exceptions::ExceptionZoo::extend(&ctx);
// Build module_defs map from builtin modules + additional modules
let mut all_module_defs: BTreeMap<&'static str, &'static builtins::PyModuleDef> =
crate::stdlib::builtin_module_defs(&ctx)