Skip to content

Commit 5f6df0c

Browse files
hjmjohnsonjcfr
andcommitted
chore: use PyConfig.verbose/optimization_level on Python >= 3.12; fallback to legacy flags
Python 3.12 deprecates access to global flags like `Py_VerboseFlag` and `Py_OptimizeFlag` in favor of per-interpreter config. Detect 3.12+ with `PY_VERSION_HEX` and read `PyConfig.verbose` / `PyConfig.optimization_level`, otherwise fall back to the legacy globals. No functional change intended. Co-authored-by: Jean-Christophe Fillion-Robin <[email protected]>
1 parent 46a2fe9 commit 5f6df0c

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/PythonQtImporter.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,11 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
308308
}
309309

310310
Py_DECREF(code);
311+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
312+
if (PyConfig.verbose) {
313+
#else
311314
if (Py_VerboseFlag) {
315+
#endif
312316
PySys_WriteStderr("import %s # loaded from %s\n",
313317
fullname, QStringToPythonConstCharPointer(fullPath));
314318
}
@@ -555,9 +559,14 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
555559
}
556560
fp = open_exclusive(filename);
557561
if (fp == nullptr) {
558-
if (Py_VerboseFlag)
562+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
563+
if (PyConfig.verbose) {
564+
#else
565+
if (Py_VerboseFlag) {
566+
#endif
559567
PySys_WriteStderr(
560568
"# can't create %s\n", QStringToPythonConstCharPointer(filename));
569+
}
561570
return;
562571
}
563572
PyMarshal_WriteLongToFile(PyImport_GetMagicNumber(), fp, Py_MARSHAL_VERSION);
@@ -566,8 +575,13 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
566575
PyMarshal_WriteLongToFile(sourceSize, fp, Py_MARSHAL_VERSION);
567576
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
568577
if (ferror(fp)) {
569-
if (Py_VerboseFlag)
578+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
579+
if (PyConfig.verbose) {
580+
#else
581+
if (Py_VerboseFlag) {
582+
#endif
570583
PySys_WriteStderr("# can't write %s\n", QStringToPythonConstCharPointer(filename));
584+
}
571585
/* Don't keep partial file */
572586
fclose(fp);
573587
QFile::remove(filename);
@@ -578,7 +592,11 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
578592
PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
579593
fflush(fp);
580594
fclose(fp);
595+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
596+
if (PyConfig.verbose) {
597+
#else
581598
if (Py_VerboseFlag) {
599+
#endif
582600
PySys_WriteStderr("# wrote %s\n", QStringToPythonConstCharPointer(filename));
583601
}
584602
}
@@ -613,9 +631,14 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_
613631
time_t timeDiff = getLong((unsigned char *)buf + 4) - mtime;
614632
if (timeDiff<0) { timeDiff = -timeDiff; }
615633
if (timeDiff > 1) {
616-
if (Py_VerboseFlag)
634+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
635+
if (PyConfig.verbose) {
636+
#else
637+
if (Py_VerboseFlag) {
638+
#endif
617639
PySys_WriteStderr("# %s has bad mtime\n",
618640
QStringToPythonConstCharPointer(path));
641+
}
619642
Py_RETURN_NONE;
620643
}
621644
}
@@ -751,9 +774,14 @@ PythonQtImport::getModuleCode(PythonQtImporter *self, const char* fullname, QStr
751774
PyObject *code = nullptr;
752775
test = path + zso->suffix;
753776

754-
if (Py_VerboseFlag > 1)
777+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
778+
if (PyConfig.verbose > 1) {
779+
#else
780+
if (Py_VerboseFlag > 1) {
781+
#endif
755782
PySys_WriteStderr("# trying %s\n",
756783
QStringToPythonConstCharPointer(test));
784+
}
757785
if (PythonQt::importInterface()->exists(test)) {
758786
time_t mtime = 0;
759787
int ispackage = zso->type & IS_PACKAGE;
@@ -860,7 +888,11 @@ void PythonQtImport::init()
860888
mlab_searchorder[0].suffix[0] = SEP;
861889
mlab_searchorder[1].suffix[0] = SEP;
862890
mlab_searchorder[2].suffix[0] = SEP;
891+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
892+
if (PyConfig.optimization_level) {
893+
#else
863894
if (Py_OptimizeFlag) {
895+
#endif
864896
/* Reverse *.pyc and *.pyo */
865897
struct st_mlab_searchorder tmp;
866898
tmp = mlab_searchorder[0];

0 commit comments

Comments
 (0)