Skip to content

Commit c875317

Browse files
hjmjohnsonjcfr
authored andcommitted
chore(importer): read sys.flags (verbose/optimize) on Python >= 3.12; fallback to legacy globals
Python 3.12 deprecates direct use of global flags (`Py_VerboseFlag`, `Py_OptimizeFlag`). Query `sys.flags.verbose` / `sys.flags.optimize` at runtime (via a small helper) when building against 3.12+, and fall back to the legacy globals on older versions. No functional change intended. Co-authored-by: Jean-Christophe Fillion-Robin <jchris.fillionr@kitware.com>
1 parent 20abb24 commit c875317

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

src/PythonQtImporter.cpp

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ struct st_mlab_searchorder {
8080
extern PyTypeObject PythonQtImporter_Type;
8181
PyObject *PythonQtImportError;
8282

83+
namespace
84+
{
85+
int getSysFlag(const char* flag_name)
86+
{
87+
PyObject* flags = PySys_GetObject("flags");
88+
if (!flags) {
89+
return false;
90+
}
91+
PyObject* flag = PyObject_GetAttrString(flags, flag_name);
92+
if (!flag) {
93+
PyErr_Clear(); return false;
94+
}
95+
int flag_value = (int)PyLong_AsLong(flag);
96+
Py_DECREF(flag);
97+
if (PyErr_Occurred()) { PyErr_Clear(); return 0; };
98+
return flag_value;
99+
}
100+
101+
}
102+
83103
QString PythonQtImport::getSubName(const QString& str)
84104
{
85105
int idx = str.lastIndexOf('.');
@@ -308,7 +328,11 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
308328
}
309329

310330
Py_DECREF(code);
331+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
332+
if (getSysFlag("verbose")) {
333+
#else
311334
if (Py_VerboseFlag) {
335+
#endif
312336
PySys_WriteStderr("import %s # loaded from %s\n",
313337
fullname, QStringToPythonConstCharPointer(fullPath));
314338
}
@@ -555,9 +579,14 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
555579
}
556580
fp = open_exclusive(filename);
557581
if (fp == nullptr) {
558-
if (Py_VerboseFlag)
582+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
583+
if (getSysFlag("verbose")) {
584+
#else
585+
if (Py_VerboseFlag) {
586+
#endif
559587
PySys_WriteStderr(
560588
"# can't create %s\n", QStringToPythonConstCharPointer(filename));
589+
}
561590
return;
562591
}
563592
PyMarshal_WriteLongToFile(PyImport_GetMagicNumber(), fp, Py_MARSHAL_VERSION);
@@ -566,8 +595,13 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
566595
PyMarshal_WriteLongToFile(sourceSize, fp, Py_MARSHAL_VERSION);
567596
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
568597
if (ferror(fp)) {
569-
if (Py_VerboseFlag)
598+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
599+
if (getSysFlag("verbose")) {
600+
#else
601+
if (Py_VerboseFlag) {
602+
#endif
570603
PySys_WriteStderr("# can't write %s\n", QStringToPythonConstCharPointer(filename));
604+
}
571605
/* Don't keep partial file */
572606
fclose(fp);
573607
QFile::remove(filename);
@@ -578,7 +612,11 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
578612
PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
579613
fflush(fp);
580614
fclose(fp);
615+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
616+
if (getSysFlag("verbose")) {
617+
#else
581618
if (Py_VerboseFlag) {
619+
#endif
582620
PySys_WriteStderr("# wrote %s\n", QStringToPythonConstCharPointer(filename));
583621
}
584622
}
@@ -603,19 +641,29 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_
603641
}
604642

605643
if (getLong((unsigned char *)buf) != PyImport_GetMagicNumber()) {
606-
if (Py_VerboseFlag)
644+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
645+
if (getSysFlag("verbose")) {
646+
#else
647+
if (Py_VerboseFlag) {
648+
#endif
607649
PySys_WriteStderr("# %s has bad magic\n",
608650
QStringToPythonConstCharPointer(path));
651+
}
609652
Py_RETURN_NONE;
610653
}
611654

612655
if (mtime != 0) {
613656
time_t timeDiff = getLong((unsigned char *)buf + 4) - mtime;
614657
if (timeDiff<0) { timeDiff = -timeDiff; }
615658
if (timeDiff > 1) {
616-
if (Py_VerboseFlag)
659+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
660+
if (getSysFlag("verbose")) {
661+
#else
662+
if (Py_VerboseFlag) {
663+
#endif
617664
PySys_WriteStderr("# %s has bad mtime\n",
618665
QStringToPythonConstCharPointer(path));
666+
}
619667
Py_RETURN_NONE;
620668
}
621669
}
@@ -751,9 +799,14 @@ PythonQtImport::getModuleCode(PythonQtImporter *self, const char* fullname, QStr
751799
PyObject *code = nullptr;
752800
test = path + zso->suffix;
753801

754-
if (Py_VerboseFlag > 1)
802+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
803+
if (getSysFlag("verbose") > 1) {
804+
#else
805+
if (Py_VerboseFlag > 1) {
806+
#endif
755807
PySys_WriteStderr("# trying %s\n",
756808
QStringToPythonConstCharPointer(test));
809+
}
757810
if (PythonQt::importInterface()->exists(test)) {
758811
time_t mtime = 0;
759812
int ispackage = zso->type & IS_PACKAGE;
@@ -860,7 +913,11 @@ void PythonQtImport::init()
860913
mlab_searchorder[0].suffix[0] = SEP;
861914
mlab_searchorder[1].suffix[0] = SEP;
862915
mlab_searchorder[2].suffix[0] = SEP;
916+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
917+
if (getSysFlag("optimize")) {
918+
#else
863919
if (Py_OptimizeFlag) {
920+
#endif
864921
/* Reverse *.pyc and *.pyo */
865922
struct st_mlab_searchorder tmp;
866923
tmp = mlab_searchorder[0];

0 commit comments

Comments
 (0)