From ccb518474bba42c89ce620bc284a0b1cb2b5e46b Mon Sep 17 00:00:00 2001 From: Pablo Diaz-Gutierrez Date: Mon, 16 Feb 2015 19:32:39 -0800 Subject: [PATCH 1/2] Support for fixed64, and fixing support for fixed32 by using uint32 instead of int32, as protobuf does when generating C++ files --- src/fastpb/generator.py | 1 + src/fastpb/template/module.jinjacc | 40 +++++++++++++++++++++++++++--- src/fastpb/template/test.jinjapy | 4 ++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/fastpb/generator.py b/src/fastpb/generator.py index 71d83fe..b997715 100755 --- a/src/fastpb/generator.py +++ b/src/fastpb/generator.py @@ -44,6 +44,7 @@ 'BOOL': descriptor_pb2.FieldDescriptorProto.TYPE_BOOL, 'ENUM': descriptor_pb2.FieldDescriptorProto.TYPE_ENUM, 'FIXED32': descriptor_pb2.FieldDescriptorProto.TYPE_FIXED32, + 'FIXED64': descriptor_pb2.FieldDescriptorProto.TYPE_FIXED64, # TODO(robbyw): More types. } diff --git a/src/fastpb/template/module.jinjacc b/src/fastpb/template/module.jinjacc index eb51fbe..54895c0 100644 --- a/src/fastpb/template/module.jinjacc +++ b/src/fastpb/template/module.jinjacc @@ -44,9 +44,15 @@ fastpb_convert{{ TYPE.UINT32 }}(::google::protobuf::uint32 value) } static PyObject * -fastpb_convert{{ TYPE.FIXED32 }}(::google::protobuf::int32 value) +fastpb_convert{{ TYPE.FIXED32 }}(::google::protobuf::uint32 value) { - return PyLong_FromLong(value); + return PyLong_FromUnsignedLong(value); +} + +static PyObject * +fastpb_convert{{ TYPE.FIXED64 }}(::google::protobuf::uint64 value) +{ + return PyLong_FromUnsignedLongLong(value); } static PyObject * @@ -419,7 +425,7 @@ namespace { return -1; } - {% elif member.type == TYPE.INT32 or member.type == TYPE.SINT32 or member.type == TYPE.FIXED32%} + {% elif member.type == TYPE.INT32 or member.type == TYPE.SINT32 %} ::google::protobuf::int32 protoValue; // int32 @@ -431,6 +437,34 @@ namespace { return -1; } + {% elif member.type == TYPE.FIXED64 %} + ::google::protobuf::uint64 protoValue; + + // uint64 + if (PyInt_Check(value)) { + protoValue = PyInt_AsUnsignedLongLongMask(value); + } else if (PyLong_Check(value)) { + protoValue = PyLong_AsUnsignedLongLong(value); + } else { + PyErr_SetString(PyExc_TypeError, + "The {{ member.name }} attribute value must be an integer"); + return -1; + } + + {% elif member.type == TYPE.FIXED32 %} + ::google::protobuf::uint32 protoValue; + + // uint32 + if (PyInt_Check(value)) { + protoValue = PyInt_AsUnsignedLongMask(value); + } else if (PyLong_Check(value)) { + protoValue = PyLong_AsUnsignedLong(value); + } else { + PyErr_SetString(PyExc_TypeError, + "The {{ member.name }} attribute value must be an integer"); + return -1; + } + {% elif member.type == TYPE.ENUM %} // {{ member.type_name }} {{ member.type_name.replace('.', '::') }} protoValue; diff --git a/src/fastpb/template/test.jinjapy b/src/fastpb/template/test.jinjapy index f6d4439..4f3fd01 100644 --- a/src/fastpb/template/test.jinjapy +++ b/src/fastpb/template/test.jinjapy @@ -24,7 +24,9 @@ import {{ file.package }} or field.type == TYPE.UINT32 or field.type == TYPE.INT64 or field.type == TYPE.SINT64 - or field.type == TYPE.UINT64 -%} + or field.type == TYPE.UINT64 + or field.type == TYPE.FIXED32 + or field.type == TYPE.FIXED64 -%} {{ index }} {%- elif field.type == TYPE.DOUBLE -%} {{ index }}.{{ index }} From 253a44a7218bfd19e989575eeb069e744110686a Mon Sep 17 00:00:00 2001 From: Pablo Diaz-Gutierrez Date: Mon, 16 Feb 2015 19:43:33 -0800 Subject: [PATCH 2/2] Updating README with supported types --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f09781c..3e10b7d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Python's json serializer. This is a very early stage project. It works for our needs. We haven't verified it works beyond that. Issue reports and patches are very much appreciated! -For example, it only supports strint, int32, int64, double, and sub message members at this time. +For example, it only supports string, int32, int64, double, fixed32, fixed64, and sub message members at this time. ### Pre-requisites: