Skip to content

vm.call_method takes owned object #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ where

fn flush_std(vm: &VirtualMachine) {
if let Ok(stdout) = sys::get_stdout(vm) {
let _ = vm.call_method(&stdout, "flush", ());
let _ = vm.call_method(stdout, "flush", ());
}
if let Ok(stderr) = sys::get_stderr(vm) {
let _ = vm.call_method(&stderr, "flush", ());
let _ = vm.call_method(stderr, "flush", ());
}
}

Expand Down Expand Up @@ -666,7 +666,7 @@ fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>

fn insert_sys_path(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<()> {
let sys_path = vm.sys_module.clone().get_attr("path", vm).unwrap();
vm.call_method(&sys_path, "insert", (0, obj))?;
vm.call_method(sys_path, "insert", (0, obj))?;
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ mod array {
let n = vm.check_repeat_or_overflow_error(itemsize, n)?;
let nbytes = n * itemsize;

let b = vm.call_method(&f, "read", (nbytes,))?;
let b = vm.call_method(f, "read", (nbytes,))?;
let b = b
.downcast::<PyBytes>()
.map_err(|_| vm.new_type_error("read() didn't return bytes".to_owned()))?;
Expand Down Expand Up @@ -898,7 +898,7 @@ mod array {

for b in bytes.chunks(BLOCKSIZE) {
let b = PyBytes::from(b.to_vec()).into_ref(vm);
vm.call_method(&f, "write", (b,))?;
vm.call_method(f.clone(), "write", (b,))?;
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/bisect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ mod _bisect {
},
vm,
)?;
vm.call_method(&a, "insert", (index, x))
vm.call_method(a, "insert", (index, x))
}

/// Insert item x in list a, and keep it sorted assuming a is sorted.
Expand All @@ -149,6 +149,6 @@ mod _bisect {
},
vm,
)?;
vm.call_method(&a, "insert", (index, x))
vm.call_method(a, "insert", (index, x))
}
}
2 changes: 1 addition & 1 deletion stdlib/src/pyexpat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ mod _pyexpat {
#[pymethod(name = "ParseFile")]
fn parse_file(&self, file: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
// todo: read chunks at a time
let read_res = vm.call_method(&file, "read", ())?;
let read_res = vm.call_method(file, "read", ())?;
let bytes_like = ArgBytesLike::try_from_object(vm, read_res)?;
let buf = bytes_like.borrow_buf().to_vec();
let reader = Cursor::new(buf);
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/mappingproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl PyMappingProxy {
PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm)
}
};
vm.call_method(&obj, "items", ())
vm.call_method(obj, "items", ())
}
#[pymethod]
pub fn keys(&self, vm: &VirtualMachine) -> PyResult {
Expand All @@ -124,7 +124,7 @@ impl PyMappingProxy {
PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm)
}
};
vm.call_method(&obj, "keys", ())
vm.call_method(obj, "keys", ())
}
#[pymethod]
pub fn values(&self, vm: &VirtualMachine) -> PyResult {
Expand All @@ -134,12 +134,12 @@ impl PyMappingProxy {
PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm)
}
};
vm.call_method(&obj, "values", ())
vm.call_method(obj, "values", ())
}
#[pymethod]
pub fn copy(&self, vm: &VirtualMachine) -> PyResult {
match &self.mapping {
MappingProxyInner::Dict(d) => vm.call_method(d, "copy", ()),
MappingProxyInner::Dict(d) => vm.call_method(d.to_owned(), "copy", ()),
MappingProxyInner::Class(c) => {
Ok(PyDict::from_attributes(c.attributes.read().clone(), vm)?.into_pyobject(vm))
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl PyBaseObject {

// Get instance attributes:
if let Some(object_dict) = obj.dict() {
vm.call_method(dict.as_object(), "update", (object_dict,))?;
vm.call_method(dict.clone(), "update", (object_dict,))?;
}

let attributes: Vec<_> = dict.into_iter().map(|(k, _v)| k).collect();
Expand Down
4 changes: 2 additions & 2 deletions vm/src/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl PyCodec {
Some(e) => vec![e.into()],
None => vec![],
};
vm.call_method(self.0.as_object(), "incrementalencoder", args)
vm.call_method(self.0.clone(), "incrementalencoder", args)
}

pub fn get_incremental_decoder(
Expand All @@ -118,7 +118,7 @@ impl PyCodec {
Some(e) => vec![e.into()],
None => vec![],
};
vm.call_method(self.0.as_object(), "incrementaldecoder", args)
vm.call_method(self.0.clone(), "incrementaldecoder", args)
}
}

Expand Down
2 changes: 1 addition & 1 deletion vm/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ fn call_object_format(
Some(FormatPreconversor::Str) => argument.str(vm)?.into(),
Some(FormatPreconversor::Repr) => argument.repr(vm)?.into(),
Some(FormatPreconversor::Ascii) => vm.ctx.new_str(builtins::ascii(argument, vm)?).into(),
Some(FormatPreconversor::Bytes) => vm.call_method(&argument, "decode", ())?,
Some(FormatPreconversor::Bytes) => vm.call_method(argument, "decode", ())?,
None => argument,
};
let result = vm.call_special_method(argument, "__format__", (format_spec,))?;
Expand Down
2 changes: 1 addition & 1 deletion vm/src/protocol/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl PyMapping<'_> {
}

fn method_output_as_list(&self, method_name: &str, vm: &VirtualMachine) -> PyResult {
let meth_output = vm.call_method(self.obj, method_name, ())?;
let meth_output = vm.call_method(self.obj.to_owned(), method_name, ())?;
if meth_output.is(&vm.ctx.types.list_type) {
return Ok(meth_output);
}
Expand Down
5 changes: 3 additions & 2 deletions vm/src/py_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ impl Write for PyWriter<'_> {
type Error = PyBaseExceptionRef;
fn write_fmt(&mut self, args: fmt::Arguments) -> Result<(), Self::Error> {
let PyWriter(obj, vm) = self;
vm.call_method(obj, "write", (args.to_string(),)).map(drop)
vm.call_method(obj.to_owned(), "write", (args.to_string(),))
.map(drop)
}
}

pub fn file_readline(obj: &PyObject, size: Option<usize>, vm: &VirtualMachine) -> PyResult {
let args = size.map_or_else(Vec::new, |size| vec![vm.ctx.new_int(size).into()]);
let ret = vm.call_method(obj, "readline", args)?;
let ret = vm.call_method(obj.to_owned(), "readline", args)?;
let eof_err = || {
vm.new_exception(
vm.ctx.exceptions.eof_error.clone(),
Expand Down
17 changes: 10 additions & 7 deletions vm/src/stdlib/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ mod builtins {
.into_option()
.unwrap_or_else(|| PyStr::from("").into_ref(vm));

vm.call_method(&value, "__format__", (format_spec,))?
vm.call_method(value, "__format__", (format_spec,))?
.downcast()
.map_err(|obj| {
vm.new_type_error(format!(
Expand Down Expand Up @@ -364,7 +364,7 @@ mod builtins {
let stdout = sys::get_stdout(vm)?;
let stderr = sys::get_stderr(vm)?;

let _ = vm.call_method(&stderr, "flush", ());
let _ = vm.call_method(stderr, "flush", ());

let fd_matches = |obj, expected| {
vm.call_method(obj, "fileno", ())
Expand All @@ -374,7 +374,10 @@ mod builtins {
};

// everything is normalish, we can just rely on rustyline to use stdin/stdout
if fd_matches(&stdin, 0) && fd_matches(&stdout, 1) && atty::is(atty::Stream::Stdin) {
if fd_matches(stdin.clone(), 0)
&& fd_matches(stdout.clone(), 1)
&& atty::is(atty::Stream::Stdin)
{
let prompt = prompt.as_ref().map_or("", |s| s.as_str());
let mut readline = Readline::new(());
match readline.readline(prompt) {
Expand All @@ -393,9 +396,9 @@ mod builtins {
}
} else {
if let OptionalArg::Present(prompt) = prompt {
vm.call_method(&stdout, "write", (prompt,))?;
vm.call_method(stdout.clone(), "write", (prompt,))?;
}
let _ = vm.call_method(&stdout, "flush", ());
let _ = vm.call_method(stdout, "flush", ());
py_io::file_readline(&stdin, None, vm)
}
}
Expand Down Expand Up @@ -648,7 +651,7 @@ mod builtins {
Some(f) => f,
None => sys::get_stdout(vm)?,
};
let write = |obj: PyStrRef| vm.call_method(&file, "write", (obj,));
let write = |obj: PyStrRef| vm.call_method(file.clone(), "write", (obj,));

let sep = options.sep.unwrap_or_else(|| PyStr::from(" ").into_ref(vm));

Expand All @@ -669,7 +672,7 @@ mod builtins {
write(end)?;

if options.flush.to_bool() {
vm.call_method(&file, "flush", ())?;
vm.call_method(file, "flush", ())?;
}

Ok(())
Expand Down
Loading