[Eric] utf-8 magic with Eric (and trouble without)
Detlev Offenbach
detlev at die-offenbachs.de
Sat Jan 31 11:39:20 GMT 2009
Hi,
the situation is as follows.
The encoding of the runtime environment is set by eric4 depending on the
encoding of the script. This is determined by the coding line. If no such line
is found, it defaults to the one returned by sys.getdefaultencoding() or
latin-1.
This behavior can be changed via the eric4 configuration dialog, Debugger->
Python page.
Now to the problem in the script itself. u'\u2192' is a unicode object and not
an UTF-8 encoded string. However, the later one is required by
QString.fromUtf8(). That said, the correct line should read
w.setText(QString.fromUtf8(u'\u2192'.encode("utf-8"))
With eric4 setting the system encoding to utf-8, which is triggered by the
coding line, an implicit conversion happens and the script runs without an
error.
Regards,
Detlev
On Freitag, 30. Januar 2009, Georg Drees wrote:
> Hello list,
>
> I'm perplexed as to why the following script runs fine
> when called from within Eric ("Run Script") but raises
> an Error when run from the command line.
> The Error also occurs when I remove the line specifying
> the encoding and run it from within Eric.
>
> ---snip main.pyw---
> #!/usr/bin/env python
> # -*- coding: utf-8 -*-
>
> import sys
>
> import PyQt4
> from PyQt4 import QtCore, QtGui
> from PyQt4.QtCore import QString
>
> if __name__=="__main__":
> app = QtGui.QApplication(sys.argv)
> w = QtGui.QLabel()
> w.setText(QString.fromUtf8(u'\u2192'))
> w.show()
> sys.exit(app.exec_())
>
> ---snap---
>
> This is the output I get:
> ---snip---
> example$ python ./main.pyw
> Traceback (most recent call last):
> File "./main.pyw", line 13, in <module>
> w.setText(QString.fromUtf8(u'\u2192'))
> TypeError: argument 1 of QString.fromUtf8() has an invalid type
> ---snap---
>
> Thanks!
> Georg
>
> P.S.:
> Arch Linux
> Eric4 4.2.5 (r2785)
> Python 2.6.1 (both standalone and within Eric)
> PyQt4 4.4.4
--
Detlev Offenbach
detlev at die-offenbachs.de
More information about the Eric
mailing list