469,903 Members | 2,013 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

can compile function have a bug?

>>compile('U""','c:/test','single')
<code object ? at 00F06B60, file "c:/test", line 1>
>>d=compile('U""','c:/test','single')
d
<code object ? at 00F06BA0, file "c:/test", line 1>
>>exec(d)
u'\xd6\xd0'
>>U""
u'\u4e2d'
>>>
why is the result different?
a bug or another reason?

Oct 8 '06 #1
4 1226
ygao wrote:
>>>compile('U"*"','c:/test','single')
<code object ? at 00F06B60, file "c:/test", line 1>
>>>d=compile('U"*"','c:/test','single')
d
<code object ? at 00F06BA0, file "c:/test", line 1>
>>>exec(d)
u'\xd6\xd0'
>>>U"*"
u'\u4e2d'
>>>>

why is the result different?
a bug or another reason?
How that particular output came to be I don't know, but you should be able
to avoid the confusion by either passing a unicode string to compile() or
specifying the encoding:
>>exec compile(u'u"*"','c:/test','single')
u'\u4e2d'
>>exec compile('# -*- coding: utf8 -*-\nu"*"','c:/test','single')
u'\u4e2d'

Peter

PS: In and all-UTF-8 environment I would have /expected/ to see
>>your_encoding = "utf8"
identity = "latin1"
u'\u4e2d'.encode(your_encoding).decode(identit y)
u'\xe4\xb8\xad'

and that's indeed what I get over here:
>>exec compile('u"*"','c:/test','single')
u'\xe4\xb8\xad'
Oct 9 '06 #2

Peter Otten wrote:
ygao wrote:
>>compile('U"*"','c:/test','single')
<code object ? at 00F06B60, file "c:/test", line 1>
>>d=compile('U"*"','c:/test','single')
d
<code object ? at 00F06BA0, file "c:/test", line 1>
>>exec(d)
u'\xd6\xd0'
>>U"*"
u'\u4e2d'
>>>
why is the result different?
a bug or another reason?

How that particular output came to be I don't know, but you should be able
to avoid the confusion by either passing a unicode string to compile() or
specifying the encoding:
>exec compile(u'u"*"','c:/test','single')
u'\u4e2d'
>exec compile('# -*- coding: utf8 -*-\nu"*"','c:/test','single')
u'\u4e2d'

Peter

PS: In and all-UTF-8 environment I would have /expected/ to see
>your_encoding = "utf8"
identity = "latin1"
u'\u4e2d'.encode(your_encoding).decode(identity )
u'\xe4\xb8\xad'

and that's indeed what I get over here:
>exec compile('u"*"','c:/test','single')
u'\xe4\xb8\xad'
But it's not an all-UTF-8 environment; his_encoding = 'gb2312' or one
of its heirs/successors :-)

Cheers,
John

Oct 9 '06 #3
John Machin wrote:
But it's not an all-UTF-8 environment; his_encoding = 'gb2312' or one
of its heirs/successors :-)
Ouch. Almost understanding a problem hurts more than not understanding it at
all. I just had a refresher of the experience...

Peter
Oct 9 '06 #4

Peter Otten wrote:
ygao wrote:
>>compile('U"*"','c:/test','single')
<code object ? at 00F06B60, file "c:/test", line 1>
>>d=compile('U"*"','c:/test','single')
d
<code object ? at 00F06BA0, file "c:/test", line 1>
>>exec(d)
u'\xd6\xd0'
>>U"*"
u'\u4e2d'
>>>
why is the result different?
a bug or another reason?

How that particular output came to be I don't know, but you should be able
to avoid the confusion by either passing a unicode string to compile() or
specifying the encoding:
>exec compile(u'u"*"','c:/test','single')
u'\u4e2d'
>exec compile('# -*- coding: utf8 -*-\nu"*"','c:/test','single')
u'\u4e2d'
this is what I want!
many thanks!
>
Peter

PS: In and all-UTF-8 environment I would have /expected/ to see
>your_encoding = "utf8"
identity = "latin1"
u'\u4e2d'.encode(your_encoding).decode(identity )
u'\xe4\xb8\xad'

and that's indeed what I get over here:
>exec compile('u"*"','c:/test','single')
u'\xe4\xb8\xad'
Oct 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by xuatla | last post: by
8 posts views Thread by Douglas | last post: by
17 posts views Thread by newbiecpp | last post: by
8 posts views Thread by Michael L. Hostbaek | last post: by
12 posts views Thread by JS | last post: by
10 posts views Thread by Chris LaJoie | last post: by
2 posts views Thread by Frederick Gotham | last post: by
27 posts views Thread by CodeMonk3y | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.