Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: What am I doing wrong here? (http://www.velocityreviews.com/forums/t319004-re-what-am-i-doing-wrong-here.html)

=?ISO-8859-1?Q?Gerhard_H=E4ring?= 06-30-2003 03:22 AM

Re: What am I doing wrong here?
 
Bill C. Wong wrote:
> ################################
> def func( s ):
> s1, s2 = s.split( ' ', 1 )
> try:
> raise s1
> except "aaaa":
> print 'except "aaaa"'
> except:
> print 'except:'
>
> s = "aaaa "
> func( s )
> ################################
> If I manually assign s1 with s1 = "aaaa", then it works fine! What am I
> doing wrong here?


This looks like a Python bug to me. The place to open Python bug reports
is at Sourceforge. I've done so. Please let's continue the analasysis of
this at
http://sourceforge.net/tracker/index...70&atid=105470

I'll try to make more sense of this by looking at the C source code of
the string methods.

-- Gerhard


Bengt Richter 06-30-2003 10:32 PM

Re: What am I doing wrong here?
 
On Mon, 30 Jun 2003 05:22:04 +0200, =?ISO-8859-1?Q?Gerhard_H=E4ring?= <gh@ghaering.de> wrote:

>Bill C. Wong wrote:
>> ################################
>> def func( s ):
>> s1, s2 = s.split( ' ', 1 )
>> try:
>> raise s1
>> except "aaaa":
>> print 'except "aaaa"'
>> except:
>> print 'except:'
>>
>> s = "aaaa "
>> func( s )
>> ################################
>> If I manually assign s1 with s1 = "aaaa", then it works fine! What am I
>> doing wrong here?

>
>This looks like a Python bug to me. The place to open Python bug reports
>is at Sourceforge. I've done so. Please let's continue the analasysis of
>this at
>http://sourceforge.net/tracker/index...70&atid=105470
>
>I'll try to make more sense of this by looking at the C source code of
>the string methods.
>

It's come up before (when I was messing with exceptions for switch/case constructs, for one).
What it boils down to UIAM is that string exception matching is based on the id of the raised
string matching the id of the string literal in the except 'somestring': statement. Apparently
dynamically computed strings are not "interned" like string literals, so they get different id's.
I guess it would be too much of a performance hit to go looking for an interned string constant
to reference in place of a computed string.

Note that you can get a match if you use the same computed string:

>>> cs = 'computed' + 'string'
>>> try:

... raise cs
... except 'computedstring': print 'caught with literal'
... except cs: print 'caught with same computed string'
... except: print 'missed'
...
caught with same computed string
>>> try:

... raise 'computedstring' # not this time
... except 'computedstring': print 'caught with literal'
... except cs: print 'caught with same computed string'
... except: print 'missed'
...
caught with literal
>>>


Regards,
Bengt Richter


All times are GMT. The time now is 08:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.