Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions src/PythonQtImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,47 @@ struct st_mlab_searchorder {
extern PyTypeObject PythonQtImporter_Type;
PyObject *PythonQtImportError;

namespace
{

#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
int getSysFlag(const char* flag_name)
{
PyObject* flags = PySys_GetObject("flags");
if (!flags) {
return false;
}
PyObject* flag = PyObject_GetAttrString(flags, flag_name);
if (!flag) {
PyErr_Clear(); return false;
}
int flag_value = (int)PyLong_AsLong(flag);
Py_DECREF(flag);
if (PyErr_Occurred()) { PyErr_Clear(); return 0; };
return flag_value;
}
#endif

int getSysVerbose()
{
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
return getSysFlag("verbose");
#else
return Py_VerboseFlag;
#endif
}

int getSysOptimizationLevel()
{
#if PY_VERSION_HEX >= 0x030C0000 // Python >= 3.12
return getSysFlag("optimize");
#else
return Py_OptimizeFlag;
#endif
}

}

QString PythonQtImport::getSubName(const QString& str)
{
int idx = str.lastIndexOf('.');
Expand Down Expand Up @@ -308,7 +349,7 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
}

Py_DECREF(code);
if (Py_VerboseFlag) {
if (getSysVerbose()) {
PySys_WriteStderr("import %s # loaded from %s\n",
fullname, QStringToPythonConstCharPointer(fullPath));
}
Expand Down Expand Up @@ -555,9 +596,10 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
}
fp = open_exclusive(filename);
if (fp == nullptr) {
if (Py_VerboseFlag)
if (getSysVerbose()) {
PySys_WriteStderr(
"# can't create %s\n", QStringToPythonConstCharPointer(filename));
}
return;
}
PyMarshal_WriteLongToFile(PyImport_GetMagicNumber(), fp, Py_MARSHAL_VERSION);
Expand All @@ -566,8 +608,9 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
PyMarshal_WriteLongToFile(sourceSize, fp, Py_MARSHAL_VERSION);
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
if (ferror(fp)) {
if (Py_VerboseFlag)
if (getSysVerbose()) {
PySys_WriteStderr("# can't write %s\n", QStringToPythonConstCharPointer(filename));
}
/* Don't keep partial file */
fclose(fp);
QFile::remove(filename);
Expand All @@ -578,7 +621,7 @@ void PythonQtImport::writeCompiledModule(PyCodeObject *co, const QString& filena
PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
fflush(fp);
fclose(fp);
if (Py_VerboseFlag) {
if (getSysVerbose()) {
PySys_WriteStderr("# wrote %s\n", QStringToPythonConstCharPointer(filename));
}
}
Expand All @@ -603,19 +646,21 @@ PythonQtImport::unmarshalCode(const QString& path, const QByteArray& data, time_
}

if (getLong((unsigned char *)buf) != PyImport_GetMagicNumber()) {
if (Py_VerboseFlag)
if (getSysVerbose()) {
PySys_WriteStderr("# %s has bad magic\n",
QStringToPythonConstCharPointer(path));
}
Py_RETURN_NONE;
}

if (mtime != 0) {
time_t timeDiff = getLong((unsigned char *)buf + 4) - mtime;
if (timeDiff<0) { timeDiff = -timeDiff; }
if (timeDiff > 1) {
if (Py_VerboseFlag)
if (getSysVerbose()) {
PySys_WriteStderr("# %s has bad mtime\n",
QStringToPythonConstCharPointer(path));
}
Py_RETURN_NONE;
}
}
Expand Down Expand Up @@ -751,9 +796,10 @@ PythonQtImport::getModuleCode(PythonQtImporter *self, const char* fullname, QStr
PyObject *code = nullptr;
test = path + zso->suffix;

if (Py_VerboseFlag > 1)
if (getSysVerbose() > 1) {
PySys_WriteStderr("# trying %s\n",
QStringToPythonConstCharPointer(test));
}
if (PythonQt::importInterface()->exists(test)) {
time_t mtime = 0;
int ispackage = zso->type & IS_PACKAGE;
Expand Down Expand Up @@ -860,7 +906,7 @@ void PythonQtImport::init()
mlab_searchorder[0].suffix[0] = SEP;
mlab_searchorder[1].suffix[0] = SEP;
mlab_searchorder[2].suffix[0] = SEP;
if (Py_OptimizeFlag) {
if (getSysOptimizationLevel()) {
/* Reverse *.pyc and *.pyo */
struct st_mlab_searchorder tmp;
tmp = mlab_searchorder[0];
Expand Down
Loading