diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 78eb692666..316085ca16 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -89,7 +89,7 @@ impl ConstantBag for PyObjBag<'_> { bytecode::ConstantData::Str { value } => vm.ctx.new_utf8_str(value), bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()), bytecode::ConstantData::Boolean { value } => ctx.new_bool(value).into(), - bytecode::ConstantData::Code { code } => ctx.new_code_object(code.map_bag(self)).into(), + bytecode::ConstantData::Code { code } => vm.new_code_object(code.map_bag(self)).into(), bytecode::ConstantData::Tuple { elements } => { let elements = elements .into_iter() @@ -116,7 +116,7 @@ impl ConstantBag for PyObjBag<'_> { bytecode::BorrowedConstant::Bytes { value } => ctx.new_bytes(value.to_vec()), bytecode::BorrowedConstant::Boolean { value } => ctx.new_bool(value).into(), bytecode::BorrowedConstant::Code { code } => { - ctx.new_code_object(code.map_clone_bag(self)).into() + vm.new_code_object(code.map_clone_bag(self)).into() } bytecode::BorrowedConstant::Tuple { elements } => { let elements = elements @@ -171,6 +171,13 @@ impl PyCode { pub fn new(code: CodeObject) -> PyCode { PyCode { code } } + + /// Create a new `PyRef` from a `code::CodeObject`. If you have a non-mapped codeobject or + /// this is giving you a type error even though you've passed a `CodeObject`, try + /// [`vm.new_code_object()`](VirtualMachine::new_code_object) instead. + pub fn new_ref(code: CodeObject, ctx: &PyContext) -> PyRef { + PyRef::new_ref(PyCode { code }, ctx.types.code_type.clone(), None) + } } impl fmt::Debug for PyCode { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index b196d9311c..28cd96dae9 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -8,7 +8,6 @@ use crate::{ builtins::{ builtinfunc::{PyBuiltinFunction, PyBuiltinMethod, PyNativeFuncDef}, bytearray, bytes, - code::{self, PyCode}, getset::{IntoPyGetterFunc, IntoPySetterFunc, PyGetSet}, object, pystr, PyBaseExceptionRef, PyBoundMethod, PyDict, PyDictRef, PyEllipsis, PyFloat, PyFrozenSet, PyInt, PyIntRef, PyList, PyNone, PyNotImplemented, PyStr, PyTuple, PyTupleRef, @@ -306,13 +305,6 @@ impl PyContext { ) } - /// Create a new `PyRef` from a `code::CodeObject`. If you have a non-mapped codeobject or - /// this is giving you a type error even though you've passed a `CodeObject`, try - /// [`vm.new_code_object()`](VirtualMachine::new_code_object) instead. - pub fn new_code_object(&self, code: code::CodeObject) -> PyRef { - PyRef::new_ref(PyCode::new(code), self.types.code_type.clone(), None) - } - pub fn new_base_object(&self, class: PyTypeRef, dict: Option) -> PyObjectRef { debug_assert_eq!( class.slots.flags.contains(PyTypeFlags::HAS_DICT), diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 5455cd073b..a1b4518eec 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -579,7 +579,7 @@ impl VirtualMachine { } pub fn new_code_object(&self, code: impl code::IntoCodeObject) -> PyRef { - self.ctx.new_code_object(code.into_codeobj(self)) + PyCode::new_ref(code.into_codeobj(self), &self.ctx) } pub fn new_module(&self, name: &str, dict: PyDictRef, doc: Option<&str>) -> PyObjectRef {