Skip to content

Commit 005d4b6

Browse files
committed
REF: Update verbose/optimize flag handling for Python 3.11 compatibility
Add conditional checks for the `verbose` flag to distinguish between legacy `Py_VerboseFlag` `Py_OptimizeFlag` and modern `sys.verbose` usage, ensuring compatibility with Python versions 3.11 and later. • Python is moving away from global interpreter state. • Use of global variables like Py_VerboseFlag is not thread-safe. • Python 3.12+ removes or restricts access to such globals in favor of per-interpreter APIs.
1 parent e886394 commit 005d4b6

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

src/PythonQtImporter.cpp

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,12 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
309309
}
310310

311311
Py_DECREF(code);
312+
#if PY_VERSION_HEX < 0x030B0000 // Python < 3.11
312313
if (Py_VerboseFlag) {
314+
#else
315+
PyObject* verbose_flag = PySys_GetObject("verbose");
316+
if (verbose_flag && PyLong_AsLong(verbose_flag) > 0) {
317+
#endif
313318
PySys_WriteStderr("import %s # loaded from %s\n",
314319
fullname, QStringToPythonConstCharPointer(fullPath));
315320
}
@@ -556,9 +561,15 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
556561
}
557562
fp = open_exclusive(filename);
558563
if (fp == nullptr) {
559-
if (Py_VerboseFlag)
564+
#if PY_VERSION_HEX < 0x030B0000 // Python < 3.11
565+
if (Py_VerboseFlag) {
566+
#else
567+
PyObject* verbose_flag = PySys_GetObject("verbose");
568+
if (verbose_flag && PyLong_AsLong(verbose_flag) > 0) {
569+
#endif
560570
PySys_WriteStderr(
561571
"# can't create %s\n", QStringToPythonConstCharPointer(filename));
572+
}
562573
return;
563574
}
564575
PyMarshal_WriteLongToFile(PyImport_GetMagicNumber(), fp, Py_MARSHAL_VERSION);
@@ -567,8 +578,14 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
567578
PyMarshal_WriteLongToFile(sourceSize, fp, Py_MARSHAL_VERSION);
568579
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
569580
if (ferror(fp)) {
570-
if (Py_VerboseFlag)
581+
#if PY_VERSION_HEX < 0x030B0000 // Python < 3.11
582+
if (Py_VerboseFlag) {
583+
#else
584+
PyObject* verbose_flag = PySys_GetObject("verbose");
585+
if (verbose_flag && PyLong_AsLong(verbose_flag) > 0) {
586+
#endif
571587
PySys_WriteStderr("# can't write %s\n", QStringToPythonConstCharPointer(filename));
588+
}
572589
/* Don't keep partial file */
573590
fclose(fp);
574591
QFile::remove(filename);
@@ -579,7 +596,12 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
579596
PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
580597
fflush(fp);
581598
fclose(fp);
582-
if (Py_VerboseFlag) {
599+
#if PY_VERSION_HEX < 0x030B0000 // Python < 3.11
600+
if (Py_VerboseFlag) {
601+
#else
602+
PyObject* verbose_flag = PySys_GetObject("verbose");
603+
if (verbose_flag && PyLong_AsLong(verbose_flag) > 0) {
604+
#endif
583605
PySys_WriteStderr("# wrote %s\n", QStringToPythonConstCharPointer(filename));
584606
}
585607
}
@@ -605,9 +627,15 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_
605627
}
606628

607629
if (getLong((unsigned char *)buf) != PyImport_GetMagicNumber()) {
608-
if (Py_VerboseFlag)
630+
#if PY_VERSION_HEX < 0x030B0000 // Python < 3.11
631+
if (Py_VerboseFlag) {
632+
#else
633+
PyObject* verbose_flag = PySys_GetObject("verbose");
634+
if (verbose_flag && PyLong_AsLong(verbose_flag) > 0) {
635+
#endif
609636
PySys_WriteStderr("# %s has bad magic\n",
610-
QStringToPythonConstCharPointer(path));
637+
QStringToPythonConstCharPointer(path));
638+
}
611639
Py_INCREF(Py_None);
612640
return Py_None;
613641
}
@@ -616,9 +644,15 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_
616644
time_t timeDiff = getLong((unsigned char *)buf + 4) - mtime;
617645
if (timeDiff<0) { timeDiff = -timeDiff; }
618646
if (timeDiff > 1) {
619-
if (Py_VerboseFlag)
647+
#if PY_VERSION_HEX < 0x030B0000 // Python < 3.11
648+
if (Py_VerboseFlag) {
649+
#else
650+
PyObject* verbose_flag = PySys_GetObject("verbose");
651+
if (verbose_flag && PyLong_AsLong(verbose_flag) > 0) {
652+
#endif
620653
PySys_WriteStderr("# %s has bad mtime\n",
621-
QStringToPythonConstCharPointer(path));
654+
QStringToPythonConstCharPointer(path));
655+
}
622656
Py_INCREF(Py_None);
623657
return Py_None;
624658
}
@@ -755,9 +789,15 @@ PythonQtImport::getModuleCode(PythonQtImporter *self, const char* fullname, QStr
755789
PyObject *code = nullptr;
756790
test = path + zso->suffix;
757791

758-
if (Py_VerboseFlag > 1)
792+
#if PY_VERSION_HEX < 0x030B0000 // Python < 3.11
793+
if (Py_VerboseFlag > 1) {
794+
#else
795+
PyObject* verbose_flag = PySys_GetObject("verbose");
796+
if (verbose_flag && PyLong_AsLong(verbose_flag) > 1) {
797+
#endif
759798
PySys_WriteStderr("# trying %s\n",
760799
QStringToPythonConstCharPointer(test));
800+
}
761801
if (PythonQt::importInterface()->exists(test)) {
762802
time_t mtime = 0;
763803
int ispackage = zso->type & IS_PACKAGE;
@@ -864,7 +904,24 @@ void PythonQtImport::init()
864904
mlab_searchorder[0].suffix[0] = SEP;
865905
mlab_searchorder[1].suffix[0] = SEP;
866906
mlab_searchorder[2].suffix[0] = SEP;
867-
if (Py_OptimizeFlag) {
907+
#if PY_VERSION_HEX < 0x030B0000 // Python < 3.11
908+
if (Py_OptimizeFlag) {
909+
#else
910+
const bool do_optimize = []() -> bool
911+
{
912+
PyObject* flags = PySys_GetObject("flags");
913+
if (flags) {
914+
PyObject* optimize = PyObject_GetAttrString(flags, "optimize");
915+
if (optimize && PyLong_AsLong(optimize) > 0)
916+
{
917+
return true;
918+
}
919+
Py_XDECREF(optimize);
920+
}
921+
return false;
922+
} ();
923+
if (do_optimize) {
924+
#endif
868925
/* Reverse *.pyc and *.pyo */
869926
struct st_mlab_searchorder tmp;
870927
tmp = mlab_searchorder[0];

0 commit comments

Comments
 (0)