Skip to content

Commit b0e65d1

Browse files
committed
ToObject for CodeObject(s) & Replace PyRef::new_ref with ctx.new_code
1 parent a2a18f2 commit b0e65d1

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

vm/src/builtins/code.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::{PyStrRef, PyTupleRef, PyTypeRef};
66
use crate::{
77
bytecode::{self, BorrowedConstant, Constant, ConstantBag},
88
class::{PyClassImpl, StaticType},
9+
convert::ToPyObject,
910
function::FuncArgs,
1011
AsObject, Context, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1112
};
@@ -112,6 +113,7 @@ pub type CodeObject = bytecode::CodeObject<PyConstant>;
112113
pub trait IntoCodeObject {
113114
fn into_codeobj(self, ctx: &Context) -> CodeObject;
114115
}
116+
115117
impl IntoCodeObject for CodeObject {
116118
fn into_codeobj(self, _ctx: &Context) -> CodeObject {
117119
self
@@ -140,13 +142,6 @@ impl PyCode {
140142
pub fn new(code: CodeObject) -> PyCode {
141143
PyCode { code }
142144
}
143-
144-
/// Create a new `PyRef<PyCode>` from a `code::CodeObject`. If you have a non-mapped codeobject or
145-
/// this is giving you a type error even though you've passed a `CodeObject`, try
146-
/// [`vm.new_code_object()`](VirtualMachine::new_code_object) instead.
147-
pub fn new_ref(code: CodeObject, ctx: &Context) -> PyRef<Self> {
148-
PyRef::new_ref(PyCode { code }, ctx.types.code_type.clone(), None)
149-
}
150145
}
151146

152147
impl fmt::Debug for PyCode {
@@ -242,6 +237,18 @@ impl fmt::Display for PyCode {
242237
}
243238
}
244239

240+
impl ToPyObject for CodeObject {
241+
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
242+
vm.ctx.new_code(self).into()
243+
}
244+
}
245+
246+
impl ToPyObject for bytecode::CodeObject {
247+
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
248+
vm.ctx.new_code(self).into()
249+
}
250+
}
251+
245252
pub fn init(ctx: &Context) {
246253
PyRef::<PyCode>::extend_class(ctx, &ctx.types.code_type);
247254
}

vm/src/vm/compile.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
builtins::PyCode,
33
compile::{self, CompileError, CompileOpts},
4-
PyPayload, PyRef, VirtualMachine,
4+
PyRef, VirtualMachine,
55
};
66

77
impl VirtualMachine {
@@ -29,7 +29,6 @@ impl VirtualMachine {
2929
source_path: String,
3030
opts: CompileOpts,
3131
) -> Result<PyRef<PyCode>, CompileError> {
32-
compile::compile(source, mode, source_path, opts)
33-
.map(|code| PyCode::new(self.ctx.new_code_object(code)).into_ref(self))
32+
compile::compile(source, mode, source_path, opts).map(|code| self.ctx.new_code(code))
3433
}
3534
}

vm/src/vm/context.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,8 @@ impl Context {
322322
}
323323

324324
pub fn new_code(&self, code: impl code::IntoCodeObject) -> PyRef<PyCode> {
325-
PyCode::new_ref(code.into_codeobj(self), self)
326-
}
327-
328-
pub fn new_code_object(&self, code: crate::bytecode::CodeObject) -> code::CodeObject {
329-
code.map_bag(code::PyObjBag(self))
325+
let code = code.into_codeobj(self);
326+
PyRef::new_ref(PyCode { code }, self.types.code_type.clone(), None)
330327
}
331328
}
332329

0 commit comments

Comments
 (0)