Skip to content

Commit

Permalink
Escape reserved names by adding "_"
Browse files Browse the repository at this point in the history
One example I stumbled upon is QActionGroup.ExclusionPolicy.None, which
is a syntax error. So from now on it will be
QActionGroup.ExclusionPolicy.None_.
  • Loading branch information
usiems authored and mrbean-bremen committed Dec 11, 2023
1 parent c35d010 commit 77eba42
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/PythonQtClassInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@

QHash<QByteArray, int> PythonQtMethodInfo::_parameterTypeDict;

// List of words that are reserved in Python, but not in C++, so they need escaping
QSet<QByteArray> PythonQtClassInfo::_reservedNames{
"None", "True", "False"
};

PythonQtClassInfo::PythonQtClassInfo() {
_meta = nullptr;
_constructors = nullptr;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/PythonQtClassInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -300,6 +304,7 @@ class PYTHONQT_EXPORT PythonQtClassInfo {
bool _searchPolymorphicHandlerOnParent;
bool _searchRefCountCB;

static QSet<QByteArray> _reservedNames;
};

//---------------------------------------------------------------
Expand Down

0 comments on commit 77eba42

Please sign in to comment.