Remove objtype::get_type_name()

This commit is contained in:
Joey Hain
2019-03-25 19:18:07 -07:00
parent cc4f3fdb40
commit 6474a4a6ef
10 changed files with 25 additions and 47 deletions

View File

@@ -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

View File

@@ -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))
}

View File

@@ -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())))
}
}
}

View File

@@ -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)
}

View File

@@ -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<Big
}
}
} else {
let type_name = objtype::get_type_name(&obj.class());
return Err(vm.new_type_error(format!(
"int() argument must be a string or a number, not '{}'",
type_name
obj.class()
)));
};
Ok(val)

View File

@@ -118,10 +118,10 @@ fn object_setattr(
dict.set_item(&vm.ctx, &attr_name.value, value);
Ok(())
} else {
let type_name = objtype::get_type_name(&obj.class());
Err(vm.new_attribute_error(format!(
"'{}' object has no attribute '{}'",
type_name, &attr_name.value
obj.class(),
&attr_name.value
)))
}
}
@@ -139,10 +139,10 @@ fn object_delattr(obj: PyObjectRef, attr_name: PyStringRef, vm: &VirtualMachine)
dict.del_item(&attr_name.value);
Ok(())
} else {
let type_name = objtype::get_type_name(&obj.class());
Err(vm.new_attribute_error(format!(
"'{}' object has no attribute '{}'",
type_name, &attr_name.value
obj.class(),
&attr_name.value
)))
}
}
@@ -154,9 +154,8 @@ fn object_str(vm: &VirtualMachine, args: PyFuncArgs) -> 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 {

View File

@@ -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()
)));
}

View File

@@ -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 {

View File

@@ -88,7 +88,7 @@ impl fmt::Display for PyObject<dyn PyObjectPayload> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::TypeProtocol;
if let Some(PyClass { ref name, .. }) = self.payload::<PyClass>() {
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<dyn PyObjectPayload> {
if let Some(PyModule { ref name, .. }) = self.payload::<PyModule>() {
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),

View File

@@ -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()
))
}