mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
18
src/main.rs
18
src/main.rs
@@ -10,8 +10,15 @@ extern crate rustyline;
|
||||
use clap::{App, Arg};
|
||||
use rustpython_parser::error::ParseError;
|
||||
use rustpython_vm::{
|
||||
compile, error::CompileError, error::CompileErrorType, frame::Scope, import, obj::objstr,
|
||||
print_exception, pyobject::PyResult, util, VirtualMachine,
|
||||
compile,
|
||||
error::CompileError,
|
||||
error::CompileErrorType,
|
||||
frame::Scope,
|
||||
import,
|
||||
obj::objstr,
|
||||
print_exception,
|
||||
pyobject::{ItemProtocol, PyResult},
|
||||
util, VirtualMachine,
|
||||
};
|
||||
use rustyline::{error::ReadlineError, Editor};
|
||||
use std::path::PathBuf;
|
||||
@@ -65,11 +72,12 @@ fn main() {
|
||||
}
|
||||
|
||||
fn _run_string(vm: &VirtualMachine, source: &str, source_path: String) -> PyResult {
|
||||
let code_obj = compile::compile(vm, source, &compile::Mode::Exec, source_path)
|
||||
let code_obj = compile::compile(vm, source, &compile::Mode::Exec, source_path.clone())
|
||||
.map_err(|err| vm.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj.borrow());
|
||||
let vars = vm.ctx.new_scope(); // Keep track of local variables
|
||||
vm.run_code_obj(code_obj, vars)
|
||||
let attrs = vm.ctx.new_dict();
|
||||
attrs.set_item("__file__", vm.new_str(source_path), vm)?;
|
||||
vm.run_code_obj(code_obj, Scope::new(None, attrs))
|
||||
}
|
||||
|
||||
fn handle_exception(vm: &VirtualMachine, result: PyResult) {
|
||||
|
||||
7
tests/snippets/builtin_file.py
Normal file
7
tests/snippets/builtin_file.py
Normal file
@@ -0,0 +1,7 @@
|
||||
import os
|
||||
|
||||
from import_file import import_file
|
||||
|
||||
import_file()
|
||||
|
||||
assert os.path.basename(__file__) == "builtin_file.py"
|
||||
4
tests/snippets/import_file.py
Normal file
4
tests/snippets/import_file.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import os
|
||||
|
||||
def import_file():
|
||||
assert os.path.basename(__file__) == "import_file.py"
|
||||
@@ -48,12 +48,13 @@ pub fn import_file(
|
||||
content: String,
|
||||
) -> PyResult {
|
||||
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules").unwrap();
|
||||
let code_obj = compile::compile(vm, &content, &compile::Mode::Exec, file_path)
|
||||
let code_obj = compile::compile(vm, &content, &compile::Mode::Exec, file_path.clone())
|
||||
.map_err(|err| vm.new_syntax_error(&err))?;
|
||||
// trace!("Code object: {:?}", code_obj);
|
||||
|
||||
let attrs = vm.ctx.new_dict();
|
||||
attrs.set_item("__name__", vm.new_str(module_name.to_string()), vm)?;
|
||||
attrs.set_item("__file__", vm.new_str(file_path), vm)?;
|
||||
let module = vm.ctx.new_module(module_name, attrs.clone());
|
||||
|
||||
// Store module in cache to prevent infinite loop with mutual importing libs:
|
||||
|
||||
Reference in New Issue
Block a user