forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoduleobject.rs
More file actions
42 lines (38 loc) · 1.65 KB
/
Copy pathmoduleobject.rs
File metadata and controls
42 lines (38 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::PyObject;
use crate::object::define_py_check;
use crate::pystate::with_vm;
use rustpython_vm::builtins::{PyModule, PyStr};
define_py_check!(fn PyModule_Check, types.module_type);
define_py_check!(exact fn PyModule_CheckExact, types.module_type);
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyModule_GetNameObject(module: *mut PyObject) -> *mut PyObject {
with_vm(|vm| {
let module = unsafe { &*module }.try_downcast_ref::<PyModule>(vm)?;
let dict = module.dict();
let name = dict
.get_item_opt(rustpython_vm::identifier!(vm, __name__), vm)?
.and_then(|obj| obj.downcast_ref::<PyStr>().map(ToOwned::to_owned));
name.ok_or_else(|| vm.new_system_error("nameless module"))
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyModule_GetFilenameObject(module: *mut PyObject) -> *mut PyObject {
with_vm(|vm| {
let module = unsafe { &*module }.try_downcast_ref::<PyModule>(vm)?;
let dict = module.dict();
let filename = dict
.get_item_opt(rustpython_vm::identifier!(vm, __file__), vm)?
.and_then(|obj| obj.downcast_ref::<PyStr>().map(ToOwned::to_owned));
filename.ok_or_else(|| vm.new_system_error("module filename missing"))
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyModule_NewObject(name: *mut PyObject) -> *mut PyObject {
with_vm(|vm| -> rustpython_vm::PyResult<_> {
let name = unsafe { &*name }.try_downcast_ref::<PyStr>(vm)?;
let name = name
.to_str()
.ok_or_else(|| vm.new_system_error("module name must be valid UTF-8"))?;
Ok(vm.new_module(name, vm.ctx.new_dict(), None))
})
}