TypeError + generator + str.join(): Bug or user error?

Discussion in 'Python' started by Paul Du Bois, Apr 11, 2006.

  1. Paul Du Bois

    Paul Du Bois Guest

    Using win32 python 2.4.1, I have a minimal test program:

    def generate():
    raise TypeError('blah')
    yield ""

    print "\n".join((generate()))

    Executing the program gives:

    Traceback (most recent call last):
    File "<stdin>", line 5, in ?
    TypeError: sequence expected, generator found

    replacing TypeError with Exception gives what I would have expected: a
    traceback starting from the raise statement. I'm not relying on one
    behavior or the other, but I had a TypeError in a generator, and the
    funny exception slowed me down finding it.

    p
    Paul Du Bois, Apr 11, 2006
    #1
    1. Advertising

  2. Paul Du Bois

    Tim Peters Guest

    [Paul Du Bois]
    > Using win32 python 2.4.1, I have a minimal test program:
    >
    > def generate():
    > raise TypeError('blah')
    > yield ""
    >
    > print "\n".join((generate()))
    >
    > Executing the program gives:
    >
    > Traceback (most recent call last):
    > File "<stdin>", line 5, in ?
    > TypeError: sequence expected, generator found
    >
    > replacing TypeError with Exception gives what I would have expected: a
    > traceback starting from the raise statement. I'm not relying on one
    > behavior or the other, but I had a TypeError in a generator, and the
    > funny exception slowed me down finding it.


    I don't really think of it as "a bug" or a "user error". Instead it's
    a consquence of intractable complexity <0.5 wink>. What can you pass
    to join()? "An iterable object" is the answer. So what's "an
    iterable object"? Well, many things, but you can't always tell for
    sure without _trying_ to iterate over it. If the internal _attempt_
    to iterate raises TypeError, the 2.4 join() interprets that as meaning
    "ah, so this object doesn't support iteration after all -- let's raise
    a TypeError of our own, explaining why we failed".

    That's usually a good thing to do, but sometimes (as in your case) it
    turns out it wasn't. The internals got fiddled in 2.5 so that your
    test case produces:

    Traceback (most recent call last):
    File "pd.py", line 5, in <module>
    print "\n".join((generate()))
    File "pd.py", line 2, in generate
    raise TypeError('blah')
    TypeError: blah

    instead. Since only God knows whether that makes some other test case
    worse (probably not, but ...), and it's a change in visible behavior
    regardless, that won't be backported to the 2.4 line.
    Tim Peters, Apr 11, 2006
    #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. Martin Koekenberg
    Replies:
    3
    Views:
    1,279
    Martin Koekenberg
    Aug 26, 2004
  2. Brett C.
    Replies:
    2
    Views:
    2,822
    Martin Koekenberg
    Aug 30, 2004
  3. fireinice

    Is this a bug of str.join?

    fireinice, Feb 16, 2011, in forum: Python
    Replies:
    4
    Views:
    197
    Terry Reedy
    Feb 16, 2011
  4. Cameron Simpson

    Re: Is this a bug of str.join?

    Cameron Simpson, Feb 17, 2011, in forum: Python
    Replies:
    0
    Views:
    456
    Cameron Simpson
    Feb 17, 2011
  5. Ethan Furman
    Replies:
    4
    Views:
    236
    Roy Smith
    May 27, 2011
Loading...

Share This Page