From 8c3c57aa325758fc44916a2dab93c9c52802369f Mon Sep 17 00:00:00 2001 From: Mike Lundy Date: Thu, 3 Mar 2016 14:29:32 -0800 Subject: [PATCH] Make sure to encode to utf-8, not the default encoding This allows text fields to take unicode strings under python 2. --- capnp/lib/capnp.pyx | 4 ++-- capnp/templates/module.pyx | 4 ++-- test/all-types.binary | Bin 2816 -> 2816 bytes test/all-types.packed | Bin 831 -> 831 bytes test/all-types.txt | 2 +- test/test_regression.py | 21 +++++++++++++++++++-- 6 files changed, 24 insertions(+), 7 deletions(-) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 290c4cf..55b3964 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -679,7 +679,7 @@ cdef _setBytes(_DynamicSetterClasses thisptr, field, value): thisptr.set(field, temp) cdef _setBaseString(_DynamicSetterClasses thisptr, field, value): - encoded_value = value.encode() + encoded_value = value.encode('utf-8') cdef capnp.StringPtr temp_string = capnp.StringPtr(encoded_value, len(encoded_value)) cdef C_DynamicValue.Reader temp = C_DynamicValue.Reader(temp_string) thisptr.set(field, temp) @@ -690,7 +690,7 @@ cdef _setBytesField(DynamicStruct_Builder thisptr, _StructSchemaField field, val thisptr.setByField(field.thisptr, temp) cdef _setBaseStringField(DynamicStruct_Builder thisptr, _StructSchemaField field, value): - encoded_value = value.encode() + encoded_value = value.encode('utf-8') cdef capnp.StringPtr temp_string = capnp.StringPtr(encoded_value, len(encoded_value)) cdef C_DynamicValue.Reader temp = C_DynamicValue.Reader(temp_string) thisptr.setByField(field.thisptr, temp) diff --git a/capnp/templates/module.pyx b/capnp/templates/module.pyx index df45582..9e51a7e 100644 --- a/capnp/templates/module.pyx +++ b/capnp/templates/module.pyx @@ -90,7 +90,7 @@ cpdef _set_{{field.name}}(self, value): if type(value) is bytes: temp_string = StringPtr(value, len(value)) else: - encoded_value = value.encode() + encoded_value = value.encode('utf-8') temp_string = StringPtr(encoded_value, len(encoded_value)) self.thisptr_child.set{{field.c_name}}(temp_string) {% elif 'data' == field['type'] -%} @@ -99,7 +99,7 @@ cpdef _set_{{field.name}}(self, value): if type(value) is bytes: temp_string = StringPtr(value, len(value)) else: - encoded_value = value.encode() + encoded_value = value.encode('utf-8') temp_string = StringPtr(encoded_value, len(encoded_value)) self.thisptr_child.set{{field.c_name}}(ArrayPtr[byte](temp_string.begin(), temp_string.size())) {% else -%} diff --git a/test/all-types.binary b/test/all-types.binary index ea39763774b2ed570407a3384a8865fbeaa79213..3381caad76714027a9f0768938dc79cd0314d8d4 100644 GIT binary patch delta 26 fcmZn=YY^LTfRX#rjAjN9C@ig*tjJ`#h=~&ba}Ecb delta 26 fcmZn=YY^LTfRQ^Xv5El%3QH>{D>9ibV&Vh