[Qgis-developer] Changes to python/utils.py

Larry Shaffer larrys at dakotacarto.com
Fri May 25 17:14:50 PDT 2012


I have two proposed changes to python/utils.py [0] (and
src/python/qgspythonutilsimpl.cpp). I think I am capable of making the
Python code changes to utils.py, but would like someone to offer to
review my work, or work with me on the changes. Since python/utils.py
is an integral part of Python support in QGIS, I don't want to screw
anything up. :)

1) Python exceptions - add lightweight debug output

Currently sys.excepthook is substituted with the qgis_excepthook
(showException() method). I propose extending showException() to
include lightweight debug info provided by additional inquiries into
the traceback object(s). A similar solution is here [1].

Output from [1]'s very simple example looks like:

# normal traceback info
Traceback (most recent call last):
  File "debug_light.py", line 72, in <module>
    pad4(data)
  File "debug_light.py", line 55, in pad4
    return_value.append("0" * (4 - len(thing)) + thing)
TypeError: object of type 'int' has no len()

# extended info on module and function/method (i.e. frame) variables
Locals by frame, innermost last

Frame <module> in debug_light.py at line 74
            __builtins__ =  <module '__builtin__' (built-in)>
                __file__ =  debug_light.py
               traceback =  <module 'traceback' from
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/traceback.pyc'>
             __package__ =  None
                     sys =  <module 'sys' (built-in)>
                    pad4 =  <function pad4 at 0x1004b8c08>
                __name__ =  __main__
                    data =  ['1', '2', 3, '4']
                 __doc__ =  None
          print_exc_plus =  <function print_exc_plus at 0x1004b8320>

Frame pad4 in debug_light_2.py at line 55
                   thing =  3
            return_value =  ['0001', '0002']
                     seq =  ['1', '2', 3, '4']

This provides users and developers with pertinent info on
exception-state attribute and variable values, in addition to the
traceback. This additional output could be directed to a second tab,
in a new exceptions dialog, labeled 'Frames' or 'Variables'.

[0] https://github.com/qgis/Quantum-GIS/blob/master/python/utils.py
[1] http://code.activestate.com/recipes/52215/


2) Module import - add deeper module tracking/reloading

There has recently been work on a module [2][3] that recursively
tracks and reloads dependent module imports in a way that I think is a
little more robust and flexible than the current method in
python/utils.py. I have incorporated a custom version of reloader.py
in my Plugin Editor, with good success, for live editing/reloading of
Python test scripts within a running QGIS instance. Plugin Installer,
Manager, Plugin Reloader, and Plugin Editor are the only plugins (I
know of) that utilize the reloading aspect of python/utils.py. I'm
guessing that Plugin Reloader is used extensively in development.

This correct-dependency-order recursive reload not only might allow
plugins to be fully reloaded live, even with in-module sys.path
adjustments, but also *retain* selected module states (see [2]
__reload__() description). This might mean a LOT less QGIS relaunches
during plugin development.

[2] http://www.indelible.org/ink/python-reloading/
[3] https://github.com/jparise/python-reloader ,
https://github.com/jparise/python-reloader/blob/master/reloader.py


Regards,

Larry Shaffer
Dakota Cartography
Black Hills, South Dakota


More information about the Qgis-developer mailing list