From f38d8af12bf260af5eba41aeb7df8a859aad3fe1 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Mon, 1 Feb 2021 20:08:17 -0600 Subject: [PATCH] Fix __reduce_ex__ and unskip tests --- Lib/test/test_copy.py | 20 -------------------- Lib/test/test_fractions.py | 2 -- Lib/test/test_operator.py | 6 ------ vm/src/builtins/object.rs | 3 ++- vm/src/builtins/tuple.rs | 14 ++++++++++---- 5 files changed, 12 insertions(+), 33 deletions(-) diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index 3c16dfbe9..66beae2a7 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -64,8 +64,6 @@ class TestCopy(unittest.TestCase): self.assertIs(y, x) self.assertEqual(c, [1]) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_copy_reduce(self): class C(object): def __reduce__(self): @@ -330,8 +328,6 @@ class TestCopy(unittest.TestCase): self.assertIs(y, x) self.assertEqual(c, [1]) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_deepcopy_reduce(self): class C(object): def __reduce__(self): @@ -605,8 +601,6 @@ class TestCopy(unittest.TestCase): # _reconstruct() - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_reconstruct_string(self): class C(object): def __reduce__(self): @@ -617,8 +611,6 @@ class TestCopy(unittest.TestCase): y = copy.deepcopy(x) self.assertIs(y, x) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_reconstruct_nostate(self): class C(object): def __reduce__(self): @@ -630,8 +622,6 @@ class TestCopy(unittest.TestCase): y = copy.deepcopy(x) self.assertIs(y.__class__, x.__class__) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_reconstruct_state(self): class C(object): def __reduce__(self): @@ -646,8 +636,6 @@ class TestCopy(unittest.TestCase): self.assertEqual(y, x) self.assertIsNot(y.foo, x.foo) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_reconstruct_state_setstate(self): class C(object): def __reduce__(self): @@ -675,8 +663,6 @@ class TestCopy(unittest.TestCase): # Additions for Python 2.3 and pickle protocol 2 - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_reduce_4tuple(self): class C(list): def __reduce__(self): @@ -694,8 +680,6 @@ class TestCopy(unittest.TestCase): self.assertIsNot(x, y) self.assertIsNot(x[0], y[0]) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_reduce_5tuple(self): class C(dict): def __reduce__(self): @@ -774,8 +758,6 @@ class TestCopy(unittest.TestCase): self.assertIsNot(x[0], y[0]) self.assertIsNot(x.foo, y.foo) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_copy_tuple_subclass(self): class C(tuple): pass @@ -784,8 +766,6 @@ class TestCopy(unittest.TestCase): y = copy.copy(x) self.assertEqual(tuple(y), (1, 2, 3)) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_deepcopy_tuple_subclass(self): class C(tuple): pass diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 26d08c88c..02a502285 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -711,8 +711,6 @@ class FractionTest(unittest.TestCase): s += num / fact * sign self.assertAlmostEqual(math.cos(1), s) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_copy_deepcopy_pickle(self): r = F(13, 7) dr = DummyFraction(13, 7) diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py index f2606fd5c..f8bec32e9 100644 --- a/Lib/test/test_operator.py +++ b/Lib/test/test_operator.py @@ -525,8 +525,6 @@ class OperatorPickleTestCase: with support.swap_item(sys.modules, 'operator', self.module2): return pickle.loads(pickled) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_attrgetter(self): attrgetter = self.module.attrgetter class A: @@ -555,8 +553,6 @@ class OperatorPickleTestCase: self.assertEqual(repr(f2), repr(f)) self.assertEqual(f2(a), f(a)) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_itemgetter(self): itemgetter = self.module.itemgetter a = 'ABCDE' @@ -572,8 +568,6 @@ class OperatorPickleTestCase: self.assertEqual(repr(f2), repr(f)) self.assertEqual(f2(a), f(a)) - # TODO: RUSTPYTHON - @unittest.expectedFailure def test_methodcaller(self): methodcaller = self.module.methodcaller class A: diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index 05f1643b2..213749139 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -254,7 +254,8 @@ impl PyBaseObject { fn reduce_ex(obj: PyObjectRef, proto: usize, vm: &VirtualMachine) -> PyResult { if let Some(reduce) = vm.get_attribute_opt(obj.clone(), "__reduce__")? { let object_reduce = vm.ctx.types.object_type.get_attr("__reduce__").unwrap(); - if !reduce.is(&object_reduce) { + let class_reduce = vm.get_attribute(obj.clone_class().into_object(), "__reduce__")?; + if !class_reduce.is(&object_reduce) { return vm.invoke(&reduce, ()); } } diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index 0444fb346..4bcb73449 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -201,10 +201,16 @@ impl PyTuple { } #[pymethod(magic)] - fn getnewargs(zelf: PyRef) -> PyTuple { - PyTuple { - elements: Box::new([zelf.into_object()]), - } + fn getnewargs(zelf: PyRef, vm: &VirtualMachine) -> (PyTupleRef,) { + // the arguments to pass to tuple() is just one tuple - so we'll be doing tuple(tup), which + // should just return tup, or tuplesubclass(tup), which'll copy/validate (e.g. for a + // structseq) + let tup_arg = if zelf.class().is(&vm.ctx.types.tuple_type) { + zelf + } else { + PyTupleRef::with_elements(zelf.elements.clone().into_vec(), &vm.ctx) + }; + (tup_arg,) } #[pyslot]