1

https://pypi.python.org/pypi/python-librsync/0.1-5

import librsync

# The destination file.
dst = file('Resume-v1.0.pdf', 'rb')

# Step 1: prepare signature of the destination file
signature = librsync.signature(dst)

I want to store the signature in a file (preferably as a dictionary entry using pickle). I would want to calculate the delta file afterwards. How do I save this signature object for future use?

Update: I tried to pickle the object but it says TypeError: can't pickle StringO objects.

Update 2: The signature object that was returned had reference to a file object. Python can't pickle a file object. I solved it using dill which is an extended version of the pickle module. Added it as an answer.

aste123
  • 1,223
  • 4
  • 20
  • 40

2 Answers2

2

I'm the author of the question and this is the solution that worked for me.

From http://www.ibm.com/developerworks/library/l-pypers/

Python cannot pickle a file object (or any object with a reference to a file object), because Python cannot guarantee that it can recreate the state of the file upon unpickling.

From https://pypi.python.org/pypi/dill

Dill extends python’s ‘pickle’ module for serializing and de-serializing python objects to the majority of the built-in python types.

Using dill, I could serialize the signature object that was returned. https://github.com/uqfoundation/dill

aste123
  • 1,223
  • 4
  • 20
  • 40
1

You don't want to pickle the StringIO object, but rather the value of the signature. You need to get the contents of the object:

signature_bytes = librsync.signature(dst).getvalue()
pickle(dict(sig=signature_bytes))
poolie
  • 9,289
  • 1
  • 47
  • 74
  • Thanks, do you know of any resource where I can get a list of all the methods available in the `librsync` module without going through the source code? I couldn't find any proper documentation. – aste123 Mar 07 '15 at 18:29
  • StringIO isn't a librsync object, it's a built-in Python object. Docs for the Python module are here: https://pypi.python.org/pypi/python-librsync/0.1-5 – poolie Mar 07 '15 at 18:57
  • No `getvalue` method found! `AttributeError: SpooledTemporaryFile instance has no attribute 'getvalue'` – aste123 Mar 07 '15 at 18:57
  • The error message says `StringO` and not `StringIO`. Maybe they are different? – aste123 Mar 07 '15 at 19:00
  • The only mention on the web of a `StringO` type is in this page. python-librsync seems to call `tempfile.SpooledTemporaryFile` which says it gives either a `StringIO` or a real file... Either way, pickling it is an over-complicated way to store a string value. – poolie Mar 31 '15 at 21:08