Commit 247d3c9f authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Fix Graph pickling in python 2

This fixes issue #190
parent d1d92728
......@@ -2144,7 +2144,10 @@ class Graph(object):
def __getstate__(self):
state = dict()
sio = BytesIO()
if sys.version_info < (3,):
sio = StringIO()
else:
sio = BytesIO()
stream = gzip.open(sio, mode="wb")
self.save(stream, "gt")
stream.close()
......@@ -2156,11 +2159,17 @@ class Graph(object):
blob = state["blob"]
if blob != "":
try:
sio = BytesIO(blob)
if sys.version_info < (3,):
sio = StringIO(blob)
else:
sio = BytesIO(blob)
stream = gzip.open(sio, mode="rb")
self.load(stream, "gt")
except OSError:
sio = BytesIO(blob)
if sys.version_info < (3,):
sio = StringIO(blob)
else:
sio = BytesIO(blob)
stream = gzip.open(sio, mode="rb")
self.load(stream, "xml")
......
  • mentioned in issue #190 (closed)

    Toggle commit list
  • mentioned in issue #190 (closed)

    Toggle commit list
  • Is this fix in the latest release? I'm getting similar problems trying to pickle with Python 2.7.

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-85-02b13c145be1> in <module>()
          3 
          4 with open('../data/graph_dumps/Uncover_flow_2014-11-13_graph.pickle', 'w') as fi:
    ----> 5     pickle.dump(g, fi)
          6 
          7 with open('../data/graph_dumps/Uncover_flow_2014-11-13_node_pos.pickle', 'w') as fi:    
    
    /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc in dump(obj, file, protocol)
       1368 
       1369 def dump(obj, file, protocol=None):
    -> 1370     Pickler(file, protocol).dump(obj)
       1371 
       1372 def dumps(obj, protocol=None):    
    
    /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc in dump(self, obj)
        222         if self.proto >= 2:
        223             self.write(PROTO + chr(self.proto))
    --> 224         self.save(obj)
        225         self.write(STOP)
        226     
    
    /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc in save(self, obj)
        304             reduce = getattr(obj, "__reduce_ex__", None)
        305             if reduce:
    --> 306                 rv = reduce(self.proto)
        307             else:
        308                 reduce = getattr(obj, "__reduce__", None)    
    
    /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy_reg.pyc in _reduce_ex(self, proto)
         82             dict = None
         83     else:
    ---> 84         dict = getstate()
         85     if dict:
         86         return _reconstructor, args, dict    
    
    /usr/local/lib/python2.7/site-packages/graph_tool/__init__.pyc in __getstate__(self)
       2145         state = dict()
       2146         sio = BytesIO()
    -> 2147         stream = gzip.open(sio, mode="wb")
       2148         self.save(stream, "gt")
       2149         stream.close()    
    
    /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gzip.pyc in open(filename, mode, compresslevel)
         32 
         33     """
    ---> 34     return GzipFile(filename, mode, compresslevel)
         35 
         36 class GzipFile(io.BufferedIOBase):    
    
    /usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/gzip.pyc in __init__(self, filename, mode, compresslevel, fileobj, mtime)
         92             mode += 'b'
         93         if fileobj is None:
    ---> 94             fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
         95         if filename is None:
         96             # Issue #13781: os.fdopen() creates a fileobj with a bogus name    
    
    TypeError: coercing to Unicode: need string or buffer, _io.BytesIO found`
  • I don't know what covers the latest release but if you use the development version it should be fixed. You need to remember to open the file in binary mode also.

Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment