Skip to content

Commit 5225f6a

Browse files
committed
update pickle/copy
1 parent f6b236b commit 5225f6a

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

Modules/_functoolsmodule.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -777,34 +777,11 @@ partial_repr(PyObject *self)
777777
operation so we define a __setstate__ that replaces all the information
778778
about the partial. If we only replaced part of it someone would use
779779
it as a hook to do strange things.
780-
781-
Additionally, since frozendict does not work for pickle protocols 0 and 1,
782-
__reduce_ex__ creates a temporary dict for protocols 0 and 1 and calls
783-
__reduce__ for protocols 2+.
784780
*/
785781

786782
static PyObject *
787783
partial_reduce(PyObject *self, PyObject *Py_UNUSED(args))
788784
{
789-
partialobject *pto = partialobject_CAST(self);
790-
return Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn,
791-
pto->args, pto->kw,
792-
pto->dict ? pto->dict : Py_None);
793-
}
794-
795-
static PyObject *
796-
partial_reduce_ex(PyObject *self, PyObject *args)
797-
{
798-
int64_t protocol;
799-
800-
if (!PyArg_ParseTuple(args, "l", &protocol)) {
801-
return NULL;
802-
}
803-
804-
if (protocol >= 2) {
805-
return partial_reduce(self, NULL);
806-
}
807-
808785
partialobject *pto = partialobject_CAST(self);
809786
PyObject *keywords_dict = PyObject_CallOneArg((PyObject*)&PyDict_Type, pto->kw);
810787
PyObject *result = Py_BuildValue("O(O)(OOOO)", Py_TYPE(pto), pto->fn, pto->fn,
@@ -902,10 +879,23 @@ partial_setstate(PyObject *self, PyObject *state)
902879
Py_RETURN_NONE;
903880
}
904881

882+
static PyObject *
883+
partial_copy(PyObject *self, PyObject *Py_UNUSED(args))
884+
{
885+
partialobject *pto = partialobject_CAST(self);
886+
PyTypeObject *type = Py_TYPE(pto);
887+
partialobject *new_pto = (partialobject *)type->tp_alloc(type, 0);
888+
new_pto->fn = Py_NewRef(pto->fn);
889+
new_pto->args = Py_NewRef(pto->args);
890+
new_pto->kw = Py_NewRef(pto->kw);
891+
new_pto->dict = Py_XNewRef(pto->dict);
892+
return (PyObject *)new_pto;
893+
}
894+
905895
static PyMethodDef partial_methods[] = {
906896
{"__reduce__", partial_reduce, METH_NOARGS},
907-
{"__reduce_ex__", partial_reduce_ex, METH_VARARGS},
908897
{"__setstate__", partial_setstate, METH_O},
898+
{"__copy__", partial_copy, METH_NOARGS},
909899
{"__class_getitem__", Py_GenericAlias,
910900
METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
911901
{NULL, NULL} /* sentinel */

0 commit comments

Comments
 (0)