@@ -7,11 +7,12 @@ use std::ops::Deref;
77
88use super :: objtype:: PyClassRef ;
99use crate :: bytecode;
10- use crate :: pyobject:: { IdProtocol , PyContext , PyObjectRef , PyRef , PyResult , PyValue } ;
10+ use crate :: pyobject:: { IdProtocol , PyClassImpl , PyContext , PyObjectRef , PyRef , PyResult , PyValue } ;
1111use crate :: vm:: VirtualMachine ;
1212
1313pub type PyCodeRef = PyRef < PyCode > ;
1414
15+ #[ pyclass]
1516pub struct PyCode {
1617 pub code : bytecode:: CodeObject ,
1718}
@@ -41,12 +42,15 @@ impl PyValue for PyCode {
4142 }
4243}
4344
45+ #[ pyimpl]
4446impl PyCodeRef {
47+ #[ pyslot]
4548 #[ allow( clippy:: new_ret_no_self) ]
4649 fn new ( _cls : PyClassRef , vm : & VirtualMachine ) -> PyResult {
4750 Err ( vm. new_type_error ( "Cannot directly create code object" . to_owned ( ) ) )
4851 }
4952
53+ #[ pymethod( magic) ]
5054 fn repr ( self ) -> String {
5155 let code = & self . code ;
5256 format ! (
@@ -58,22 +62,32 @@ impl PyCodeRef {
5862 )
5963 }
6064
65+ #[ pyproperty]
66+ fn co_posonlyargcount ( self ) -> usize {
67+ self . code . posonlyarg_count
68+ }
69+
70+ #[ pyproperty]
6171 fn co_argcount ( self ) -> usize {
6272 self . code . arg_names . len ( )
6373 }
6474
75+ #[ pyproperty]
6576 fn co_filename ( self ) -> String {
6677 self . code . source_path . clone ( )
6778 }
6879
80+ #[ pyproperty]
6981 fn co_firstlineno ( self ) -> usize {
7082 self . code . first_line_number
7183 }
7284
85+ #[ pyproperty]
7386 fn co_kwonlyargcount ( self ) -> usize {
7487 self . code . kwonlyarg_names . len ( )
7588 }
7689
90+ #[ pyproperty]
7791 fn co_consts ( self , vm : & VirtualMachine ) -> PyObjectRef {
7892 let consts = self
7993 . code
@@ -83,26 +97,17 @@ impl PyCodeRef {
8397 vm. ctx . new_tuple ( consts)
8498 }
8599
100+ #[ pyproperty]
86101 fn co_name ( self ) -> String {
87102 self . code . obj_name . clone ( )
88103 }
89104
105+ #[ pyproperty]
90106 fn co_flags ( self ) -> u8 {
91107 self . code . flags . bits ( )
92108 }
93109}
94110
95111pub fn init ( ctx : & PyContext ) {
96- extend_class ! ( ctx, & ctx. types. code_type, {
97- ( slot new) => PyCodeRef :: new,
98- "__repr__" => ctx. new_method( PyCodeRef :: repr) ,
99-
100- "co_argcount" => ctx. new_readonly_getset( "co_argcount" , PyCodeRef :: co_argcount) ,
101- "co_consts" => ctx. new_readonly_getset( "co_consts" , PyCodeRef :: co_consts) ,
102- "co_filename" => ctx. new_readonly_getset( "co_filename" , PyCodeRef :: co_filename) ,
103- "co_firstlineno" => ctx. new_readonly_getset( "co_firstlineno" , PyCodeRef :: co_firstlineno) ,
104- "co_kwonlyargcount" => ctx. new_readonly_getset( "co_kwonlyargcount" , PyCodeRef :: co_kwonlyargcount) ,
105- "co_name" => ctx. new_readonly_getset( "co_name" , PyCodeRef :: co_name) ,
106- "co_flags" => ctx. new_readonly_getset( "co_flags" , PyCodeRef :: co_flags) ,
107- } ) ;
112+ PyCodeRef :: extend_class ( ctx, & ctx. types . code_type ) ;
108113}
0 commit comments