diff --git a/src/PythonQtClassInfo.cpp b/src/PythonQtClassInfo.cpp index b9da640ad..937909d20 100644 --- a/src/PythonQtClassInfo.cpp +++ b/src/PythonQtClassInfo.cpp @@ -49,6 +49,11 @@ QHash PythonQtMethodInfo::_parameterTypeDict; +// List of words that are reserved in Python, but not in C++, so they need escaping +QSet PythonQtClassInfo::_reservedNames{ + "None", "True", "False" +}; + PythonQtClassInfo::PythonQtClassInfo() { _meta = nullptr; _constructors = nullptr; @@ -279,7 +284,7 @@ bool PythonQtClassInfo::lookForEnumAndCache(const QMetaObject* meta, const char* if (e.isFlag()) continue; for (int j=0; j < e.keyCount(); j++) { - if (qstrcmp(e.key(j), memberName)==0) { + if (escapeReservedNames(e.key(j)) == memberName) { PyObject* enumType = findEnumWrapper(e.name()); if (enumType) { PythonQtObjectPtr enumValuePtr; @@ -869,7 +874,7 @@ void PythonQtClassInfo::createEnumWrappers(const QMetaObject* meta) for (int j = 0; j < e.keyCount(); j++) { PythonQtObjectPtr enumValuePtr; enumValuePtr.setNewRef(PythonQtPrivate::createEnumValueInstance(p.object(), e.value(j))); - p.addVariable(e.key(j), QVariant::fromValue(enumValuePtr)); + p.addVariable(escapeReservedNames(e.key(j)), QVariant::fromValue(enumValuePtr)); } } #endif @@ -1018,6 +1023,16 @@ PythonQtVoidPtrCB* PythonQtClassInfo::referenceCountingUnrefCB() return _unrefCallback; } +QByteArray PythonQtClassInfo::escapeReservedNames(const QByteArray& name) +{ + if (_reservedNames.contains(name)) { + return name + "_"; + } + else { + return name; + } +} + void PythonQtClassInfo::updateRefCountingCBs() { if (!_refCallback) { diff --git a/src/PythonQtClassInfo.h b/src/PythonQtClassInfo.h index 5e3faf54a..48f7364d9 100644 --- a/src/PythonQtClassInfo.h +++ b/src/PythonQtClassInfo.h @@ -237,6 +237,10 @@ class PYTHONQT_EXPORT PythonQtClassInfo { //! _typeSlots with Type_RichCompare. The result is cached internally. bool supportsRichCompare(); + //! Sometimes enum values use a reserved name in Python. In this case + //! replace it with something that is not reserved + QByteArray escapeReservedNames(const QByteArray& name); + private: void updateRefCountingCBs(); @@ -300,6 +304,7 @@ class PYTHONQT_EXPORT PythonQtClassInfo { bool _searchPolymorphicHandlerOnParent; bool _searchRefCountCB; + static QSet _reservedNames; }; //---------------------------------------------------------------