Unicode again ... default codec ...

Lele Gaifax lele at metapensiero.it
Thu Oct 22 04:25:16 EDT 2009


"Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> writes:

> En Wed, 21 Oct 2009 06:24:55 -0300, Lele Gaifax <lele at metapensiero.it>
> escribió:
>
>> "Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> writes:
>>
> nosetest should do nothing special. You should configure the environment
> so Python *knows* that your console understands utf-8. Once Python is
> aware of the *real* encoding your console is using, sys.stdout.encoding
> will be utf-8 automatically and your problem is solved. I don't know how
> to do that within virtualenv, but the answer certainly does NOT involve
> sys.setdefaultencoding()
>
> On Windows, a "normal" console window on my system uses cp850:
>
>
> D:\USERDATA\Gabriel>chcp
> Tabla de códigos activa: 850
>
> D:\USERDATA\Gabriel>python
> Python 2.6.3 (r263rc1:75186, Oct  2 2009, 20:40:30) [MSC v.1500 32 bit
> (Intel)]
> on win32
> Type "help", "copyright", "credits" or "license" for more information.
> py> import sys
> py> sys.getdefaultencoding()
> 'ascii'
> py> sys.stdout.encoding
> 'cp850'
> py> u = u"áñç"
> py> print u
> áñç

This is the same on my virtualenv:

    $ python -c "import sys; print sys.getdefaultencoding(), sys.stdout.encoding"
    ascii UTF-8
    $ python -c "print u'\xe1\xf1\xe7'"
    áñç
    
But look at this:

    $ cat test.py
    # -*- coding: utf-8 -*-

    class TestAccents(object):
        u'\xe1\xf1\xe7'

        def test_simple(self):
            u'cioè'

            pass


    $ nosetests  test.py
    .
    ----------------------------------------------------------------------
    Ran 1 test in 0.002s

    OK

    $ nosetests -v test.py
    ERROR

    ======================================================================
    Traceback (most recent call last):
      File "/tmp/env/bin/nosetests", line 8, in <module>
        load_entry_point('nose==0.11.1', 'console_scripts', 'nosetests')()
      File "/tmp/env/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/core.py", line 113, in __init__
        argv=argv, testRunner=testRunner, testLoader=testLoader)
      File "/usr/lib/python2.6/unittest.py", line 817, in __init__
        self.runTests()
      File "/tmp/env/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/core.py", line 192, in runTests
        result = self.testRunner.run(self.test)
      File "/tmp/env/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/core.py", line 63, in run
        result.printErrors()
      File "/tmp/env/lib/python2.6/site-packages/nose-0.11.1-py2.6.egg/nose/result.py", line 81, in printErrors
        _TextTestResult.printErrors(self)
      File "/usr/lib/python2.6/unittest.py", line 724, in printErrors
        self.printErrorList('ERROR', self.errors)
      File "/usr/lib/python2.6/unittest.py", line 730, in printErrorList
        self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
      File "/usr/lib/python2.6/unittest.py", line 665, in writeln
        if arg: self.write(arg)
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 10: ordinal not in range(128)

Who is the culprit here?

The fact is, encodings are the real Y2k problem, and they are here to
stay for a while!

thank you,
ciao, lele.
-- 
nickname: Lele Gaifax    | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas    | comincerò ad aver paura di chi mi copia.
lele at nautilus.homeip.net |                 -- Fortunato Depero, 1929.




More information about the Python-list mailing list