Re: What am I doing wrong here?

Discussion in 'Python' started by =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jun 30, 2003.

  1. 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.php?func=detail&aid=762985&group_id=5470&atid=105470

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

    -- Gerhard
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jun 30, 2003
    #1
    1. Advertising

  2. On Mon, 30 Jun 2003 05:22:04 +0200, =?ISO-8859-1?Q?Gerhard_H=E4ring?= <> 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.php?func=detail&aid=762985&group_id=5470&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
    Bengt Richter, Jun 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miguel Dias Moura
    Replies:
    1
    Views:
    421
  2. Jeremy Watts

    what am i doing wrong here?

    Jeremy Watts, Jun 4, 2005, in forum: Java
    Replies:
    12
    Views:
    578
    Jeremy Watts
    Jun 5, 2005
  3. Replies:
    9
    Views:
    22,825
  4. =?ISO-8859-1?Q?Gerhard_H=E4ring?=

    Re: What am I doing wrong here?

    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    617
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Jun 30, 2003
  5. Aahz
    Replies:
    0
    Views:
    620
Loading...

Share This Page