GNOME Bugzilla – Bug 310563
pygtk.require() enhancement
Last modified: 2007-07-05 11:49:47 UTC
>>> import gtk >>> gtk.pygtk_version (2, 6, 0) >>> import pygtk >>> pygtk.require('2.0') Traceback (most recent call last):
+ Trace 61780
assert not sys.modules.has_key('gtk'), \
If the required version of gtk has already been successfully imported before calling pygtk.require() (perhaps in a different 3rd party module), then sys.path is already OK and does not need 'fixing'. In this case pygtk.require() raises an exception and terminates a program which would otherwise run successfully. It could be a bit more forgiving and allow the program to continue rather than raising the exception.
Created attachment 49277 [details] [review] suggested patch
pygtk.require sucks, I think we all agree :| Your patch looks fine to me (not sure about jdahlin), although I had an idea once to deprecate pygtk.require and instead create a pygtk2 module more or less equivalent to pygtk.require("2.0"); this module would also be generated when building pygtk so as to contain the hardcoded path to the gtk-2.0 dir created by pygtk, instead o searching sys.path for it.
I guess this can go in, don't think we want to add yet another module, .pth files seems to work fine in most cases.
Created attachment 62559 [details] [review] patch A patch by Gustavo, he's too lazy to attach it himself.
The main point of my patch is that it makes pygtk.require("2.0") not only more robust, since it doesn't search sys.path for gtk-2.0 dirs, but also extremely fast, for the same reason. The new code looks like this: def require20(): if _pygtk_2_0_dir not in sys.path: sys.path.insert(0, _pygtk_2_0_dir) def require(version): if version == '2.0': return require20() # otherwise use old method .... and _pygtk_2_0_dir is defined at installation time.
OK, committed the patch with a couple more minor fixes to pygobject HEAD.
*** Bug 320221 has been marked as a duplicate of this bug. ***
*** Bug 318300 has been marked as a duplicate of this bug. ***
(Reassigning to pygobject.)