Skip to content

Commit 8eacbcb

Browse files
committed
Add builtins to sys.modules. Add __float__ method to int class.
1 parent f227ce0 commit 8eacbcb

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

vm/src/builtins.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -850,14 +850,9 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
850850
},
851851
ctx.type_type(),
852852
);
853-
let obj = PyObject::new(
854-
PyObjectKind::Module {
855-
name: "__builtins__".to_string(),
856-
dict: scope,
857-
},
858-
ctx.type_type(),
859-
);
860-
obj
853+
let mod_name = "__builtins__".to_string();
854+
let py_mod = ctx.new_module(&mod_name, scope);
855+
py_mod
861856
}
862857

863858
pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {

vm/src/obj/objfloat.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ pub fn get_value(obj: &PyObjectRef) -> f64 {
4242
pub fn make_float(vm: &mut VirtualMachine, obj: &PyObjectRef) -> Result<f64, PyObjectRef> {
4343
if objtype::isinstance(obj, &vm.ctx.float_type()) {
4444
Ok(get_value(obj))
45-
} else if objtype::isinstance(obj, &vm.ctx.int_type()) {
46-
Ok(objint::get_value(obj).to_f64().unwrap())
45+
} else if let Ok(method) = vm.get_method(obj.clone(), "__float__") {
46+
let res = vm.invoke(
47+
method,
48+
PyFuncArgs {
49+
args: vec![],
50+
kwargs: vec![],
51+
},
52+
)?;
53+
Ok(get_value(&res))
4754
} else {
4855
Err(vm.new_type_error(format!("Cannot cast {:?} to float", obj)))
4956
}

vm/src/obj/objint.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ fn int_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
201201
}
202202
}
203203

204+
fn int_float(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
205+
arg_check!(vm, args, required = [(i, Some(vm.ctx.int_type()))]);
206+
let i = get_value(i);
207+
Ok(vm.ctx.new_float(i.to_f64().unwrap()))
208+
}
209+
204210
fn int_floordiv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
205211
arg_check!(
206212
vm,
@@ -391,6 +397,7 @@ pub fn init(context: &PyContext) {
391397
int_type.set_attr("__add__", context.new_rustfunc(int_add));
392398
int_type.set_attr("__and__", context.new_rustfunc(int_and));
393399
int_type.set_attr("__divmod__", context.new_rustfunc(int_divmod));
400+
int_type.set_attr("__float__", context.new_rustfunc(int_float));
394401
int_type.set_attr("__floordiv__", context.new_rustfunc(int_floordiv));
395402
int_type.set_attr("__hash__", context.new_rustfunc(int_hash));
396403
int_type.set_attr("__new__", context.new_rustfunc(int_new));

vm/src/obj/objtype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn get_type_name(typ: &PyObjectRef) -> String {
8181
{
8282
name.clone()
8383
} else {
84-
panic!("Cannot get type_name of non-type type");
84+
panic!("Cannot get type_name of non-type type {:?}", typ);
8585
}
8686
}
8787

vm/src/vm.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ impl VirtualMachine {
4242
/// Create a new `VirtualMachine` structure.
4343
pub fn new() -> VirtualMachine {
4444
let ctx = PyContext::new();
45+
46+
// Hard-core modules:
4547
let builtins = builtins::make_module(&ctx);
4648
let sysmod = sysmodule::mk_module(&ctx);
49+
4750
// Add builtins as builtins module:
48-
// sysmod.get_attr("modules").unwrap().set_item("builtins", builtins.clone());
51+
let modules = sysmod.get_attr("modules").unwrap();
52+
modules.set_item("builtins", builtins.clone());
53+
4954
let stdlib_inits = stdlib::get_module_inits();
5055
VirtualMachine {
5156
builtins: builtins,

0 commit comments

Comments
 (0)