@@ -6,6 +6,7 @@ use super::{PyStrRef, PyTupleRef, PyTypeRef};
66use 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>;
112113pub trait IntoCodeObject {
113114 fn into_codeobj ( self , ctx : & Context ) -> CodeObject ;
114115}
116+
115117impl 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
152147impl 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+
245252pub fn init ( ctx : & Context ) {
246253 PyRef :: < PyCode > :: extend_class ( ctx, & ctx. types . code_type ) ;
247254}
0 commit comments