File tree 2 files changed +26
-0
lines changed 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ mod json;
16
16
mod keyword;
17
17
mod marshal;
18
18
mod math;
19
+ mod operator;
19
20
mod platform;
20
21
mod pystruct;
21
22
mod random;
@@ -74,6 +75,7 @@ pub fn get_module_inits() -> HashMap<String, StdlibInitFunc> {
74
75
"json" . to_string( ) => Box :: new( json:: make_module) ,
75
76
"marshal" . to_string( ) => Box :: new( marshal:: make_module) ,
76
77
"math" . to_string( ) => Box :: new( math:: make_module) ,
78
+ "_operator" . to_string( ) => Box :: new( operator:: make_module) ,
77
79
"platform" . to_string( ) => Box :: new( platform:: make_module) ,
78
80
"regex_crate" . to_string( ) => Box :: new( re:: make_module) ,
79
81
"random" . to_string( ) => Box :: new( random:: make_module) ,
Original file line number Diff line number Diff line change
1
+ use crate :: function:: OptionalArg ;
2
+ use crate :: obj:: { objiter, objtype} ;
3
+ use crate :: pyobject:: { PyObjectRef , PyResult , TypeProtocol } ;
4
+ use crate :: VirtualMachine ;
5
+
6
+ fn operator_length_hint ( obj : PyObjectRef , default : OptionalArg , vm : & VirtualMachine ) -> PyResult {
7
+ let default = default. unwrap_or_else ( || vm. new_int ( 0 ) ) ;
8
+ if !objtype:: isinstance ( & default, & vm. ctx . types . int_type ) {
9
+ return Err ( vm. new_type_error ( format ! (
10
+ "'{}' type cannot be interpreted as an integer" ,
11
+ default . class( ) . name
12
+ ) ) ) ;
13
+ }
14
+ let hint = objiter:: length_hint ( vm, obj) ?
15
+ . map ( |i| vm. new_int ( i) )
16
+ . unwrap_or ( default) ;
17
+ Ok ( hint)
18
+ }
19
+
20
+ pub fn make_module ( vm : & VirtualMachine ) -> PyObjectRef {
21
+ py_module ! ( vm, "_operator" , {
22
+ "length_hint" => vm. ctx. new_rustfunc( operator_length_hint) ,
23
+ } )
24
+ }
You can’t perform that action at this time.
0 commit comments