mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
7 files changed
+50
-32
No files matched your search
@@ -16,6 +16,9 @@ assert bool() == False
|
||||
assert bool(1) == True
|
||||
assert bool({}) == False
|
||||
|
||||
assert bool(NotImplemented) == True
|
||||
assert bool(...) == True
|
||||
|
||||
if not 1:
|
||||
raise BaseException
|
||||
|
||||
|
||||
+17
-16
@@ -286,10 +286,14 @@ impl Frame {
|
||||
|
||||
let mut out: Vec<Option<BigInt>> = elements
|
||||
.into_iter()
|
||||
.map(|x| match x.payload {
|
||||
PyObjectPayload::Integer { ref value } => Some(value.clone()),
|
||||
PyObjectPayload::None => None,
|
||||
_ => panic!("Expect Int or None as BUILD_SLICE arguments, got {:?}", x),
|
||||
.map(|x| {
|
||||
if x.is(&vm.get_none()) {
|
||||
None
|
||||
} else if let PyObjectPayload::Integer { ref value } = x.payload {
|
||||
Some(value.clone())
|
||||
} else {
|
||||
panic!("Expect Int or None as BUILD_SLICE arguments, got {:?}", x);
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -575,18 +579,15 @@ impl Frame {
|
||||
}
|
||||
bytecode::Instruction::PrintExpr => {
|
||||
let expr = self.pop_value();
|
||||
match expr.payload {
|
||||
PyObjectPayload::None => (),
|
||||
_ => {
|
||||
let repr = vm.to_repr(&expr)?;
|
||||
builtins::builtin_print(
|
||||
vm,
|
||||
PyFuncArgs {
|
||||
args: vec![repr],
|
||||
kwargs: vec![],
|
||||
},
|
||||
)?;
|
||||
}
|
||||
if !expr.is(&vm.get_none()) {
|
||||
let repr = vm.to_repr(&expr)?;
|
||||
builtins::builtin_print(
|
||||
vm,
|
||||
PyFuncArgs {
|
||||
args: vec![repr],
|
||||
kwargs: vec![],
|
||||
},
|
||||
)?;
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ impl IntoPyObject for bool {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObjectRef> {
|
||||
pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> PyResult<bool> {
|
||||
if let Some(s) = obj.payload::<PyString>() {
|
||||
return Ok(!s.value.is_empty());
|
||||
}
|
||||
@@ -27,7 +27,6 @@ pub fn boolval(vm: &mut VirtualMachine, obj: PyObjectRef) -> Result<bool, PyObje
|
||||
let result = match obj.payload {
|
||||
PyObjectPayload::Integer { ref value } => !value.is_zero(),
|
||||
PyObjectPayload::Sequence { ref elements } => !elements.borrow().is_empty(),
|
||||
PyObjectPayload::None { .. } => false,
|
||||
_ => {
|
||||
if let Ok(f) = vm.get_method(obj.clone(), "__bool__") {
|
||||
let bool_res = vm.invoke(f, PyFuncArgs::default())?;
|
||||
|
||||
@@ -5,6 +5,7 @@ pub fn init(context: &PyContext) {
|
||||
let none_type = &context.none.typ();
|
||||
context.set_attr(&none_type, "__new__", context.new_rustfunc(none_new));
|
||||
context.set_attr(&none_type, "__repr__", context.new_rustfunc(none_repr));
|
||||
context.set_attr(&none_type, "__bool__", context.new_rustfunc(none_bool));
|
||||
}
|
||||
|
||||
fn none_new(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
@@ -20,3 +21,8 @@ fn none_repr(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]);
|
||||
Ok(vm.ctx.new_str("None".to_string()))
|
||||
}
|
||||
|
||||
fn none_bool(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
|
||||
arg_check!(vm, args, required = [(_zelf, Some(vm.ctx.none().typ()))]);
|
||||
Ok(vm.ctx.new_bool(false))
|
||||
}
|
||||
+15
-8
@@ -156,7 +156,9 @@ pub struct PyContext {
|
||||
|
||||
fn _nothing() -> PyObjectRef {
|
||||
PyObject {
|
||||
payload: PyObjectPayload::None,
|
||||
payload: PyObjectPayload::AnyRustValue {
|
||||
value: Box::new(()),
|
||||
},
|
||||
typ: None,
|
||||
}
|
||||
.into_ref()
|
||||
@@ -224,14 +226,23 @@ impl PyContext {
|
||||
let exceptions = exceptions::ExceptionZoo::new(&type_type, &object_type, &dict_type);
|
||||
|
||||
let none = PyObject::new(
|
||||
PyObjectPayload::None,
|
||||
PyObjectPayload::AnyRustValue {
|
||||
value: Box::new(()),
|
||||
},
|
||||
create_type("NoneType", &type_type, &object_type, &dict_type),
|
||||
);
|
||||
|
||||
let ellipsis = PyObject::new(PyObjectPayload::None, ellipsis_type.clone());
|
||||
let ellipsis = PyObject::new(
|
||||
PyObjectPayload::AnyRustValue {
|
||||
value: Box::new(()),
|
||||
},
|
||||
ellipsis_type.clone(),
|
||||
);
|
||||
|
||||
let not_implemented = PyObject::new(
|
||||
PyObjectPayload::NotImplemented,
|
||||
PyObjectPayload::AnyRustValue {
|
||||
value: Box::new(()),
|
||||
},
|
||||
create_type("NotImplementedType", &type_type, &object_type, &dict_type),
|
||||
);
|
||||
|
||||
@@ -1500,8 +1511,6 @@ pub enum PyObjectPayload {
|
||||
name: String,
|
||||
scope: ScopeRef,
|
||||
},
|
||||
None,
|
||||
NotImplemented,
|
||||
Class {
|
||||
name: String,
|
||||
dict: RefCell<PyAttributes>,
|
||||
@@ -1542,8 +1551,6 @@ impl fmt::Debug for PyObjectPayload {
|
||||
ref object,
|
||||
} => write!(f, "bound-method: {:?} of {:?}", function, object),
|
||||
PyObjectPayload::Module { .. } => write!(f, "module"),
|
||||
PyObjectPayload::None => write!(f, "None"),
|
||||
PyObjectPayload::NotImplemented => write!(f, "NotImplemented"),
|
||||
PyObjectPayload::Class { ref name, .. } => write!(f, "class {:?}", name),
|
||||
PyObjectPayload::Instance { .. } => write!(f, "instance"),
|
||||
PyObjectPayload::RustFunction { .. } => write!(f, "rust function"),
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::obj::{
|
||||
objtype,
|
||||
};
|
||||
use crate::pyobject::{
|
||||
create_type, DictProtocol, PyContext, PyFuncArgs, PyObjectPayload, PyObjectRef, PyResult,
|
||||
create_type, DictProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult,
|
||||
TypeProtocol,
|
||||
};
|
||||
use crate::VirtualMachine;
|
||||
@@ -69,7 +69,7 @@ impl<'s> serde::Serialize for PyObjectSerializer<'s> {
|
||||
map.serialize_entry(&key, &self.clone_with_object(&e.1))?;
|
||||
}
|
||||
map.end()
|
||||
} else if let PyObjectPayload::None = self.pyobject.payload {
|
||||
} else if self.pyobject.is(&self.vm.get_none()) {
|
||||
serializer.serialize_none()
|
||||
} else {
|
||||
Err(serde::ser::Error::custom(format!(
|
||||
|
||||
+6
-4
@@ -444,10 +444,12 @@ impl VirtualMachine {
|
||||
// Add missing positional arguments, if we have fewer positional arguments than the
|
||||
// function definition calls for
|
||||
if nargs < nexpected_args {
|
||||
let available_defaults = match defaults.payload {
|
||||
PyObjectPayload::Sequence { ref elements } => elements.borrow().clone(),
|
||||
PyObjectPayload::None => vec![],
|
||||
_ => panic!("function defaults not tuple or None"),
|
||||
let available_defaults = if defaults.is(&self.get_none()) {
|
||||
vec![]
|
||||
} else if let PyObjectPayload::Sequence { ref elements } = defaults.payload {
|
||||
elements.borrow().clone()
|
||||
} else {
|
||||
panic!("function defaults not tuple or None");
|
||||
};
|
||||
|
||||
// Given the number of defaults available, check all the arguments for which we
|
||||
|
||||
Reference in new issue
Block a user