From f70eee4cce17deba190f2e0018c26430f7ab4da2 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 6 May 2022 20:15:02 -0700 Subject: [PATCH] Fixed broken tests by implementing RepeatedScalarContainer.__reduce__. PiperOrigin-RevId: 447125225 --- python/pb_unit_tests/message_test_wrapper.py | 2 -- python/repeated.c | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/python/pb_unit_tests/message_test_wrapper.py b/python/pb_unit_tests/message_test_wrapper.py index dde0d94188..ac1b6e74c6 100644 --- a/python/pb_unit_tests/message_test_wrapper.py +++ b/python/pb_unit_tests/message_test_wrapper.py @@ -46,8 +46,6 @@ MessageTest.testFloatPrinting_proto3.__unittest_expecting_failure__ = True Proto3Test.testCopyFromBadType.__unittest_expecting_failure__ = True Proto3Test.testMergeFromBadType.__unittest_expecting_failure__ = True -MessageTest.testPickleRepeatedScalarContainer_proto2.__unittest_expecting_failure__ = True -MessageTest.testPickleRepeatedScalarContainer_proto3.__unittest_expecting_failure__ = True Proto2Test.testPythonicInit.__unittest_expecting_failure__ = True Proto2Test.test_documentation.__unittest_expecting_failure__ = True Proto3Test.testModifyMapEntryWhileIterating.__unittest_expecting_failure__ = True diff --git a/python/repeated.c b/python/repeated.c index 16407a508f..5f32b0042a 100644 --- a/python/repeated.c +++ b/python/repeated.c @@ -649,7 +649,6 @@ static PyObject* PyUpb_RepeatedContainer_Insert(PyObject* _self, } static PyMethodDef PyUpb_RepeatedCompositeContainer_Methods[] = { - // TODO(https://github.com/protocolbuffers/upb/issues/459) {"__deepcopy__", PyUpb_RepeatedContainer_DeepCopy, METH_VARARGS, "Makes a deep copy of the class."}, {"add", (PyCFunction)PyUpb_RepeatedCompositeContainer_Add, @@ -731,12 +730,24 @@ static int PyUpb_RepeatedScalarContainer_AssignItem(PyObject* _self, return 0; } +static PyObject* PyUpb_RepeatedScalarContainer_Reduce(PyObject* unused_self, + PyObject* unused_other) { + PyObject* pickle_module = PyImport_ImportModule("pickle"); + if (!pickle_module) return NULL; + PyObject* pickle_error = PyObject_GetAttrString(pickle_module, "PickleError"); + Py_DECREF(pickle_module); + if (!pickle_error) return NULL; + PyErr_Format(pickle_error, + "can't pickle repeated message fields, convert to list first"); + Py_DECREF(pickle_error); + return NULL; +} + static PyMethodDef PyUpb_RepeatedScalarContainer_Methods[] = { - // TODO(https://github.com/protocolbuffers/upb/issues/459) {"__deepcopy__", PyUpb_RepeatedContainer_DeepCopy, METH_VARARGS, "Makes a deep copy of the class."}, - // {"__reduce__", Reduce, METH_NOARGS, - // "Outputs picklable representation of the repeated field."}, + {"__reduce__", PyUpb_RepeatedScalarContainer_Reduce, METH_NOARGS, + "Outputs picklable representation of the repeated field."}, {"append", PyUpb_RepeatedScalarContainer_Append, METH_O, "Appends an object to the repeated container."}, {"extend", PyUpb_RepeatedContainer_Extend, METH_O,