From fddf371c86bb3c2339c518859f6bd3e1721dda88 Mon Sep 17 00:00:00 2001 From: John Ehresman Date: Fri, 12 Jul 2013 11:15:08 -0400 Subject: [PATCH] Return None for .name of enum if no name for value. This fixes segfaults when trying to get the name of a dead key from a qt key event. Change-Id: Ie631e2245bca0a3fee8616854f6479d3dec0c2bb Reviewed-by: Roman Lacko Reviewed-by: John Cummings --- libshiboken/sbkenum.cpp | 9 +++++++-- tests/samplebinding/enum_test.py | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libshiboken/sbkenum.cpp b/libshiboken/sbkenum.cpp index ba6d2db..df3a534 100644 --- a/libshiboken/sbkenum.cpp +++ b/libshiboken/sbkenum.cpp @@ -76,8 +76,13 @@ static int SbkEnumObject_print(PyObject* self, FILE* fp, int) static PyObject* SbkEnumObject_name(PyObject* self, void*) { - Py_INCREF(((SbkEnumObject*)self)->ob_name); - return ((SbkEnumObject*)self)->ob_name; + SbkEnumObject* enum_self = (SbkEnumObject*)self; + + if (enum_self->ob_name == NULL) + Py_RETURN_NONE; + + Py_INCREF(enum_self->ob_name); + return enum_self->ob_name; } static PyObject* SbkEnum_tp_new(PyTypeObject* type, PyObject* args, PyObject* kwds) diff --git a/tests/samplebinding/enum_test.py b/tests/samplebinding/enum_test.py index 81f4c59..a428834 100644 --- a/tests/samplebinding/enum_test.py +++ b/tests/samplebinding/enum_test.py @@ -136,6 +136,11 @@ def testValue(self): e = MyEvent() self.assertEqual(repr(e.eventType()), 'sample.Event.EventType(999)') + def testNoneName(self): + e = MyEvent() + t = e.eventType() + self.assertEqual(t.name, None) + class EnumOverloadTest(unittest.TestCase): '''Test case for overloads involving enums'''