Skip to content

Commit

Permalink
don't convert (unindexed) ascii str to bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
pnico committed Aug 21, 2023
1 parent a900a13 commit fb42ca1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/google/appengine/ext/ndb/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2741,8 +2741,9 @@ def _db_get_value(self, v, p):
sval = modelclass._from_pb(pb)
elif meaning != entity_pb2.Property.BYTESTRING:
try:
sval.decode('ascii')

decoded = sval.decode('ascii')
if meaning == entity_pb2.Property.TEXT:
sval = six.ensure_str(decoded, encoding='ascii')
except UnicodeDecodeError:
try:
sval = six.text_type(sval.decode('utf-8'))
Expand Down
25 changes: 23 additions & 2 deletions tests/google/appengine/ext/ndb/polymodel_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,18 @@ class Mammal(Animal):
self.assertEqual(cat1.naps, 18)
self.assertEqual(cat1.sound, b'purr')

def testExpandoPoly(self):

# un-indexed properties retain str type regardless of encoding:
Mammal._default_indexed = False
cat = Mammal(name='Clémentine', naps=18, sound=b'meow', action='stretch')
cat1 = cat.put().get(use_cache=False)
self.assertFalse(cat1 is cat)
self.assertEqual(cat1, cat)
self.assertEqual(cat1.name, 'Clémentine')
self.assertEqual(cat1.naps, 18)
self.assertEqual(cat1.sound, b'meow')
self.assertEqual(cat1.action, 'stretch')

def testExpandoPoly(self):

class Animal(model.Expando, PolyModel):
pass
Expand All @@ -162,6 +171,18 @@ class Mammal(Animal):
self.assertEqual(18, actual.naps)
self.assertEqual(b'purr', actual.sound)

# un-indexed properties retain str type regardless of encoding:
Mammal._default_indexed = False
expected = Mammal(name='Clémentine', naps=18, sound=b'meow', action='stretch')
actual = expected.put().get()

self.assertIsNot(expected, actual)
self.assertEqual(expected, actual)
self.assertEqual('Clémentine', actual.name)
self.assertEqual(18, actual.naps)
self.assertEqual(b'meow', actual.sound)
self.assertEqual('stretch', actual.action)

def testInheritance(self):

class NamedThing(model.Model):
Expand Down

0 comments on commit fb42ca1

Please sign in to comment.