TypeError: can't pickle HASH objects?

mdsherry at gmail.com mdsherry at gmail.com
Wed Oct 1 16:49:24 EDT 2008


On Oct 1, 3:50 pm, est <electronix... at gmail.com> wrote:
> >>> import md5
> >>> a=md5.md5()
> >>> import pickle
> >>> pickle.dumps(a)
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "C:\Python25\lib\pickle.py", line 1366, in dumps
>     Pickler(file, protocol).dump(obj)
>   File "C:\Python25\lib\pickle.py", line 224, in dump
>     self.save(obj)
>   File "C:\Python25\lib\pickle.py", line 306, in save
>     rv = reduce(self.proto)
>   File "C:\Python25\lib\copy_reg.py", line 69, in _reduce_ex
>     raise TypeError, "can't pickle %s objects" % base.__name__
> TypeError: can't pickle HASH objects
>
> Why can't I pickle a md5 object? Is it because md5 algorithm needs to
> read 512-bits at a time?
>
> I need to md5() some stream, pause(python.exe quits), and resume
> later.  It seems that the md5 and hashlib in  std module could not be
> serialized?
>
> Do I have to implement md5 algorithm again for this special occasion?
>
> Or is there anyway to assige a digest when creating md5 objects?

I'm sure some of the regulars can correct me if I'm wrong, but looking
at the source code, it seems that this is the error that you'll see if
the object doesn't explicitly support pickling, or possibly isn't
composed of objects that do.

Examining the md5 and hashlib source files, it seems that they rely on
C implementations, and so have internal states opaque to Python. If
you feel confident, you could write your own MD5 class that would have
methods to dump and restore state, but I think you're out of luck when
it comes to the official module.

Mark Sherry



More information about the Python-list mailing list