fix: make dict.{__or__,__ror__,__ior__} with other types should return NotImplemented

This commit is contained in:
voidsatisfaction
2021-08-11 20:11:23 +09:00
parent 56469ae204
commit 3500a58138
2 changed files with 10 additions and 22 deletions

View File

@@ -42,27 +42,15 @@ def test_dunion_ror0():
def test_dunion_other_types():
def perf_test_or(other_obj):
d={1:2}
try:
d.__or__(other_obj)
except:
return True
return False
return d.__or__(other_obj) is NotImplemented
def perf_test_ior(other_obj):
d={1:2}
try:
d.__ior__(other_obj)
except:
return True
return False
return d.__ior__(other_obj) is NotImplemented
def perf_test_ror(other_obj):
d={1:2}
try:
d.__ror__(other_obj)
except:
return True
return False
return d.__ror__(other_obj) is NotImplemented
test_fct={'__or__':perf_test_or, '__ror__':perf_test_ror, '__ior__':perf_test_ior}
others=['FooBar', 42, [36], set([19]), ['aa'], None]

View File

@@ -316,29 +316,29 @@ impl PyDict {
PyDict::merge_dict(&zelf.entries, other, vm)?;
return Ok(zelf.into_object());
}
Err(vm.new_type_error("__ior__ not implemented for non-dict type".to_owned()))
Ok(vm.ctx.not_implemented())
}
#[pymethod(name = "__ror__")]
fn ror(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyDict> {
fn ror(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let dicted: Result<PyDictRef, _> = other.downcast();
if let Ok(other) = dicted {
let other_cp = other.copy();
PyDict::merge_dict(&other_cp.entries, zelf, vm)?;
return Ok(other_cp);
return Ok(other_cp.into_object(vm));
}
Err(vm.new_type_error("__ror__ not implemented for non-dict type".to_owned()))
Ok(vm.ctx.not_implemented())
}
#[pymethod(name = "__or__")]
fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyDict> {
fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let dicted: Result<PyDictRef, _> = other.downcast();
if let Ok(other) = dicted {
let self_cp = self.copy();
PyDict::merge_dict(&self_cp.entries, other, vm)?;
return Ok(self_cp);
return Ok(self_cp.into_object(vm));
}
Err(vm.new_type_error("__or__ not implemented for non-dict type".to_owned()))
Ok(vm.ctx.not_implemented())
}
#[pymethod]