Skip to content

Commit 83788b9

Browse files
Merge pull request RustPython#667 from adrian17/no-option-typ
Remove Option from PyObject.typ; Refactor type hierarchy initialization.
2 parents 051b382 + b334689 commit 83788b9

File tree

6 files changed

+109
-177
lines changed

6 files changed

+109
-177
lines changed

vm/src/exceptions.rs

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -108,56 +108,29 @@ pub struct ExceptionZoo {
108108
}
109109

110110
impl ExceptionZoo {
111-
pub fn new(
112-
type_type: &PyObjectRef,
113-
object_type: &PyObjectRef,
114-
dict_type: &PyObjectRef,
115-
) -> Self {
111+
pub fn new(type_type: &PyObjectRef, object_type: &PyObjectRef) -> Self {
116112
// Sorted By Hierarchy then alphabetized.
117-
let base_exception_type =
118-
create_type("BaseException", &type_type, &object_type, &dict_type);
119-
120-
let exception_type = create_type("Exception", &type_type, &base_exception_type, &dict_type);
121-
122-
let arithmetic_error =
123-
create_type("ArithmeticError", &type_type, &exception_type, &dict_type);
124-
let assertion_error =
125-
create_type("AssertionError", &type_type, &exception_type, &dict_type);
126-
let attribute_error =
127-
create_type("AttributeError", &type_type, &exception_type, &dict_type);
128-
let import_error = create_type("ImportError", &type_type, &exception_type, &dict_type);
129-
let index_error = create_type("IndexError", &type_type, &exception_type, &dict_type);
130-
let key_error = create_type("KeyError", &type_type, &exception_type, &dict_type);
131-
let name_error = create_type("NameError", &type_type, &exception_type, &dict_type);
132-
let os_error = create_type("OSError", &type_type, &exception_type, &dict_type);
133-
let runtime_error = create_type("RuntimeError", &type_type, &exception_type, &dict_type);
134-
let stop_iteration = create_type("StopIteration", &type_type, &exception_type, &dict_type);
135-
let syntax_error = create_type("SyntaxError", &type_type, &exception_type, &dict_type);
136-
let type_error = create_type("TypeError", &type_type, &exception_type, &dict_type);
137-
let value_error = create_type("ValueError", &type_type, &exception_type, &dict_type);
138-
139-
let overflow_error =
140-
create_type("OverflowError", &type_type, &arithmetic_error, &dict_type);
141-
let zero_division_error = create_type(
142-
"ZeroDivisionError",
143-
&type_type,
144-
&arithmetic_error,
145-
&dict_type,
146-
);
147-
148-
let module_not_found_error =
149-
create_type("ModuleNotFoundError", &type_type, &import_error, &dict_type);
150-
151-
let not_implemented_error = create_type(
152-
"NotImplementedError",
153-
&type_type,
154-
&runtime_error,
155-
&dict_type,
156-
);
157-
158-
let file_not_found_error =
159-
create_type("FileNotFoundError", &type_type, &os_error, &dict_type);
160-
let permission_error = create_type("PermissionError", &type_type, &os_error, &dict_type);
113+
let base_exception_type = create_type("BaseException", &type_type, &object_type);
114+
let exception_type = create_type("Exception", &type_type, &base_exception_type);
115+
let arithmetic_error = create_type("ArithmeticError", &type_type, &exception_type);
116+
let assertion_error = create_type("AssertionError", &type_type, &exception_type);
117+
let attribute_error = create_type("AttributeError", &type_type, &exception_type);
118+
let import_error = create_type("ImportError", &type_type, &exception_type);
119+
let index_error = create_type("IndexError", &type_type, &exception_type);
120+
let key_error = create_type("KeyError", &type_type, &exception_type);
121+
let name_error = create_type("NameError", &type_type, &exception_type);
122+
let os_error = create_type("OSError", &type_type, &exception_type);
123+
let runtime_error = create_type("RuntimeError", &type_type, &exception_type);
124+
let stop_iteration = create_type("StopIteration", &type_type, &exception_type);
125+
let syntax_error = create_type("SyntaxError", &type_type, &exception_type);
126+
let type_error = create_type("TypeError", &type_type, &exception_type);
127+
let value_error = create_type("ValueError", &type_type, &exception_type);
128+
let overflow_error = create_type("OverflowError", &type_type, &arithmetic_error);
129+
let zero_division_error = create_type("ZeroDivisionError", &type_type, &arithmetic_error);
130+
let module_not_found_error = create_type("ModuleNotFoundError", &type_type, &import_error);
131+
let not_implemented_error = create_type("NotImplementedError", &type_type, &runtime_error);
132+
let file_not_found_error = create_type("FileNotFoundError", &type_type, &os_error);
133+
let permission_error = create_type("PermissionError", &type_type, &os_error);
161134

162135
ExceptionZoo {
163136
arithmetic_error,

vm/src/obj/objdict.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::collections::HashMap;
33
use std::ops::{Deref, DerefMut};
44

55
use crate::pyobject::{
6-
FromPyObjectRef, PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectRef,
7-
PyRef, PyResult, PyValue, TypeProtocol,
6+
PyAttributes, PyContext, PyFuncArgs, PyIteratorValue, PyObject, PyObjectRef, PyRef, PyResult,
7+
PyValue, TypeProtocol,
88
};
99
use crate::vm::{ReprGuard, VirtualMachine};
1010

@@ -362,20 +362,6 @@ fn dict_get(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
362362
}
363363
}
364364

365-
pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, dict_type: PyObjectRef) {
366-
let object_type = FromPyObjectRef::from_pyobj(&object_type);
367-
// this is not ideal
368-
let ptr = PyObjectRef::into_raw(dict_type.clone()) as *mut PyObject;
369-
unsafe {
370-
(*ptr).payload = Box::new(objtype::PyClass {
371-
name: String::from("dict"),
372-
mro: vec![object_type],
373-
});
374-
(*ptr).dict = Some(RefCell::new(HashMap::new()));
375-
(*ptr).typ = Some(type_type.clone());
376-
}
377-
}
378-
379365
pub fn init(context: &PyContext) {
380366
let dict_type = &context.dict_type;
381367
context.set_attr(&dict_type, "__len__", context.new_rustfunc(dict_len));

vm/src/obj/objobject.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ use super::objstr;
33
use super::objtype;
44
use crate::obj::objproperty::PropertyBuilder;
55
use crate::pyobject::{
6-
AttributeProtocol, DictProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObject,
7-
PyObjectRef, PyRef, PyResult, TypeProtocol,
6+
AttributeProtocol, DictProtocol, IdProtocol, PyAttributes, PyContext, PyFuncArgs, PyObjectRef,
7+
PyRef, PyResult, TypeProtocol,
88
};
99
use crate::vm::VirtualMachine;
10-
use std::cell::RefCell;
11-
use std::collections::HashMap;
1210

1311
#[derive(Clone, Debug)]
1412
pub struct PyInstance;
@@ -22,19 +20,6 @@ pub fn new_instance(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
2220
Ok(obj)
2321
}
2422

25-
pub fn create_object(type_type: PyObjectRef, object_type: PyObjectRef, _dict_type: PyObjectRef) {
26-
// this is not ideal
27-
let ptr = PyObjectRef::into_raw(object_type.clone()) as *mut PyObject;
28-
unsafe {
29-
(*ptr).payload = Box::new(objtype::PyClass {
30-
name: String::from("object"),
31-
mro: vec![],
32-
});
33-
(*ptr).dict = Some(RefCell::new(HashMap::new()));
34-
(*ptr).typ = Some(type_type.clone());
35-
}
36-
}
37-
3823
fn object_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
3924
arg_check!(
4025
vm,

vm/src/obj/objtype.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,6 @@ impl PyClassRef {
108108
* The magical type type
109109
*/
110110

111-
pub fn create_type(type_type: PyObjectRef, object_type: PyObjectRef, _dict_type: PyObjectRef) {
112-
let object_type = FromPyObjectRef::from_pyobj(&object_type);
113-
// this is not ideal
114-
let ptr = PyObjectRef::into_raw(type_type.clone()) as *mut PyObject;
115-
unsafe {
116-
(*ptr).payload = Box::new(PyClass {
117-
name: String::from("type"),
118-
mro: vec![object_type],
119-
});
120-
(*ptr).dict = Some(RefCell::new(PyAttributes::new()));
121-
(*ptr).typ = Some(type_type);
122-
}
123-
}
124-
125111
pub fn init(ctx: &PyContext) {
126112
let type_doc = "type(object_or_name, bases, dict)\n\
127113
type(object) -> the object's type\n\
@@ -349,7 +335,7 @@ pub fn new(
349335
mro,
350336
}),
351337
dict: Some(RefCell::new(dict)),
352-
typ: Some(typ),
338+
typ,
353339
}
354340
.into_ref())
355341
}

0 commit comments

Comments
 (0)