From f65f3e118cc51e446e47443478766404589b2bdb 2009-03-30 07:48:19 From: florianlink Date: 2009-03-30 07:48:19 Subject: [PATCH] - removed warnings - fixed some memory leaks (only on cleanup) - fixed bad QString usage as %s argument - added PythonQtQFileImporter, which implements a default importer using QFile - added PythonQt::self()->installDefaultImporter() to enable the new default importer - modified example to make use of new importer - removed ugly goto, which was copied from original zip import git-svn-id: svn://svn.code.sf.net/p/pythonqt/code/trunk@43 ea8d5007-eb21-0410-b261-ccb3ea6e24a9 --- diff --git a/examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp b/examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp index 1f01134..1e3f78c 100644 --- a/examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp +++ b/examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp @@ -1,6 +1,5 @@ #include #include -#include "QFileImportInterface.h" int main (int argc, char* argv[]) { QApplication app(argc, argv); @@ -19,11 +18,10 @@ int main (int argc, char* argv[]) { Q_ASSERT(fn.toString() == QString("t.mp3")); // tag goes out of scope, reference count decremented. } - qDebug() << "test1"; -/* { // Allow the python system path to recognize QFile paths in the sys.path - QFileImportInterface qfii; + //QFileImportInterface qfii; + PythonQt::self()->setImporter(NULL); // append the Qt resource root directory to the sys.path mainModule.evalScript("sys.path.append(':')\n"); mainModule.evalScript("import eyed3tagger\n"); @@ -33,7 +31,6 @@ int main (int argc, char* argv[]) { QVariant fn = tag.call("fileName", QVariantList()); Q_ASSERT(fn.toString() == QString("t.mp3")); } - qDebug() << "test2"; */ { // alternative using import and loading it as a real module from sys.path // import sys first mainModule.evalScript(QString("sys.path.append('%1')\n").arg(QDir::currentPath())); diff --git a/examples/CPPPyWrapperExample/CPPPyWrapperExample.pro b/examples/CPPPyWrapperExample/CPPPyWrapperExample.pro index 626ac4b..1ab6d51 100644 --- a/examples/CPPPyWrapperExample/CPPPyWrapperExample.pro +++ b/examples/CPPPyWrapperExample/CPPPyWrapperExample.pro @@ -2,10 +2,10 @@ CONFIG += debug VPATH += INCLUDEPATH += . $$(PYTHONQT_ROOT)/src /usr/include/python2.5 -SOURCES += CPPPyWrapperExample.cpp QFileImportInterface.cpp -HEADERS += QFileImportInterface.h +SOURCES += CPPPyWrapperExample.cpp +mac { CONFIG -= app_bundle } -LIBS += -L$$(PYTHONQT_ROOT)/lib -lPythonQt -lutil +LIBS += -L$$(PYTHONQT_ROOT)/lib -lPythonQt_d -lutil RESOURCES += CPPPyWrapperExample.qrc diff --git a/examples/CPPPyWrapperExample/QFileImportInterface.cpp b/examples/CPPPyWrapperExample/QFileImportInterface.cpp deleted file mode 100644 index 707bea8..0000000 --- a/examples/CPPPyWrapperExample/QFileImportInterface.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include - -#include -#include "QFileImportInterface.h" - -QFileImportInterface::QFileImportInterface() { - m_oldInterface = PythonQt::importInterface(); - PythonQt::self()->setImporter(this); -} - -QFileImportInterface::~QFileImportInterface() { - PythonQt::self()->setImporter(m_oldInterface); -} - -QByteArray QFileImportInterface::readFileAsBytes (const QString &filename) { - qDebug() << "readFileAsBytes: " << filename; - QFile f(filename); - return f.readAll(); -} - -QByteArray QFileImportInterface::readSourceFile (const QString &filename, bool &ok) { - QFile f(filename); - if (!exists(filename)) { - ok = false; - return QByteArray(); - } - else { - ok = true; - return readFileAsBytes(filename); - } -} - -bool QFileImportInterface::exists (const QString &filename) { - QFile f(filename); - return f.exists(); -} - -QDateTime QFileImportInterface::lastModifiedDate (const QString &filename) { - QFileInfo fi(filename); - return fi.lastModified(); -} diff --git a/examples/CPPPyWrapperExample/QFileImportInterface.h b/examples/CPPPyWrapperExample/QFileImportInterface.h deleted file mode 100644 index 86aa56b..0000000 --- a/examples/CPPPyWrapperExample/QFileImportInterface.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef QFILEIMPORTINTERFACE_H -#define QFILEIMPORTINTERFACE_H - -#include - -/** Under Construction : This does not work yet. -*/ -class QFileImportInterface : public PythonQtImportFileInterface { -public: - QFileImportInterface(); - ~QFileImportInterface(); - QByteArray readFileAsBytes (const QString &filename); - QByteArray readSourceFile (const QString &filename, bool &ok); - bool exists (const QString &filename); - QDateTime lastModifiedDate (const QString &filename); -private: - PythonQtImportFileInterface *m_oldInterface; -}; - -#endif // #ifndef QFILEIMPORTINTERFACE_H diff --git a/src/PythonQt.cpp b/src/PythonQt.cpp index 2529576..ff6fe23 100644 --- a/src/PythonQt.cpp +++ b/src/PythonQt.cpp @@ -49,6 +49,7 @@ #include "PythonQtCppWrapperFactory.h" #include "PythonQtVariants.h" #include "PythonQtStdDecorators.h" +#include "PythonQtQFileImporter.h" #include PythonQt* PythonQt::_self = NULL; @@ -163,6 +164,14 @@ PythonQt::~PythonQt() { } PythonQtPrivate::~PythonQtPrivate() { + delete _defaultImporter; + _defaultImporter = NULL; + { + QHashIterator i(_knownQtDecoratorSlots); + while (i.hasNext()) { + delete i.next().value(); + } + } { QHashIterator i(_knownQtClasses); while (i.hasNext()) { @@ -184,13 +193,25 @@ PythonQtPrivate::~PythonQtPrivate() { { QHashIterator i(_constructorSlots); while (i.hasNext()) { - delete i.next().value(); + PythonQtSlotInfo* cur = i.next().value(); + while(cur->nextInfo()) { + PythonQtSlotInfo* next = cur->nextInfo(); + delete cur; + cur = next; + } + delete cur; } } { QHashIterator i(_destructorSlots); while (i.hasNext()) { - delete i.next().value(); + PythonQtSlotInfo* cur = i.next().value(); + while(cur->nextInfo()) { + PythonQtSlotInfo* next = cur->nextInfo(); + delete cur; + cur = next; + } + delete cur; } } PythonQtConv::global_valueStorage.clear(); @@ -204,7 +225,7 @@ PythonQtPrivate::~PythonQtPrivate() { PythonQtImportFileInterface* PythonQt::importInterface() { - return _self->_p->_importInterface; + return _self->_p->_importInterface?_self->_p->_importInterface:_self->_p->_defaultImporter; } void PythonQt::registerClass(const QMetaObject* metaobject) @@ -760,12 +781,8 @@ void PythonQt::registerQObjectClassNames(const QStringList& names) void PythonQt::setImporter(PythonQtImportFileInterface* importInterface) { - static bool first = true; - if (first) { - first = false; - _p->_importInterface = importInterface; - PythonQtImport::init(); - } + PythonQtImport::init(); + _p->_importInterface = importInterface; } void PythonQt::setImporterIgnorePaths(const QStringList& paths) @@ -797,6 +814,7 @@ const QList& PythonQt::constructorHandlers() PythonQtPrivate::PythonQtPrivate() { _importInterface = NULL; + _defaultImporter = new PythonQtQFileImporter; _noLongerWrappedCB = NULL; _wrappedCB = NULL; } diff --git a/src/PythonQt.h b/src/PythonQt.h index 3b7e549..42e58de 100644 --- a/src/PythonQt.h +++ b/src/PythonQt.h @@ -65,6 +65,7 @@ class PythonQtSignalReceiver; class PythonQtImportFileInterface; class PythonQtCppWrapperFactory; class PythonQtConstructorHandler; +class PythonQtQFileImporter; typedef void PythonQtQObjectWrappedCB(QObject* object); typedef void PythonQtQObjectNoLongerWrappedCB(QObject* object); @@ -278,9 +279,23 @@ public: //! replace the internal import implementation and use the supplied interface to load files (both py and pyc files) //! (this method should be called directly after initialization of init() and before calling overwriteSysPath(). - //! It can only be called once, further calls will be ignored silently. (ownership stays with caller) + //! On the first call to this method, it will install a generic PythonQt importer in Pythons "path_hooks". + //! This is not reversible, so even setting setImporter(NULL) afterwards will + //! keep the custom PythonQt importer with a QFile default import interface. + //! Subsequent python import calls will make use of the passed importInterface + //! which forwards all import calls to the given \c importInterface. + //! Passing NULL will install a default QFile importer. + //! (\c importInterface ownership stays with caller) void setImporter(PythonQtImportFileInterface* importInterface); + //! this installs the default QFile importer (which effectively does a setImporter(NULL)) + //! (without calling setImporter or installDefaultImporter at least once, the default python import + //! mechanism is in place) + //! the default importer allows to import files from anywhere QFile can read from, + //! including the Qt resource system using ":". Keep in mind that you need to extend + //! "sys.path" with ":" to be able to import from the Qt resources. + void installDefaultImporter() { setImporter(NULL); } + //! set paths that the importer should ignore void setImporterIgnorePaths(const QStringList& paths); @@ -442,6 +457,9 @@ private: //! the importer interface (if set) PythonQtImportFileInterface* _importInterface; + //! the default importer + PythonQtQFileImporter* _defaultImporter; + PythonQtQObjectNoLongerWrappedCB* _noLongerWrappedCB; PythonQtQObjectWrappedCB* _wrappedCB; diff --git a/src/PythonQtConversion.cpp b/src/PythonQtConversion.cpp index 0ef2271..a8be2ba 100644 --- a/src/PythonQtConversion.cpp +++ b/src/PythonQtConversion.cpp @@ -588,7 +588,7 @@ QString PythonQtConv::PyObjGetString(PyObject* val, bool strict, bool& ok) { return r; } -QByteArray PythonQtConv::PyObjGetBytes(PyObject* val, bool strict, bool& ok) { +QByteArray PythonQtConv::PyObjGetBytes(PyObject* val, bool /*strict*/, bool& ok) { QByteArray r; ok = true; if (val->ob_type == &PyString_Type) { @@ -980,7 +980,7 @@ PyObject* PythonQtConv::ConvertQListWithPointersToPython(QList* list, con return result; } -bool PythonQtConv::ConvertPythonListToQListOfType(PyObject* obj, QList* list, const QByteArray& type, bool strict) +bool PythonQtConv::ConvertPythonListToQListOfType(PyObject* obj, QList* list, const QByteArray& type, bool /*strict*/) { bool result = false; if (PySequence_Check(obj)) { diff --git a/src/PythonQtDoc.h b/src/PythonQtDoc.h index 48188ba..3b219da 100644 --- a/src/PythonQtDoc.h +++ b/src/PythonQtDoc.h @@ -51,7 +51,7 @@ \section Introduction - \b PythonQt is a dynamic Python (http://www.python.org) binding for Qt (http://www.trolltech.com). + \b PythonQt is a dynamic Python (http://www.python.org) binding for Qt (http://www.qtsoftware.com). It offers an easy way to embed the Python scripting language into your Qt applications. It makes heavy use of the QMetaObject system and thus requires Qt4.x. @@ -339,7 +339,7 @@ yourCpp = None \section Building - PythonQt requires at least Qt 4.2.2 (or higher) and Python 2.3, 2.4 or 2.5 on Windows, Linux and MacOS X. + PythonQt requires at least Qt 4.2.2 (or higher) and Python 2.3, 2.4, 2.5 or 2.6 on Windows, Linux and MacOS X. It has not yet been tested with Python 3.x, but it should only require minor changes. To compile PythonQt, you will need a python developer installation which includes Python's header files and the python2x.[lib | dll | so | dynlib]. The build scripts a currently set to use Python 2.5. @@ -459,8 +459,4 @@ the python2x.[lib | dll | so | dynlib]. \endcode - \section TODOs - - - add more information on how to distribute an application that uses PythonQt, including the Python distribution - */ diff --git a/src/PythonQtImportFileInterface.h b/src/PythonQtImportFileInterface.h index 53a22f3..8c26989 100644 --- a/src/PythonQtImportFileInterface.h +++ b/src/PythonQtImportFileInterface.h @@ -51,6 +51,9 @@ class PythonQtImportFileInterface { public: + // get rid of warnings + virtual ~PythonQtImportFileInterface() {} + //! read the given file as byte array, without doing any linefeed translations virtual QByteArray readFileAsBytes(const QString& filename) = 0; diff --git a/src/PythonQtImporter.cpp b/src/PythonQtImporter.cpp index f27b1cf..460faee 100644 --- a/src/PythonQtImporter.cpp +++ b/src/PythonQtImporter.cpp @@ -112,7 +112,7 @@ PythonQtImport::module_info PythonQtImport::getModuleInfo(PythonQtImporter* self /* PythonQtImporter.__init__ Just store the path argument */ -int PythonQtImporter_init(PythonQtImporter *self, PyObject *args, PyObject *kwds) +int PythonQtImporter_init(PythonQtImporter *self, PyObject *args, PyObject * /*kwds*/) { self->_path = NULL; @@ -205,8 +205,11 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args) } dict = PyModule_GetDict(mod); - if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) - goto error; + if (PyDict_SetItemString(dict, "__loader__", (PyObject *)self) != 0) { + Py_DECREF(code); + Py_DECREF(mod); + return NULL; + } if (ispackage) { PyObject *pkgpath, *fullpath; @@ -217,33 +220,38 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args) self->_path->toLatin1().constData(), SEP, subname.toLatin1().constData()); - if (fullpath == NULL) - goto error; + if (fullpath == NULL) { + Py_DECREF(code); + Py_DECREF(mod); + return NULL; + } pkgpath = Py_BuildValue("[O]", fullpath); Py_DECREF(fullpath); - if (pkgpath == NULL) - goto error; + if (pkgpath == NULL) { + Py_DECREF(code); + Py_DECREF(mod); + return NULL; + } err = PyDict_SetItemString(dict, "__path__", pkgpath); Py_DECREF(pkgpath); - if (err != 0) - goto error; + if (err != 0) { + Py_DECREF(code); + Py_DECREF(mod); + return NULL; + } } mod = PyImport_ExecCodeModuleEx(fullname, code, (char*)modpath.toLatin1().data()); Py_DECREF(code); if (Py_VerboseFlag) PySys_WriteStderr("import %s # loaded from %s\n", - fullname, modpath); + fullname, modpath.toLatin1().constData()); return mod; -error: - Py_DECREF(code); - Py_DECREF(mod); - return NULL; } PyObject * -PythonQtImporter_get_data(PyObject *obj, PyObject *args) +PythonQtImporter_get_data(PyObject* /*obj*/, PyObject* /*args*/) { // EXTRA, NOT YET IMPLEMENTED return NULL; @@ -263,7 +271,7 @@ PythonQtImporter_get_code(PyObject *obj, PyObject *args) } PyObject * -PythonQtImporter_get_source(PyObject *obj, PyObject *args) +PythonQtImporter_get_source(PyObject * /*obj*/, PyObject * /*args*/) { // EXTRA, NOT YET IMPLEMENTED /* @@ -353,7 +361,7 @@ PyMethodDef PythonQtImporter_methods[] = { doc_get_code}, {"get_source", PythonQtImporter_get_source, METH_VARARGS, doc_get_source}, - {NULL, NULL} /* sentinel */ + {NULL, NULL, 0 , NULL} /* sentinel */ }; @@ -579,24 +587,11 @@ PythonQtImport::compileSource(const QString& path, const QByteArray& data) /* Return the code object for the module named by 'fullname' from the Zip archive as a new reference. */ PyObject * -PythonQtImport::getCodeFromData(const QString& path, int isbytecode,int ispackage, time_t mtime) +PythonQtImport::getCodeFromData(const QString& path, int isbytecode,int /*ispackage*/, time_t mtime) { - bool hasImporter = PythonQt::importInterface()!=NULL; - PyObject *code; QByteArray qdata; - if (!hasImporter) { - QFile file(path); - QIODevice::OpenMode flags = QIODevice::ReadOnly; - if (!isbytecode) { - flags |= QIODevice::Text; - } - if (!file.open(flags)) { - return NULL; - } - qdata = file.readAll(); - } else { if (!isbytecode) { // mlabDebugConst("MLABPython", "reading source " << path); bool ok; @@ -611,7 +606,6 @@ PythonQtImport::getCodeFromData(const QString& path, int isbytecode,int ispackag } else { qdata = PythonQt::importInterface()->readFileAsBytes(path); } - } if (isbytecode) { // mlabDebugConst("MLABPython", "reading bytecode " << path); @@ -623,7 +617,7 @@ PythonQtImport::getCodeFromData(const QString& path, int isbytecode,int ispackag if (code) { // save a pyc file if possible QDateTime time; - time = hasImporter?PythonQt::importInterface()->lastModifiedDate(path):QFileInfo(path).lastModified(); + time = PythonQt::importInterface()->lastModifiedDate(path); writeCompiledModule((PyCodeObject*)code, path+"c", time.toTime_t()); } } @@ -637,16 +631,10 @@ PythonQtImport::getMTimeOfSource(const QString& path) QString path2 = path; path2.truncate(path.length()-1); - bool hasImporter = PythonQt::importInterface()!=NULL; - if (hasImporter) { - if (PythonQt::importInterface()->exists(path2)) { - mtime = PythonQt::importInterface()->lastModifiedDate(path2).toTime_t(); - } - } else { - if (QFile::exists(path2)) { - mtime = QFileInfo(path2).lastModified().toTime_t(); - } + if (PythonQt::importInterface()->exists(path2)) { + mtime = PythonQt::importInterface()->lastModifiedDate(path2).toTime_t(); } + return mtime; } @@ -708,13 +696,10 @@ QString PythonQtImport::replaceExtension(const QString& str, const QString& ext) PyObject* PythonQtImport::getCodeFromPyc(const QString& file) { - bool hasImporter = PythonQt::importInterface()!=NULL; - PyObject* code; const static QString pycStr("pyc"); QString pyc = replaceExtension(file, pycStr); - if ((hasImporter && PythonQt::importInterface()->exists(pyc)) || - (!hasImporter && QFile::exists(pyc))) { + if (PythonQt::importInterface()->exists(pyc)) { time_t mtime = 0; mtime = getMTimeOfSource(pyc); code = getCodeFromData(pyc, true, false, mtime); @@ -732,10 +717,16 @@ PyObject* PythonQtImport::getCodeFromPyc(const QString& file) /* Module init */ PyDoc_STRVAR(mlabimport_doc, -"Imports python files into MeVisLab, completely replaces internal python import"); +"Imports python files into PythonQt, completely replaces internal python import"); void PythonQtImport::init() { + static bool first = true; + if (!first) { + return; + } + first = false; + PyObject *mod; if (PyType_Ready(&PythonQtImporter_Type) < 0) @@ -778,7 +769,7 @@ void PythonQtImport::init() PyObject* classobj = PyDict_GetItemString(PyModule_GetDict(mod), "PythonQtImporter"); PyObject* path_hooks = PySys_GetObject("path_hooks"); PyList_Append(path_hooks, classobj); - + #ifndef WIN32 // reload the encodings module, because it might fail to custom import requirements (e.g. encryption). PyObject* modules = PyImport_GetModuleDict(); diff --git a/src/PythonQtMetaObjectWrapper.cpp b/src/PythonQtMetaObjectWrapper.cpp index 8123344..2db7123 100644 --- a/src/PythonQtMetaObjectWrapper.cpp +++ b/src/PythonQtMetaObjectWrapper.cpp @@ -52,7 +52,7 @@ static void PythonQtMetaObjectWrapper_dealloc(PythonQtMetaObjectWrapper* self) self->ob_type->tp_free((PyObject*)self); } -static PyObject* PythonQtMetaObjectWrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +static PyObject* PythonQtMetaObjectWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/) { PythonQtMetaObjectWrapper *self; @@ -63,7 +63,7 @@ static PyObject* PythonQtMetaObjectWrapper_new(PyTypeObject *type, PyObject *arg return (PyObject *)self; } -static int PythonQtMetaObjectWrapper_init(PythonQtMetaObjectWrapper *self, PyObject *args, PyObject *kwds) +static int PythonQtMetaObjectWrapper_init(PythonQtMetaObjectWrapper * /*self*/, PyObject * /*args*/, PyObject * /*kwds*/) { return 0; } @@ -123,7 +123,7 @@ static PyMethodDef PythonQtMetaObjectWrapper_methods[] = { {"help", (PyCFunction)PythonQtMetaObjectWrapper_help, METH_NOARGS, "Shows the help of available methods for this class" }, - {NULL} /* Sentinel */ + {NULL, NULL, 0 , NULL} /* Sentinel */ }; diff --git a/src/PythonQtMetaObjectWrapper.h b/src/PythonQtMetaObjectWrapper.h index 6cfff20..fe95cc7 100644 --- a/src/PythonQtMetaObjectWrapper.h +++ b/src/PythonQtMetaObjectWrapper.h @@ -70,6 +70,9 @@ typedef struct { // an abstact class for handling construction of objects class PythonQtConstructorHandler { public: + //! get rid of warnings + virtual ~PythonQtConstructorHandler() {} + virtual QObject* create(const QMetaObject* meta, PyObject *args, PyObject *kw, QString& error) = 0; }; diff --git a/src/PythonQtMisc.h b/src/PythonQtMisc.h index ffc83b5..cf27d4b 100644 --- a/src/PythonQtMisc.h +++ b/src/PythonQtMisc.h @@ -79,6 +79,7 @@ public: foreach(chunk, _chunks) { delete[]chunk; } + _chunks.clear(); } //! reset the storage to 0 (without freeing memory, thus caching old entries for reuse) diff --git a/src/PythonQtQFileImporter.cpp b/src/PythonQtQFileImporter.cpp new file mode 100644 index 0000000..f11b206 --- /dev/null +++ b/src/PythonQtQFileImporter.cpp @@ -0,0 +1,80 @@ +/* + * + * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Contact information: MeVis Research GmbH, Universitaetsallee 29, + * 28359 Bremen, Germany or: + * + * http://www.mevis.de + * + */ + +//---------------------------------------------------------------------------------- +/*! + // \file PythonQtQFileImporter.h + // \author Florian Link + // \author Last changed by $Author: florian $ + // \date 2009-03 + */ +//---------------------------------------------------------------------------------- + +#include +#include + +#include "PythonQtQFileImporter.h" + +PythonQtQFileImporter::PythonQtQFileImporter() { +} + +PythonQtQFileImporter::~PythonQtQFileImporter() { +} + +QByteArray PythonQtQFileImporter::readFileAsBytes (const QString &filename) { + QFile f(filename); + if (f.open(QIODevice::ReadOnly)) { + return f.readAll(); + } else { + return QByteArray(); + } +} + +QByteArray PythonQtQFileImporter::readSourceFile (const QString &filename, bool &ok) { + QFile f(filename); + if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { + ok = true; + return f.readAll(); + } else { + ok = false; + return QByteArray(); + } +} + +bool PythonQtQFileImporter::exists (const QString &filename) { + return QFile::exists(filename); +} + +QDateTime PythonQtQFileImporter::lastModifiedDate (const QString &filename) { + QFileInfo fi(filename); + return fi.lastModified(); +} diff --git a/src/PythonQtQFileImporter.h b/src/PythonQtQFileImporter.h new file mode 100644 index 0000000..fc65b3a --- /dev/null +++ b/src/PythonQtQFileImporter.h @@ -0,0 +1,63 @@ +#ifndef _PYTHONQTQFILEIMPORTER_H +#define _PYTHONQTQFILEIMPORTER_H + +/* + * + * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Contact information: MeVis Research GmbH, Universitaetsallee 29, + * 28359 Bremen, Germany or: + * + * http://www.mevis.de + * + */ + +//---------------------------------------------------------------------------------- +/*! + // \file PythonQtQFileImporter.h + // \author Florian Link + // \author Last changed by $Author: florian $ + // \date 2009-03 + */ +//---------------------------------------------------------------------------------- + +#include + +//! default importer implementation using QFile to load python code +class PythonQtQFileImporter : public PythonQtImportFileInterface { +public: + PythonQtQFileImporter(); + ~PythonQtQFileImporter(); + + QByteArray readFileAsBytes (const QString &filename); + + QByteArray readSourceFile (const QString &filename, bool &ok); + + bool exists (const QString &filename); + + QDateTime lastModifiedDate (const QString &filename); + +}; + +#endif diff --git a/src/PythonQtSlot.cpp b/src/PythonQtSlot.cpp index 84cec69..bd0ddce 100644 --- a/src/PythonQtSlot.cpp +++ b/src/PythonQtSlot.cpp @@ -178,7 +178,7 @@ PyObject *PythonQtSlotFunction_Call(PyObject *func, PyObject *args, PyObject *kw } } -PyObject *PythonQtSlotFunction_CallImpl(QObject* objectToCall, PythonQtSlotInfo* info, PyObject *args, PyObject *kw, bool isVariantCall, void* firstArg) +PyObject *PythonQtSlotFunction_CallImpl(QObject* objectToCall, PythonQtSlotInfo* info, PyObject *args, PyObject * /*kw*/, bool isVariantCall, void* firstArg) { int argc = PyTuple_Size(args); @@ -298,14 +298,14 @@ meth_dealloc(PythonQtSlotFunctionObject *m) } static PyObject * -meth_get__doc__(PythonQtSlotFunctionObject *m, void *closure) +meth_get__doc__(PythonQtSlotFunctionObject * /*m*/, void * /*closure*/) { Py_INCREF(Py_None); return Py_None; } static PyObject * -meth_get__name__(PythonQtSlotFunctionObject *m, void *closure) +meth_get__name__(PythonQtSlotFunctionObject *m, void * /*closure*/) { return PyString_FromString(m->m_ml->metaMethod()->signature()); } @@ -328,7 +328,7 @@ meth_traverse(PythonQtSlotFunctionObject *m, visitproc visit, void *arg) } static PyObject * -meth_get__self__(PythonQtSlotFunctionObject *m, void *closure) +meth_get__self__(PythonQtSlotFunctionObject *m, void * /*closure*/) { PyObject *self; if (PyEval_GetRestricted()) { @@ -347,13 +347,17 @@ static PyGetSetDef meth_getsets [] = { {"__doc__", (getter)meth_get__doc__, NULL, NULL}, {"__name__", (getter)meth_get__name__, NULL, NULL}, {"__self__", (getter)meth_get__self__, NULL, NULL}, - {0} + {NULL, NULL, NULL,NULL}, }; +#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 6 +#define PY_WRITE_RESTRICTED WRITE_RESTRICTED +#endif + #define OFF(x) offsetof(PythonQtSlotFunctionObject, x) static PyMemberDef meth_members[] = { - {"__module__", T_OBJECT, OFF(m_module), WRITE_RESTRICTED}, + {"__module__", T_OBJECT, OFF(m_module), PY_WRITE_RESTRICTED}, {NULL} }; diff --git a/src/PythonQtStdOut.cpp b/src/PythonQtStdOut.cpp index f103a65..a44da98 100644 --- a/src/PythonQtStdOut.cpp +++ b/src/PythonQtStdOut.cpp @@ -41,7 +41,7 @@ #include "PythonQtStdOut.h" -static PyObject *PythonQtStdOutRedirect_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +static PyObject *PythonQtStdOutRedirect_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/) { PythonQtStdOutRedirect *self; self = (PythonQtStdOutRedirect *)type->tp_alloc(type, 0); @@ -70,7 +70,7 @@ static PyObject *PythonQtStdOutRedirect_write(PyObject *self, PyObject *args) return Py_BuildValue(""); } -static PyObject *PythonQtStdOutRedirect_flush(PyObject *self, PyObject *args) +static PyObject *PythonQtStdOutRedirect_flush(PyObject * /*self*/, PyObject * /*args*/) { return Py_BuildValue(""); } @@ -83,7 +83,7 @@ static PyMethodDef PythonQtStdOutRedirect_methods[] = { {"flush", (PyCFunction)PythonQtStdOutRedirect_flush, METH_VARARGS, "flush the output, currently not implemented but needed for logging framework" }, - {NULL} /* Sentinel */ + {NULL, NULL, 0 , NULL} /* sentinel */ }; static PyMemberDef PythonQtStdOutRedirect_members[] = { diff --git a/src/PythonQtVariantWrapper.cpp b/src/PythonQtVariantWrapper.cpp index 1855d39..3936937 100644 --- a/src/PythonQtVariantWrapper.cpp +++ b/src/PythonQtVariantWrapper.cpp @@ -58,7 +58,7 @@ static void PythonQtVariantWrapper_dealloc(PythonQtVariantWrapper* self) self->ob_type->tp_free((PyObject*)self); } -static PyObject* PythonQtVariantWrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +static PyObject* PythonQtVariantWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/) { PythonQtVariantWrapper *self; @@ -70,7 +70,7 @@ static PyObject* PythonQtVariantWrapper_new(PyTypeObject *type, PyObject *args, return (PyObject *)self; } -static int PythonQtVariantWrapper_init(PythonQtVariantWrapper *self, PyObject *args, PyObject *kwds) +static int PythonQtVariantWrapper_init(PythonQtVariantWrapper * /*self*/, PyObject * /*args*/, PyObject * /*kwds*/) { return 0; } @@ -93,7 +93,7 @@ static PyMethodDef PythonQtVariantWrapper_methods[] = { {"help", (PyCFunction)PythonQtVariantWrapper_help, METH_NOARGS, "Shows the help of available methods for this class" }, - {NULL} /* Sentinel */ + {NULL,NULL,0,NULL} /* Sentinel */ }; diff --git a/src/PythonQtWrapper.cpp b/src/PythonQtWrapper.cpp index 663b98a..44df9df 100644 --- a/src/PythonQtWrapper.cpp +++ b/src/PythonQtWrapper.cpp @@ -91,7 +91,7 @@ static void PythonQtWrapper_dealloc(PythonQtWrapper* self) self->ob_type->tp_free((PyObject*)self); } -static PyObject* PythonQtWrapper_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +static PyObject* PythonQtWrapper_new(PyTypeObject *type, PyObject * /*args*/, PyObject * /*kwds*/) { PythonQtWrapper *self; @@ -105,7 +105,7 @@ static PyObject* PythonQtWrapper_new(PyTypeObject *type, PyObject *args, PyObjec return (PyObject *)self; } -static int PythonQtWrapper_init(PythonQtWrapper *self, PyObject *args, PyObject *kwds) +static int PythonQtWrapper_init(PythonQtWrapper * /*self*/, PyObject * /*args*/, PyObject * /*kwds*/) { return 0; } @@ -128,7 +128,7 @@ static PyMethodDef PythonQtWrapper_methods[] = { {"help", (PyCFunction)PythonQtWrapper_help, METH_NOARGS, "Shows the help of available methods for this class" }, - {NULL} /* Sentinel */ + {NULL, NULL, 0, NULL} /* Sentinel */ }; @@ -164,6 +164,9 @@ static PyObject *PythonQtWrapper_getattro(PyObject *obj,PyObject *name) case PythonQtMemberInfo::EnumValue: return PyInt_FromLong(member._enumValue); break; + default: + // is an invalid type, go on + break; } // look for the interal methods (className(), help()) diff --git a/src/src.pri b/src/src.pri index b61cb50..e40bfff 100644 --- a/src/src.pri +++ b/src/src.pri @@ -18,6 +18,7 @@ HEADERS += \ $$PWD/PythonQtCppWrapperFactory.h \ $$PWD/PythonQtVariants.h \ $$PWD/PythonQtVariantWrapper.h \ + $$PWD/PythonQtQFileImporter.h \ $$PWD/wrapper/PythonQtWrappedVariants.h \ $$PWD/gui/PythonQtScriptingConsole.h \ $$PWD/PythonQtSystem.h @@ -37,6 +38,7 @@ SOURCES += \ $$PWD/PythonQtVariants.cpp \ $$PWD/PythonQtVariantWrapper.cpp \ $$PWD/PythonQtWrapper.cpp \ + $$PWD/PythonQtQFileImporter.cpp \ $$PWD/PythonQtMetaObjectWrapper.cpp \ $$PWD/gui/PythonQtScriptingConsole.cpp