Skip to content

Commit 55c91c4

Browse files
fix: non type template instantiation for enums (#128)
example: ```c++ enum MyEnum { A, B, C }; template<MyEnum T> struct MyClass {}; ``` ```python gbl.MyClass[gbl.MyEnum.A]() // instantiation with enum ```
1 parent 5959de4 commit 55c91c4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Utility.cxx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,22 @@ static bool AddTypeName(std::vector<Cpp::TemplateArgInfo>& types, PyObject* tn,
829829
for (auto nn : {PyStrings::gCppName, PyStrings::gName}) {
830830
PyObject* tpName = PyObject_GetAttr(tn, nn);
831831
if (tpName) {
832-
types.push_back(Cppyy::GetType(CPyCppyy_PyText_AsString(tpName), /* enable_slow_lookup */ true));
832+
Cppyy::TCppType_t type = Cppyy::GetType(CPyCppyy_PyText_AsString(tpName), /* enable_slow_lookup */ true);
833+
if (Cppyy::IsEnumType(type)) {
834+
PyObject *value_int = PyNumber_Index(tn);
835+
if (!value_int) {
836+
types.push_back(type);
837+
PyErr_Clear();
838+
} else {
839+
PyObject* pystr = PyObject_Str(tn);
840+
std::string num = CPyCppyy_PyText_AsString(pystr);
841+
types.push_back({type, strdup(num.c_str())});
842+
Py_DECREF(pystr);
843+
Py_DECREF(value_int);
844+
}
845+
} else {
846+
types.push_back(type);
847+
}
833848
Py_DECREF(tpName);
834849
return true;
835850
}

0 commit comments

Comments
 (0)