mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge branch 'master' into coolreader18/immutable-vm-ref
This commit is contained in:
14
src/main.rs
14
src/main.rs
@@ -10,14 +10,8 @@ extern crate rustyline;
|
||||
use clap::{App, Arg};
|
||||
use rustpython_parser::error::ParseError;
|
||||
use rustpython_vm::{
|
||||
compile,
|
||||
error::CompileError,
|
||||
frame::Scope,
|
||||
import,
|
||||
obj::objstr,
|
||||
print_exception,
|
||||
pyobject::{AttributeProtocol, PyResult},
|
||||
util, VirtualMachine,
|
||||
compile, error::CompileError, frame::Scope, import, obj::objstr, print_exception,
|
||||
pyobject::PyResult, util, VirtualMachine,
|
||||
};
|
||||
use rustyline::{error::ReadlineError, Editor};
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -176,8 +170,8 @@ fn run_shell(vm: &VirtualMachine) -> PyResult {
|
||||
println!("No previous history.");
|
||||
}
|
||||
|
||||
let ps1 = &objstr::get_value(&vm.sys_module.get_attr("ps1").unwrap());
|
||||
let ps2 = &objstr::get_value(&vm.sys_module.get_attr("ps2").unwrap());
|
||||
let ps1 = &objstr::get_value(&vm.get_attribute(vm.sys_module.clone(), "ps1").unwrap());
|
||||
let ps2 = &objstr::get_value(&vm.get_attribute(vm.sys_module.clone(), "ps2").unwrap());
|
||||
let mut prompt = ps1;
|
||||
|
||||
loop {
|
||||
|
||||
@@ -21,8 +21,8 @@ use crate::obj::objslice::PySlice;
|
||||
use crate::obj::objstr;
|
||||
use crate::obj::objtype;
|
||||
use crate::pyobject::{
|
||||
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyObjectRef, PyResult, PyValue,
|
||||
TryFromObject, TypeProtocol,
|
||||
DictProtocol, IdProtocol, PyContext, PyObjectRef, PyResult, PyValue, TryFromObject,
|
||||
TypeProtocol,
|
||||
};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
@@ -805,13 +805,10 @@ impl Frame {
|
||||
// If we're importing a symbol, look it up and use it, otherwise construct a module and return
|
||||
// that
|
||||
let obj = match symbol {
|
||||
Some(symbol) => module.get_attr(symbol).map_or_else(
|
||||
|| {
|
||||
let import_error = vm.context().exceptions.import_error.clone();
|
||||
Err(vm.new_exception(import_error, format!("cannot import name '{}'", symbol)))
|
||||
},
|
||||
Ok,
|
||||
),
|
||||
Some(symbol) => vm.get_attribute(module, symbol.as_str()).map_err(|_| {
|
||||
let import_error = vm.context().exceptions.import_error.clone();
|
||||
vm.new_exception(import_error, format!("cannot import name '{}'", symbol))
|
||||
}),
|
||||
None => Ok(module),
|
||||
};
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ use std::path::PathBuf;
|
||||
use crate::compile;
|
||||
use crate::frame::Scope;
|
||||
use crate::obj::{objsequence, objstr};
|
||||
use crate::pyobject::{AttributeProtocol, DictProtocol, PyResult};
|
||||
use crate::pyobject::{DictProtocol, PyResult};
|
||||
use crate::util;
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
@@ -46,7 +46,7 @@ fn import_uncached_module(vm: &VirtualMachine, current_path: PathBuf, module: &s
|
||||
|
||||
pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &str) -> PyResult {
|
||||
// First, see if we already loaded the module:
|
||||
let sys_modules = vm.sys_module.get_attr("modules").unwrap();
|
||||
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules")?;
|
||||
if let Some(module) = sys_modules.get_item(module_name) {
|
||||
return Ok(module);
|
||||
}
|
||||
@@ -55,8 +55,12 @@ pub fn import_module(vm: &VirtualMachine, current_path: PathBuf, module_name: &s
|
||||
Ok(module)
|
||||
}
|
||||
|
||||
fn find_source(vm: &VirtualMachine, current_path: PathBuf, name: &str) -> Result<PathBuf, String> {
|
||||
let sys_path = vm.sys_module.get_attr("path").unwrap();
|
||||
fn find_source(
|
||||
vm: &mut VirtualMachine,
|
||||
current_path: PathBuf,
|
||||
name: &str,
|
||||
) -> Result<PathBuf, String> {
|
||||
let sys_path = vm.get_attribute(vm.sys_module.clone(), "path").unwrap();
|
||||
let mut paths: Vec<PathBuf> = objsequence::get_elements(&sys_path)
|
||||
.iter()
|
||||
.map(|item| PathBuf::from(objstr::get_value(item)))
|
||||
|
||||
@@ -1,43 +1,42 @@
|
||||
use crate::function::PyFuncArgs;
|
||||
use crate::pyobject::{AttributeProtocol, PyContext, PyResult, TypeProtocol};
|
||||
use super::objtype::PyClassRef;
|
||||
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PyClassMethod {
|
||||
pub callable: PyObjectRef,
|
||||
}
|
||||
pub type PyClassMethodRef = PyRef<PyClassMethod>;
|
||||
|
||||
impl PyValue for PyClassMethod {
|
||||
fn class(vm: &mut VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.classmethod_type()
|
||||
}
|
||||
}
|
||||
|
||||
impl PyClassMethodRef {
|
||||
fn new(
|
||||
cls: PyClassRef,
|
||||
callable: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyClassMethodRef> {
|
||||
PyClassMethod {
|
||||
callable: callable.clone(),
|
||||
}
|
||||
.into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
fn get(self, _inst: PyObjectRef, owner: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
Ok(vm
|
||||
.ctx
|
||||
.new_bound_method(self.callable.clone(), owner.clone()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(context: &PyContext) {
|
||||
let classmethod_type = &context.classmethod_type;
|
||||
extend_class!(context, classmethod_type, {
|
||||
"__get__" => context.new_rustfunc(classmethod_get),
|
||||
"__new__" => context.new_rustfunc(classmethod_new)
|
||||
"__get__" => context.new_rustfunc(PyClassMethodRef::get),
|
||||
"__new__" => context.new_rustfunc(PyClassMethodRef::new)
|
||||
});
|
||||
}
|
||||
|
||||
fn classmethod_get(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
trace!("classmethod.__get__ {:?}", args.args);
|
||||
arg_check!(
|
||||
vm,
|
||||
args,
|
||||
required = [
|
||||
(cls, Some(vm.ctx.classmethod_type())),
|
||||
(_inst, None),
|
||||
(owner, None)
|
||||
]
|
||||
);
|
||||
match cls.get_attr("function") {
|
||||
Some(function) => {
|
||||
let py_obj = owner.clone();
|
||||
let py_method = vm.ctx.new_bound_method(function, py_obj);
|
||||
Ok(py_method)
|
||||
}
|
||||
None => Err(vm.new_attribute_error(
|
||||
"Attribute Error: classmethod must have 'function' attribute".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn classmethod_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
trace!("classmethod.__new__ {:?}", args.args);
|
||||
arg_check!(vm, args, required = [(cls, None), (callable, None)]);
|
||||
|
||||
let py_obj = vm.ctx.new_instance(cls.clone(), None);
|
||||
vm.ctx.set_attr(&py_obj, "function", callable.clone());
|
||||
Ok(py_obj)
|
||||
}
|
||||
|
||||
@@ -1,40 +1,40 @@
|
||||
use crate::function::PyFuncArgs;
|
||||
use crate::pyobject::{AttributeProtocol, PyContext, PyResult, TypeProtocol};
|
||||
use super::objtype::PyClassRef;
|
||||
use crate::pyobject::{PyContext, PyObjectRef, PyRef, PyResult, PyValue};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PyStaticMethod {
|
||||
pub callable: PyObjectRef,
|
||||
}
|
||||
pub type PyStaticMethodRef = PyRef<PyStaticMethod>;
|
||||
|
||||
impl PyValue for PyStaticMethod {
|
||||
fn class(vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.staticmethod_type()
|
||||
}
|
||||
}
|
||||
|
||||
impl PyStaticMethodRef {
|
||||
fn new(
|
||||
cls: PyClassRef,
|
||||
callable: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyStaticMethodRef> {
|
||||
PyStaticMethod {
|
||||
callable: callable.clone(),
|
||||
}
|
||||
.into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
fn get(self, _inst: PyObjectRef, _owner: PyObjectRef, _vm: &VirtualMachine) -> PyResult {
|
||||
Ok(self.callable.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(context: &PyContext) {
|
||||
let staticmethod_type = &context.staticmethod_type;
|
||||
extend_class!(context, staticmethod_type, {
|
||||
"__get__" => context.new_rustfunc(staticmethod_get),
|
||||
"__new__" => context.new_rustfunc(staticmethod_new),
|
||||
"__get__" => context.new_rustfunc(PyStaticMethodRef::get),
|
||||
"__new__" => context.new_rustfunc(PyStaticMethodRef::new),
|
||||
});
|
||||
}
|
||||
|
||||
// `staticmethod` methods.
|
||||
fn staticmethod_get(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
trace!("staticmethod.__get__ {:?}", args.args);
|
||||
arg_check!(
|
||||
vm,
|
||||
args,
|
||||
required = [
|
||||
(cls, Some(vm.ctx.staticmethod_type())),
|
||||
(_inst, None),
|
||||
(_owner, None)
|
||||
]
|
||||
);
|
||||
match cls.get_attr("function") {
|
||||
Some(function) => Ok(function),
|
||||
None => Err(vm.new_attribute_error(
|
||||
"Attribute Error: staticmethod must have 'function' attribute".to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn staticmethod_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
trace!("staticmethod.__new__ {:?}", args.args);
|
||||
arg_check!(vm, args, required = [(cls, None), (callable, None)]);
|
||||
|
||||
let py_obj = vm.ctx.new_instance(cls.clone(), None);
|
||||
vm.ctx.set_attr(&py_obj, "function", callable.clone());
|
||||
Ok(py_obj)
|
||||
}
|
||||
|
||||
@@ -20,8 +20,7 @@ use crate::obj::objbytes;
|
||||
use crate::obj::objint;
|
||||
use crate::obj::objstr;
|
||||
use crate::pyobject::{
|
||||
AttributeProtocol, BufferProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
TypeProtocol,
|
||||
BufferProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
|
||||
};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
@@ -165,7 +164,7 @@ fn file_io_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
|
||||
fn file_io_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
arg_check!(vm, args, required = [(file_io, None)]);
|
||||
let py_name = file_io.get_attr("name").unwrap();
|
||||
let py_name = vm.get_attribute(file_io.clone(), "name")?;
|
||||
let f = match File::open(objstr::get_value(&py_name)) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => Err(vm.new_type_error("Error opening file".to_string())),
|
||||
@@ -200,7 +199,7 @@ fn file_io_readinto(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
let py_length = vm.call_method(obj, "__len__", PyFuncArgs::default())?;
|
||||
let length = objint::get_value(&py_length).to_u64().unwrap();
|
||||
|
||||
let file_no = file_io.get_attr("fileno").unwrap();
|
||||
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
|
||||
let raw_fd = objint::get_value(&file_no).to_i64().unwrap();
|
||||
|
||||
//extract unix file descriptor.
|
||||
@@ -230,7 +229,7 @@ fn file_io_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
required = [(file_io, None), (obj, Some(vm.ctx.bytes_type()))]
|
||||
);
|
||||
|
||||
let file_no = file_io.get_attr("fileno").unwrap();
|
||||
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
|
||||
let raw_fd = objint::get_value(&file_no).to_i64().unwrap();
|
||||
|
||||
//unsafe block - creates file handle from the UNIX file descriptor
|
||||
|
||||
@@ -12,14 +12,12 @@ use regex::{Match, Regex};
|
||||
use crate::function::PyFuncArgs;
|
||||
use crate::import;
|
||||
use crate::obj::objstr;
|
||||
use crate::pyobject::{
|
||||
AttributeProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol,
|
||||
};
|
||||
use crate::pyobject::{PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
impl PyValue for Regex {
|
||||
fn class(vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.import("re").unwrap().get_attr("Pattern").unwrap()
|
||||
vm.class("re", "Pattern")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ fn sys_getsizeof(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
Ok(vm.ctx.new_int(size))
|
||||
}
|
||||
|
||||
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
|
||||
pub fn make_module(ctx: &PyContext, builtins: PyObjectRef) -> PyObjectRef {
|
||||
let path_list = match env::var_os("PYTHONPATH") {
|
||||
Some(paths) => env::split_paths(&paths)
|
||||
.map(|path| {
|
||||
@@ -156,6 +156,7 @@ settrace() -- set the global debug tracing function
|
||||
});
|
||||
|
||||
modules.set_item(&ctx, sys_name, sys_mod.clone());
|
||||
modules.set_item(&ctx, "builtins", builtins);
|
||||
ctx.set_attr(&sys_mod, "modules", modules);
|
||||
|
||||
sys_mod
|
||||
|
||||
15
vm/src/vm.rs
15
vm/src/vm.rs
@@ -58,11 +58,7 @@ impl VirtualMachine {
|
||||
|
||||
// Hard-core modules:
|
||||
let builtins = builtins::make_module(&ctx);
|
||||
let sysmod = sysmodule::make_module(&ctx);
|
||||
|
||||
// Add builtins as builtins module:
|
||||
let modules = sysmod.get_attr("modules").unwrap();
|
||||
modules.set_item(&ctx, "builtins", builtins.clone());
|
||||
let sysmod = sysmodule::make_module(&ctx, builtins.clone());
|
||||
|
||||
let stdlib_inits = RefCell::new(stdlib::get_module_inits());
|
||||
VirtualMachine {
|
||||
@@ -109,10 +105,11 @@ impl VirtualMachine {
|
||||
}
|
||||
|
||||
pub fn class(&self, module: &str, class: &str) -> PyObjectRef {
|
||||
self.import(module)
|
||||
.unwrap_or_else(|_| panic!("unable to import {}", module))
|
||||
.get_attr(class)
|
||||
.unwrap_or_else(|| panic!("module {} has no class {}", module, class))
|
||||
let module = self
|
||||
.import(module)
|
||||
.unwrap_or_else(|_| panic!("unable to import {}", module));
|
||||
self.get_attribute(module.clone(), class)
|
||||
.unwrap_or_else(|_| panic!("module {} has no class {}", module, class))
|
||||
}
|
||||
|
||||
/// Create a new python string object.
|
||||
|
||||
Reference in New Issue
Block a user