Re: Prefix for class attributes in DynamicModule and IntrospectionModule.



A fix for this was committed yesterday. Instead of putting a _pygi_
prefix, I have just changed the two attributes which didn't already
have an underscore before their name. The presumption is that since
underscore refers to private members, no typelib should have an
attribute name starting with an underscore.

On Thu, Jan 13, 2011 at 4:57 PM, Laszlo Pandy <laszlok2 gmail com> wrote:
> Both DynamicModule and IntrospectionModule work by implementing
> __getattr__ so that if Python cannot find an attribute as an instance
> of the class, the custom implementations will be able to wrap and load
> the requested attribute from the typelib.
>
> However, if the attribute requested already exists, Python will grab
> it directly from the instance's __dict__ and not call __getattr__ at
> all. You can see that third party developers can access all the class
> attributes, for which __getattr__ will not be called:
>
>>>> from gi.repository import Gtk
>>>> Gtk._namespace
> 'Gtk'
>>>> Gtk.version
> '3.0'
>
> The attributes '_namespace' and 'version' are not in the typelib, they
> are class attributes from DynamicModule and IntrospectionModule and
> respectively.
>
> But what if the typelib defines an attribute with the same name as one
> of the class attributes? Gstreamer does exactly that. It has a
> *function* gst_version[1] which is Gst.version in the typelib. This
> obviously will fail, because the class attribute (a string) is
> returned instead of the wrapped function.
>
>>>> from gi.repository import Gst
>>>> Gst.version()
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: 'str' object is not callable
>>>>
>
> The simplest way to fix this is to simply put a prefix on all class
> members (variables and methods). I have included a patch using the
> prefix '_pygi_', starting with a single underscore to avoid Python's
> name mangling, since overrides still need access to these members
> outside the class.
>
> I am looking for feedback on this patch. It passes all tests, but is a
> quick first attempt.
>
> [1] http://www.gstreamer.net/data/doc/gstreamer/head/gstreamer/html/gstreamer-Gst.html#gst-version
>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]