[Qgis-developer] segfault python: no catch for QgsException?

Martin Dobias wonder.sk at gmail.com
Mon Nov 23 09:22:28 EST 2009


On Sat, Nov 21, 2009 at 7:52 PM, Richard Duivenvoorde
<rdmailings at duif.net> wrote:
> Hi devs,
>
> in the mapserverexport plugin we want the user to be able to load another
> project into QGIS to export:
>
> qgisProjectFile = QFileDialog.getOpenFileName(self.dlg, "Choose a QGIS
>              Project", ".", "QGIS Project Files (*.qgs);", "...")
>    try:
>      QgsProject.instance().read(QFileInfo(qgisProjectFile))
>    #except QgsException, err:
>    except:
>      QMessageBox.information(self.dlg, "Not a valid QGIS projectfile.")
>      return
>
> While we do filter the view on *.qgs files, it's still possible that the
> user just fills in an excisting NON-qgs-project file.
>
> On the 'read'-call of this (off course) wrong file, qgis halts fiercely
> with:
> "
> terminate called after throwing an instance of 'QgsException'
>  what():  Project file read error: error occurred while parsing element at
> line 1 column 1 for file /home/richard/temp/foo.jpg
> Aborted
> "

Hi Richard

what QGIS version is that? Here on svn trunk I get just a message box
with the error message and no crash - supposedly thanks to a last
resort try...catch block in QgsApplication.

> Questions:
>
> - neither the "except QgsException:" nor 'except:" seem to catch the
> exception? Doing someting wrong there? Missing Python interface?
>
> - Isn't it possible to catch this exception? (doing exact the same thing via
> the normal (non python way) 'open project'-dialog, the exception is caught:
> "Project file read error: error occurred while parsing element at line 1
> column 1 for file /home/richard/temp/foo.jpg"

The exceptions are not recognized in Python because they're not
wrapped in SIP. Barry had a similar problem few months ago:
http://lists.osgeo.org/pipermail/qgis-developer/2009-January/005887.html

I still think we should remove usage of exceptions from QGIS codebase
- either to make the API simpler and also more Qt-like. But someone
would have to do it :-)

> - shouldn't we load qgs-projects via the python interface? (for valid
> qgs-files it's working ...).

Loading projects should be possible in python, no reason not to allow it...


> - one option would be to disable the possibility of setting the path by
> keyboard in QFileDialog, but I have to find out how to do that ...

Or eventually prior to opening the file in QGIS you could check
whether it exists and it's readable (i.e. try to open it).


Bye
Martin


More information about the Qgis-developer mailing list