Skip to content

Commit 6474a4a

Browse files
Remove objtype::get_type_name()
1 parent cc4f3fd commit 6474a4a

File tree

10 files changed

+25
-47
lines changed

10 files changed

+25
-47
lines changed

vm/src/builtins.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ fn builtin_len(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
368368
Ok(value) => vm.invoke(value, PyFuncArgs::default()),
369369
Err(..) => Err(vm.new_type_error(format!(
370370
"object of type '{}' has no method {:?}",
371-
objtype::get_type_name(&obj.class()),
371+
obj.class(),
372372
len_method_name
373373
))),
374374
}
@@ -605,10 +605,7 @@ fn builtin_reversed(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
605605
match vm.get_method(obj.clone(), "__reversed__") {
606606
Ok(value) => vm.invoke(value, PyFuncArgs::default()),
607607
// TODO: fallback to using __len__ and __getitem__, if object supports sequence protocol
608-
Err(..) => Err(vm.new_type_error(format!(
609-
"'{}' object is not reversible",
610-
objtype::get_type_name(&obj.class()),
611-
))),
608+
Err(..) => Err(vm.new_type_error(format!("'{}' object is not reversible", obj.class()))),
612609
}
613610
}
614611
// builtin_reversed

vm/src/exceptions.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ fn exception_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
6868
args,
6969
required = [(exc, Some(vm.ctx.exceptions.exception_type.clone()))]
7070
);
71-
let type_name = objtype::get_type_name(&exc.class());
7271
let msg = if let Ok(m) = vm.get_attribute(exc.clone(), "msg") {
7372
match vm.to_pystr(&m) {
7473
Ok(msg) => msg,
@@ -77,7 +76,7 @@ fn exception_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
7776
} else {
7877
panic!("Error message must be set");
7978
};
80-
let s = format!("{}: {}", type_name, msg);
79+
let s = format!("{}: {}", exc.class(), msg);
8180
Ok(vm.new_str(s))
8281
}
8382

vm/src/frame.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,20 +1091,18 @@ impl Frame {
10911091
fn _in(&self, vm: &VirtualMachine, needle: PyObjectRef, haystack: PyObjectRef) -> PyResult {
10921092
match self._membership(vm, needle, &haystack) {
10931093
Ok(found) => Ok(found),
1094-
Err(_) => Err(vm.new_type_error(format!(
1095-
"{} has no __contains__ method",
1096-
objtype::get_type_name(&haystack.class())
1097-
))),
1094+
Err(_) => {
1095+
Err(vm.new_type_error(format!("{} has no __contains__ method", haystack.class())))
1096+
}
10981097
}
10991098
}
11001099

11011100
fn _not_in(&self, vm: &VirtualMachine, needle: PyObjectRef, haystack: PyObjectRef) -> PyResult {
11021101
match self._membership(vm, needle, &haystack) {
11031102
Ok(found) => Ok(vm.ctx.new_bool(!objbool::get_value(&found))),
1104-
Err(_) => Err(vm.new_type_error(format!(
1105-
"{} has no __contains__ method",
1106-
objtype::get_type_name(&haystack.class())
1107-
))),
1103+
Err(_) => {
1104+
Err(vm.new_type_error(format!("{} has no __contains__ method", haystack.class())))
1105+
}
11081106
}
11091107
}
11101108

vm/src/obj/objfloat.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ impl PyFloatRef {
190190
}
191191
}
192192
} else {
193-
let type_name = objtype::get_type_name(&arg.class());
194-
return Err(vm.new_type_error(format!("can't convert {} to float", type_name)));
193+
return Err(vm.new_type_error(format!("can't convert {} to float", arg.class())));
195194
};
196195
PyFloat { value }.into_ref_with_type(vm, cls)
197196
}

