Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

__repr__ for protobuf types

  • Loading branch information...
commit a810cd2db1ac6688f2ee89b9ceaf6a0968e564ce 1 parent e3778f4
@robbywalker robbywalker authored
Showing with 30 additions and 1 deletion.
  1. +30 −1 src/fastpb/template/module.jinjacc
View
31 src/fastpb/template/module.jinjacc
@@ -1,6 +1,7 @@
// -*- C++ -*-
#include <Python.h>
#include <string>
+#include <sstream>
#include "structmember.h"
#include "{{ fileName }}.pb.h"
@@ -588,6 +589,34 @@ namespace {
}
+ static PyObject *
+ {{ message.name }}_repr(PyObject *selfObject)
+ {
+ {{ message.name }} *self = ({{ message.name }} *)selfObject;
+ PyObject *member;
+ PyObject *memberRepr;
+ std::stringstream result;
+ result << "{{ message.name}}(";
+
+ {% for member in message.field %}
+ {% if not loop.first %}
+ result << ", ";
+ {% endif %}
+ result << "{{ member.name }}=";
+ member = {{ message.name }}_get{{ member.name }}(self, NULL);
+ memberRepr = PyObject_Repr(member);
+ result << PyString_AsString(memberRepr);
+ Py_XDECREF(memberRepr);
+ Py_XDECREF(member);
+ {% endfor %}
+
+ result << ")";
+
+ std::string resultString = result.str();
+ return PyUnicode_Decode(resultString.data(), resultString.length(), "utf-8", NULL);
+ }
+
+
PyMemberDef {{ message.name }}_members[] = {
{NULL} // Sentinel
};
@@ -638,7 +667,7 @@ namespace {
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
- 0, /*tp_repr*/
+ {{ message.name }}_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
Please sign in to comment.
Something went wrong with that request. Please try again.