forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.rs
More file actions
45 lines (37 loc) · 1.43 KB
/
types.rs
File metadata and controls
45 lines (37 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Dynamic type creation and names for built in types.
*/
use super::super::obj::{objsequence, objstr, objtype};
use super::super::pyobject::{PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol};
use super::super::VirtualMachine;
fn types_new_class(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [(name, Some(vm.ctx.str_type()))],
optional = [(bases, None), (_kwds, None), (_exec_body, None)]
);
let name = objstr::get_value(name);
let dict = vm.ctx.new_dict();
let bases = match bases {
Some(b) => {
if objtype::isinstance(b, &vm.ctx.tuple_type()) {
objsequence::get_elements(b).to_vec()
} else {
return Err(vm.new_type_error("Bases must be a tuple".to_string()));
}
}
None => vec![vm.ctx.object()],
};
objtype::new(vm.ctx.type_type(), &name, bases, dict)
}
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module(&"types".to_string(), ctx.new_scope(None));
// Number theory functions:
ctx.set_attr(&py_mod, "new_class", ctx.new_rustfunc(types_new_class));
ctx.set_attr(&py_mod, "FunctionType", ctx.function_type());
ctx.set_attr(&py_mod, "LambdaType", ctx.function_type());
ctx.set_attr(&py_mod, "CodeType", ctx.code_type());
ctx.set_attr(&py_mod, "FrameType", ctx.frame_type());
py_mod
}