[PyQt] Unicode attributes of QObjects not raising AttributeError

Arusekk arek_koz at o2.pl
Sat Jun 22 20:49:42 BST 2019


I just encountered a misguiding error message while using unicode identifiers 
that cost me several hours of debugging.

The code to reproduce is as easy as:

	>>> from PyQt5.QtCore import QObject
	>>> QObject().ą
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	UnicodeEncodeError: 'ascii' codec can't encode character '\u0105'
	in position 0: ordinal not in range(128)

Whereas the expected behaviour would be like here:

	>>> "".ą
	Traceback (most recent call last):
	  File "<stdin>", line 1, in <module>
	AttributeError: 'str' object has no attribute 'ą'

The possible solutions to it are (fix1, possibly having negative impact 

--- sip/QtCore/QtCoremod.sip    2019-05-06 11:03:34.000000000 +0200
+++ sip/QtCore/QtCoremod.sip.fix0       2019-06-22 20:03:35.190091685 +0200
@@ -64,7 +64,7 @@
 %Plugin PyQt5
 %If (Py_v3)
-%DefaultEncoding "ASCII"
+%DefaultEncoding "UTF-8"
 %If (!Py_v3)

Or the following (fix2, only changing QObject's getattr):

--- sip/QtCore/qobject.sip      2019-05-06 11:03:34.000000000 +0200
+++ sip/QtCore/qobject.sip.fix2 2019-06-22 20:10:16.075104723 +0200
@@ -434,7 +434,7 @@
         return qpycore_pyqtconfigure(sipSelf, sipArgs, sipKwds);
-    SIP_PYOBJECT __getattr__(const char *name) const;
+    SIP_PYOBJECT __getattr__(const char *name /Encoding="UTF-8"/) const;
         sipRes = qpycore_qobject_getattr(sipCpp, sipSelf, a0);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190622/e057136f/attachment.sig>

More information about the PyQt mailing list