Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,9 @@ fn setup_main_module(vm: &VirtualMachine) -> PyResult<Scope> {
})
.expect("Failed to initialize __main__.__annotations__");

vm.get_attribute(vm.sys_module.clone(), "modules")?
vm.sys_module
.clone()
.get_attr("modules", vm)?
.set_item("__main__", main_module, vm)?;

Ok(scope)
Expand Down Expand Up @@ -626,19 +628,19 @@ fn run_command(vm: &VirtualMachine, scope: Scope, source: String) -> PyResult<()
fn run_module(vm: &VirtualMachine, module: &str) -> PyResult<()> {
debug!("Running module {}", module);
let runpy = vm.import("runpy", None, 0)?;
let run_module_as_main = vm.get_attribute(runpy, "_run_module_as_main")?;
let run_module_as_main = runpy.get_attr("_run_module_as_main", vm)?;
vm.invoke(&run_module_as_main, (module,))?;
Ok(())
}

fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> {
let path_importer_cache = vm.get_attribute(vm.sys_module.clone(), "path_importer_cache")?;
let path_importer_cache = vm.sys_module.clone().get_attr("path_importer_cache", vm)?;
let path_importer_cache = PyDictRef::try_from_object(vm, path_importer_cache)?;
if let Some(importer) = path_importer_cache.get_item_option(path, vm)? {
return Ok(Some(importer));
}
let path = vm.ctx.new_str(path);
let path_hooks = vm.get_attribute(vm.sys_module.clone(), "path_hooks")?;
let path_hooks = vm.sys_module.clone().get_attr("path_hooks", vm)?;
let mut importer = None;
let path_hooks: Vec<PyObjectRef> = vm.extract_elements(&path_hooks)?;
for path_hook in path_hooks {
Expand All @@ -660,7 +662,7 @@ fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>
}

fn insert_sys_path(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<()> {
let sys_path = vm.get_attribute(vm.sys_module.clone(), "path").unwrap();
let sys_path = vm.sys_module.clone().get_attr("path", vm).unwrap();
vm.call_method(&sys_path, "insert", (0, obj))?;
Ok(())
}
Expand All @@ -670,7 +672,7 @@ fn run_script(vm: &VirtualMachine, scope: Scope, script_file: &str) -> PyResult<
if get_importer(script_file, vm)?.is_some() {
insert_sys_path(vm, vm.ctx.new_str(script_file).into())?;
let runpy = vm.import("runpy", None, 0)?;
let run_module_as_main = vm.get_attribute(runpy, "_run_module_as_main")?;
let run_module_as_main = runpy.get_attr("_run_module_as_main", vm)?;
vm.invoke(&run_module_as_main, (vm.ctx.new_str("__main__"), false))?;
return Ok(());
}
Expand Down
4 changes: 3 additions & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
loop {
let prompt_name = if continuing { "ps2" } else { "ps1" };
let prompt = vm
.get_attribute(vm.sys_module.clone(), prompt_name)
.sys_module
.clone()
.get_attr(prompt_name, vm)
.and_then(|prompt| vm.to_str(&prompt));
let prompt = match prompt {
Ok(ref s) => s.as_str(),
Expand Down
2 changes: 1 addition & 1 deletion src/shell/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'vm> ShellHelper<'vm> {
.ok()??;

for attr in parents {
current = self.vm.get_attribute(current.clone(), attr.as_str()).ok()?;
current = current.clone().get_attr(attr.as_str(), self.vm).ok()?;
}

let current_iter = str_iter_method(current, "__dir__").ok()?;
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ mod array {
let code = MachineFormatCode::from_typecode(array.typecode()).unwrap();
let code = PyInt::from(u8::from(code)).into_object(vm);
let module = vm.import("array", None, 0)?;
let func = vm.get_attribute(module, "_array_reconstructor")?;
let func = module.get_attr("_array_reconstructor", vm)?;
Ok((
func,
vm.new_tuple((cls, typecode, code, bytes)),
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/dis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod decl {

#[pyfunction]
fn dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let co = if let Ok(co) = vm.get_attribute(obj.clone(), "__code__") {
let co = if let Ok(co) = obj.clone().get_attr("__code__", vm) {
// Method or function:
PyRef::try_from_object(vm, co)?
} else if let Ok(co_str) = PyStrRef::try_from_object(vm, obj.clone()) {
Expand Down
16 changes: 8 additions & 8 deletions stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@ mod _json {
type Args = PyObjectRef;

fn py_new(cls: PyTypeRef, ctx: Self::Args, vm: &VirtualMachine) -> PyResult {
let strict = vm.get_attribute(ctx.clone(), "strict")?.try_to_bool(vm)?;
let object_hook = vm.option_if_none(vm.get_attribute(ctx.clone(), "object_hook")?);
let strict = ctx.clone().get_attr("strict", vm)?.try_to_bool(vm)?;
let object_hook = vm.option_if_none(ctx.clone().get_attr("object_hook", vm)?);
let object_pairs_hook =
vm.option_if_none(vm.get_attribute(ctx.clone(), "object_pairs_hook")?);
let parse_float = vm.get_attribute(ctx.clone(), "parse_float")?;
vm.option_if_none(ctx.clone().get_attr("object_pairs_hook", vm)?);
let parse_float = ctx.clone().get_attr("parse_float", vm)?;
let parse_float =
if vm.is_none(&parse_float) || parse_float.is(&vm.ctx.types.float_type) {
None
} else {
Some(parse_float)
};
let parse_int = vm.get_attribute(ctx.clone(), "parse_int")?;
let parse_int = ctx.clone().get_attr("parse_int", vm)?;
let parse_int = if vm.is_none(&parse_int) || parse_int.is(&vm.ctx.types.int_type) {
None
} else {
Some(parse_int)
};
let parse_constant = vm.get_attribute(ctx.clone(), "parse_constant")?;
let parse_constant = ctx.clone().get_attr("parse_constant", vm)?;

Self {
strict,
Expand Down Expand Up @@ -89,7 +89,7 @@ mod _json {
}
'{' => {
// TODO: parse the object in rust
let parse_obj = vm.get_attribute(self.ctx.clone(), "parse_object")?;
let parse_obj = self.ctx.clone().get_attr("parse_object", vm)?;
return PyIterReturn::from_pyresult(
vm.invoke(
&parse_obj,
Expand All @@ -106,7 +106,7 @@ mod _json {
}
'[' => {
// TODO: parse the array in rust
let parse_array = vm.get_attribute(self.ctx.clone(), "parse_array")?;
let parse_array = self.ctx.clone().get_attr("parse_array", vm)?;
return PyIterReturn::from_pyresult(
vm.invoke(&parse_array, ((pystr, next_idx), scan_once)),
vm,
Expand Down
6 changes: 4 additions & 2 deletions stdlib/src/unicodedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ use crate::vm::{PyObjectRef, PyValue, VirtualMachine};
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let module = unicodedata::make_module(vm);

let ucd = unicodedata::Ucd::new(unic_ucd_age::UNICODE_VERSION).into_ref(vm);
let ucd: PyObjectRef = unicodedata::Ucd::new(unic_ucd_age::UNICODE_VERSION)
.into_ref(vm)
.into();

for attr in ["category", "lookup", "name", "bidirectional", "normalize"]
.iter()
.copied()
{
crate::vm::extend_module!(vm, &module, {
attr => vm.get_attribute(ucd.clone().into(), attr).unwrap(),
attr => ucd.clone().get_attr(attr, vm).unwrap(),
});
}

Expand Down
15 changes: 8 additions & 7 deletions vm/src/builtins/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,10 @@ impl PyFunction {

#[pymethod(magic)]
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> String {
let qualname = vm
.get_attribute(zelf.as_object().clone(), "__qualname__")
let qualname = zelf
.as_object()
.clone()
.get_attr("__qualname__", vm)
.ok()
.and_then(|qualname_attr| qualname_attr.downcast::<PyStr>().ok())
.map(|qualname| qualname.as_str().to_owned())
Expand Down Expand Up @@ -462,7 +464,7 @@ impl GetAttr for PyBoundMethod {
if let Some(obj) = zelf.get_class_attr(name.as_str()) {
return vm.call_if_get_descriptor(obj, zelf.into());
}
vm.get_attribute(zelf.function.clone(), name)
zelf.function.clone().get_attr(name, vm)
}
}

Expand Down Expand Up @@ -520,7 +522,7 @@ impl PyBoundMethod {

#[pyproperty(magic)]
fn doc(&self, vm: &VirtualMachine) -> PyResult {
vm.get_attribute(self.function.clone(), "__doc__")
self.function.clone().get_attr("__doc__", vm)
}

#[pyproperty(magic)]
Expand All @@ -535,7 +537,7 @@ impl PyBoundMethod {

#[pyproperty(magic)]
fn module(&self, vm: &VirtualMachine) -> Option<PyObjectRef> {
vm.get_attribute(self.function.clone(), "__module__").ok()
self.function.clone().get_attr("__module__", vm).ok()
}

#[pyproperty(magic)]
Expand All @@ -557,8 +559,7 @@ impl PyBoundMethod {
))
.into());
}

vm.get_attribute(self.function.clone(), "__qualname__")
self.function.clone().get_attr("__qualname__", vm)
}
}

Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/function/jitfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ pub fn get_jit_arg_types(func: &PyRef<PyFunction>, vm: &VirtualMachine) -> PyRes
return Ok(Vec::new());
}

let annotations = vm.get_attribute(func.clone().into(), "__annotations__")?;
let func_obj: PyObjectRef = func.clone().into();
let annotations = func_obj.get_attr("__annotations__", vm)?;
if vm.is_none(&annotations) {
Err(new_jit_error(
"Jitting function requires arguments to have annotations".to_owned(),
Expand Down
7 changes: 4 additions & 3 deletions vm/src/builtins/genericalias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ fn make_parameters(args: &PyTupleRef, vm: &VirtualMachine) -> PyTupleRef {
for arg in args.as_slice() {
if is_typevar(arg.clone()) {
parameters.push(arg.clone());
} else if let Ok(tuple) = vm
.get_attribute(arg.clone(), "__parameters__")
} else if let Ok(tuple) = arg
.clone()
.get_attr("__parameters__", vm)
.and_then(|obj| PyTupleRef::try_from_object(vm, obj))
{
for subparam in tuple.as_slice() {
Expand All @@ -177,7 +178,7 @@ impl GetAttr for PyGenericAlias {
return vm.generic_getattribute(zelf.as_object().clone(), attr);
}
}
vm.get_attribute(zelf.origin(), attr)
zelf.origin().get_attr(attr, vm)
}
}

Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ pub fn builtins_iter(vm: &VirtualMachine) -> &PyObjectRef {
static_cell! {
static INSTANCE: PyObjectRef;
}
INSTANCE.get_or_init(|| vm.get_attribute(vm.builtins.clone(), "iter").unwrap())
INSTANCE.get_or_init(|| vm.builtins.clone().get_attr("iter", vm).unwrap())
}

pub fn builtins_reversed(vm: &VirtualMachine) -> &PyObjectRef {
static_cell! {
static INSTANCE: PyObjectRef;
}
INSTANCE.get_or_init(|| vm.get_attribute(vm.builtins.clone(), "reversed").unwrap())
INSTANCE.get_or_init(|| vm.builtins.clone().get_attr("reversed", vm).unwrap())
}

#[pyclass(module = false, name = "iterator")]
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl PyModule {
#[pymethod(magic)]
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
let importlib = vm.import("_frozen_importlib", None, 0)?;
let module_repr = vm.get_attribute(importlib, "_module_repr")?;
let module_repr = importlib.get_attr("_module_repr", vm)?;
vm.invoke(&module_repr, (zelf,))
}

Expand Down
7 changes: 4 additions & 3 deletions vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ impl PyBaseObject {
fn reduce_ex(obj: PyObjectRef, proto: usize, vm: &VirtualMachine) -> PyResult {
if let Some(reduce) = vm.get_attribute_opt(obj.clone(), "__reduce__")? {
let object_reduce = vm.ctx.types.object_type.get_attr("__reduce__").unwrap();
let class_reduce = vm.get_attribute(obj.clone_class().into(), "__reduce__")?;
let typ_obj: PyObjectRef = obj.clone_class().into();
let class_reduce = typ_obj.get_attr("__reduce__", vm)?;
if !class_reduce.is(&object_reduce) {
return vm.invoke(&reduce, ());
}
Expand Down Expand Up @@ -364,11 +365,11 @@ pub fn init(ctx: &PyContext) {
fn common_reduce(obj: PyObjectRef, proto: usize, vm: &VirtualMachine) -> PyResult {
if proto >= 2 {
let reducelib = vm.import("__reducelib", None, 0)?;
let reduce_2 = vm.get_attribute(reducelib, "reduce_2")?;
let reduce_2 = reducelib.get_attr("reduce_2", vm)?;
vm.invoke(&reduce_2, (obj,))
} else {
let copyreg = vm.import("copyreg", None, 0)?;
let reduce_ex = vm.get_attribute(copyreg, "_reduce_ex")?;
let reduce_ex = copyreg.get_attr("_reduce_ex", vm)?;
vm.invoke(&reduce_ex, (obj, proto))
}
}
2 changes: 1 addition & 1 deletion vm/src/builtins/pysuper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn supercheck(ty: PyTypeRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<
if obj.isinstance(&ty) {
return Ok(obj.clone_class());
}
let class_attr = vm.get_attribute(obj, "__class__")?;
let class_attr = obj.get_attr("__class__", vm)?;
if let Ok(cls) = class_attr.downcast::<PyType>() {
if !cls.is(&ty) && cls.issubclass(&ty) {
return Ok(cls);
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/weakproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl PyWeakProxy {
"weakly-referenced object no longer exists".to_owned(),
)
})?;
vm.get_attribute(obj, attr_name)
obj.get_attr(attr_name, vm)
}
}

Expand Down
24 changes: 12 additions & 12 deletions vm/src/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ fn normalize_encoding_name(encoding: &str) -> Cow<'_, str> {

// TODO: exceptions with custom payloads
fn extract_unicode_error_range(err: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Range<usize>> {
let start = vm.get_attribute(err.clone(), "start")?;
let start = err.clone().get_attr("start", vm)?;
let start = start.try_into_value(vm)?;
let end = vm.get_attribute(err.clone(), "end")?;
let end = err.clone().get_attr("end", vm)?;
let end = end.try_into_value(vm)?;
Ok(Range { start, end })
}
Expand Down Expand Up @@ -396,7 +396,7 @@ fn xmlcharrefreplace_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(
return Err(bad_err_type(err, vm));
}
let range = extract_unicode_error_range(&err, vm)?;
let s = PyStrRef::try_from_object(vm, vm.get_attribute(err, "object")?)?;
let s = PyStrRef::try_from_object(vm, err.get_attr("object", vm)?)?;
let s_after_start = crate::common::str::try_get_chars(s.as_str(), range.start..).unwrap_or("");
let num_chars = range.len();
// capacity rough guess; assuming that the codepoints are 3 digits in decimal + the &#;
Expand All @@ -411,7 +411,7 @@ fn xmlcharrefreplace_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(
fn backslashreplace_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(String, usize)> {
if is_decode_err(&err, vm) {
let range = extract_unicode_error_range(&err, vm)?;
let b = PyBytesRef::try_from_object(vm, vm.get_attribute(err, "object")?)?;
let b = PyBytesRef::try_from_object(vm, err.get_attr("object", vm)?)?;
let mut replace = String::with_capacity(4 * range.len());
for &c in &b[range.clone()] {
use std::fmt::Write;
Expand All @@ -422,7 +422,7 @@ fn backslashreplace_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(S
return Err(bad_err_type(err, vm));
}
let range = extract_unicode_error_range(&err, vm)?;
let s = PyStrRef::try_from_object(vm, vm.get_attribute(err, "object")?)?;
let s = PyStrRef::try_from_object(vm, err.get_attr("object", vm)?)?;
let s_after_start = crate::common::str::try_get_chars(s.as_str(), range.start..).unwrap_or("");
let num_chars = range.len();
// minimum 4 output bytes per char: \xNN
Expand All @@ -444,7 +444,7 @@ fn backslashreplace_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(S
fn namereplace_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(String, usize)> {
if err.isinstance(&vm.ctx.exceptions.unicode_encode_error) {
let range = extract_unicode_error_range(&err, vm)?;
let s = PyStrRef::try_from_object(vm, vm.get_attribute(err, "object")?)?;
let s = PyStrRef::try_from_object(vm, err.get_attr("object", vm)?)?;
let s_after_start =
crate::common::str::try_get_chars(s.as_str(), range.start..).unwrap_or("");
let num_chars = range.len();
Expand Down Expand Up @@ -539,8 +539,8 @@ fn get_standard_encoding(encoding: &str) -> (usize, StandardEncoding) {
fn surrogatepass_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(String, usize)> {
if err.isinstance(&vm.ctx.exceptions.unicode_encode_error) {
let range = extract_unicode_error_range(&err, vm)?;
let s = PyStrRef::try_from_object(vm, vm.get_attribute(err.clone(), "object")?)?;
let s_encoding = PyStrRef::try_from_object(vm, vm.get_attribute(err.clone(), "encoding")?)?;
let s = PyStrRef::try_from_object(vm, err.clone().get_attr("object", vm)?)?;
let s_encoding = PyStrRef::try_from_object(vm, err.clone().get_attr("encoding", vm)?)?;
let (_, standard_encoding) = get_standard_encoding(s_encoding.as_str());
if let StandardEncoding::Unknown = standard_encoding {
// Not supported, fail with original exception
Expand Down Expand Up @@ -590,8 +590,8 @@ fn surrogatepass_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(Stri
Ok((out, range.end))
} else if is_decode_err(&err, vm) {
let range = extract_unicode_error_range(&err, vm)?;
let s = PyStrRef::try_from_object(vm, vm.get_attribute(err.clone(), "object")?)?;
let s_encoding = PyStrRef::try_from_object(vm, vm.get_attribute(err.clone(), "encoding")?)?;
let s = PyStrRef::try_from_object(vm, err.clone().get_attr("object", vm)?)?;
let s_encoding = PyStrRef::try_from_object(vm, err.clone().get_attr("encoding", vm)?)?;
let (byte_length, standard_encoding) = get_standard_encoding(s_encoding.as_str());
if let StandardEncoding::Unknown = standard_encoding {
// Not supported, fail with original exception
Expand Down Expand Up @@ -652,7 +652,7 @@ fn surrogatepass_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(Stri
fn surrogateescape_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(String, usize)> {
if err.isinstance(&vm.ctx.exceptions.unicode_encode_error) {
let range = extract_unicode_error_range(&err, vm)?;
let s = PyStrRef::try_from_object(vm, vm.get_attribute(err.clone(), "object")?)?;
let s = PyStrRef::try_from_object(vm, err.clone().get_attr("object", vm)?)?;
let s_after_start =
crate::common::str::try_get_chars(s.as_str(), range.start..).unwrap_or("");
let num_chars = range.len();
Expand All @@ -668,7 +668,7 @@ fn surrogateescape_errors(err: PyObjectRef, vm: &VirtualMachine) -> PyResult<(St
Ok((out, range.end))
} else if is_decode_err(&err, vm) {
let range = extract_unicode_error_range(&err, vm)?;
let s = PyStrRef::try_from_object(vm, vm.get_attribute(err.clone(), "object")?)?;
let s = PyStrRef::try_from_object(vm, err.clone().get_attr("object", vm)?)?;
let s_after_start = crate::common::str::try_get_chars(s.as_str(), range.start..)
.unwrap_or("")
.as_bytes();
Expand Down
Loading