After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 639229 - dir(module) does not list all available attributes
dir(module) does not list all available attributes
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: introspection
unspecified
Other Linux
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2011-01-11 17:01 UTC by Laszlo Pandy
Modified: 2011-01-13 16:43 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Change __dir__() to report all the attributes that __getattr__ supports. (1.04 KB, patch)
2011-01-11 17:06 UTC, Laszlo Pandy
committed Details | Review
Fixes __dir__() method for DynamicModule and IntrospectionModule, without breaking tests. (1.77 KB, patch)
2011-01-11 21:01 UTC, Laszlo Pandy
none Details | Review
new patch for __dir__() method in DynamicModule and IntrospectionModule (2.25 KB, patch)
2011-01-13 14:44 UTC, Laszlo Pandy
none Details | Review
Simple test case for __dir__() (1.01 KB, patch)
2011-01-13 16:20 UTC, Laszlo Pandy
none Details | Review
new patch for __dir__() with test case included (3.81 KB, patch)
2011-01-13 16:39 UTC, Laszlo Pandy
committed Details | Review

Description Laszlo Pandy 2011-01-11 17:01:20 UTC
In the DynamicModule class, the __dir__() method only returns the attributes available in the loaded typelib. It does not return the class attributes. This leads to the case where some attributes can be accessed, but do not show up in dir().

To make them more consistent, __dir__() should return the dynamic attributes from the typelib, as well as the keys from self.__dict__.

As an aside to this bug, it is also possible to access class members of IntrospectionModule, though they are not reported by DynamicModule.__dir__() either. This is because DynamicModule.__getattr__() calls getattr() on the IntrospectionModule instance. This should be changed to call IntrospectionModule.__getattr__ directly to avoid the case where a class variable has the same name as an attribute in the typelib. With the current call to getattr(IntrospectionModule, ...) instead of IntrospectionModule.__getattr__(...), the class variable would be returned instead of the typelib attribute.
Comment 1 Laszlo Pandy 2011-01-11 17:06:02 UTC
Created attachment 178063 [details] [review]
Change __dir__() to report all the attributes that __getattr__ supports.

Change DynamicModule.__dir__() to return the local class members as well as the typelib attributes.

Change DynamicModule.__getattr__() to call IntrospectionModule.__getattr__() directly, so that it won't inadvertently return class attributes from IntrospectionModule.
Comment 2 Tomeu Vizoso 2011-01-11 18:28:35 UTC
Thanks!
Comment 3 Laszlo Pandy 2011-01-11 21:01:42 UTC
Created attachment 178086 [details] [review]
Fixes __dir__() method for DynamicModule and IntrospectionModule, without breaking tests.

Here is an updated patch, applied to master, after the getattr() which broke the tests was reverted.

The previous patch made tests fail because calling __getattr__ directly skips the check of __dict__. IntrospectionModule was caching wrapped objects in __dict__, and the patch caused it to create and register a new type each time, instead of using the wrapped one from previous calls.

Thus, we cannot avoid __dict__, so all class methods will be available to applications importing from gi.repository. If there any any name collisions, a prefix would have to be (used such as _pygi_...) for all class methods.

This patch does not try to hide the class methods, it simply puts those that are already accessible in DynamicModule and IntrospectionModule  in the list returned by __dir__().
Comment 4 Laszlo Pandy 2011-01-13 14:44:32 UTC
Created attachment 178230 [details] [review]
new patch for __dir__() method in DynamicModule and IntrospectionModule

This patch fixes two things that the previous patch didn't:
- For DynamicModule, overrides are included in the __dir__() output.
- For both IntrospectionModule and DynamicModule, dir(self.__class__) is included, so method names of the classes are returned as well.

In this patch set() is used to avoid reporting duplicate elements.

dir(self.__class__) and self.__dict__.keys() are included to match what Python does by default for classes without a __dir__ method. With this patch, the __dir__() method includes all of attributes the default dir() does (ie, nothing is hidden by having a custom __dir__() method).
Comment 5 Laszlo Pandy 2011-01-13 16:20:51 UTC
Created attachment 178242 [details] [review]
Simple test case for __dir__()

Add a simple test case to test __dir__() method. It does not test to see if every attribute is listed, just a few of each kind:
- (wrapped) typelib attributes
- class attributes and methods
- instance attributes
Comment 6 Laszlo Pandy 2011-01-13 16:39:38 UTC
Created attachment 178244 [details] [review]
new patch for __dir__() with test case included

This patch includes both of the previous patches for the fix and test case together in one patch.
Comment 7 Tomeu Vizoso 2011-01-13 16:43:12 UTC
Thanks!