clean AsPyObject usage

This commit is contained in:
Jeong Yunwon
2022-04-17 20:51:41 +09:00
parent 75e589cbc1
commit a7de700ef6
5 changed files with 13 additions and 12 deletions

View File

@@ -102,7 +102,7 @@ impl PyGenericAlias {
Ok(format!(
"{}[{}]",
repr_item(self.origin.as_object().to_owned(), vm)?,
repr_item(self.origin.clone().into(), vm)?,
if self.args.len() == 0 {
"()".to_owned()
} else {
@@ -118,17 +118,17 @@ impl PyGenericAlias {
#[pyproperty(magic)]
fn parameters(&self) -> PyObjectRef {
self.parameters.as_object().to_owned()
self.parameters.clone().into()
}
#[pyproperty(magic)]
fn args(&self) -> PyObjectRef {
self.args.as_object().to_owned()
self.args.clone().into()
}
#[pyproperty(magic)]
fn origin(&self) -> PyObjectRef {
self.origin.as_object().to_owned()
self.origin.clone().into()
}
#[pymethod(magic)]

View File

@@ -52,7 +52,7 @@ impl PyModule {
fn getattr_inner(zelf: &PyObjectView<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult {
if let Some(attr) =
vm.generic_getattribute_opt(zelf.as_object().to_owned(), name.clone(), None)?
vm.generic_getattribute_opt(zelf.to_owned().into(), name.clone(), None)?
{
return Ok(attr);
}

View File

@@ -10,7 +10,7 @@ use crate::common::{
use crate::{
builtins::{PyInt, PyStr, PyStrRef},
function::IntoPyObject,
AsPyObject, PyObject, PyObjectRef, PyObjectWrap, PyRefExact, PyResult, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyRefExact, PyResult, VirtualMachine,
};
use num_traits::ToPrimitive;
use std::{fmt, mem::size_of, ops::ControlFlow};
@@ -846,7 +846,7 @@ impl DictKey for usize {
}
} else {
let int = vm.ctx.new_int(*self);
vm.bool_eq(&int.into_object(), other_key)
vm.bool_eq(int.as_ref(), other_key)
}
}

View File

@@ -79,7 +79,7 @@ impl VirtualMachine {
// This function should not be called directly,
// use `wite_exception` as a public interface.
// It is similar to `print_exception_recursive` from `CPython`.
seen.insert(exc.as_object().get_id());
seen.insert(exc.get_id());
#[allow(clippy::manual_map)]
if let Some((cause_or_context, msg)) = if let Some(cause) = exc.cause() {
@@ -102,11 +102,11 @@ impl VirtualMachine {
} else {
None
} {
if !seen.contains(&cause_or_context.as_object().get_id()) {
if !seen.contains(&cause_or_context.get_id()) {
self.write_exception_recursive(output, &cause_or_context, seen)?;
writeln!(output, "{}", msg)?;
} else {
seen.insert(cause_or_context.as_object().get_id());
seen.insert(cause_or_context.get_id());
}
}
@@ -494,9 +494,9 @@ impl PyBaseException {
}
#[pymethod]
fn with_traceback(zelf: PyRef<Self>, tb: Option<PyTracebackRef>) -> PyResult {
fn with_traceback(zelf: PyRef<Self>, tb: Option<PyTracebackRef>) -> PyResult<PyRef<Self>> {
*zelf.traceback.write() = tb;
Ok(zelf.as_object().to_owned())
Ok(zelf)
}
#[pymethod(magic)]

View File

@@ -33,6 +33,7 @@ impl VirtualMachine {
})
}
#[inline]
pub fn bool_eq(&self, a: &PyObject, b: &PyObject) -> PyResult<bool> {
a.rich_compare_bool(b, PyComparisonOp::Eq, self)
}