diff --git a/vm/src/builtins/namespace.rs b/vm/src/builtins/namespace.rs index a41ea993aa..c0b86e2943 100644 --- a/vm/src/builtins/namespace.rs +++ b/vm/src/builtins/namespace.rs @@ -2,7 +2,7 @@ use super::PyTypeRef; use crate::{ function::{IntoPyObject, KwArgs}, slots::SlotConstructor, - PyClassImpl, PyContext, PyResult, PyValue, VirtualMachine, + PyClassImpl, PyContext, PyRef, PyResult, PyValue, VirtualMachine, }; /// A simple attribute-based namespace. @@ -30,6 +30,12 @@ impl SlotConstructor for PyNamespace { } } +impl PyNamespace { + pub fn new_ref(ctx: &PyContext) -> PyRef { + PyRef::new_ref(Self, ctx.types.namespace_type.clone(), Some(ctx.new_dict())) + } +} + #[pyimpl(flags(BASETYPE, HAS_DICT), with(SlotConstructor))] impl PyNamespace {} diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 60b3324a46..a653e960d9 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -56,7 +56,7 @@ macro_rules! extend_class { macro_rules! py_namespace { ( $vm:expr, { $($name:expr => $value:expr),* $(,)* }) => { { - let namespace = $vm.ctx.new_namespace(); + let namespace = $crate::builtins::PyNamespace::new_ref(&$vm.ctx).into(); $( $vm.__module_set_attr(&namespace, $name, $value).unwrap(); )* diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 84922ba99a..4af499707c 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -10,7 +10,6 @@ use crate::{ bytearray, bytes, code::{self, PyCode}, getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet}, - namespace::PyNamespace, object, pystr, set::{self, PyFrozenSet}, PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat, PyInt, PyIntRef, @@ -250,14 +249,6 @@ impl PyContext { create_type_with_slots(name, &self.types.type_type, base, slots) } - pub fn new_namespace(&self) -> PyObjectRef { - PyObject::new( - PyNamespace, - self.types.namespace_type.clone(), - Some(self.new_dict()), - ) - } - pub(crate) fn new_stringref(&self, s: String) -> pystr::PyStrRef { PyRef::new_ref(pystr::PyStr::from(s), self.types.str_type.clone(), None) }