diff --git a/vm/src/builtins.rs b/vm/src/builtins.rs index fbd2d5356..27ce8aadd 100644 --- a/vm/src/builtins.rs +++ b/vm/src/builtins.rs @@ -368,7 +368,7 @@ fn builtin_len(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { Ok(value) => vm.invoke(value, PyFuncArgs::default()), Err(..) => Err(vm.new_type_error(format!( "object of type '{}' has no method {:?}", - objtype::get_type_name(&obj.class()), + obj.class(), len_method_name ))), } @@ -605,10 +605,7 @@ fn builtin_reversed(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { match vm.get_method(obj.clone(), "__reversed__") { Ok(value) => vm.invoke(value, PyFuncArgs::default()), // TODO: fallback to using __len__ and __getitem__, if object supports sequence protocol - Err(..) => Err(vm.new_type_error(format!( - "'{}' object is not reversible", - objtype::get_type_name(&obj.class()), - ))), + Err(..) => Err(vm.new_type_error(format!("'{}' object is not reversible", obj.class()))), } } // builtin_reversed diff --git a/vm/src/exceptions.rs b/vm/src/exceptions.rs index 7177527da..f850d7697 100644 --- a/vm/src/exceptions.rs +++ b/vm/src/exceptions.rs @@ -68,7 +68,6 @@ fn exception_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { args, required = [(exc, Some(vm.ctx.exceptions.exception_type.clone()))] ); - let type_name = objtype::get_type_name(&exc.class()); let msg = if let Ok(m) = vm.get_attribute(exc.clone(), "msg") { match vm.to_pystr(&m) { Ok(msg) => msg, @@ -77,7 +76,7 @@ fn exception_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { } else { panic!("Error message must be set"); }; - let s = format!("{}: {}", type_name, msg); + let s = format!("{}: {}", exc.class(), msg); Ok(vm.new_str(s)) } diff --git a/vm/src/frame.rs b/vm/src/frame.rs index 53479f8fc..6f8815cbe 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -1091,20 +1091,18 @@ impl Frame { fn _in(&self, vm: &VirtualMachine, needle: PyObjectRef, haystack: PyObjectRef) -> PyResult { match self._membership(vm, needle, &haystack) { Ok(found) => Ok(found), - Err(_) => Err(vm.new_type_error(format!( - "{} has no __contains__ method", - objtype::get_type_name(&haystack.class()) - ))), + Err(_) => { + Err(vm.new_type_error(format!("{} has no __contains__ method", haystack.class()))) + } } } fn _not_in(&self, vm: &VirtualMachine, needle: PyObjectRef, haystack: PyObjectRef) -> PyResult { match self._membership(vm, needle, &haystack) { Ok(found) => Ok(vm.ctx.new_bool(!objbool::get_value(&found))), - Err(_) => Err(vm.new_type_error(format!( - "{} has no __contains__ method", - objtype::get_type_name(&haystack.class()) - ))), + Err(_) => { + Err(vm.new_type_error(format!("{} has no __contains__ method", haystack.class()))) + } } } diff --git a/vm/src/obj/objfloat.rs b/vm/src/obj/objfloat.rs index 5e68f5960..e261a689e 100644 --- a/vm/src/obj/objfloat.rs +++ b/vm/src/obj/objfloat.rs @@ -190,8 +190,7 @@ impl PyFloatRef { } } } else { - let type_name = objtype::get_type_name(&arg.class()); - return Err(vm.new_type_error(format!("can't convert {} to float", type_name))); + return Err(vm.new_type_error(format!("can't convert {} to float", arg.class()))); }; PyFloat { value }.into_ref_with_type(vm, cls) } diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index f31997ead..e2203469a 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -211,11 +211,7 @@ impl PyIntRef { fn lshift(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { if !objtype::isinstance(&other, &vm.ctx.int_type()) { - return Err(vm.new_type_error(format!( - "unsupported operand type(s) for << '{}' and '{}'", - objtype::get_type_name(&self.as_object().class()), - objtype::get_type_name(&other.class()) - ))); + return Ok(vm.ctx.not_implemented()); } if let Some(n_bits) = get_value(&other).to_usize() { @@ -234,11 +230,7 @@ impl PyIntRef { fn rshift(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { if !objtype::isinstance(&other, &vm.ctx.int_type()) { - return Err(vm.new_type_error(format!( - "unsupported operand type(s) for >> '{}' and '{}'", - objtype::get_type_name(&self.as_object().class()), - objtype::get_type_name(&other.class()) - ))); + return Ok(vm.ctx.not_implemented()); } if let Some(n_bits) = get_value(&other).to_usize() { @@ -420,10 +412,9 @@ pub fn to_int(vm: &VirtualMachine, obj: &PyObjectRef, base: u32) -> PyResult PyResult { fn object_repr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { arg_check!(vm, args, required = [(obj, Some(vm.ctx.object()))]); - let type_name = objtype::get_type_name(&obj.class()); let address = obj.get_id(); - Ok(vm.new_str(format!("<{} object at 0x{:x}>", type_name, address))) + Ok(vm.new_str(format!("<{} object at 0x{:x}>", obj.class(), address))) } pub fn object_dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyList { diff --git a/vm/src/obj/objsuper.rs b/vm/src/obj/objsuper.rs index fad365e24..0061b41e9 100644 --- a/vm/src/obj/objsuper.rs +++ b/vm/src/obj/objsuper.rs @@ -112,10 +112,9 @@ fn super_new( // Check type argument: if !objtype::isinstance(py_type.as_object(), &vm.get_type()) { - let type_name = objtype::get_type_name(&py_type.as_object().class()); return Err(vm.new_type_error(format!( "super() argument 1 must be type, not {}", - type_name + py_type.class() ))); } diff --git a/vm/src/obj/objtype.rs b/vm/src/obj/objtype.rs index 857ec402f..14958300b 100644 --- a/vm/src/obj/objtype.rs +++ b/vm/src/obj/objtype.rs @@ -223,10 +223,6 @@ pub fn issubclass(subclass: &PyClassRef, cls: &PyClassRef) -> bool { subclass.is(cls) || mro.iter().any(|c| c.is(cls.as_object())) } -pub fn get_type_name(typ: &PyClassRef) -> String { - typ.name.clone() -} - pub fn type_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { debug!("type.__new__ {:?}", args); if args.args.len() == 2 { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 739012c5e..f15c5e09b 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -88,7 +88,7 @@ impl fmt::Display for PyObject { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use self::TypeProtocol; if let Some(PyClass { ref name, .. }) = self.payload::() { - let type_name = objtype::get_type_name(&self.class()); + let type_name = self.class().name.clone(); // We don't have access to a vm, so just assume that if its parent's name // is type, it's a type if type_name == "type" { @@ -101,7 +101,7 @@ impl fmt::Display for PyObject { if let Some(PyModule { ref name, .. }) = self.payload::() { return write!(f, "module '{}'", name); } - write!(f, "'{}' object", objtype::get_type_name(&self.class())) + write!(f, "'{}' object", self.class()) } } @@ -956,7 +956,7 @@ pub trait BufferProtocol { impl BufferProtocol for PyObjectRef { fn readonly(&self) -> bool { - match objtype::get_type_name(&self.class()).as_ref() { + match self.class().name.as_str() { "bytes" => false, "bytearray" | "memoryview" => true, _ => panic!("Bytes-Like type expected not {:?}", self), diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 92c264bcd..df092bdc8 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -174,11 +174,11 @@ impl VirtualMachine { b: PyObjectRef, op: &str, ) -> PyObjectRef { - let a_type_name = objtype::get_type_name(&a.class()); - let b_type_name = objtype::get_type_name(&b.class()); self.new_type_error(format!( "Unsupported operand types for '{}': '{}' and '{}'", - op, a_type_name, b_type_name + op, + a.class(), + b.class() )) }