From 8c3c57aa325758fc44916a2dab93c9c52802369f Mon Sep 17 00:00:00 2001 From: Mike Lundy Date: Thu, 3 Mar 2016 14:29:32 -0800 Subject: [PATCH 1/3] 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 Date: Thu, 3 Mar 2016 16:32:01 -0800 Subject: [PATCH 2/3] Include the traceback in exceptions This makes debugging into the cython side of the code much easier. --- capnp/lib/capnp.pyx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 55b3964..ab65392 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -1025,7 +1025,7 @@ cdef class _DynamicStructReader: try: return self._get(field) except KjException as e: - raise e._to_python() + raise e._to_python(), None, _sys.exc_info()[2] cpdef _get_by_field(self, _StructSchemaField field): return to_python_reader(self.thisptr.getByField(field.thisptr), self._parent) @@ -1225,7 +1225,7 @@ cdef class _DynamicStructBuilder: try: return self._get(field) except KjException as e: - raise e._to_python() + raise e._to_python(), None, _sys.exc_info()[2] cpdef _set(self, field, value): _setDynamicField(self.thisptr, field, value, self._parent) @@ -1237,7 +1237,7 @@ cdef class _DynamicStructBuilder: try: self._set(field, value) except KjException as e: - raise e._to_python() + raise e._to_python(), None, _sys.exc_info()[2] cpdef _has(self, field): return self.thisptr.has(field) @@ -1456,7 +1456,7 @@ cdef class _DynamicStructPipeline: try: return self._get(field) except KjException as e: - raise e._to_python() + raise e._to_python(), None, _sys.exc_info()[2] property schema: """A property that returns the _StructSchema object matching this reader""" @@ -1936,7 +1936,7 @@ cdef class _RemotePromise: try: return self._get(field) except KjException as e: - raise e._to_python() + raise e._to_python(), None, _sys.exc_info()[2] property schema: """A property that returns the _StructSchema object matching this reader""" @@ -2039,7 +2039,7 @@ cdef class _DynamicCapabilityServer: try: return getattr(self.server, field) except KjException as e: - raise e._to_python() + raise e._to_python(), None, _sys.exc_info()[2] cdef class _DynamicCapabilityClient: cdef C_DynamicCapability.Client thisptr @@ -2121,7 +2121,7 @@ cdef class _DynamicCapabilityClient: raise AttributeError('Method named %s not found' % name) return _partial(self._send, name) except KjException as e: - raise e._to_python() + raise e._to_python(), None, _sys.exc_info()[2] cpdef upcast(self, schema) except +reraise_kj_exception: cdef _InterfaceSchema s From 7164f04a7cf98ebe0f7ffb3814fe42f19f18ff76 Mon Sep 17 00:00:00 2001 From: Mike Lundy Date: Thu, 3 Mar 2016 15:03:33 -0800 Subject: [PATCH 3/3] Eliminate outdated function --- capnp/templates/module.pyx | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/capnp/templates/module.pyx b/capnp/templates/module.pyx index 9e51a7e..943e802 100644 --- a/capnp/templates/module.pyx +++ b/capnp/templates/module.pyx @@ -133,40 +133,6 @@ cdef _from_list(_DynamicListBuilder msg, list d): msg._set(count, val) count += 1 -cdef DynamicValue.Reader to_dynamic_value(value): - cdef DynamicValue.Reader temp - cdef StringPtr temp_string - value_type = type(value) - - if value_type is int or value_type is long: - if value < 0: - temp = DynamicValue.Reader(value) - else: - temp = DynamicValue.Reader(value) - elif value_type is float: - temp = DynamicValue.Reader(value) - elif value_type is bool: - temp = DynamicValue.Reader(value) - elif value_type is bytes: - temp_string = StringPtr(value, len(value)) - temp = DynamicValue.Reader(temp_string) - elif isinstance(value, basestring): - encoded_value = value.encode() - temp_string = StringPtr(encoded_value, len(encoded_value)) - temp = DynamicValue.Reader(temp_string) - elif value is None: - temp = DynamicValue.Reader(VOID) - elif value_type is _DynamicStructBuilder: - temp = _extract_dynamic_struct_builder(value) - elif value_type is _DynamicStructReader: - temp = _extract_dynamic_struct_reader(value) - elif value_type is _DynamicEnum: - temp = _extract_dynamic_enum(value) - else: - raise ValueError("Tried to convert value of: '{}' which is an unsupported type: '{}'".format(str(value), str(type(value)))) - - return temp - cdef extern from "{{file.filename}}.h": {%- for node in code.nodes %}