Skip to content

Commit b59c6b1

Browse files
Vipul-Cariappaaaronj0
authored andcommitted
refactor pyval_from_enum
1 parent b1c76d0 commit b59c6b1

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

src/CPPDataMember.cxx

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "PyStrings.h"
55
#include "CPPDataMember.h"
66
#include "CPPInstance.h"
7+
#include "CPPEnum.h"
78
#include "Dimensions.h"
89
#include "LowLevelViews.h"
910
#include "ProxyWrappers.h"
@@ -84,22 +85,7 @@ static PyObject* dm_get(CPPDataMember* dm, CPPInstance* pyobj, PyObject* /* kls
8485

8586
if (Cppyy::IsEnumConstant(dm->fScope)) {
8687
// anonymous enum
87-
long long llval = Cppyy::GetEnumDataValue(dm->fScope);
88-
const std::string& enum_type = Cppyy::ResolveEnum(dm->fScope);
89-
90-
PyObject* bval;
91-
if (enum_type == "bool") {
92-
bval = (bool)llval ? Py_True : Py_False;
93-
Py_INCREF(bval);
94-
} else if (enum_type == "char") {
95-
char val = (char)llval;
96-
bval = CPyCppyy_PyText_FromStringAndSize(&val, 1);
97-
} else if (enum_type == "int" || enum_type == "unsigned int")
98-
bval = PyInt_FromLong((long)llval);
99-
else
100-
bval = PyLong_FromLongLong(llval);
101-
102-
return bval;
88+
return pyval_from_enum(Cppyy::ResolveEnum(dm->fScope), nullptr, nullptr, dm->fScope);
10389
}
10490
}
10591

src/CPPEnum.cxx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static PyObject* pytype_from_enum_type(const std::string& enum_type)
1818
}
1919

2020
//----------------------------------------------------------------------------
21-
static PyObject* pyval_from_enum(const std::string& enum_type, PyObject* pytype,
21+
PyObject* CPyCppyy::pyval_from_enum(const std::string& enum_type, PyObject* pytype,
2222
PyObject* btype, Cppyy::TCppScope_t enum_constant) {
2323
long long llval = Cppyy::GetEnumDataValue(enum_constant);
2424

@@ -37,11 +37,13 @@ static PyObject* pyval_from_enum(const std::string& enum_type, PyObject* pytype,
3737
else
3838
bval = PyLong_FromLongLong(llval);
3939

40-
PyObject* args = PyTuple_New(1);
41-
PyTuple_SET_ITEM(args, 0, bval);
42-
PyObject* result = ((PyTypeObject*)btype)->tp_new((PyTypeObject*)pytype, args, nullptr);
43-
Py_DECREF(args);
44-
return result;
40+
if (pytype && btype) {
41+
PyObject* args = PyTuple_New(1);
42+
PyTuple_SET_ITEM(args, 0, bval);
43+
bval = ((PyTypeObject*)btype)->tp_new((PyTypeObject*)pytype, args, nullptr);
44+
Py_DECREF(args);
45+
}
46+
return bval;
4547
}
4648

4749

src/CPPEnum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ typedef PyObject CPPEnum;
1111
//- creation -----------------------------------------------------------------
1212
CPPEnum* CPPEnum_New(const std::string& name, Cppyy::TCppScope_t scope);
1313

14+
PyObject* pyval_from_enum(const std::string& enum_type, PyObject* pytype,
15+
PyObject* btype, Cppyy::TCppScope_t enum_constant);
1416
} // namespace CPyCppyy
1517

1618
#endif // !CPYCPPYY_CPPENUM_H

0 commit comments

Comments
 (0)