[Fixed]-Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

6👍

Where can I print out the object that is giving issues from this error so that it will appear in the Django error page?

The short answer – without recompiling cPickle, you can’t.

Longer answer: This is the piece of code that raises the exception:

root $ grep -Hra "attribute lookup" /usr/lib64/ 2>/dev/null | grep -a failed
/usr/lib64/python2.7/lib-dynload/cPickle.so:H�H���P0H�5zM H�=1��M��H��H�ZM �����H�=�1�H���M��H��H��M �����H�4M H�5~H���H���������H�M H�5tH���H���������H��L H�5�H���qH�����d���H�M H�5NH���SH�����F���H�dM H�5bH���5H�����(���H�=X1��O��H�HC H�5I H�=U1�A��H��L �vH��H��I�������H���RK��H�=.H����J��H�5$H��H��H�D$�G��H�D$H��tH�H��H��H��gH�DL�ttH�qH�5nH�=kH��1��XK��H�5bH��H��I����F��H�5\L��H���F��H��tH�EH��H��H�E��M�������I�$H��H��I�$�t���I�DL���P0�d���f�H�EH��H��H�E�F���H�H���P0�7���@H�|$H��P0�����H�H���P0�����H�H���P0�k�����UH��SH�H�H: H���tH�;: H���H�H���u�H�[��H��M��H��attribute deletion is not supportedunsupported pickle protocol: %dargument must have 'read' and 'readline' attributespickle protocol %d asked for; the highest available protocol is %dargument must have 'write' attributeGlobal and instance pickles are not supported.Attempt to getvalue() a non-list-based picklerUnexpected data in internal listBINSTRING pickle has negative byte countno int where int expected in memoCan't pickle %s: import of module %s failedCan't pickle %s: attribute lookup %s.%s failedCan't pickle %s: it's not the same object as %s.%sCan't pickle %s: extension code %s isn't an integerCan't pickle %s: extension code %ld is out of rangecould not convert string to intLONG pickle has negative byte countcould not convert string to floatBINUNICODE pickle has negative byte countunregistered extension code %ld_inverted_registry[%ld] isn't a 2-tuple of stringsA load persistent id instruction was encountered,

If you look close enough, there’s a piece that says

Can't pickle %s: attribute lookup %s.%s failed

Now, if you download Python sources, you can easily find the piece of code responsible for raising the exception in the function static int save_global(Picklerobject *self, PyObject *args, PyObject *name) of ./Modules/cPickle.c:

klass = PyObject_GetAttrString(mod, name_str);
if (klass == NULL) {
    cPickle_ErrFormat(PicklingError,
                      "Can't pickle %s: attribute lookup %s.%s "
                      "failed",
                      "OSS", args, module, global_name);
    goto finally;
}

So, the best you can do to debug this error is to format the string differently (probably providing PyString_AS_STRING((PyStringObject *)name), recompile and install the modified version of Python.

Yeah, I know that’s too bad. I just had the same problem myself.

10👍

In my case (not Django related) this exception was thrown by multiprocessing.Pool.map when a lambda was passed as the target function. Creating a named function and passing the needed context data structures via the initargs parameter (rather than via the closure) solved the issue.

To summarize, the bad use-case that triggered the exception was:

import multiprocessing as mp
context = some_object
pool = mp.Pool()
worker_func = lambda x: work(x, context)
results = pool.map(worker_func, data_list)
👤alexei

1👍

Use something like django-extensions to install the werkzeug debugger. You will be able to interact with each stackframe. At that point, you can try pickling all of the keys and values in the session dict.

👤Marcin

1👍

If you are storing a user object, you may be affected by this bug:
https://code.djangoproject.com/ticket/16563

That said, your best bet is going to be to just modify the Django source code at line 93 of /usr/local/lib/python2.6/dist-packages/django/contrib/sessions/backends/base.py where your exception occurs.

Just log session_dict. In most cases it will be really obvious what is wrong. (Indeed, if your traceback shows Local Vars, you already have this)

Leave a comment