File tree Expand file tree Collapse file tree 2 files changed +15
-5
lines changed
example_projects/pyo3_embed/src Expand file tree Collapse file tree 2 files changed +15
-5
lines changed Original file line number Diff line number Diff line change 1- use core:: ptr;
2-
31use crate :: PyObject ;
2+ use crate :: pystate:: with_vm;
3+ use rustpython_vm:: builtins:: PyStr ;
44
55#[ unsafe( no_mangle) ]
6- pub extern "C" fn PyImport_Import ( _name : * mut PyObject ) -> * mut PyObject {
7- crate :: log_stub ( "PyImport_Import" ) ;
8- ptr:: null_mut ( )
6+ pub extern "C" fn PyImport_Import ( name : * mut PyObject ) -> * mut PyObject {
7+ with_vm ( |vm| {
8+ let name = unsafe { ( & * name) . downcast_unchecked_ref :: < PyStr > ( ) } ;
9+ vm. import ( name, 0 ) . map_or_else (
10+ |err| {
11+ vm. push_exception ( Some ( err) ) ;
12+ std:: ptr:: null_mut ( )
13+ } ,
14+ |module| module. into_raw ( ) . as_ptr ( ) ,
15+ )
16+ } )
917}
Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ fn main() {
3939 PyTypeError :: new_err ( "This is a type error" ) . restore ( py) ;
4040 assert ! ( PyErr :: take( py) . is_some( ) ) ;
4141
42+ py. import ( "sys" ) ?;
43+
4244 PyResult :: Ok ( ( ) )
4345 } )
4446 . unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments