[Python-bugs-list] [ python-Bugs-628246 ] Set constructor fails with NameError

noreply@sourceforge.net noreply@sourceforge.net
Thu, 31 Oct 2002 18:03:52 -0800


Bugs item #628246, was opened at 2002-10-24 16:52
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=628246&group_id=5470

Category: Python Library
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Jeremy Hylton (jhylton)
Assigned to: Guido van Rossum (gvanrossum)
Summary: Set constructor fails with NameError

Initial Comment:
Here is a toy demo of the problem:

def baditer():
    raise TypeError
    yield 1

import sets
sets.Set(baditer())

Traceback (most recent call last):
  File "/tmp/baditer.py", line 8, in ?
    sets.Set(baditer())
  File "/usr/local/lib/python2.3/sets.py", line 367, in
__init__
    self._update(iterable)
  File "/usr/local/lib/python2.3/sets.py", line 330, in
_update
    transform = getattr(element, "_as_immutable", None)
UnboundLocalError: local variable 'element' referenced
before assignment

The _update() method has a try/except for TypeError
that is trying to catch failures to place set elements
in the dictionary.  The try/except also wraps a for
loop over an iterator.  So if something goes wrong and
the iterator raises TypeError, the
failed-to-insert-element-in-dict code is run.

The obvious solution, it seems, is to have the
try/except inside the for loop so that it only catches
errors in dict insertion.  Tim points out that this
would be slow.

The example above is a toy, but I ran into this bug in
a real application where debugging was made harded
because the Set code was catching an exception when it
shouldn't.


----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-10-31 21:03

Message:
Logged In: YES 
user_id=6380

That patch makes no sense -- it first materializes the
iterator as a list, then iterates over that. Was that what
you intended?

I'd rather give up on the speediness and put the try/except
where it belongs.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-10-25 23:39

Message:
Logged In: YES 
user_id=80475

A simple solution is to run through the iterator once before 
starting the update.  A one line patch is attached.

The only thing I don't like about it is that the code is 
starting to look hare-brained.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=628246&group_id=5470