diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-01-21 10:21:16 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-01-21 11:07:38 -0300 |
commit | 3293847076330f07de9c70dd82f32bfeede6d317 (patch) | |
tree | f57e1913a39bab2f2c6536df32757ca149a7b711 | |
parent | dc0a3de17e9786037746cb02fb52e342f8cae34c (diff) | |
download | shiboken-3293847076330f07de9c70dd82f32bfeede6d317.tar.gz shiboken-3293847076330f07de9c70dd82f32bfeede6d317.tar.xz shiboken-3293847076330f07de9c70dd82f32bfeede6d317.zip |
Shiboken enums now have a tp_print representation.
This fixes the bug #611[1], and an unit test was also added.
[1] http://bugs.openbossa.org/show_bug.cgi?id=611
Reviewed by Hugo Parente <hugo.lima@openbossa.org>
Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
Reviewed by Renato Araújo <renato.filho@openbossa.org>
-rw-r--r-- | libshiboken/sbkenum.cpp | 16 | ||||
-rw-r--r-- | tests/samplebinding/enum_test.py | 14 |
2 files changed, 29 insertions, 1 deletions
diff --git a/libshiboken/sbkenum.cpp b/libshiboken/sbkenum.cpp index 34a4b772..7aea88ad 100644 --- a/libshiboken/sbkenum.cpp +++ b/libshiboken/sbkenum.cpp @@ -36,14 +36,27 @@ struct SbkEnumObject PyObject* ob_name; }; +#define SBKENUMOBJECT_REPR_STRING "<enum-item %s.%s (%ld)>" + static PyObject* SbkEnumObject_repr(PyObject* self) { - return PyString_FromFormat("<enum-item %s.%s (%ld)>", + return PyString_FromFormat(SBKENUMOBJECT_REPR_STRING, self->ob_type->tp_name, PyString_AS_STRING(((SbkEnumObject*)self)->ob_name), ((SbkEnumObject*)self)->ob_ival); } +static int SbkEnumObject_print(PyObject* self, FILE* fp, int) +{ + Py_BEGIN_ALLOW_THREADS + fprintf(fp, SBKENUMOBJECT_REPR_STRING, + self->ob_type->tp_name, + PyString_AS_STRING(((SbkEnumObject*)self)->ob_name), + ((SbkEnumObject*)self)->ob_ival); + Py_END_ALLOW_THREADS + return 0; +} + static PyObject* SbkEnumObject_name(PyObject* self, void*) { Py_INCREF(((SbkEnumObject*)self)->ob_name); @@ -169,6 +182,7 @@ PyTypeObject* newType(const char* name) ::memset(type, 0, sizeof(PyTypeObject)); type->ob_type = &SbkEnumType_Type; type->tp_basicsize = sizeof(SbkEnumObject); + type->tp_print = &SbkEnumObject_print; type->tp_repr = &SbkEnumObject_repr; type->tp_str = &SbkEnumObject_repr; type->tp_flags = Py_TPFLAGS_DEFAULT; diff --git a/tests/samplebinding/enum_test.py b/tests/samplebinding/enum_test.py index 71ebef60..698287f9 100644 --- a/tests/samplebinding/enum_test.py +++ b/tests/samplebinding/enum_test.py @@ -26,6 +26,8 @@ '''Test cases for Python representation of C++ enums.''' +import os +import sys import unittest import sample @@ -83,6 +85,18 @@ class EnumTest(unittest.TestCase): self.assertEqual(SampleNamespace.AnonymousClassEnum_Value0, 0) self.assertEqual(SampleNamespace.AnonymousClassEnum_Value1, 1) + def testEnumTpPrintImplementation(self): + '''Without SbkEnum.tp_print 'print' returns the enum represented as an int.''' + tmpfile = os.tmpfile() + sys.stdout = tmpfile + print Event.ANY_EVENT + sys.stdout = sys.__stdout__ + tmpfile.seek(0) + text = tmpfile.read().strip() + tmpfile.close() + self.assertEqual(text, str(Event.ANY_EVENT)) + self.assertEqual(text, repr(Event.ANY_EVENT)) + class EnumOverloadTest(unittest.TestCase): '''Test case for overloads involving enums''' |