Skip to content

Commit

Permalink
Fixed issue where pickle helpers didnt use new from_bytes with context
Browse files Browse the repository at this point in the history
- Added with context to benchmarks that use with_bytes
  • Loading branch information
madhavajay committed May 24, 2022
1 parent 26de61d commit fc61714
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
13 changes: 6 additions & 7 deletions benchmark/addressbook.capnp.orphan.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ def writeAddressBook():


def printAddressBook(msg_bytes):
addressBook = addressbook.AddressBook.from_bytes(msg_bytes)

for person in addressBook.people:
print(person.name, ":", person.email)
for phone in person.phones:
print(phone.type, ":", phone.number)
print()
with addressbook.AddressBook.from_bytes(msg_bytes) as addressBook:
for person in addressBook.people:
print(person.name, ":", person.email)
for phone in person.phones:
print(phone.type, ":", phone.number)
print()


if __name__ == "__main__":
Expand Down
11 changes: 5 additions & 6 deletions benchmark/addressbook.capnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ def writeAddressBook():

@profile
def printAddressBook(msg_bytes):
addressBook = addressbook.AddressBook.from_bytes(msg_bytes)

for person in addressBook.people:
person.name, person.email
for phone in person.phones:
phone.type, phone.number
with addressbook.AddressBook.from_bytes(msg_bytes) as addressBook:
for person in addressBook.people:
person.name, person.email
for phone in person.phones:
phone.type, phone.number


@profile
Expand Down
3 changes: 2 additions & 1 deletion capnp/lib/capnp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,8 @@ if getattr(_sys, 'subversion', [''])[0] == 'PyPy':
from pickle_helper import _struct_reducer
else:
def _struct_reducer(schema_id, data):
return _global_schema_parser.modules_by_id[schema_id].from_bytes(data)
with _global_schema_parser.modules_by_id[schema_id].from_bytes(data) as msg:
return msg


cdef class _DynamicStructReader:
Expand Down
3 changes: 2 additions & 1 deletion capnp/lib/pickle_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

def _struct_reducer(schema_id, data):
'Hack to deal with pypy not allowing reduce functions to be "built-in" methods (ie. compiled from a .pyx)'
return capnp._global_schema_parser.modules_by_id[schema_id].from_bytes(data)
with capnp._global_schema_parser.modules_by_id[schema_id].from_bytes(data) as msg:
return msg
3 changes: 2 additions & 1 deletion scripts/capnp_test_pycapnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

def decode(name):
class_name = name[0].upper() + name[1:]
print(getattr(test_capnp, class_name).from_bytes(sys.stdin.read())._short_str())
with getattr(test_capnp, class_name).from_bytes(sys.stdin.read()) as msg:
print(msg._short_str())


def encode(name):
Expand Down

0 comments on commit fc61714

Please sign in to comment.