GNOME Bugzilla – Bug 646667
The '__info__' override is not well controlled
Last modified: 2012-04-04 14:01:27 UTC
Created attachment 185067 [details] [review] Patch for better controlling the KeyErrors on gi.override If I try the code: <code> import gtk from gi.repository import Gio </code> I get this error: <code> Traceback (most recent call last):
+ Trace 226567
from gi.repository import Gio
dynamic_module._load()
overrides_modules = __import__('gi.overrides', fromlist=[self._namespace])
FileEnumerator = override(FileEnumerator)
registry.register(type_)
self[override_class] = override_class
info = getattr(value, '__info__')
</code> I know there are some issues using old gtk and introspection, that's not the bug, the bug is that there is a 'raise' statement for controlling when a type object has no attribute '__info__' with some nice error message and some info, but it won't ever be shown because the 'getattr' from the line before is going to launch its own exception. As far I know, this is happening with Gio and Pango. I'll attach a patch which will make this more clear.
Comment on attachment 185067 [details] [review] Patch for better controlling the KeyErrors on gi.override >From 144fe78d76331c5510fd57ea50c0225faa2e3a27 Mon Sep 17 00:00:00 2001 >From: Juanje Ojeda <jojeda@emergya.es> >Date: Mon, 4 Apr 2011 01:18:43 +0100 >Subject: [PATCH] [gi-overrides] Added try statement for checking the __info__ attrib from the module > >--- > gi/overrides/__init__.py | 5 +++-- > 1 files changed, 3 insertions(+), 2 deletions(-) > >diff --git a/gi/overrides/__init__.py b/gi/overrides/__init__.py >index a98974f..6de2ac4 100644 >--- a/gi/overrides/__init__.py >+++ b/gi/overrides/__init__.py >@@ -17,8 +17,9 @@ class _Registry(dict): > if not key == value: > raise KeyError('You have tried to modify the registry. This should only be done by the override decorator') > >- info = getattr(value, '__info__') >- if info == None: if getattr(value, '__info__', None) is None: Is a better/shorter version of this.
(In reply to comment #1) > > > >- info = getattr(value, '__info__') > >- if info == None: > > if getattr(value, '__info__', None) is None: > > Is a better/shorter version of this. Maybe a intermediate solution in order to be more clear? Just saying... - info = getattr(value, '__info__') - if info == None: + info = getattr(value, '__info__', None) + if info is None: raise KeyError('Can not override a type %s, which is not in a gobject introspection typelib' % value.__name__)
Thanks for this! I cleaned up the patch and added a test case. While I was at it, I also fixed the exception. KeyError makes no sense there, TypeError is more appropriate. http://git.gnome.org/browse/pygobject/commit/?id=05030a95a4d3090162ed5f510a26d69bbb152942