Skip to content

Commit 123c790

Browse files
hjmjohnsonjcfr
andcommitted
chore: Remove obsolete Python 2 compatibility code
Remove all `#ifndef PY3K` conditionals and related branches, consolidating on Python 3 APIs. This drops redundant code paths and consistently renames `nb_nonzero` helpers to `nb_bool`. Remove the comment related to the use of `Py_FlushLine` in `custom_system_exit_exception_handle` in `PythonQt.cpp`, which is not needed in Python 3.x. See python/cpython@79139b247b0 ("Kill off softspace completely (except in formatter.py which seems to have a different feature with the same name). The change to test_doctest.txt reduces the doctest failures to 3.", 2007-02-09) Co-authored-by: Jean-Christophe Fillion-Robin <[email protected]>
1 parent 5c859be commit 123c790

11 files changed

+13
-268
lines changed

src/PythonQt.cpp

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
101101
_self->_p->_pySourcelessFileLoader = importlib.getVariable("SourcelessFileLoader");
102102
}
103103

104-
#ifdef PY3K
105104
// Import asyncio only when not explicitly disabled.
106105
// Importing asyncio on Py3.12+ pulls in ssl/_ssl; some environments/tests
107106
// want to avoid that during early embedded init.
@@ -114,7 +113,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
114113
_self->_p->_pyFutureClass = asyncio.getVariable("Future");
115114
}
116115
}
117-
#endif
118116

119117
PythonQt::priv()->setupSharedLibrarySuffixes();
120118

@@ -348,11 +346,7 @@ PythonQt::PythonQt(int flags, const QByteArray& pythonQtModuleName)
348346
_p->_initFlags = flags;
349347

