Bug#534943: reportbug: python / GTK error about being unable to open display when used from console

Jakub Wilk ubanus at users.sf.net
Sun Jun 28 16:47:15 UTC 2009

* Sandro Tosi <morph at debian.org>, 2009-06-28, 17:09:
>> I find it perfectly reasonable that python-gtk2 issues a warning; in 
>> *most cases* the module has limited usefulness when $DISPLAY is not 
>> set to a valid value.
>ok, so this is informative for a user, but how can this be useful for a program?

The nice thing about warnings is that you can silence them or you can 
turn them into exceptions:

>>> import warnings
>>> warnings.filterwarnings('error', module='gtk')
>>> import gtk
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/lib/pymodules/python2.5/gtk-2.0/gtk/__init__.py", line 84, in <module>
   File "/usr/lib/pymodules/python2.5/gtk-2.0/gtk/__init__.py", line 72, in _init
     warnings.warn(str(e), _gtk.Warning)
   File "/usr/lib/python2.5/warnings.py", line 62, in warn
   File "/usr/lib/python2.5/warnings.py", line 102, in warn_explicit
     raise message
gtk.GtkWarning: could not open display

Don't get me wrong: I don't claim that issuing a warning is an optimal 
solution for dealing with missing $DISPLAY. It's just *better* than 
raising an exception at import time.

>>> We have to know what UIs are available: if there is no DISPLAY, gtk should
>>> not issue a warning but raising an exception.
>> A Python module should not raise exceptions at import time without
>> a good reason. The solution you propose would break pydoc, for instance.
>If it can't work without DISPLAY, then raise an exception; if it can
>work without DISPLAY then remote the worning.

Importing a module might be useful even if the module did not "work", as 
you could still:
- Import it to ensure that it is *installed*. (And if it were not, 
instruct the user to install python-something.)
- Import it to inspect the module contents (e.g. read docstrings).

>> BTW, I am currenly working around this bug by using the attached 
>> wrapper.
>It doesn't seem an elegant solution.
Work-arounds are intended to be effective rather than elegant.

Jakub Wilk

More information about the pkg-gnome-maintainers mailing list