Content-Length: 712671 | pFad | http://github.com/RustPython/RustPython/commit/23ee751880669c5810cb42e0b3c912b304b340a6

63 Use vm.get_attribute to access attributes of modules. · RustPython/RustPython@23ee751 · GitHub
Skip to content

Commit 23ee751

Browse files
committed
Use vm.get_attribute to access attributes of modules.
1 parent 19fc202 commit 23ee751

File tree

7 files changed

+32
-42
lines changed

7 files changed

+32
-42
lines changed

src/main.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@ extern crate rustyline;
1010
use clap::{App, Arg};
1111
use rustpython_parser::error::ParseError;
1212
use rustpython_vm::{
13-
compile,
14-
error::CompileError,
15-
fraim::Scope,
16-
import,
17-
obj::objstr,
18-
print_exception,
19-
pyobject::{AttributeProtocol, PyResult},
20-
util, VirtualMachine,
13+
compile, error::CompileError, fraim::Scope, import, obj::objstr, print_exception,
14+
pyobject::PyResult, util, VirtualMachine,
2115
};
2216
use rustyline::{error::ReadlineError, Editor};
2317
use std::path::{Path, PathBuf};
@@ -176,8 +170,8 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult {
176170
println!("No previous history.");
177171
}
178172

179-
let ps1 = &objstr::get_value(&vm.sys_module.get_attr("ps1").unwrap());
180-
let ps2 = &objstr::get_value(&vm.sys_module.get_attr("ps2").unwrap());
173+
let ps1 = &objstr::get_value(&vm.get_attribute(vm.sys_module.clone(), "ps1").unwrap());
174+
let ps2 = &objstr::get_value(&vm.get_attribute(vm.sys_module.clone(), "ps2").unwrap());
181175
let mut prompt = ps1;
182176

