Skip to content

Commit f52ff96

Browse files
authored
Add support for Python 3.11 (commontk#91)
1 parent 1e794e5 commit f52ff96

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144
python-version: '3.6'
145145
qt-version: '5.11.*'
146146
- macos-version: '12'
147-
python-version: '3.10'
147+
python-version: '3.11'
148148
qt-version: '5.12.*'
149149
runs-on: macos-${{ matrix.macos-version }}
150150
steps:

src/PythonQt.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,20 @@
5858

5959
#include <QDir>
6060

61-
#if PY_MINOR_VERSION >= 10
61+
#if PY_VERSION_HEX >= 0x030A0000
6262
#include <cpython/pydebug.h>
6363
#else
6464
#include <pydebug.h>
6565
#endif
6666

67+
// .co_varnames was removed in 3.11, but the helper function was added
68+
#if PY_VERSION_HEX < 0x030B0000
69+
static inline PyObject *PyCode_GetVarnames(PyCodeObject *o) {
70+
Py_XINCREF(o->co_varnames);
71+
return o->co_varnames;
72+
}
73+
#endif
74+
6775
#include <vector>
6876

6977
PythonQt* PythonQt::_self = nullptr;
@@ -2427,25 +2435,26 @@ QString PythonQtPrivate::getSignature(PyObject* object)
24272435
// inspect.getargs() can handle anonymous (tuple) arguments, while this code does not.
24282436
// It can be implemented, but it may be rarely needed and not necessary.
24292437
PyCodeObject* code = (PyCodeObject*)func->func_code;
2430-
if (code->co_varnames) {
2438+
if (auto co_varnames = PyCode_GetVarnames(code)) {
24312439
int nargs = code->co_argcount;
2432-
Q_ASSERT(PyTuple_Check(code->co_varnames));
2440+
Q_ASSERT(PyTuple_Check(co_varnames));
24332441
for (int i=0; i<nargs; i++) {
2434-
PyObject* name = PyTuple_GetItem(code->co_varnames, i);
2442+
PyObject* name = PyTuple_GetItem(co_varnames, i);
24352443
Q_ASSERT(PyString_Check(name));
24362444
arguments << PyString_AsString(name);
24372445
}
24382446
if (code->co_flags & CO_VARARGS) {
2439-
PyObject* s = PyTuple_GetItem(code->co_varnames, nargs);
2447+
PyObject* s = PyTuple_GetItem(co_varnames, nargs);
24402448
Q_ASSERT(PyString_Check(s));
24412449
varargs = PyString_AsString(s);
24422450
nargs += 1;
24432451
}
24442452
if (code->co_flags & CO_VARKEYWORDS) {
2445-
PyObject* s = PyTuple_GetItem(code->co_varnames, nargs);
2453+
PyObject* s = PyTuple_GetItem(co_varnames, nargs);
24462454
Q_ASSERT(PyString_Check(s));
24472455
varkeywords = PyString_AsString(s);
24482456
}
2457+
Py_DECREF(co_varnames);
24492458
}
24502459

24512460
PyObject* defaultsTuple = func->func_defaults;

src/PythonQtClassWrapper.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#include "structmember.h"
5050
#include "methodobject.h"
5151
#include "compile.h"
52-
#include "eval.h"
5352
#include <QString>
5453

5554
class PythonQtClassInfo;

src/PythonQtInstanceWrapper.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include "structmember.h"
5252
#include "methodobject.h"
5353
#include "compile.h"
54-
#include "eval.h"
5554

5655
class PythonQtClassInfo;
5756
class QObject;

src/PythonQtSignalReceiver.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#include "PythonQtConversion.h"
4646
#include <QMetaObject>
4747
#include <QMetaMethod>
48-
#include "funcobject.h"
4948

5049
// use -2 to signal that the variable is uninitialized
5150
int PythonQtSignalReceiver::_destroyedSignal1Id = -2;

0 commit comments

Comments
 (0)