8000 rustpython_vm::import::import_source (#5214) · moreal/RustPython@2475728 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2475728

Browse files
authored
rustpython_vm::import::import_source (RustPython#5214)
* rustpython_vm::import::import_source * always print exceptions when panic by expect_pyresult if users want simple panic, Result::expect could be used.
1 parent 5a5ac35 commit 2475728

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

vm/src/import.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,32 @@ pub fn import_file(
106106
vm: &VirtualMachine,
107107
module_name: &str,
108108
file_path: String,
109-
content: String,
109+
content: &str,
110110
) -> PyResult {
111111
let code = vm
112112
.compile_with_opts(
113-
&content,
113+
content,
114114
crate::compiler::Mode::Exec,
115115
file_path,
116116
vm.compile_opts(),
117117
)
118-
.map_err(|err| vm.new_syntax_error(&err, Some(&content)))?;
118+
.map_err(|err| vm.new_syntax_error(&err, Some(content)))?;
119119
import_codeobj(vm, module_name, code, true)
120120
}
121121

122+
#[cfg(feature = "rustpython-compiler")]
123+
pub fn import_source(vm: &VirtualMachine, module_name: &str, content: &str) -> PyResult {
124+
let code = vm
125+
.compile_with_opts(
126+
content,
127+
crate::compiler::Mode::Exec,
128+
"<source>".to_owned(),
129+
vm.compile_opts(),
130+
)
131+
.map_err(|err| vm.new_syntax_error(&err, Some(content)))?;
132+
import_codeobj(vm, module_name, code, false)
133+
}
134+
122135
pub fn import_codeobj(
123136
vm: &VirtualMachine,
124137
module_name: &str,

vm/src/vm/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ impl VirtualMachine {
517517
/// Roughly equivalent to `import module_name` or `import top.submodule`.
518518
///
519519
/// See also [`import_from`] for more advanced import.
520+
/// See also [`rustpython_vm::import::import_source`] and other primitive import functions.
520521
#[inline]
521522
pub fn import<'a>(&self, module_name: impl AsPyStr<'a>, level: usize) -> PyResult {
522523
8000 let module_name = module_name.as_pystr(&self.ctx);

wasm/lib/src/browser_module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod _browser {
1111
class::PyClassImpl,
1212
convert::ToPyObject,
1313
function::{ArgCallable, OptionalArg},
14-
import::import_file,
14+
import::import_source,
1515
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
1616
};
1717
use wasm_bindgen::{prelude::*, JsCast};
@@ -245,7 +245,7 @@ mod _browser {
245245
.expect("that the vm is valid when the promise resolves");
246246
stored_vm.interp.enter(move |vm| {
247247
let resp_text = text.as_string().unwrap();
248-
let res = import_file(vm, module.as_str(), "WEB".to_owned(), resp_text);
248+
let res = import_source(vm, module.as_str(), &resp_text);
249249
match res {
250250
Ok(_) => Ok(JsValue::null()),
251251
Err(err) => Err(convert::py_err_to_js_err(vm, &err)),

0 commit comments

Comments
 (0)
0