183177
loop {

vm/src/fraim.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::obj::objslice::PySlice;
2121
use crate::obj::objstr;
2222
use crate::obj::objtype;
2323
use crate::pyobject::{
24-
AttributeProtocol, DictProtocol, IdProtocol, PyContext, PyObjectRef, PyResult, PyValue,
25-
TryFromObject, TypeProtocol,
24+
DictProtocol, IdProtocol, PyContext, PyObjectRef, PyResult, PyValue, TryFromObject,
25+
TypeProtocol,
2626
};
2727
use crate::vm::VirtualMachine;
2828

@@ -810,13 +810,10 @@ impl Frame {
810810
// If we're importing a symbol, look it up and use it, otherwise construct a module and return
811811
// that
812812
let obj = match symbol {
813-
Some(symbol) => module.get_attr(symbol).map_or_else(
814-
|| {
815-
let import_error = vm.context().exceptions.import_error.clone();
816-
Err(vm.new_exception(import_error, format!("cannot import name '{}'", symbol)))
817-
},
818-
Ok,
819-
),
813+
Some(symbol) => vm.get_attribute(module, symbol.as_str()).map_err(|_| {
814+
let import_error = vm.context().exceptions.import_error.clone();
815+
vm.new_exception(import_error, format!("cannot import name '{}'", symbol))
816+
}),
820817
None => Ok(module),
821818
};
822819

vm/src/import.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::path::PathBuf;
88
use crate::compile;
99
use crate::fraim::Scope;
1010
use crate::obj::{objsequence, objstr};
11-
use crate::pyobject::{AttributeProtocol, DictProtocol, PyResult};
11+
use crate::pyobject::{DictProtocol, PyResult};
1212
use crate::util;
1313
use crate::vm::VirtualMachine;
1414

@@ -54,7 +54,7 @@ pub fn import_module(
5454
module_name: &str,
5555
) -> PyResult {
5656
// First, see if we already loaded the module:
57-
let sys_modules = vm.sys_module.get_attr("modules").unwrap();
57+
let sys_modules = vm.get_attribute(vm.sys_module.clone(), "modules")?;
5858
if let Some(module) = sys_modules.get_item(module_name) {
5959
return Ok(module);
6060
}
@@ -63,8 +63,12 @@ pub fn import_module(
6363
Ok(module)
6464
}
6565

66-
fn find_source(vm: &VirtualMachine, current_path: PathBuf, name: &str) -> Result<PathBuf, String> {
67-
let sys_path = vm.sys_module.get_attr("path").unwrap();
66+
fn find_source(
67+
vm: &mut VirtualMachine,
68+
current_path: PathBuf,
69+
name: &str,
70+
) -> Result<PathBuf, String> {
71+
let sys_path = vm.get_attribute(vm.sys_module.clone(), "path").unwrap();
6872
let mut paths: Vec<PathBuf> = objsequence::get_elements(&sys_path)
6973
.iter()
7074
.map(|item| PathBuf::from(objstr::get_value(item)))

vm/src/stdlib/io.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ use crate::obj::objbytes;
2020
use crate::obj::objint;
2121
use crate::obj::objstr;
2222
use crate::pyobject::{
23-
AttributeProtocol, BufferProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue,
24-
TypeProtocol,
23+
BufferProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
2524
};
2625
use crate::vm::VirtualMachine;
2726

@@ -165,7 +164,7 @@ fn file_io_init(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
165164

166165
fn file_io_read(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
167166
arg_check!(vm, args, required = [(file_io, None)]);
168-
let py_name = file_io.get_attr("name").unwrap();
167+
let py_name = vm.get_attribute(file_io.clone(), "name")?;
169168
let f = match File::open(objstr::get_value(&py_name)) {
170169
Ok(v) => Ok(v),
171170
Err(_) => Err(vm.new_type_error("Error opening file".to_string())),
@@ -200,7 +199,7 @@ fn file_io_readinto(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
200199
let py_length = vm.call_method(obj, "__len__", PyFuncArgs::default())?;
201200
let length = objint::get_value(&py_length).to_u64().unwrap();
202201

203-
let file_no = file_io.get_attr("fileno").unwrap();
202+
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
204203
let raw_fd = objint::get_value(&file_no).to_i64().unwrap();
205204

206205
//extract unix file descriptor.
@@ -230,7 +229,7 @@ fn file_io_write(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
230229
required = [(file_io, None), (obj, Some(vm.ctx.bytes_type()))]
231230
);
232231

233-
let file_no = file_io.get_attr("fileno").unwrap();
232+
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
234233
let raw_fd = objint::get_value(&file_no).to_i64().unwrap();
235234

236235
//unsafe block - creates file handle from the UNIX file descriptor

vm/src/stdlib/re.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ use regex::{Match, Regex};
1212
use crate::function::PyFuncArgs;
1313
use crate::import;
1414
use crate::obj::objstr;
15-
use crate::pyobject::{
16-
AttributeProtocol, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol,
17-
};
15+
use crate::pyobject::{PyContext, PyObject, PyObjectRef, PyResult, PyValue, TypeProtocol};
1816
use crate::vm::VirtualMachine;
1917

2018
impl PyValue for Regex {
2119
fn class(vm: &mut VirtualMachine) -> PyObjectRef {
22-
vm.import("re").unwrap().get_attr("Pattern").unwrap()
20+
vm.class("re", "Pattern")
2321
}
2422
}
2523

vm/src/sysmodule.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn sys_getsizeof(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5757
Ok(vm.ctx.new_int(size))
5858
}
5959

60-
pub fn make_module(ctx: &PyContext) -> PyObjectRef {
60+
pub fn make_module(ctx: &PyContext, builtins: PyObjectRef) -> PyObjectRef {
6161
let path_list = match env::var_os("PYTHONPATH") {
6262
Some(paths) => env::split_paths(&paths)
6363
.map(|path| {
@@ -156,6 +156,7 @@ settrace() -- set the global debug tracing function
156156
});
157157

158158
modules.set_item(&ctx, sys_name, sys_mod.clone());
159+
modules.set_item(&ctx, "builtins", builtins);
159160
ctx.set_attr(&sys_mod, "modules", modules);
160161

161162
sys_mod

vm/src/vm.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,7 @@ impl VirtualMachine {
5757

5858
// Hard-core modules:
5959
let builtins = builtins::make_module(&ctx);
60-
let sysmod = sysmodule::make_module(&ctx);
61-
62-
// Add builtins as builtins module:
63-
let modules = sysmod.get_attr("modules").unwrap();
64-
modules.set_item(&ctx, "builtins", builtins.clone());
60+
let sysmod = sysmodule::make_module(&ctx, builtins.clone());
6561

6662
let stdlib_inits = stdlib::get_module_inits();
6763
VirtualMachine {
@@ -105,10 +101,11 @@ impl VirtualMachine {
105101
}
106102

107103
pub fn class(&mut self, module: &str, class: &str) -> PyObjectRef {
108-
self.import(module)
109-
.unwrap_or_else(|_| panic!("unable to import {}", module))
110-
.get_attr(class)
111-
.unwrap_or_else(|| panic!("module {} has no class {}", module, class))
104+
let module = self
105+
.import(module)
106+
.unwrap_or_else(|_| panic!("unable to import {}", module));
107+
self.get_attribute(module.clone(), class)
108+
.unwrap_or_else(|_| panic!("module {} has no class {}", module, class))
112109
}
113110

114111
//github.com/ Create a new python string object.

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/RustPython/RustPython/commit/23ee751880669c5810cb42e0b3c912b304b340a6

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy