[Python-checkins] cpython: Make Ellipsis and NotImplemented picklable through the reduce protocol.

alexandre.vassalotti python-checkins at python.org
Sun Nov 24 11:53:50 CET 2013


http://hg.python.org/cpython/rev/04ccb2e0307e
changeset:   87494:04ccb2e0307e
user:        Alexandre Vassalotti <alexandre at peadrop.com>
date:        Sun Nov 24 02:53:45 2013 -0800
summary:
  Make Ellipsis and NotImplemented picklable through the reduce protocol.

files:
  Lib/pickle.py         |   8 -------
  Modules/_pickle.c     |  32 -------------------------------
  Objects/object.c      |  13 +++++++++++-
  Objects/sliceobject.c |  13 +++++++++++-
  4 files changed, 24 insertions(+), 42 deletions(-)


diff --git a/Lib/pickle.py b/Lib/pickle.py
--- a/Lib/pickle.py
+++ b/Lib/pickle.py
@@ -633,14 +633,6 @@
         self.write(NONE)
     dispatch[type(None)] = save_none
 
-    def save_ellipsis(self, obj):
-        self.save_global(Ellipsis, 'Ellipsis')
-    dispatch[type(Ellipsis)] = save_ellipsis
-
-    def save_notimplemented(self, obj):
-        self.save_global(NotImplemented, 'NotImplemented')
-    dispatch[type(NotImplemented)] = save_notimplemented
-
     def save_bool(self, obj):
         if self.proto >= 2:
             self.write(NEWTRUE if obj else NEWFALSE)
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -3172,30 +3172,6 @@
 }
 
 static int
-save_ellipsis(PicklerObject *self, PyObject *obj)
-{
-    PyObject *str = PyUnicode_FromString("Ellipsis");
-    int res;
-    if (str == NULL)
-        return -1;
-    res = save_global(self, Py_Ellipsis, str);
-    Py_DECREF(str);
-    return res;
-}
-
-static int
-save_notimplemented(PicklerObject *self, PyObject *obj)
-{
-    PyObject *str = PyUnicode_FromString("NotImplemented");
-    int res;
-    if (str == NULL)
-        return -1;
-    res = save_global(self, Py_NotImplemented, str);
-    Py_DECREF(str);
-    return res;
-}
-
-static int
 save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
 {
     PyObject *pid = NULL;
@@ -3552,14 +3528,6 @@
         status = save_none(self, obj);
         goto done;
     }
-    else if (obj == Py_Ellipsis) {
-        status = save_ellipsis(self, obj);
-        goto done;
-    }
-    else if (obj == Py_NotImplemented) {
-        status = save_notimplemented(self, obj);
-        goto done;
-    }
     else if (obj == Py_False || obj == Py_True) {
         status = save_bool(self, obj);
         goto done;
diff --git a/Objects/object.c b/Objects/object.c
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1465,6 +1465,17 @@
 }
 
 static PyObject *
+NotImplemented_reduce(PyObject *op)
+{
+    return PyUnicode_FromString("NotImplemented");
+}
+
+static PyMethodDef notimplemented_methods[] = {
+    {"__reduce__", (PyCFunction)NotImplemented_reduce, METH_NOARGS, NULL},
+    {NULL, NULL}
+};
+
+static PyObject *
 notimplemented_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
 {
     if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_Size(kwargs))) {
@@ -1511,7 +1522,7 @@
     0,                  /*tp_weaklistoffset */
     0,                  /*tp_iter */
     0,                  /*tp_iternext */
-    0,                  /*tp_methods */
+    notimplemented_methods, /*tp_methods */
     0,                  /*tp_members */
     0,                  /*tp_getset */
     0,                  /*tp_base */
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -33,6 +33,17 @@
     return PyUnicode_FromString("Ellipsis");
 }
 
+static PyObject *
+ellipsis_reduce(PyObject *op)
+{
+    return PyUnicode_FromString("Ellipsis");
+}
+
+static PyMethodDef ellipsis_methods[] = {
+    {"__reduce__", (PyCFunction)ellipsis_reduce, METH_NOARGS, NULL},
+    {NULL, NULL}
+};
+
 PyTypeObject PyEllipsis_Type = {
     PyVarObject_HEAD_INIT(&PyType_Type, 0)
     "ellipsis",                         /* tp_name */
@@ -61,7 +72,7 @@
     0,                                  /* tp_weaklistoffset */
     0,                                  /* tp_iter */
     0,                                  /* tp_iternext */
-    0,                                  /* tp_methods */
+    ellipsis_methods,                   /* tp_methods */
     0,                                  /* tp_members */
     0,                                  /* tp_getset */
     0,                                  /* tp_base */

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list