mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
Rename PyValue::{required_type => class}
This commit is contained in:
1 parent
a1e0c03a34
commit
737ec12fa5
32 files changed
+49
-49
No files matched your search
+1
-1
@@ -184,7 +184,7 @@ pub struct Frame {
|
||||
}
|
||||
|
||||
impl PyValue for Frame {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.frame_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ pub struct PyBuiltinFunction {
|
||||
}
|
||||
|
||||
impl PyValue for PyBuiltinFunction {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.builtin_function_or_method_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ impl PyByteArray {
|
||||
}
|
||||
|
||||
impl PyValue for PyByteArray {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.bytearray_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ impl Deref for PyBytes {
|
||||
}
|
||||
|
||||
impl PyValue for PyBytes {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.bytes_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ impl fmt::Debug for PyCode {
|
||||
}
|
||||
|
||||
impl PyValue for PyCode {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.code_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ pub struct PyComplex {
|
||||
}
|
||||
|
||||
impl PyValue for PyComplex {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.complex_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ impl fmt::Debug for PyDict {
|
||||
}
|
||||
|
||||
impl PyValue for PyDict {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.dict_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ pub struct PyEnumerate {
|
||||
}
|
||||
|
||||
impl PyValue for PyEnumerate {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.enumerate_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ pub struct PyFilter {
|
||||
}
|
||||
|
||||
impl PyValue for PyFilter {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.filter_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ pub struct PyFloat {
|
||||
}
|
||||
|
||||
impl PyValue for PyFloat {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.float_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ impl PyFunction {
|
||||
}
|
||||
|
||||
impl PyValue for PyFunction {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.function_type()
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,7 @@ impl PyMethod {
|
||||
}
|
||||
|
||||
impl PyValue for PyMethod {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.bound_method_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ pub struct PyGenerator {
|
||||
}
|
||||
|
||||
impl PyValue for PyGenerator {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.generator_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ impl IntoPyObject for BigInt {
|
||||
}
|
||||
|
||||
impl PyValue for PyInt {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.int_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ impl From<Vec<PyObjectRef>> for PyList {
|
||||
}
|
||||
|
||||
impl PyValue for PyList {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.list_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ pub struct PyMap {
|
||||
}
|
||||
|
||||
impl PyValue for PyMap {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.map_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ pub struct PyMemoryView {
|
||||
}
|
||||
|
||||
impl PyValue for PyMemoryView {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.memoryview_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ pub struct PyModule {
|
||||
pub type PyModuleRef = PyRef<PyModule>;
|
||||
|
||||
impl PyValue for PyModule {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.module_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ pub struct PyNone;
|
||||
pub type PyNoneRef = PyRef<PyNone>;
|
||||
|
||||
impl PyValue for PyNone {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.none().typ()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::vm::VirtualMachine;
|
||||
pub struct PyInstance;
|
||||
|
||||
impl PyValue for PyInstance {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.object()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ pub struct PyReadOnlyProperty {
|
||||
}
|
||||
|
||||
impl PyValue for PyReadOnlyProperty {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.readonly_property_type()
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ pub struct PyProperty {
|
||||
}
|
||||
|
||||
impl PyValue for PyProperty {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.property_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ pub struct PyRange {
|
||||
}
|
||||
|
||||
impl PyValue for PyRange {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.range_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ impl fmt::Debug for PySet {
|
||||
}
|
||||
|
||||
impl PyValue for PySet {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.set_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ pub struct PySlice {
|
||||
}
|
||||
|
||||
impl PyValue for PySlice {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.slice_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -597,7 +597,7 @@ impl PyStringRef {
|
||||
}
|
||||
|
||||
impl PyValue for PyString {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.str_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ impl From<Vec<PyObjectRef>> for PyTuple {
|
||||
}
|
||||
|
||||
impl PyValue for PyTuple {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.tuple_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ pub struct PyClass {
|
||||
pub type PyClassRef = PyRef<PyClass>;
|
||||
|
||||
impl PyValue for PyClass {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.type_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ impl PyWeak {
|
||||
}
|
||||
|
||||
impl PyValue for PyWeak {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.weakref_type()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ pub struct PyZip {
|
||||
}
|
||||
|
||||
impl PyValue for PyZip {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.zip_type()
|
||||
}
|
||||
}
|
||||
|
||||
+15
-15
@@ -164,7 +164,7 @@ pub fn create_type(name: &str, type_type: &PyObjectRef, base: &PyObjectRef) -> P
|
||||
pub struct PyNotImplemented;
|
||||
|
||||
impl PyValue for PyNotImplemented {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.not_implemented().typ()
|
||||
}
|
||||
}
|
||||
@@ -173,7 +173,7 @@ impl PyValue for PyNotImplemented {
|
||||
pub struct PyEllipsis;
|
||||
|
||||
impl PyValue for PyEllipsis {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.ellipsis_type.clone()
|
||||
}
|
||||
}
|
||||
@@ -715,21 +715,21 @@ pub struct PyRef<T> {
|
||||
impl<T: PyValue> PyRef<T> {
|
||||
pub fn new(ctx: &PyContext, payload: T) -> Self {
|
||||
PyRef {
|
||||
obj: PyObject::new(payload, T::required_type(ctx)),
|
||||
obj: PyObject::new(payload, T::class(ctx)),
|
||||
_payload: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_type(vm: &mut VirtualMachine, payload: T, cls: PyClassRef) -> PyResult<Self> {
|
||||
let required_type = T::required_type(&vm.ctx);
|
||||
if objtype::issubclass(&cls.obj, &required_type) {
|
||||
let class = T::class(&vm.ctx);
|
||||
if objtype::issubclass(&cls.obj, &class) {
|
||||
Ok(PyRef {
|
||||
obj: PyObject::new(payload, cls.obj),
|
||||
_payload: PhantomData,
|
||||
})
|
||||
} else {
|
||||
let subtype = vm.to_pystr(&cls.obj)?;
|
||||
let basetype = vm.to_pystr(&required_type)?;
|
||||
let basetype = vm.to_pystr(&class)?;
|
||||
Err(vm.new_type_error(format!("{} is not a subtype of {}", subtype, basetype)))
|
||||
}
|
||||
}
|
||||
@@ -765,13 +765,13 @@ where
|
||||
T: PyValue,
|
||||
{
|
||||
fn try_from_object(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
if objtype::isinstance(&obj, &T::required_type(&vm.ctx)) {
|
||||
if objtype::isinstance(&obj, &T::class(&vm.ctx)) {
|
||||
Ok(PyRef {
|
||||
obj,
|
||||
_payload: PhantomData,
|
||||
})
|
||||
} else {
|
||||
let expected_type = vm.to_pystr(&T::required_type(&vm.ctx))?;
|
||||
let expected_type = vm.to_pystr(&T::class(&vm.ctx))?;
|
||||
let actual_type = vm.to_pystr(&obj.typ())?;
|
||||
Err(vm.new_type_error(format!(
|
||||
"Expected type {}, not {}",
|
||||
@@ -1408,7 +1408,7 @@ where
|
||||
T: PyValue + Sized,
|
||||
{
|
||||
fn into_pyobject(self, ctx: &PyContext) -> PyResult {
|
||||
Ok(PyObject::new(self, T::required_type(ctx)))
|
||||
Ok(PyObject::new(self, T::class(ctx)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1547,7 +1547,7 @@ pub struct PyIteratorValue {
|
||||
}
|
||||
|
||||
impl PyValue for PyIteratorValue {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(ctx: &PyContext) -> PyObjectRef {
|
||||
ctx.iter_type()
|
||||
}
|
||||
}
|
||||
@@ -1574,25 +1574,25 @@ impl PyObject {
|
||||
}
|
||||
|
||||
pub trait PyValue: fmt::Debug + Sized + 'static {
|
||||
fn required_type(ctx: &PyContext) -> PyObjectRef;
|
||||
fn class(ctx: &PyContext) -> PyObjectRef;
|
||||
|
||||
fn into_ref(self, ctx: &PyContext) -> PyRef<Self> {
|
||||
PyRef {
|
||||
obj: PyObject::new(self, Self::required_type(ctx)),
|
||||
obj: PyObject::new(self, Self::class(ctx)),
|
||||
_payload: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
fn into_ref_with_type(self, vm: &mut VirtualMachine, cls: PyClassRef) -> PyResult<PyRef<Self>> {
|
||||
let required_type = Self::required_type(&vm.ctx);
|
||||
if objtype::issubclass(&cls.obj, &required_type) {
|
||||
let class = Self::class(&vm.ctx);
|
||||
if objtype::issubclass(&cls.obj, &class) {
|
||||
Ok(PyRef {
|
||||
obj: PyObject::new(self, cls.obj),
|
||||
_payload: PhantomData,
|
||||
})
|
||||
} else {
|
||||
let subtype = vm.to_pystr(&cls.obj)?;
|
||||
let basetype = vm.to_pystr(&required_type)?;
|
||||
let basetype = vm.to_pystr(&class)?;
|
||||
Err(vm.new_type_error(format!("{} is not a subtype of {}", subtype, basetype)))
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@ use crate::pyobject::{
|
||||
use crate::VirtualMachine;
|
||||
|
||||
impl PyValue for Regex {
|
||||
fn required_type(_ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(_ctx: &PyContext) -> PyObjectRef {
|
||||
// TODO
|
||||
unimplemented!()
|
||||
}
|
||||
@@ -111,7 +111,7 @@ struct PyMatch {
|
||||
}
|
||||
|
||||
impl PyValue for PyMatch {
|
||||
fn required_type(_ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(_ctx: &PyContext) -> PyObjectRef {
|
||||
// TODO
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ pub struct Socket {
|
||||
}
|
||||
|
||||
impl PyValue for Socket {
|
||||
fn required_type(_ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(_ctx: &PyContext) -> PyObjectRef {
|
||||
// TODO
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ pub struct PyPromise {
|
||||
}
|
||||
|
||||
impl PyValue for PyPromise {
|
||||
fn required_type(_ctx: &PyContext) -> PyObjectRef {
|
||||
fn class(_ctx: &PyContext) -> PyObjectRef {
|
||||
// TODO
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user