Skip to content

Commit 97ad9d2

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 97ad9d2

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

src/PythonQtImporter.cpp

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

310310
Py_DECREF(code);
311+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
312+
PyConfig config;
313+
PyConfig_Read(&config);
314+
if (config.verbose) {
315+
#else
311316
if (Py_VerboseFlag) {
317+
#endif
312318
PySys_WriteStderr("import %s # loaded from %s\n",
313319
fullname, QStringToPythonConstCharPointer(fullPath));
314320
}
@@ -555,19 +561,35 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
555561
}
556562
fp = open_exclusive(filename);
557563
if (fp == nullptr) {
558-
if (Py_VerboseFlag)
564+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
565+
PyConfig config;
566+
PyConfig_Read(&config);
567+
if (config.verbose) {
568+
#else
569+
if (Py_VerboseFlag) {
570+
#endif
559571
PySys_WriteStderr(
560572
"# can't create %s\n", QStringToPythonConstCharPointer(filename));
573+
}
561574
return;
562575
}
563576
PyMarshal_WriteLongToFile(PyImport_GetMagicNumber(), fp, Py_MARSHAL_VERSION);
564577
/* First write a 0 for mtime */
565578
PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
566579
PyMarshal_WriteLongToFile(sourceSize, fp, Py_MARSHAL_VERSION);
567580
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
581+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
582+
PyConfig config;
583+
PyConfig_Read(&config);
584+
#endif
568585
if (ferror(fp)) {
569-
if (Py_VerboseFlag)
586+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
587+
if (config.verbose) {
588+
#else
589+
if (Py_VerboseFlag) {
590+
#endif
570591
PySys_WriteStderr("# can't write %s\n", QStringToPythonConstCharPointer(filename));
592+
}
571593
/* Don't keep partial file */
572594
fclose(fp);
573595
QFile::remove(filename);
@@ -578,7 +600,11 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
578600
PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
579601
fflush(fp);
580602
fclose(fp);
603+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
604+
if (config.verbose) {
605+
#else
581606
if (Py_VerboseFlag) {
607+
#endif
582608
PySys_WriteStderr("# wrote %s\n", QStringToPythonConstCharPointer(filename));
583609
}
584610
}
@@ -603,19 +629,33 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_
603629
}
604630

605631
if (getLong((unsigned char *)buf) != PyImport_GetMagicNumber()) {
606-
if (Py_VerboseFlag)
632+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
633+
PyConfig config;
634+
PyConfig_Read(&config);
635+
if (config.verbose) {
636+
#else
637+
if (Py_VerboseFlag) {
638+
#endif
607639
PySys_WriteStderr("# %s has bad magic\n",
608640
QStringToPythonConstCharPointer(path));
641+
}
609642
Py_RETURN_NONE;
610643
}
611644

612645
if (mtime != 0) {
613646
time_t timeDiff = getLong((unsigned char *)buf + 4) - mtime;
614647
if (timeDiff<0) { timeDiff = -timeDiff; }
615648
if (timeDiff > 1) {
616-
if (Py_VerboseFlag)
649+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
650+
PyConfig config;
651+
PyConfig_Read(&config);
652+
if (config.verbose) {
653+
#else
654+
if (Py_VerboseFlag) {
655+
#endif
617656
PySys_WriteStderr("# %s has bad mtime\n",
618657
QStringToPythonConstCharPointer(path));
658+
}
619659
Py_RETURN_NONE;
620660
}
621661
}
@@ -751,9 +791,16 @@ PythonQtImport::getModuleCode(PythonQtImporter *self, const char* fullname, QStr
751791
PyObject *code = nullptr;
752792
test = path + zso->suffix;
753793

754-
if (Py_VerboseFlag > 1)
794+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
795+
PyConfig config;
796+
PyConfig_Read(&config);
797+
if (config.verbose > 1) {
798+
#else
799+
if (Py_VerboseFlag > 1) {
800+
#endif
755801
PySys_WriteStderr("# trying %s\n",
756802
QStringToPythonConstCharPointer(test));
803+
}
757804
if (PythonQt::importInterface()->exists(test)) {
758805
time_t mtime = 0;
759806
int ispackage = zso->type & IS_PACKAGE;
@@ -860,7 +907,13 @@ void PythonQtImport::init()
860907
mlab_searchorder[0].suffix[0] = SEP;
861908
mlab_searchorder[1].suffix[0] = SEP;
862909
mlab_searchorder[2].suffix[0] = SEP;
910+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
911+
PyConfig config;
912+
PyConfig_Read(&config);
913+
if (config.optimization_level) {
914+
#else
863915
if (Py_OptimizeFlag) {
916+
#endif
864917
/* Reverse *.pyc and *.pyo */
865918
struct st_mlab_searchorder tmp;
866919
tmp = mlab_searchorder[0];

0 commit comments

Comments
 (0)