GNOME Bugzilla – Bug 686828
Add API for getting wrapped gi modules without overrides
Last modified: 2012-10-25 07:45:23 UTC
We need a public API for explicitly getting a wrapped gi module from within overrides. Currently is done by peeking into the importers module cache and pulling out an internal variable: gi/overrides/GLib.py: GLib = gi.importer.modules['GLib']._introspection_module This is ok for the general case of importing from the registry because the importer ensures the DynamicModule and IntrospectionModule are loaded before the overrides. However, from the perspective of a discrete override file it is somewhat magical and should be replaced with an explicit getter.
Created attachment 227214 [details] [review] [API add] Add get_introspection_module for getting un-overridden modules Add gi.module.get_introspection_module to explicitly get a wrapped module pulled in through introspection without static and python override handling. This API is intended for python overrides to use rather than having them access gi.importer.modules['<name>']._introspection_module directly. Replace aforementioned usage in all overrides.
Created attachment 227215 [details] [review] [API add] Add get_introspection_module for getting un-overridden modules Add gi.module.get_introspection_module to explicitly get a wrapped module pulled in through introspection without static and python override handling. This API is intended for python overrides to use rather than having them access gi.importer.modules['<name>']._introspection_module directly. Replace aforementioned usage in all overrides.
Thanks, this is a nice cleanup indeed! In this testcase: + # Using a DynamicModule will use get_introspection_module internally. + mod_overridden = gi.module.DynamicModule(mod_name) + mod_overridden._load() ... I don't understand this line: + self.assertTrue(mod1 is mod_overridden._introspection_module) mod1 is the module without overrides, so why this should be identical to the overridden one here?
The test is emulating what gi.importer does in a simpler way. The unittest is intended to test the precise behavior of introspection module caching. So going through gi.importer (from gi.repository import Foo) would have side effects in regards to modifying sys.modules and gi.importer.modules. The test attempts to minimize side effects which should be tested as a separate unit elsewhere. A DynamicModule is what is returned when using "from gi.repository import Foo". DynamicModule wraps up an IntrospectionModule and an overrides module (if found) as instance variables named _introspection_module and _overrides_module respectively. DynamicModule.__getattr__ then uses these to return gi classes and functions back (overrides are tried first). In short, a DynamicModule is composed of the same introspection module returned from a call to get_introspection_module. The last assert checks that loading a DynamicModule will retrieve the correct cache for its composition. I will add some of this text as comments.
Created attachment 227224 [details] [review] [API add] Add get_introspection_module for getting un-overridden modules Additionally added doc strings and comments.
The following fix has been pushed: e9624ed [API add] Add get_introspection_module for getting un-overridden modules
Created attachment 227225 [details] [review] [API add] Add get_introspection_module for getting un-overridden modules Add gi.module.get_introspection_module to explicitly get a wrapped module pulled in through introspection without static and python override handling. This API is intended for python overrides to use rather than having them access gi.importer.modules['<name>']._introspection_module directly. Replace aforementioned usage in all overrides.