[PyQt] 'import PyQt4.uic.Compiler' fails with Python 3.2

Phil Thompson phil at riverbankcomputing.com
Sat Sep 25 12:49:54 BST 2010


On Fri, 24 Sep 2010 20:01:07 +0200, Arfrever Frehtes Taifersar Arahesis
<arfrever.fta at gmail.com> wrote:
> 2010-09-23 18:22:20 Phil Thompson napisał(a):
>> On Wed, 22 Sep 2010 16:16:05 +0200, Arfrever Frehtes Taifersar Arahesis
>> <arfrever.fta at gmail.com> wrote:
>> > 2010-09-19 22:58:26 Phil Thompson napisał(a):
>> >> On Sun, 19 Sep 2010 22:55:17 +0200, Arfrever Frehtes Taifersar
>> >> Arahesis
>> >> <arfrever.fta at gmail.com> wrote:
>> >> > 2010-09-19 15:24:53 Phil Thompson napisał(a):
>> >> >> On Fri, 17 Sep 2010 20:51:55 +0200, Arfrever Frehtes Taifersar
>> >> >> Arahesis
>> >> >> <arfrever.fta at gmail.com> wrote:
>> >> >> > I use SIP 4.11.1 and PyQt4 4.7.6.
>> >> >> > 
>> >> >> > $ python3.1 -c 'import PyQt4.uic.Compiler'
>> >> >> > $ python3.2 -c 'import PyQt4.uic.Compiler'
>> >> >> > Traceback (most recent call last):
>> >> >> >   File
>> >> >> >  
>> >>
"/usr/lib64/python3.2/site-packages/PyQt4/uic/Compiler/proxy_type.py",
>> >> >> >   line 16, in __getattribute__
>> >> >> >     return type.__getattribute__(cls, name)
>> >> >> > AttributeError: type object 'ProxyBase' has no attribute
'module'
>> >> >> > 
>> >> >> > During handling of the above exception, another exception
>> occurred:
>> >> >> > 
>> >> >> > Traceback (most recent call last):
>> >> >> >   File "<string>", line 1, in <module>
>> >> >> >   File
>> >> >> >   "/usr/lib64/python3.2/site-packages/PyQt4/uic/__init__.py",
>> >> line
>> >> >> 3,
>> >> >> >   in <module>
>> >> >> >     from PyQt4.uic.Compiler import indenter, compiler
>> >> >> >   File
>> >> >> >  
>> >> "/usr/lib64/python3.2/site-packages/PyQt4/uic/Compiler/compiler.py",
>> >> >> line
>> >> >> >   5, in <module>
>> >> >> >     from PyQt4.uic.Compiler import qtproxies
>> >> >> >   File
>> >> >> >  
>> >>
"/usr/lib64/python3.2/site-packages/PyQt4/uic/Compiler/qtproxies.py",
>> >> >> >   line 8, in <module>
>> >> >> >     from PyQt4.uic.port_v3.proxy_base import ProxyBase
>> >> >> >   File
>> >> >> >  
>> >>
"/usr/lib64/python3.2/site-packages/PyQt4/uic/port_v3/proxy_base.py",
>> >> >> >   line 4, in <module>
>> >> >> >     class ProxyBase(metaclass=ProxyType):
>> >> >> >   File
>> >> >> >  
>> >>
"/usr/lib64/python3.2/site-packages/PyQt4/uic/Compiler/proxy_type.py",
>> >> >> >   line 11, in __init__
>> >> >> >     if not hasattr(args[0], "module"):
>> >> >> >   File
>> >> >> >  
>> >>
"/usr/lib64/python3.2/site-packages/PyQt4/uic/Compiler/proxy_type.py",
>> >> >> >   line 19, in __getattribute__
>> >> >> >     from PyQt4.uic.Compiler.qtproxies import LiteralProxyClass
>> >> >> > ImportError: cannot import name LiteralProxyClass
>> >> >> 
>> >> >> This looks like a Python 3.2a2 bug.
>> >> > 
>> >> > If it's really a bug in Python, and not another intentional,
>> >> incompatible
>> >> > change,
>> >> > then could you report it to http://bugs.python.org/ ?
>> >> > (I use snapshots of Python, updated at least once per week, not
>> 3.2a2.)
>> >> 
>> >> It disappears if you add a str(cls) just before the import statement
>> >> which
>> >> suggests it's a bug rather than a change. However I don't have
enough
>> >> information to put together a proper bug report.
>> > 
>> > It's not a bug in Python 3.2 :) .
>> > 
>> > http://docs.python.org/dev/py3k/whatsnew/3.2.html:
>> > "The hasattr() function used to catch and suppress any Exception.
>> > Now, it only catches AttributeError. Under the hood, hasattr() works
>> > by calling getattr() and throwing away the results. This is necessary
>> > because dynamic attribute creation is possible using
__getattribute__()
>> > or __getattr__(). If hasattr() were to just scan instance and class
>> > dictionaries it would miss the dynamic methods and make it difficult
>> > to implement proxy objects."
>> > 
>> > The attached patch changes ImportError into AttributeError, which
fixes
>> > 'import PyQt4.uic.Compiler'.
>> 
>> This doesn't explain the problem...
>> 
>> - the import of the qtproxies module shouldn't fail so wrapping it in
>> try/except shouldn't make any difference
> 
> I don't know why 'from PyQt4.uic.Compiler.qtproxies import
> LiteralProxyClass'
> initially fails, but it also fails with Python 2.6, 2.7 and 3.1.
> 
> You can apply the following patch to see this behavior:
> 
> --- pyuic/uic/Compiler/proxy_type.py
> +++ pyuic/uic/Compiler/proxy_type.py
> @@ -16,7 +16,12 @@
>              return type.__getattribute__(cls, name)
>          except AttributeError:
>              # Avoid a circular import.
> -            from PyQt4.uic.Compiler.qtproxies import LiteralProxyClass
> +            try:
> +                from PyQt4.uic.Compiler.qtproxies import
LiteralProxyClass
> +                print("'from PyQt4.uic.Compiler.qtproxies import
> LiteralProxyClass' succeeded")
> +            except ImportError:
> +                print("'from PyQt4.uic.Compiler.qtproxies import
> LiteralProxyClass' failed")
> +                raise
>  
>              return type(name, (LiteralProxyClass, ),
>                          {"module":
>                          moduleMember(type.__getattribute__(cls,
"module"),
> 
> Try to import PyQt4.uic.Compiler after applying this patch.
> 
>> - if Python is no longer suppressing certain exceptions then you would
>> expect to see new exceptions raised, not a segfault
> 
> I see exceptions, not a segmentation fault.
> 
>> - if adding a call to str(), which shouldn't have any side effects,
>> avoids
>> the segfault then that implies a memory corruption problem.
> 
> str(x) calls x.__str__(), which can have side effects.

Got it. The import fails the first time and succeeds subsequently. It will
be fixed properly in tonight's snapshot.

Thanks for your perseverance.

Phil


More information about the PyQt mailing list