Add shortcuts for reading from / writing to files. In Python, it doesn't make much sense to force people to muck around with MessageReaders and MessageBuilders since everything is landing on the heap anyway. Instead, let's make it easy: MyType.read[Packed]From(file) reads a file and returns a MyType reader. MyType.newMessage() returns a MyType builder representing the root of a new message. You can call this builder's write[Packed]To(file) method to write it to a file.

This commit is contained in:
Kenton Varda
2013-08-31 18:19:02 -07:00
parent 6731d7eb7d
commit 1cfea9c846
2 changed files with 68 additions and 9 deletions

View File

@@ -266,16 +266,14 @@ def check_all_types(reader):
check_list(reader.enumList, ["foo", "garply"])
def test_build(all_types):
builder = capnp.MallocMessageBuilder()
root = builder.getRoot(all_types.TestAllTypes)
root = all_types.TestAllTypes.newMessage()
init_all_types(root)
expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read()
assert str(root) + '\n' == expectedText
def test_binary_read(all_types):
f = open(os.path.join(this_dir, 'all-types.binary'), 'r')
message = capnp.StreamFdMessageReader(f.fileno())
root = message.getRoot(all_types.TestAllTypes)
root = all_types.TestAllTypes.readFrom(f)
check_all_types(root)
expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read()
@@ -292,9 +290,22 @@ def test_binary_read(all_types):
def test_packed_read(all_types):
f = open(os.path.join(this_dir, 'all-types.packed'), 'r')
message = capnp.PackedFdMessageReader(f.fileno())
root = message.getRoot(all_types.TestAllTypes)
root = all_types.TestAllTypes.readPackedFrom(f)
check_all_types(root)
expectedText = open(os.path.join(this_dir, 'all-types.txt'), 'r').read()
assert str(root) + '\n' == expectedText
def test_binary_write(all_types):
root = all_types.TestAllTypes.newMessage()
init_all_types(root)
root.writeTo(open('example', 'w'))
check_all_types(all_types.TestAllTypes.readFrom(open('example', 'r')))
def test_packed_write(all_types):
root = all_types.TestAllTypes.newMessage()
init_all_types(root)
root.writePackedTo(open('example', 'w'))
check_all_types(all_types.TestAllTypes.readPackedFrom(open('example', 'r')))