PEP263 + exec statement

Nick Coghlan ncoghlan at iinet.net.au
Fri Nov 26 12:02:12 EST 2004


Carlos Ribeiro wrote:
> In [3]: s = """
>    ...: a = "Olá!"
>    ...: print "a:", a
>    ...: print "repr(a):", repr(a)
>    ...: b = u"Olá"
>    ...: print "b:", b
>    ...: print "b(latin-1):", b.encode('latin-1')
>    ...: """

Even with a source encoding declared, I believe this statement still creates an 
ASCII string, so the encoding information gets lost. What happens if you make it 
explicitly unicode? (i.e. s = u"""". . . etc)

The way I read the PEP, compile() always treats string objects as ASCII, and 
only deals with encodings for unicode objects.

My results below at least show that whether the string object is str or unicode 
makes a difference, whereas exec vs compile() does not. ('u' in the following 
code is the same as your 's', but unicode instead an ASCII string).

My system is well and truly set up for ASCII though, so I don't think too much 
more can be read into my results (I also don't know what your expected output is!).

 >>> exec s
a: Olá!
repr(a): 'Ol\xa0!'
b: Ol 
b(latin-1): Olá

 >>> exec u
a: Olá!
repr(a): 'Ol\xc3\xa1!'
b: Olá
b(latin-1): Olß

 >>> exec compile(s, '<string>', 'exec')
a: Olá!
repr(a): 'Ol\xa0!'
b: Ol 
b(latin-1): Olá

 >>> exec compile(u, '<string>', 'exec')
a: Olá!
repr(a): 'Ol\xc3\xa1!'
b: Olá
b(latin-1): Olß

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list