Skip to content

Commit 95f99ff

Browse files
jcfrmrbean-bremen
authored andcommitted
chore(importer): add helpers for runtime flags
Introduce getSysVerbose() and getSysOptimizationLevel() to read verbosity/optimization in a version-safe way: On Python ≥ 3.12: query sys.flags.verbose / sys.flags.optimize. On older Pythons: fall back to Py_VerboseFlag / Py_OptimizeFlag.
1 parent c875317 commit 95f99ff

File tree

1 file changed

+29
-40
lines changed

1 file changed

+29
-40
lines changed

src/PythonQtImporter.cpp

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ PyObject *PythonQtImportError;
8282

8383
namespace
8484
{
85+
86+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
8587
int getSysFlag(const char* flag_name)
8688
{
8789
PyObject* flags = PySys_GetObject("flags");
@@ -97,6 +99,25 @@ int getSysFlag(const char* flag_name)
9799
if (PyErr_Occurred()) { PyErr_Clear(); return 0; };
98100
return flag_value;
99101
}
102+
#endif
103+
104+
int getSysVerbose()
105+
{
106+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
107+
return getSysFlag("verbose");
108+
#else
109+
return Py_VerboseFlag;
110+
#endif
111+
}
112+
113+
int getSysOptimizationLevel()
114+
{
115+
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
116+
return getSysFlag("optimize");
117+
#else
118+
return Py_OptimizeFlag;
119+
#endif
120+
}
100121

101122
}
102123

@@ -328,11 +349,7 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
328349
}
329350

330351
Py_DECREF(code);
331-
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
332-
if (getSysFlag("verbose")) {
333-
#else
334-
if (Py_VerboseFlag) {
335-
#endif
352+
if (getSysVerbose()) {
336353
PySys_WriteStderr("import %s # loaded from %s\n",
337354
fullname, QStringToPythonConstCharPointer(fullPath));
338355
}
@@ -579,11 +596,7 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
579596
}
580597
fp = open_exclusive(filename);
581598
if (fp == nullptr) {
582-
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
583-
if (getSysFlag("verbose")) {
584-
#else
585-
if (Py_VerboseFlag) {
586-
#endif
599+
if (getSysVerbose()) {
587600
PySys_WriteStderr(
588601
"# can't create %s\n", QStringToPythonConstCharPointer(filename));
589602
}
@@ -595,11 +608,7 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
595608
PyMarshal_WriteLongToFile(sourceSize, fp, Py_MARSHAL_VERSION);
596609
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
597610
if (ferror(fp)) {
598-
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
599-
if (getSysFlag("verbose")) {
600-
#else
601-
if (Py_VerboseFlag) {
602-
#endif
611+
if (getSysVerbose()) {
603612
PySys_WriteStderr("# can't write %s\n", QStringToPythonConstCharPointer(filename));
604613
}
605614
/* Don't keep partial file */
@@ -612,11 +621,7 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
612621
PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
613622
fflush(fp);
614623
fclose(fp);
615-
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
616-
if (getSysFlag("verbose")) {
617-
#else
618-
if (Py_VerboseFlag) {
619-
#endif
624+
if (getSysVerbose()) {
620625
PySys_WriteStderr("# wrote %s\n", QStringToPythonConstCharPointer(filename));
621626
}
622627
}
@@ -641,11 +646,7 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_
641646
}
642647

643648
if (getLong((unsigned char *)buf) != PyImport_GetMagicNumber()) {
644-
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
645-
if (getSysFlag("verbose")) {
646-
#else
647-
if (Py_VerboseFlag) {
648-
#endif
649+
if (getSysVerbose()) {
649650
PySys_WriteStderr("# %s has bad magic\n",
650651
QStringToPythonConstCharPointer(path));
651652
}
@@ -656,11 +657,7 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_
656657
time_t timeDiff = getLong((unsigned char *)buf + 4) - mtime;
657658
if (timeDiff<0) { timeDiff = -timeDiff; }
658659
if (timeDiff > 1) {
659-
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
660-
if (getSysFlag("verbose")) {
661-
#else
662-
if (Py_VerboseFlag) {
663-
#endif
660+
if (getSysVerbose()) {
664661
PySys_WriteStderr("# %s has bad mtime\n",
665662
QStringToPythonConstCharPointer(path));
666663
}
@@ -799,11 +796,7 @@ PythonQtImport::getModuleCode(PythonQtImporter *self, const char* fullname, QStr
799796
PyObject *code = nullptr;
800797
test = path + zso->suffix;
801798

802-
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
803-
if (getSysFlag("verbose") > 1) {
804-
#else
805-
if (Py_VerboseFlag > 1) {
806-
#endif
799+
if (getSysVerbose() > 1) {
807800
PySys_WriteStderr("# trying %s\n",
808801
QStringToPythonConstCharPointer(test));
809802
}
@@ -913,11 +906,7 @@ void PythonQtImport::init()
913906
mlab_searchorder[0].suffix[0] = SEP;
914907
mlab_searchorder[1].suffix[0] = SEP;
915908
mlab_searchorder[2].suffix[0] = SEP;
916-
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
917-
if (getSysFlag("optimize")) {
918-
#else
919-
if (Py_OptimizeFlag) {
920-
#endif
909+
if (getSysOptimizationLevel()) {
921910
/* Reverse *.pyc and *.pyo */
922911
struct st_mlab_searchorder tmp;
923912
tmp = mlab_searchorder[0];

0 commit comments

Comments
 (0)