Skip to content

Commit 9bfc8e0

Browse files
betamooharleyk
authored andcommitted
Fix deserialization bug in BinarySetAttribute + better error msg when parsing json (pynamodb#480)
1 parent ae03f55 commit 9bfc8e0

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

pynamodb/attributes.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,11 @@ def deserialize(self, value):
328328
"""
329329
Returns a decoded string from base64
330330
"""
331-
if value and len(value):
332-
return set([b64decode(val.encode(DEFAULT_ENCODING)) for val in value])
331+
try:
332+
if value and len(value):
333+
return set([b64decode(val.decode(DEFAULT_ENCODING)) for val in value])
334+
except AttributeError:
335+
return set([b64decode(val) for val in value])
333336

334337

335338
class UnicodeSetAttribute(SetMixin, Attribute):

pynamodb/connection/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ def _make_api_call(self, operation_name, operation_kwargs):
349349
attempt_number = i + 1
350350
is_last_attempt_for_exceptions = i == self._max_retry_attempts_exception
351351

352+
response = None
352353
try:
353354
response = self.requests_session.send(
354355
prepared_request,
@@ -359,6 +360,8 @@ def _make_api_call(self, operation_name, operation_kwargs):
359360
except (requests.RequestException, ValueError) as e:
360361
if is_last_attempt_for_exceptions:
361362
log.debug('Reached the maximum number of retry attempts: %s', attempt_number)
363+
if response:
364+
e.args += (str(response.content),)
362365
raise
363366
else:
364367
# No backoff for fast-fail exceptions that likely failed at the frontend

pynamodb/tests/integration/binary_update_test.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import pytest
22

3-
from pynamodb.attributes import UnicodeAttribute, BinaryAttribute
3+
from pynamodb.attributes import UnicodeAttribute, BinaryAttribute, BinarySetAttribute
44
from pynamodb.models import Model
55

66

77
@pytest.mark.ddblocal
8-
def test_update(ddb_url):
8+
def test_binary_attribute_update(ddb_url):
99
class DataModel(Model):
1010
class Meta:
1111
table_name = 'binary_attr_update'
@@ -23,3 +23,23 @@ class Meta:
2323
new_data = b'\xff'
2424
m.update(actions=[DataModel.data.set(new_data)])
2525
assert new_data == m.data
26+
27+
@pytest.mark.ddblocal
28+
def test_binary_set_attribute_update(ddb_url):
29+
class DataModel(Model):
30+
class Meta:
31+
table_name = 'binary_set_attr_update'
32+
host = ddb_url
33+
pkey = UnicodeAttribute(hash_key=True)
34+
data = BinarySetAttribute()
35+
36+
DataModel.create_table(read_capacity_units=1, write_capacity_units=1)
37+
data = set([b'\x00hey\xfb', b'\x00beautiful\xfb'])
38+
pkey = 'pkey'
39+
DataModel(pkey, data=data).save()
40+
m = DataModel.get(pkey)
41+
assert m.data == data
42+
43+
new_data = set([b'\xff'])
44+
m.update(actions=[DataModel.data.set(new_data)])
45+
assert new_data == m.data

0 commit comments

Comments
 (0)