vm/src/obj/objint.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,7 @@ impl PyIntRef {
211211

212212
fn lshift(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
213213
if !objtype::isinstance(&other, &vm.ctx.int_type()) {
214-
return Err(vm.new_type_error(format!(
215-
"unsupported operand type(s) for << '{}' and '{}'",
216-
objtype::get_type_name(&self.as_object().class()),
217-
objtype::get_type_name(&other.class())
218-
)));
214+
return Ok(vm.ctx.not_implemented());
219215
}
220216

221217
if let Some(n_bits) = get_value(&other).to_usize() {
@@ -234,11 +230,7 @@ impl PyIntRef {
234230

235231
fn rshift(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
236232
if !objtype::isinstance(&other, &vm.ctx.int_type()) {
237-
return Err(vm.new_type_error(format!(
238-
"unsupported operand type(s) for >> '{}' and '{}'",
239-
objtype::get_type_name(&self.as_object().class()),
240-
objtype::get_type_name(&other.class())
241-
)));
233+
return Ok(vm.ctx.not_implemented());
242234
}
243235

244236
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
420412
}
421413
}
422414
} else {
423-
let type_name = objtype::get_type_name(&obj.class());
424415
return Err(vm.new_type_error(format!(
425416
"int() argument must be a string or a number, not '{}'",
426-
type_name
417+
obj.class()
427418
)));
428419
};
429420
Ok(val)

vm/src/obj/objobject.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ fn object_setattr(
118118
dict.set_item(&vm.ctx, &attr_name.value, value);
119119
Ok(())
120120
} else {
121-
let type_name = objtype::get_type_name(&obj.class());
122121
Err(vm.new_attribute_error(format!(
123122
"'{}' object has no attribute '{}'",
124-
type_name, &attr_name.value
123+
obj.class(),
124+
&attr_name.value
125125
)))
126126
}
127127
}
@@ -139,10 +139,10 @@ fn object_delattr(obj: PyObjectRef, attr_name: PyStringRef, vm: &VirtualMachine)
139139
dict.del_item(&attr_name.value);
140140
Ok(())
141141
} else {
142-
let type_name = objtype::get_type_name(&obj.class());
143142
Err(vm.new_attribute_error(format!(
144143
"'{}' object has no attribute '{}'",
145-
type_name, &attr_name.value
144+
obj.class(),
145+
&attr_name.value
146146
)))
147147
}
148148
}
@@ -154,9 +154,8 @@ fn object_str(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
154154

155155
fn object_repr(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
156156
arg_check!(vm, args, required = [(obj, Some(vm.ctx.object()))]);
157-
let type_name = objtype::get_type_name(&obj.class());
158157
let address = obj.get_id();
159-
Ok(vm.new_str(format!("<{} object at 0x{:x}>", type_name, address)))
158+
Ok(vm.new_str(format!("<{} object at 0x{:x}>", obj.class(), address)))
160159
}
161160

162161
pub fn object_dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyList {

vm/src/obj/objsuper.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ fn super_new(
112112

113113
// Check type argument:
114114
if !objtype::isinstance(py_type.as_object(), &vm.get_type()) {
115-
let type_name = objtype::get_type_name(&py_type.as_object().class());
116115
return Err(vm.new_type_error(format!(
117116
"super() argument 1 must be type, not {}",
118-
type_name
117+
py_type.class()
119118
)));
120119
}
121120

vm/src/obj/objtype.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,6 @@ pub fn issubclass(subclass: &PyClassRef, cls: &PyClassRef) -> bool {
223223
subclass.is(cls) || mro.iter().any(|c| c.is(cls.as_object()))
224224
}
225225

226-
pub fn get_type_name(typ: &PyClassRef) -> String {
227-
typ.name.clone()
228-
}
229-
230226
pub fn type_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
231227
debug!("type.__new__ {:?}", args);
232228
if args.args.len() == 2 {

vm/src/pyobject.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl fmt::Display for PyObject<dyn PyObjectPayload> {
8888
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8989
use self::TypeProtocol;
9090
if let Some(PyClass { ref name, .. }) = self.payload::<PyClass>() {
91-
let type_name = objtype::get_type_name(&self.class());
91+
let type_name = self.class().name.clone();
9292
// We don't have access to a vm, so just assume that if its parent's name
9393
// is type, it's a type
9494
if type_name == "type" {
@@ -101,7 +101,7 @@ impl fmt::Display for PyObject<dyn PyObjectPayload> {
101101
if let Some(PyModule { ref name, .. }) = self.payload::<PyModule>() {
102102
return write!(f, "module '{}'", name);
103103
}
104-
write!(f, "'{}' object", objtype::get_type_name(&self.class()))
104+
write!(f, "'{}' object", self.class())
105105
}
106106
}
107107

@@ -956,7 +956,7 @@ pub trait BufferProtocol {
956956

957957
impl BufferProtocol for PyObjectRef {
958958
fn readonly(&self) -> bool {
959-
match objtype::get_type_name(&self.class()).as_ref() {
959+
match self.class().name.as_str() {
960960
"bytes" => false,
961961
"bytearray" | "memoryview" => true,
962962
_ => panic!("Bytes-Like type expected not {:?}", self),

vm/src/vm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ impl VirtualMachine {
174174
b: PyObjectRef,
175175
op: &str,
176176
) -> PyObjectRef {
177-
let a_type_name = objtype::get_type_name(&a.class());
178-
let b_type_name = objtype::get_type_name(&b.class());
179177
self.new_type_error(format!(
180178
"Unsupported operand types for '{}': '{}' and '{}'",
181-
op, a_type_name, b_type_name
179+
op,
180+
a.class(),
181+
b.class()
182182
))
183183
}
184184

0 commit comments

Comments
 (0)