Skip to content

Commit

Permalink
fixed ref counting of types
Browse files Browse the repository at this point in the history
ignore eggs in importer
  • Loading branch information
florianlink committed Feb 26, 2018
1 parent 488d8c4 commit 5e0d26c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
16 changes: 10 additions & 6 deletions src/PythonQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)

_self->priv()->pythonQtModule().addObject("Debug", _self->priv()->_debugAPI);

Py_INCREF((PyObject*)&PythonQtSlotDecorator_Type);
Py_INCREF((PyObject*)&PythonQtSignalFunction_Type);
Py_INCREF((PyObject*)&PythonQtProperty_Type);
PyModule_AddObject(pack, "Slot", (PyObject*)&PythonQtSlotDecorator_Type);
PyModule_AddObject(pack, "Signal", (PyObject*)&PythonQtSignalFunction_Type);
PyModule_AddObject(pack, "Property", (PyObject*)&PythonQtProperty_Type);
Expand Down Expand Up @@ -695,6 +698,7 @@ PythonQtClassWrapper* PythonQtPrivate::createNewPythonQtClassWrapper(PythonQtCla
PyObject* className = PyString_FromString(pythonClassName.constData());

PyObject* baseClasses = PyTuple_New(1);
Py_INCREF((PyObject*)&PythonQtInstanceWrapper_Type);
PyTuple_SET_ITEM(baseClasses, 0, (PyObject*)&PythonQtInstanceWrapper_Type);

PyObject* typeDict = PyDict_New();
Expand Down Expand Up @@ -1677,11 +1681,13 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
#ifdef PY3K
PythonQtModuleDef.m_name = name.constData();
_p->_pythonQtModule = PyModule_Create(&PythonQtModuleDef);
_PyImport_FixupBuiltin(_p->_pythonQtModule, name);
#else
_p->_pythonQtModule = Py_InitModule(name.constData(), PythonQtMethods);
#endif
_p->_pythonQtModuleName = name;


Py_INCREF((PyObject*)&PythonQtBoolResult_Type);
PyModule_AddObject(_p->pythonQtModule().object(), "BoolResult", (PyObject*)&PythonQtBoolResult_Type);
PythonQtObjectPtr sys;
sys.setNewRef(PyImport_ImportModule("sys"));
Expand All @@ -1705,16 +1711,14 @@ void PythonQt::initPythonQtModule(bool redirectStdOut, const QByteArray& pythonQ
Py_ssize_t old_size = PyTuple_Size(old_module_names);
PyObject *module_names = PyTuple_New(old_size + 1);
for (Py_ssize_t i = 0; i < old_size; i++) {
PyTuple_SetItem(module_names, i, PyTuple_GetItem(old_module_names, i));
PyObject* val = PyTuple_GetItem(old_module_names, i);
Py_INCREF(val);
PyTuple_SetItem(module_names, i, val);
}
PyTuple_SetItem(module_names, old_size, PyString_FromString(name.constData()));
PyModule_AddObject(sys.object(), "builtin_module_names", module_names);
}
Py_XDECREF(old_module_names);

#ifdef PY3K
PyDict_SetItem(PyObject_GetAttrString(sys.object(), "modules"), PyUnicode_FromString(name.constData()), _p->_pythonQtModule.object());
#endif
}

QString PythonQt::getReturnTypeOfWrappedMethod(PyObject* module, const QString& name)
Expand Down
2 changes: 1 addition & 1 deletion src/PythonQtConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
// since Qt uses QByteArray in many places for identifier strings,
// we need to allow implicit conversion from unicode as well.
// We allow that for both Python 2.x and 3.x to be compatible.
bytes = PyObjGetString(obj, strict, ok).toUtf8();
bytes = PyObjGetString(obj, true, ok).toUtf8();
}
if (ok) {
PythonQtValueStorage_ADD_VALUE_IF_NEEDED(alreadyAllocatedCPPObject,global_variantStorage, QVariant, QVariant(bytes), ptr);
Expand Down
4 changes: 4 additions & 0 deletions src/PythonQtImportFileInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class PythonQtImportFileInterface {
//! returns if the file exists
virtual bool exists(const QString& filename) = 0;

//! returns true if the given file is an egg archive (e.g. zip). If the egg is a directory
//! then false is returned.
virtual bool isEggArchive(const QString& filename) = 0;

//! get the last modified data of a file
virtual QDateTime lastModifiedDate(const QString& filename) = 0;

Expand Down
24 changes: 15 additions & 9 deletions src/PythonQtImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,23 @@ int PythonQtImporter_init(PythonQtImporter *self, PyObject *args, PyObject * /*k

QString path(cpath);
if (PythonQt::importInterface()->exists(path)) {
const QStringList& ignorePaths = PythonQt::self()->getImporterIgnorePaths();
Q_FOREACH(QString ignorePath, ignorePaths) {
if (path.startsWith(ignorePath)) {
PyErr_SetString(PythonQtImportError,
"path ignored");
return -1;
if (PythonQt::importInterface()->isEggArchive(path)) {
PyErr_SetString(PythonQtImportError,
"path is an egg archive, which is unsupported by PythonQt");
return -1;
} else {
const QStringList& ignorePaths = PythonQt::self()->getImporterIgnorePaths();
Q_FOREACH(QString ignorePath, ignorePaths) {
if (path.startsWith(ignorePath)) {
PyErr_SetString(PythonQtImportError,
"path ignored");
return -1;
}
}
}

self->_path = new QString(path);
return 0;
self->_path = new QString(path);
return 0;
}
} else {
PyErr_SetString(PythonQtImportError,
"path does not exist error");
Expand Down
4 changes: 2 additions & 2 deletions src/PythonQtInstanceWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,10 @@ static int PythonQtInstanceWrapper_builtin_nonzero(PyObject *obj)
static long PythonQtInstanceWrapper_hash(PythonQtInstanceWrapper *obj)
{
if (obj->_wrappedPtr != NULL) {
return reinterpret_cast<long>(obj->_wrappedPtr);
return static_cast<long>(reinterpret_cast<size_t>(obj->_wrappedPtr));
} else {
QObject* qobj = obj->_obj; // get pointer from QPointer wrapper
return reinterpret_cast<long>(qobj);
return static_cast<long>(reinterpret_cast<size_t>(qobj));
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/PythonQtQFileImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ bool PythonQtQFileImporter::exists (const QString &filename) {
return QFile::exists(filename);
}

bool PythonQtQFileImporter::isEggArchive(const QString& filename) {
return filename.toLower().endsWith(".egg") && !QFileInfo(filename).isDir();
}

QDateTime PythonQtQFileImporter::lastModifiedDate (const QString &filename) {
QFileInfo fi(filename);
return fi.lastModified();
Expand Down
1 change: 1 addition & 0 deletions src/PythonQtQFileImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class PythonQtQFileImporter : public PythonQtImportFileInterface {
QByteArray readSourceFile (const QString &filename, bool &ok);

bool exists (const QString &filename);
bool isEggArchive(const QString& filename);

QDateTime lastModifiedDate (const QString &filename);

Expand Down

0 comments on commit 5e0d26c

Please sign in to comment.