mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #730 from RustPython/joey/remove-frompyobjecterf
Remove FromPyObjectRef, replace with downcast
This commit is contained in:
@@ -7,8 +7,7 @@ use num_traits::{Pow, Signed, ToPrimitive, Zero};
|
||||
use crate::format::FormatSpec;
|
||||
use crate::function::{OptionalArg, PyFuncArgs};
|
||||
use crate::pyobject::{
|
||||
FromPyObjectRef, IntoPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
|
||||
TypeProtocol,
|
||||
IntoPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
|
||||
};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
@@ -388,7 +387,7 @@ fn int_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
None => Zero::zero(),
|
||||
};
|
||||
Ok(PyInt::new(val)
|
||||
.into_ref_with_type(vm, PyClassRef::from_pyobj(cls))?
|
||||
.into_ref_with_type(vm, cls.clone().downcast().unwrap())?
|
||||
.into_object())
|
||||
}
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ use std::fmt;
|
||||
|
||||
use crate::function::{Args, KwArgs, PyFuncArgs};
|
||||
use crate::pyobject::{
|
||||
FromPyObjectRef, IdProtocol, PyAttributes, PyContext, PyObject, PyObjectRef, PyRef, PyResult,
|
||||
PyValue, TypeProtocol,
|
||||
IdProtocol, PyAttributes, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
TypeProtocol,
|
||||
};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
@@ -236,7 +236,7 @@ pub fn type_new_class(
|
||||
let mut bases: Vec<PyClassRef> = vm
|
||||
.extract_elements(bases)?
|
||||
.iter()
|
||||
.map(|x| FromPyObjectRef::from_pyobj(x))
|
||||
.map(|x| x.clone().downcast().unwrap())
|
||||
.collect();
|
||||
bases.push(vm.ctx.object());
|
||||
let name = objstr::get_value(name);
|
||||
@@ -385,7 +385,6 @@ pub fn new(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::FromPyObjectRef;
|
||||
use super::{linearise_mro, new};
|
||||
use super::{HashMap, IdProtocol, PyClassRef, PyContext};
|
||||
|
||||
@@ -417,8 +416,8 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let a: PyClassRef = FromPyObjectRef::from_pyobj(&a);
|
||||
let b: PyClassRef = FromPyObjectRef::from_pyobj(&b);
|
||||
let a: PyClassRef = a.downcast().unwrap();
|
||||
let b: PyClassRef = b.downcast().unwrap();
|
||||
|
||||
assert_eq!(
|
||||
map_ids(linearise_mro(vec![
|
||||
|
||||
@@ -159,7 +159,7 @@ pub fn create_type(name: &str, type_type: &PyClassRef, base: &PyClassRef) -> PyC
|
||||
dict,
|
||||
)
|
||||
.unwrap();
|
||||
FromPyObjectRef::from_pyobj(&new_type)
|
||||
new_type.downcast().unwrap()
|
||||
}
|
||||
|
||||
pub type PyNotImplementedRef = PyRef<PyNotImplemented>;
|
||||
@@ -205,7 +205,7 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
|
||||
dict: Some(RefCell::new(PyAttributes::new())),
|
||||
payload: PyClass {
|
||||
name: String::from("type"),
|
||||
mro: vec![FromPyObjectRef::from_pyobj(&object_type)],
|
||||
mro: vec![object_type.clone().downcast().unwrap()],
|
||||
},
|
||||
}
|
||||
.into_ref();
|
||||
@@ -216,8 +216,8 @@ fn init_type_hierarchy() -> (PyClassRef, PyClassRef) {
|
||||
ptr::write(&mut (*type_type_ptr).typ, type_type.clone());
|
||||
|
||||
(
|
||||
PyClassRef::from_pyobj(&type_type),
|
||||
PyClassRef::from_pyobj(&object_type),
|
||||
type_type.downcast().unwrap(),
|
||||
object_type.downcast().unwrap(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -583,7 +583,7 @@ impl PyContext {
|
||||
PyAttributes::new(),
|
||||
)
|
||||
.unwrap();
|
||||
PyClassRef::from_pyobj(&typ)
|
||||
typ.downcast().unwrap()
|
||||
}
|
||||
|
||||
pub fn new_scope(&self) -> Scope {
|
||||
@@ -721,6 +721,21 @@ where
|
||||
pub payload: T,
|
||||
}
|
||||
|
||||
impl PyObject<dyn PyObjectPayload> {
|
||||
pub fn downcast<T: PyObjectPayload>(self: Rc<Self>) -> Option<PyRef<T>> {
|
||||
if self.payload_is::<T>() {
|
||||
Some({
|
||||
PyRef {
|
||||
obj: self,
|
||||
_payload: PhantomData,
|
||||
}
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A reference to a Python object.
|
||||
///
|
||||
/// Note that a `PyRef<T>` can only deref to a shared / immutable reference.
|
||||
@@ -860,16 +875,12 @@ impl<T: PyObjectPayload> IdProtocol for PyRef<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FromPyObjectRef {
|
||||
fn from_pyobj(obj: &PyObjectRef) -> Self;
|
||||
}
|
||||
|
||||
pub trait TypeProtocol {
|
||||
fn typ(&self) -> PyObjectRef {
|
||||
self.type_ref().clone()
|
||||
}
|
||||
fn type_pyref(&self) -> PyClassRef {
|
||||
FromPyObjectRef::from_pyobj(self.type_ref())
|
||||
self.typ().downcast().unwrap()
|
||||
}
|
||||
fn type_ref(&self) -> &PyObjectRef;
|
||||
}
|
||||
@@ -1207,19 +1218,6 @@ impl<T: PyValue + 'static> PyObjectPayload for T {
|
||||
}
|
||||
}
|
||||
|
||||
impl FromPyObjectRef for PyRef<PyClass> {
|
||||
fn from_pyobj(obj: &PyObjectRef) -> Self {
|
||||
if obj.payload_is::<PyClass>() {
|
||||
PyRef {
|
||||
obj: obj.clone(),
|
||||
_payload: PhantomData,
|
||||
}
|
||||
} else {
|
||||
panic!("Error getting inner type: {:?}", obj.typ)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -6,15 +6,13 @@ use serde::ser::{SerializeMap, SerializeSeq};
|
||||
use serde_json;
|
||||
|
||||
use crate::function::PyFuncArgs;
|
||||
use crate::obj::objtype::PyClassRef;
|
||||
use crate::obj::{
|
||||
objbool, objdict, objfloat, objint, objsequence,
|
||||
objstr::{self, PyString},
|
||||
objtype,
|
||||
};
|
||||
use crate::pyobject::{
|
||||
create_type, DictProtocol, FromPyObjectRef, IdProtocol, PyContext, PyObjectRef, PyResult,
|
||||
TypeProtocol,
|
||||
create_type, DictProtocol, IdProtocol, PyContext, PyObjectRef, PyResult, TypeProtocol,
|
||||
};
|
||||
use crate::VirtualMachine;
|
||||
use num_traits::cast::ToPrimitive;
|
||||
@@ -208,7 +206,7 @@ pub fn de_pyobject(vm: &VirtualMachine, s: &str) -> PyResult {
|
||||
.unwrap()
|
||||
.get_item("JSONDecodeError")
|
||||
.unwrap();
|
||||
let json_decode_error = PyClassRef::from_pyobj(&json_decode_error);
|
||||
let json_decode_error = json_decode_error.downcast().unwrap();
|
||||
let exc = vm.new_exception(json_decode_error, format!("{}", err));
|
||||
vm.ctx.set_attr(&exc, "lineno", vm.ctx.new_int(err.line()));
|
||||
vm.ctx.set_attr(&exc, "colno", vm.ctx.new_int(err.column()));
|
||||
|
||||
@@ -30,8 +30,8 @@ use crate::obj::objtuple::PyTuple;
|
||||
use crate::obj::objtype;
|
||||
use crate::obj::objtype::PyClassRef;
|
||||
use crate::pyobject::{
|
||||
DictProtocol, FromPyObjectRef, IdProtocol, PyContext, PyObjectRef, PyResult, TryFromObject,
|
||||
TryIntoRef, TypeProtocol,
|
||||
DictProtocol, IdProtocol, PyContext, PyObjectRef, PyResult, TryFromObject, TryIntoRef,
|
||||
TypeProtocol,
|
||||
};
|
||||
use crate::stdlib;
|
||||
use crate::sysmodule;
|
||||
@@ -112,7 +112,7 @@ impl VirtualMachine {
|
||||
let class = self
|
||||
.get_attribute(module.clone(), class)
|
||||
.unwrap_or_else(|_| panic!("module {} has no class {}", module, class));
|
||||
PyClassRef::from_pyobj(&class)
|
||||
class.downcast().unwrap()
|
||||
}
|
||||
|
||||
/// Create a new python string object.
|
||||
|
||||
Reference in New Issue
Block a user