Pickle in C does not work

Marco Sulla Marco.Sulla.Python at gmail.com
Mon Oct 19 18:28:08 EDT 2020


I tried this code:

static PyObject *
frozendict_reduce(PyFrozenDictObject* mp, PyObject *Py_UNUSED(ignored))
{
    PyObject* args = PyTuple_New(1);

    if (args == NULL) {
        return NULL;
    }

    PyTuple_SET_ITEM(args, 0, (PyObject *)mp);

    PyObject *d = PyObject_Call((PyObject *)&PyDict_Type, args, NULL);
    Py_INCREF(mp);
    return Py_BuildValue("O(N)", Py_TYPE(mp), d);
}

but I get:
_pickle.PicklingError: Can't pickle <class 'frozendict'>: attribute lookup
frozendict on builtins failed

The identical (?) Python code works:


def __reduce__(self, *args, **kwargs):
    return (self.__class__, (dict(self), ))


More information about the Python-list mailing list