Skip to content

Commit

Permalink
Fixes #355: Python2 buffer PrimaryKey returns as read-write buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
sashaaero committed Jul 31, 2018
1 parent 68e0f1f commit cfad342
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pony/orm/dbapiprovider.py
Expand Up @@ -641,7 +641,8 @@ def validate(converter, val, obj=None):
if isinstance(val, str): return buffer(val)
throw(TypeError, "Attribute %r: expected type is 'buffer'. Got: %r" % (converter.attr, type(val)))
def sql2py(converter, val):
if not isinstance(val, buffer):
if not isinstance(val, buffer) or \
(PY2 and converter.attr.pk_offset is not None and 'read-write' in repr(val)): # Issue 355
try: val = buffer(val)
except: pass
return val
Expand Down
23 changes: 23 additions & 0 deletions pony/orm/tests/test_bug_355.py
@@ -0,0 +1,23 @@
import unittest

from pony import orm
from pony.py23compat import buffer

class Test(unittest.TestCase):
def test_1(self):
db = orm.Database('sqlite', ':memory:')

class Buf(db.Entity):
pk = orm.PrimaryKey(buffer)

db.generate_mapping(create_tables=True)

x = buffer(b'123')

with orm.db_session:
Buf(pk=x)
orm.commit()

with orm.db_session:
t = Buf[x]

0 comments on commit cfad342

Please sign in to comment.