Skip to content

Commit

Permalink
Merge pull request #1076 from dwf/fix_serialization_protocol0
Browse files Browse the repository at this point in the history
Fix serialization with protocol 0 on Python 3.
  • Loading branch information
rizar committed May 4, 2016
2 parents 41dd4af + c1d444a commit dba9741
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions blocks/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,10 @@ def _mangle_parameter_name(parameter, name):


def _unmangle_parameter_name(mangled_name):
if not isinstance(mangled_name, str):
# This fixes an issue with protocol 0 on Python 3 where
# 'mangled_name' is a bytes object, for some reason.
mangled_name = mangled_name.decode('utf8')
if mangled_name.startswith('#1'):
type_, context_name, name = mangled_name[2:].split('.', 2)
if context_name == 'None':
Expand Down
13 changes: 13 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import warnings
import tarfile
from pickle import PicklingError
from io import BytesIO
from tempfile import NamedTemporaryFile

import numpy
Expand Down Expand Up @@ -159,3 +160,15 @@ def test_dump_and_add_to_dump():
dump_and_add_to_dump(x, f, None, {'y': y})
assert load(open(f.name, 'rb')) == x
assert load(open(f.name, 'rb'), 'y') == y


def test_protocol0_regression():
"""Check for a regression where protocol 0 dumps fail on load."""
brick = Linear(5, 10)
brick.allocate()
buf = BytesIO()
dump(brick, buf, parameters=list(brick.parameters), protocol=0)
try:
load(buf)
except TypeError:
assert False # Regression

0 comments on commit dba9741

Please sign in to comment.