Skip to content

Commit 77eba42

Browse files
usiemsmrbean-bremen
authored andcommitted
Escape reserved names by adding "_"
One example I stumbled upon is QActionGroup.ExclusionPolicy.None, which is a syntax error. So from now on it will be QActionGroup.ExclusionPolicy.None_.
1 parent c35d010 commit 77eba42

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/PythonQtClassInfo.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@
4949

5050
QHash<QByteArray, int> PythonQtMethodInfo::_parameterTypeDict;
5151

52+
// List of words that are reserved in Python, but not in C++, so they need escaping
53+
QSet<QByteArray> PythonQtClassInfo::_reservedNames{
54+
"None", "True", "False"
55+
};
56+
5257
PythonQtClassInfo::PythonQtClassInfo() {
5358
_meta = nullptr;
5459
_constructors = nullptr;
@@ -279,7 +284,7 @@ bool PythonQtClassInfo::lookForEnumAndCache(const QMetaObject* meta, const char*
279284
if (e.isFlag()) continue;
280285

281286
for (int j=0; j < e.keyCount(); j++) {
282-
if (qstrcmp(e.key(j), memberName)==0) {
287+
if (escapeReservedNames(e.key(j)) == memberName) {
283288
PyObject* enumType = findEnumWrapper(e.name());
284289
if (enumType) {
285290
PythonQtObjectPtr enumValuePtr;
@@ -869,7 +874,7 @@ void PythonQtClassInfo::createEnumWrappers(const QMetaObject* meta)
869874
for (int j = 0; j < e.keyCount(); j++) {
870875
PythonQtObjectPtr enumValuePtr;
871876
enumValuePtr.setNewRef(PythonQtPrivate::createEnumValueInstance(p.object(), e.value(j)));
872-
p.addVariable(e.key(j), QVariant::fromValue(enumValuePtr));
877+
p.addVariable(escapeReservedNames(e.key(j)), QVariant::fromValue(enumValuePtr));
873878
}
874879
}
875880
#endif
@@ -1018,6 +1023,16 @@ PythonQtVoidPtrCB* PythonQtClassInfo::referenceCountingUnrefCB()
10181023
return _unrefCallback;
10191024
}
10201025

1026+
QByteArray PythonQtClassInfo::escapeReservedNames(const QByteArray& name)
1027+
{
1028+
if (_reservedNames.contains(name)) {
1029+
return name + "_";
1030+
}
1031+
else {
1032+
return name;
1033+
}
1034+
}
1035+
10211036
void PythonQtClassInfo::updateRefCountingCBs()
10221037
{
10231038
if (!_refCallback) {

src/PythonQtClassInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ class PYTHONQT_EXPORT PythonQtClassInfo {
237237
//! _typeSlots with Type_RichCompare. The result is cached internally.
238238
bool supportsRichCompare();
239239

240+
//! Sometimes enum values use a reserved name in Python. In this case
241+
//! replace it with something that is not reserved
242+
QByteArray escapeReservedNames(const QByteArray& name);
243+
240244
private:
241245
void updateRefCountingCBs();
242246

@@ -300,6 +304,7 @@ class PYTHONQT_EXPORT PythonQtClassInfo {
300304
bool _searchPolymorphicHandlerOnParent;
301305
bool _searchRefCountCB;
302306

307+
static QSet<QByteArray> _reservedNames;
303308
};
304309

305310
//---------------------------------------------------------------

0 commit comments

Comments
 (0)