Skip to content

Commit

Permalink
Use Eduardo's implementation for csv.dialect __reduce__
Browse files Browse the repository at this point in the history
See: python#16078
This is slightly nicer.

Co-authored-by: Eddie Elizondo <eduardo.elizondorueda@gmail.com>
  • Loading branch information
2 people authored and shihai1991 committed Jun 4, 2020
1 parent 820324b commit e2b5649
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,17 +491,20 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}

/* Since dialect is now a heap type, it inherits pickling method for
* protocol 0 and 1 from object, therefore it needs to be overrided */
* protocol 0 and 1 from object, therefore it needs to be overriden */

PyDoc_STRVAR(dialect_reduce_doc, "raises an exception to avoid pickling");

static PyObject *
_csv_dialect__reduce_ex__(PyObject *self, PyObject *args) {
PyErr_SetString(PyExc_TypeError,
"Dialect object cannot be pickled.");
Dialect_reduce(PyObject *self, PyObject *args) {
PyErr_Format(PyExc_TypeError,
"cannot pickle '%.100s' instances", _PyType_Name(Py_TYPE(self)));
return NULL;
}

static struct PyMethodDef dialect_methods[] = {
{"__reduce_ex__", _csv_dialect__reduce_ex__, METH_VARARGS, NULL},
{"__reduce__", Dialect_reduce, METH_VARARGS, dialect_reduce_doc},
{"__reduce_ex__", Dialect_reduce, METH_VARARGS, dialect_reduce_doc},
{NULL, NULL}
};

Expand Down

0 comments on commit e2b5649

Please sign in to comment.