Skip to content

Commit

Permalink
Merge pull request #6 from zopefoundation/leave-none-untouched
Browse files Browse the repository at this point in the history
Leave None values untouched during decoding
  • Loading branch information
frisi committed Oct 4, 2018
2 parents b3e31cc + c49f8e7 commit e7ccaf4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changes
1.1 (unreleased)
----------------

- When migrating databases to python3, do not fail when converting
attributes containing None.

- Fix tests on Python 2 with ZODB >= 5.4.0, which now uses pickle
protocol 3.

Expand Down
4 changes: 2 additions & 2 deletions src/zodbupdate/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def decode_attribute(attribute, encoding):

def decode(data):
value = data.get(attribute)
if not isinstance(value, six.text_type):
if value is not None and not isinstance(value, six.text_type):
data[attribute] = value.decode(encoding)
return True
return False
Expand All @@ -70,7 +70,7 @@ def encode_binary(attribute):

def encode(data):
value = data.get(attribute)
if not isinstance(value, zodbpickle.binary):
if value is not None and not isinstance(value, zodbpickle.binary):
data[attribute] = zodbpickle.binary(value)
return True
return False
Expand Down
18 changes: 18 additions & 0 deletions src/zodbupdate/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,24 @@ def test_old_style_class(self):
)
)

def test_encode_binary_leaves_none_untouched(self):
from zodbupdate.convert import encode_binary
mock = dict()
mock['foo'] = None
encoder = encode_binary('foo')
result = encoder(mock)
self.assertEquals(result, False)
self.assertEquals(mock['foo'], None)

def test_decode_attribute_leaves_none_untouched(self):
from zodbupdate.convert import decode_attribute
mock = dict()
mock['foo'] = None
encoder = decode_attribute('foo', 'utf-8')
result = encoder(mock)
self.assertEquals(result, False)
self.assertEquals(mock['foo'], None)


class Python3Tests(Tests):

Expand Down

0 comments on commit e7ccaf4

Please sign in to comment.