PEP263 + exec statement

Carlos Ribeiro carribeiro at gmail.com
Fri Nov 26 12:42:52 EST 2004


On Sat, 27 Nov 2004 03:02:12 +1000, Nick Coghlan <ncoghlan at iinet.net.au> wrote:
> 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)

Well, that was an quite obvious mistake of mine. But even so... when a
file is read, it's supposed to be ASCII-only, but it still gives the
warning for extended ASCII characters.

Anyway, I decided to give it a try. I'm still not sure if these are
the expected results. u is a unicode string (the same as for the
previous ASCII test). e is the same source code, but with encoding
enabled on the second line. I

In [9]: u = u"""
   ...: a = "Olá!"
   ...: print "a:", a
   ...: print "repr(a):", repr(a)
   ...: b = u"Olá"
   ...: print "b:", b
   ...: print "b(latin-1):", b.encode('latin-1')
   ...: """

In [10]: e = u"""
   ....: # -*- coding: iso-8859-1 -*-
   ....: a = "Olá!"
   ....: print "a:", a
   ....: print "repr(a):", repr(a)
   ....: b = u"Olá"
   ....: print "b:", b
   ....: """

In [11]: exec u
a: Olá!
repr(a): 'Ol\xc3\xa1!'
b: Olá
b(latin-1): Olá

In [12]: exec e
a: Olá!
repr(a): 'Ol\xc3\xa1!'
b: Olá

In [13]:

Curious as I am, I decided to do some tests. I found something quite
weird when I added I tried to concatenate the comment on the string.
Exec failed with a weird message, but worked after encoding.

In [64]: u
Out[64]: u'\na = "Ol\xe1!"\nprint "a:", a\nprint "repr(a):",
repr(a)\nb = u"Ol\xe1"\nprint "b:", b\nprint "b(latin-1):",
b.encode(\'latin-1\')\n'

In [65]: e = "# -*- coding: iso-8859-1 -*-\n" + u

In [66]: exec e
---------------------------------------------------------------------------
SystemError                               Traceback (most recent call last)

/home/cribeiro/work/CherryPy/branches/cribeiro-experimental/<console>

SystemError: compile_node: unexpected node type

In [67]: e = u"# -*- coding: iso-8859-1 -*-\n" + u

In [68]: exec e
---------------------------------------------------------------------------
SystemError                               Traceback (most recent call last)

/home/cribeiro/work/CherryPy/branches/cribeiro-experimental/<console>

SystemError: compile_node: unexpected node type

In [69]: exec e.encode('latin-1')
a: Olá!
repr(a): 'Ol\xe1!'
b: Olá
b(latin-1): Olá

Quite weird. Is it appropriate to post it at python-dev?

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list