Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 205e00c

Browse files
bpo-29914: Fix default implementations of __reduce__ and __reduce_ex__(). (python#843)
object.__reduce__() no longer takes arguments, object.__reduce_ex__() now requires one argument.
1 parent dd9a0a1 commit 205e00c

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

Lib/test/test_descr.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5236,7 +5236,20 @@ def __getattr__(self, attr):
52365236

52375237
import copyreg
52385238
expected = (copyreg.__newobj__, (A,), (None, {'spam': 42}), None, None)
5239-
self.assertEqual(A().__reduce__(2), expected) # Shouldn't crash
5239+
self.assertEqual(A().__reduce_ex__(2), expected) # Shouldn't crash
5240+
5241+
def test_object_reduce(self):
5242+
# Issue #29914
5243+
# __reduce__() takes no arguments
5244+
object().__reduce__()
5245+
with self.assertRaises(TypeError):
5246+
object().__reduce__(0)
5247+
# __reduce_ex__() takes one integer argument
5248+
object().__reduce_ex__(0)
5249+
with self.assertRaises(TypeError):
5250+
object().__reduce_ex__()
5251+
with self.assertRaises(TypeError):
5252+
object().__reduce_ex__(None)
52405253

52415254

52425255
class SharedKeyTests(unittest.TestCase):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29914: Fixed default implementations of __reduce__ and __reduce_ex__().
14+
object.__reduce__() no longer takes arguments, object.__reduce_ex__() now
15+
requires one argument.
16+
1317
- bpo-29949: Fix memory usage regression of set and frozenset object.
1418

1519
- bpo-29935: Fixed error messages in the index() method of tuple, list and deque

Objects/clinic/typeobject.c.h

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -131,61 +131,42 @@ type___sizeof__(PyTypeObject *self, PyObject *Py_UNUSED(ignored))
131131
}
132132

133133
PyDoc_STRVAR(object___reduce____doc__,
134-
"__reduce__($self, protocol=0, /)\n"
134+
"__reduce__($self, /)\n"
135135
"--\n"
136136
"\n"
137137
"Helper for pickle.");
138138

139139
#define OBJECT___REDUCE___METHODDEF \
140-
{"__reduce__", (PyCFunction)object___reduce__, METH_FASTCALL, object___reduce____doc__},
140+
{"__reduce__", (PyCFunction)object___reduce__, METH_NOARGS, object___reduce____doc__},
141141

142142
static PyObject *
143-
object___reduce___impl(PyObject *self, int protocol);
143+
object___reduce___impl(PyObject *self);
144144

145145
static PyObject *
146-
object___reduce__(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
146+
object___reduce__(PyObject *self, PyObject *Py_UNUSED(ignored))
147147
{
148-
PyObject *return_value = NULL;
149-
int protocol = 0;
150-
151-
if (!_PyArg_ParseStack(args, nargs, "|i:__reduce__",
152-
&protocol)) {
153-
goto exit;
154-
}
155-
156-
if (!_PyArg_NoStackKeywords("__reduce__", kwnames)) {
157-
goto exit;
158-
}
159-
return_value = object___reduce___impl(self, protocol);
160-
161-
exit:
162-
return return_value;
148+
return object___reduce___impl(self);
163149
}
164150

165151
PyDoc_STRVAR(object___reduce_ex____doc__,
166-
"__reduce_ex__($self, protocol=0, /)\n"
152+
"__reduce_ex__($self, protocol, /)\n"
167153
"--\n"
168154
"\n"
169155
"Helper for pickle.");
170156

171157
#define OBJECT___REDUCE_EX___METHODDEF \
172-
{"__reduce_ex__", (PyCFunction)object___reduce_ex__, METH_FASTCALL, object___reduce_ex____doc__},
158+
{"__reduce_ex__", (PyCFunction)object___reduce_ex__, METH_O, object___reduce_ex____doc__},
173159

174160
static PyObject *
175161
object___reduce_ex___impl(PyObject *self, int protocol);
176162

177163
static PyObject *
178-
object___reduce_ex__(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
164+
object___reduce_ex__(PyObject *self, PyObject *arg)
179165
{
180166
PyObject *return_value = NULL;
181-
int protocol = 0;
182-
183-
if (!_PyArg_ParseStack(args, nargs, "|i:__reduce_ex__",
184-
&protocol)) {
185-
goto exit;
186-
}
167+
int protocol;
187168

188-
if (!_PyArg_NoStackKeywords("__reduce_ex__", kwnames)) {
169+
if (!PyArg_Parse(arg, "i:__reduce_ex__", &protocol)) {
189170
goto exit;
190171
}
191172
return_value = object___reduce_ex___impl(self, protocol);
@@ -256,4 +237,4 @@ object___dir__(PyObject *self, PyObject *Py_UNUSED(ignored))
256237
{
257238
return object___dir___impl(self);
258239
}
259-
/*[clinic end generated code: output=ce354e436e2360a0 input=a9049054013a1b77]*/
240+
/*[clinic end generated code: output=8c4c856859564eaa input=a9049054013a1b77]*/

Objects/typeobject.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,31 +4393,28 @@ _common_reduce(PyObject *self, int proto)
43934393
/*[clinic input]
43944394
object.__reduce__
43954395
4396-
protocol: int = 0
4397-
/
4398-
43994396
Helper for pickle.
44004397
[clinic start generated code]*/
44014398

44024399
static PyObject *
4403-
object___reduce___impl(PyObject *self, int protocol)
4404-
/*[clinic end generated code: output=5572e699c467dd5b input=227f37ed68bd938a]*/
4400+
object___reduce___impl(PyObject *self)
4401+
/*[clinic end generated code: output=d4ca691f891c6e2f input=11562e663947e18b]*/
44054402
{
4406-
return _common_reduce(self, protocol);
4403+
return _common_reduce(self, 0);
44074404
}
44084405

44094406
/*[clinic input]
44104407
object.__reduce_ex__
44114408
4412-
protocol: int = 0
4409+
protocol: int
44134410
/
44144411
44154412
Helper for pickle.
44164413
[clinic start generated code]*/
44174414

44184415
static PyObject *
44194416
object___reduce_ex___impl(PyObject *self, int protocol)
4420-
/*[clinic end generated code: output=2e157766f6b50094 input=8dd6a9602a12749e]*/
4417+
/*[clinic end generated code: output=2e157766f6b50094 input=f326b43fb8a4c5ff]*/
44214418
{
44224419
static PyObject *objreduce;
44234420
PyObject *reduce, *res;

0 commit comments

Comments
 (0)