350348
if ((flags & PythonAlreadyInitialized) == 0) {
351-
#ifdef PY3K
352349
Py_SetProgramName(const_cast<wchar_t*>(L"PythonQt"));
353-
#else
354-
Py_SetProgramName(const_cast<char*>("PythonQt"));
355-
#endif
356350
if (flags & IgnoreSiteModule) {
357351
// this prevents the automatic importing of Python site files
358352
Py_NoSiteFlag = 1;
@@ -443,7 +437,6 @@ void PythonQtPrivate::setTaskDoneCallback(const PythonQtObjectPtr & callable)
443437
PythonQtObjectPtr PythonQtPrivate::checkAndRunCoroutine(const PythonQtObjectPtr& object)
444438
{
445439
PythonQtObjectPtr result;
446-
#ifdef PY3K
447440
if (!PyCoro_CheckExact(object))
448441
{
449442
return result;
@@ -466,9 +459,6 @@ PythonQtObjectPtr PythonQtPrivate::checkAndRunCoroutine(const PythonQtObjectPtr&
466459
Py_XDECREF(methodName);
467460
}
468461
Py_XDECREF(args);
469-
#else
470-
Q_UNUSED(object)
471-
#endif
472462
return result;
473463
}
474464

@@ -1000,11 +990,7 @@ QVariant PythonQt::evalCode(PyObject* object, PyObject* pycode) {
1000990
}
1001991
PyObject* r = nullptr;
1002992
if (dict) {
1003-
#ifdef PY3K
1004993
r = PyEval_EvalCode(pycode, globals, dict);
1005-
#else
1006-
r = PyEval_EvalCode((PyCodeObject*)pycode, globals, dict);
1007-
#endif
1008994
}
1009995
if (r) {
1010996
result = PythonQtConv::PyObjToQVariant(r);
@@ -1264,14 +1250,7 @@ QStringList PythonQt::introspectObject(PyObject* object, ObjectType type)
12641250
keys = PyDict_Keys(object);
12651251
isDict = true;
12661252
} else {
1267-
#if defined(MEVISLAB) && !defined(PY3K)
1268-
int oldPy3kWarningFlag = Py_Py3kWarningFlag;
1269-
Py_Py3kWarningFlag = 0; // temporarily disable Python 3 warnings
12701253
keys = PyObject_Dir(object);
1271-
Py_Py3kWarningFlag = oldPy3kWarningFlag;
1272-
#else
1273-
keys = PyObject_Dir(object);
1274-
#endif
12751254
}
12761255
if (keys) {
12771256
int count = PyList_Size(keys);
@@ -1307,9 +1286,6 @@ QStringList PythonQt::introspectObject(PyObject* object, ObjectType type)
13071286
&& value->ob_type != &PyModule_Type
13081287
&& value->ob_type != &PyType_Type
13091288
&& value->ob_type != &PythonQtSlotFunction_Type
1310-
#ifndef PY3K
1311-
&& value->ob_type != &PyClass_Type
1312-
#endif
13131289
) {
13141290
results << keystr;
13151291
}
@@ -1662,14 +1638,9 @@ int custom_system_exit_exception_handler()
16621638
// return exitcode;
16631639

16641640
PyErr_Fetch(&exception, &value, &tb);
1665-
#ifndef PY3K
1666-
if (Py_FlushLine()) {
1667-
PyErr_Clear();
1668-
}
1669-
#else
1670-
// TODO: unclear what to do, since Py_FlushLine is gone...
1671-
#endif
1641+
16721642
fflush(stdout);
1643+
16731644
if (value == nullptr || value == Py_None)
16741645
goto done;
16751646
if (PyExceptionInstance_Check(value)) {
@@ -1846,7 +1817,6 @@ static PyMethodDef PythonQtMethods[] = {
18461817
{nullptr, nullptr, 0, nullptr}
18471818
};
18481819

1849-
#ifdef PY3K
18501820
static PyModuleDef PythonQtModuleDef = {
18511821
PyModuleDef_HEAD_INIT,
18521822
"",
@@ -1858,20 +1828,15 @@ static PyModuleDef PythonQtModuleDef = {
18581828
nullptr,
18591829
nullptr
18601830
};
1861-
#endif
18621831

18631832
void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQtModuleName)
18641833
{
18651834
QByteArray name = "PythonQt";
18661835
if (!pythonQtModuleName.isEmpty()) {
18671836
name = pythonQtModuleName;
18681837
}
1869-
#ifdef PY3K
18701838
PythonQtModuleDef.m_name = name.constData();
18711839
_p->_pythonQtModule = PyModule_Create(&PythonQtModuleDef);
1872-
#else
1873-
_p->_pythonQtModule = Py_InitModule(name.constData(), PythonQtMethods);
1874-
#endif
18751840
_p->_pythonQtModuleName = name;
18761841

18771842
Py_INCREF((PyObject*)&PythonQtBoolResult_Type);
@@ -1911,13 +1876,11 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
19111876
}
19121877
Py_XDECREF(old_module_names);
19131878

1914-
#ifdef PY3K
19151879
PyObject* modulesAttr = PyObject_GetAttrString(sys.object(), "modules");
19161880
PyObject* pyUnicodeObject = PyUnicode_FromString(name.constData());
19171881
PyDict_SetItem(modulesAttr, pyUnicodeObject, _p->_pythonQtModule.object());
19181882
Py_XDECREF(modulesAttr);
19191883
Py_XDECREF(pyUnicodeObject);
1920-
#endif
19211884
}
19221885

19231886
bool PythonQt::redirectStdOutCallbackEnabled() const
@@ -2287,9 +2250,6 @@ bool PythonQtPrivate::isMethodDescriptor(PyObject* object) const
22872250
!PyObject_HasAttrString(object, "__set__") &&
22882251
!PyMethod_Check(object) &&
22892252
!PyFunction_Check(object)
2290-
#ifndef PY3K
2291-
&& !PyClass_Check(object)
2292-
#endif
22932253
) {
22942254
return true;
22952255
}
@@ -2651,20 +2611,12 @@ void PythonQtPrivate::shellClassDeleted( void* shellClass )
26512611

26522612
PyObject* PythonQtPrivate::wrapMemoryAsBuffer( const void* data, Py_ssize_t size )
26532613
{
2654-
#ifdef PY3K
26552614
return PyMemoryView_FromMemory((char*)data, size, PyBUF_READ);
2656-
#else
2657-
return PyBuffer_FromMemory((char*)data, size);
2658-
#endif
26592615
}
26602616

26612617
PyObject* PythonQtPrivate::wrapMemoryAsBuffer( void* data, Py_ssize_t size )
26622618
{
2663-
#ifdef PY3K
26642619
return PyMemoryView_FromMemory((char*)data, size, PyBUF_WRITE);
2665-
#else
2666-
return PyBuffer_FromReadWriteMemory((char*)data, size);
2667-
#endif
26682620
}
26692621

26702622
PythonQtClassInfo* PythonQtPrivate::getClassInfo( const QMetaObject* meta )

src/PythonQt.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,9 @@ typedef QObject* PythonQtQObjectCreatorFunctionCB();
147147
template<class T> QObject* PythonQtCreateObject() { return new T(); }
148148

149149
//! Helper define to convert from QString to Python C-API
150-
#ifdef PY3K
151150
#define QStringToPythonConstCharPointer(arg) ((arg).toUtf8().constData())
152151
#define QStringToPythonCharPointer(arg) ((arg).toUtf8().data())
153152
#define QStringToPythonEncoding(arg) ((arg).toUtf8())
154-
#else
155-
#define QStringToPythonConstCharPointer(arg) ((arg).toLatin1().constData())
156-
#define QStringToPythonCharPointer(arg) ((arg).toLatin1().data())
157-
#define QStringToPythonEncoding(arg) ((arg).toLatin1())
158-
#endif
159153

160154
//! The main interface to the Python Qt binding, realized as a singleton
161155
/*!
@@ -368,7 +362,6 @@ class PYTHONQT_EXPORT PythonQt : public QObject {
368362
//! Parses the given file and returns the python code object, this can then be used to call evalCode()
369363
//! It uses Python's importlib machinery to load the file's code and supports source and sourceless loading
370364
//! and generation of cache files.
371-
//! This method is PY3K only!
372365
PythonQtObjectPtr parseFileWithPythonLoaders(const QString& filename);
373366

374367
//! evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string)

src/PythonQtBoolResult.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static PyObject *PythonQtBoolResult_repr(PythonQtBoolResultObject *obj)
5858
return PyString_FromString(wrapper->_value?"BoolResult(True)":"BoolResult(False)");
5959
}
6060

61-
static int PythonQtBoolResult_nonzero(PyObject *obj)
61+
static int PythonQtBoolResult_bool(PyObject *obj)
6262
{
6363
PythonQtBoolResultObject* wrapper = (PythonQtBoolResultObject*)obj;
6464
return wrapper->_value;
@@ -69,38 +69,25 @@ static PyNumberMethods PythonQtBoolResult_as_number = {
6969
nullptr, /* nb_add */
7070
nullptr, /* nb_subtract */
7171
nullptr, /* nb_multiply */
72-
#ifndef PY3K
73-
nullptr, /* nb_divide */
74-
#endif
7572
nullptr, /* nb_remainder */
7673
nullptr, /* nb_divmod */
7774
nullptr, /* nb_power */
7875
nullptr, /* nb_negative */
7976
nullptr, /* nb_positive */
8077
nullptr, /* nb_absolute */
81-
PythonQtBoolResult_nonzero, /* nb_nonzero / nb_bool in Py3K */
78+
PythonQtBoolResult_bool, /* nb_bool */
8279
nullptr, /* nb_invert */
8380
nullptr, /* nb_lshift */
8481
nullptr, /* nb_rshift */
8582
nullptr, /* nb_and */
8683
nullptr, /* nb_xor */
8784
nullptr, /* nb_or */
88-
#ifndef PY3K
89-
nullptr, /* nb_coerce */
90-
#endif
9185
nullptr, /* nb_int */
92-
nullptr, /* nb_long / nb_reserved in Py3K */
86+
nullptr, /* nb_reserved */
9387
nullptr, /* nb_float */
94-
#ifndef PY3K
95-
nullptr, /* nb_oct */
96-
nullptr, /* nb_hex */
97-
#endif
9888
nullptr, /* nb_inplace_add */
9989
nullptr, /* nb_inplace_subtract */
10090
nullptr, /* nb_inplace_multiply */
101-
#ifndef PY3K
102-
nullptr, /* nb_inplace_divide */
103-
#endif
10491
nullptr, /* nb_inplace_remainder */
10592
nullptr, /* nb_inplace_power */
10693
nullptr, /* nb_inplace_lshift */
@@ -112,9 +99,7 @@ static PyNumberMethods PythonQtBoolResult_as_number = {
11299
nullptr, /* nb_true_divide */
113100
nullptr, /* nb_inplace_floor_divide */
114101
nullptr, /* nb_inplace_true_divide */
115-
#ifdef PY3K
116-
nullptr, /* nb_index in Py3K */
117-
#endif
102+
nullptr, /* nb_index */
118103
};
119104

120105
PyTypeObject PythonQtBoolResult_Type = {
@@ -155,4 +140,3 @@ PyTypeObject PythonQtBoolResult_Type = {
155140
0, /* tp_dictoffset */
156141
(initproc)&PythonQtBoolResult_init, /* tp_init */
157142
};
158-

src/PythonQtClassWrapper.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ static void initializeSlots(PythonQtClassWrapper* wrap)
260260
wrap->_base.as_number.nb_multiply = (binaryfunc)PythonQtInstanceWrapper_mul;
261261
}
262262
if (typeSlots & PythonQt::Type_Divide) {
263-
#ifndef PY3K
264-
wrap->_base.as_number.nb_divide = (binaryfunc)PythonQtInstanceWrapper_div;
265-
#endif
266263
wrap->_base.as_number.nb_true_divide = (binaryfunc)PythonQtInstanceWrapper_div;
267264
}
268265
if (typeSlots & PythonQt::Type_And) {
@@ -294,9 +291,6 @@ static void initializeSlots(PythonQtClassWrapper* wrap)
294291
wrap->_base.as_number.nb_inplace_multiply = (binaryfunc)PythonQtInstanceWrapper_imul;
295292
}
296293
if (typeSlots & PythonQt::Type_InplaceDivide) {
297-
#ifndef PY3K
298-
wrap->_base.as_number.nb_inplace_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
299-
#endif
300294
wrap->_base.as_number.nb_inplace_true_divide = (binaryfunc)PythonQtInstanceWrapper_idiv;
301295
}
302296
if (typeSlots & PythonQt::Type_InplaceAnd) {
@@ -321,11 +315,7 @@ static void initializeSlots(PythonQtClassWrapper* wrap)
321315
wrap->_base.as_number.nb_invert = (unaryfunc)PythonQtInstanceWrapper_invert;
322316
}
323317
if (typeSlots & PythonQt::Type_NonZero) {
324-
#ifdef PY3K
325318
wrap->_base.as_number.nb_bool = (inquiry)PythonQtInstanceWrapper_nonzero;
326-
#else
327-
wrap->_base.as_number.nb_nonzero = (inquiry)PythonQtInstanceWrapper_nonzero;
328-
#endif
329319
}
330320
}
331321
}
@@ -563,11 +553,7 @@ static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
563553
}
564554

565555
// look for the internal methods (className(), help())
566-
#ifdef PY3K
567556
PyObject* internalMethod = PyObject_GenericGetAttr(obj, name);
568-
#else
569-
PyObject* internalMethod = Py_FindMethod( PythonQtClassWrapper_methods, obj, (char*)attributeName);
570-
#endif
571557
if (internalMethod) {
572558
return internalMethod;
573559
}
@@ -634,11 +620,7 @@ PyTypeObject PythonQtClassWrapper_Type = {
634620
0, /* tp_weaklistoffset */
635621
nullptr, /* tp_iter */
636622
nullptr, /* tp_iternext */
637-
#ifdef PY3K
638623
PythonQtClassWrapper_methods, /* tp_methods */
639-
#else
640-
nullptr, /* tp_methods */
641-
#endif
642624
nullptr, /* tp_members */
643625
nullptr, /* tp_getset */
644626
nullptr, /* tp_base */
@@ -653,4 +635,3 @@ PyTypeObject PythonQtClassWrapper_Type = {
653635
};
654636

655637
//-------------------------------------------------------
656-

0 commit comments

Comments
 (0)