From 3500a58138f9f96b8adccb8307000c8e8dbd8f5f Mon Sep 17 00:00:00 2001 From: voidsatisfaction Date: Wed, 11 Aug 2021 20:11:23 +0900 Subject: [PATCH] fix: make dict.{__or__,__ror__,__ior__} with other types should return NotImplemented --- extra_tests/snippets/dict_union.py | 18 +++--------------- vm/src/builtins/dict.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/extra_tests/snippets/dict_union.py b/extra_tests/snippets/dict_union.py index 29e0718d4..34bff423c 100644 --- a/extra_tests/snippets/dict_union.py +++ b/extra_tests/snippets/dict_union.py @@ -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] diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index d5d3988e8..283dcc10d 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -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, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { + fn ror(zelf: PyRef, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { let dicted: Result = 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 { + fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult { let dicted: Result = 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]