mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Add builtins to sys.modules. Add __float__ method to int class.
This commit is contained in:
@@ -850,14 +850,9 @@ pub fn make_module(ctx: &PyContext) -> PyObjectRef {
|
||||
},
|
||||
ctx.type_type(),
|
||||
);
|
||||
let obj = PyObject::new(
|
||||
PyObjectKind::Module {
|
||||
name: "__builtins__".to_string(),
|
||||
dict: scope,
|
||||
},
|
||||
ctx.type_type(),
|
||||
);
|
||||
obj
|
||||
let mod_name = "__builtins__".to_string();
|
||||
let py_mod = ctx.new_module(&mod_name, scope);
|
||||
py_mod
|
||||
}
|
||||
|
||||
pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> PyResult {
|
||||
|
||||
@@ -42,8 +42,15 @@ pub fn get_value(obj: &PyObjectRef) -> f64 {
|
||||
pub fn make_float(vm: &mut VirtualMachine, obj: &PyObjectRef) -> Result<f64, PyObjectRef> {
|
||||
if objtype::isinstance(obj, &vm.ctx.float_type()) {
|
||||
Ok(get_value(obj))
|
||||
} else if objtype::isinstance(obj, &vm.ctx.int_type()) {
|
||||
Ok(objint::get_value(obj).to_f64().unwrap())
|
||||
} else if let Ok(method) = vm.get_method(obj.clone(), "__float__") {
|
||||
let res = vm.invoke(
|
||||
method,
|
||||
PyFuncArgs {
|
||||
args: vec![],
|
||||
kwargs: vec![],
|
||||
},
|
||||
)?;
|
||||
Ok(get_value(&res))
|
||||
} else {
|
||||
Err(vm.new_type_error(format!("Cannot cast {:?} to float", obj)))
|
||||
}
|
||||
|
||||
@@ -201,6 +201,12 @@ fn int_add(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
}
|
||||
}
|
||||
|
||||
fn int_float(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
arg_check!(vm, args, required = [(i, Some(vm.ctx.int_type()))]);
|
||||
let i = get_value(i);
|
||||
Ok(vm.ctx.new_float(i.to_f64().unwrap()))
|
||||
}
|
||||
|
||||
fn int_floordiv(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
arg_check!(
|
||||
vm,
|
||||
@@ -391,6 +397,7 @@ pub fn init(context: &PyContext) {
|
||||
int_type.set_attr("__add__", context.new_rustfunc(int_add));
|
||||
int_type.set_attr("__and__", context.new_rustfunc(int_and));
|
||||
int_type.set_attr("__divmod__", context.new_rustfunc(int_divmod));
|
||||
int_type.set_attr("__float__", context.new_rustfunc(int_float));
|
||||
int_type.set_attr("__floordiv__", context.new_rustfunc(int_floordiv));
|
||||
int_type.set_attr("__hash__", context.new_rustfunc(int_hash));
|
||||
int_type.set_attr("__new__", context.new_rustfunc(int_new));
|
||||
|
||||
@@ -81,7 +81,7 @@ pub fn get_type_name(typ: &PyObjectRef) -> String {
|
||||
{
|
||||
name.clone()
|
||||
} else {
|
||||
panic!("Cannot get type_name of non-type type");
|
||||
panic!("Cannot get type_name of non-type type {:?}", typ);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,10 +42,15 @@ impl VirtualMachine {
|
||||
/// Create a new `VirtualMachine` structure.
|
||||
pub fn new() -> VirtualMachine {
|
||||
let ctx = PyContext::new();
|
||||
|
||||
// Hard-core modules:
|
||||
let builtins = builtins::make_module(&ctx);
|
||||
let sysmod = sysmodule::mk_module(&ctx);
|
||||
|
||||
// Add builtins as builtins module:
|
||||
// sysmod.get_attr("modules").unwrap().set_item("builtins", builtins.clone());
|
||||
let modules = sysmod.get_attr("modules").unwrap();
|
||||
modules.set_item("builtins", builtins.clone());
|
||||
|
||||
let stdlib_inits = stdlib::get_module_inits();
|
||||
VirtualMachine {
|
||||
builtins: builtins,
|
||||
|
||||
Reference in New Issue
Block a user