[QGIS-Developer] Catch Python exceptions in QGIS console

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[QGIS-Developer] Catch Python exceptions in QGIS console

Jorge Gustavo Rocha
Hi devs,

Am I able to catch Python exceptions in Python console?

I'm trying to debug a DB Manager related issue [1] using pg_services. I have the following piece of code, that runs fine in my computer, but fails on any other computer, within a specific intranet.

import psycopg2
print (psycopg2.__version__)
try:
    connection = psycopg2.connect(service='pg_trabalho')
    cursor = connection.cursor()
    print ( connection.get_dsn_parameters(),"\n")
    cursor.execute("SELECT version();")
    record = cursor.fetchone()
    print("Connected to - ", record,"\n")
except:
    print( psycopg2.InterfaceError )
    print( psycopg2.OperationalError )

When I run it, QGIS crashes [2]. I would prefer to handle the exception myself. How can I catch the exception without crashing QGIS?

[1] https://github.com/qgis/QGIS/issues/30027
[2] http://blog.geomaster.pt/wp-content/uploads/2019/06/qgis-crash-python-try-except.jpg

Thanks,

J. Gustavo
--
Jorge Gustavo Rocha
Departamento de Informática
Universidade do Minho
4710-057 Braga
Tel: +351 253604480
Fax: +351 253604471
Móvel: +351 910333888
skype: nabocudnosor
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Catch Python exceptions in QGIS console

Nyall Dawson
On Fri, 14 Jun 2019 at 00:11, Jorge Gustavo Pereira Bastos Rocha
<[hidden email]> wrote:
>
> Hi devs,
>
> Am I able to catch Python exceptions in Python console?

Yes, definitely

>
> I'm trying to debug a DB Manager related issue [1] using pg_services. I have the following piece of code, that runs fine in my computer, but fails on any other computer, within a specific intranet.
>
> import psycopg2
> print (psycopg2.__version__)
> try:
>     connection = psycopg2.connect(service='pg_trabalho')
>     cursor = connection.cursor()
>     print ( connection.get_dsn_parameters(),"\n")
>     cursor.execute("SELECT version();")
>     record = cursor.fetchone()
>     print("Connected to - ", record,"\n")
> except:
>     print( psycopg2.InterfaceError )
>     print( psycopg2.OperationalError )
>
> When I run it, QGIS crashes [2]. I would prefer to handle the exception myself. How can I catch the exception without crashing QGIS?

This crash isn't caused by a Python exception -- it's something
deeper. If it was just a Python exception you'd see exactly the same
results as something like

    a = [1]
    print(a[2])

Unfortunately, these kinds of crashes can be extremely difficult to
diagnose (especially if they are only reproducible on Windows). If you
can reproduce on Linux, I'd suggest running under valgrind and looking
for clues.

Nyall
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Catch Python exceptions in QGIS console

Jorge Gustavo Rocha
Hi Nyall,

This problem only happens in Windows. In Linux, everything is running fine.

I was convinced that it was a Python exception, but for some reason, it
was not caught.

I will investigate this issue further. It makes me crazy!

Thank you for the feedback,

Jorge


Às 00:05 de 14/06/19, Nyall Dawson escreveu:

> On Fri, 14 Jun 2019 at 00:11, Jorge Gustavo Pereira Bastos Rocha
> <[hidden email]> wrote:
>>
>> Hi devs,
>>
>> Am I able to catch Python exceptions in Python console?
>
> Yes, definitely
>
>>
>> I'm trying to debug a DB Manager related issue [1] using pg_services. I have the following piece of code, that runs fine in my computer, but fails on any other computer, within a specific intranet.
>>
>> import psycopg2
>> print (psycopg2.__version__)
>> try:
>>     connection = psycopg2.connect(service='pg_trabalho')
>>     cursor = connection.cursor()
>>     print ( connection.get_dsn_parameters(),"\n")
>>     cursor.execute("SELECT version();")
>>     record = cursor.fetchone()
>>     print("Connected to - ", record,"\n")
>> except:
>>     print( psycopg2.InterfaceError )
>>     print( psycopg2.OperationalError )
>>
>> When I run it, QGIS crashes [2]. I would prefer to handle the exception myself. How can I catch the exception without crashing QGIS?
>
> This crash isn't caused by a Python exception -- it's something
> deeper. If it was just a Python exception you'd see exactly the same
> results as something like
>
>     a = [1]
>     print(a[2])
>
> Unfortunately, these kinds of crashes can be extremely difficult to
> diagnose (especially if they are only reproducible on Windows). If you
> can reproduce on Linux, I'd suggest running under valgrind and looking
> for clues.
>
> Nyall
>

J. Gustavo
--
Jorge Gustavo Rocha
Departamento de Informática
Universidade do Minho
4710-057 Braga
Tel: +351 253604480
Fax: +351 253604471
Móvel: +351 910333888
skype: nabocudnosor
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Reply | Threaded
Open this post in threaded view
|

Re: Catch Python exceptions in QGIS console

Nyall Dawson
On Fri, 14 Jun 2019 at 09:41, Jorge Gustavo Rocha <[hidden email]> wrote:
>
> Hi Nyall,
>
> This problem only happens in Windows. In Linux, everything is running fine.
>
> I was convinced that it was a Python exception, but for some reason, it
> was not caught.
>
> I will investigate this issue further. It makes me crazy!

Welcome to the wonderful world of Python bindings. When they work --
it's magic. When they don't -- get ready to kiss goodbye two weeks of
wasted time and half your remaining hair.

Nyall
_______________________________________________
QGIS-Developer mailing list
[hidden email]
List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer