[Python-checkins] python/dist/src/Lib sets.py,1.33,1.34

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Fri, 08 Nov 2002 09:03:38 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv3971

Modified Files:
	sets.py 
Log Message:
Another attempt at making the set constructor both safe and fast.  [SF
bug 628246]


Index: sets.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/sets.py,v
retrieving revision 1.33
retrieving revision 1.34
diff -C2 -d -r1.33 -r1.34
*** sets.py	8 Nov 2002 05:26:52 -0000	1.33
--- sets.py	8 Nov 2002 17:03:36 -0000	1.34
***************
*** 320,343 ****
              return
  
-         # If the mere process of iterating may raise TypeError, materialize
-         # the iterable into a tuple first.  Then the TypeError will get
-         # raised here and propagated back to the caller.  Once we get into
-         # the loop following, TypeError is assumed to mean that element
-         # can't be used as a dict key.
-         if type(iterable) not in (list, tuple, dict, file, xrange, str):
-             iterable = tuple(iterable)
- 
-         it = iter(iterable)
          value = True
!         while True:
!             try:
!                 for element in it:
                      data[element] = value
!                 return
!             except TypeError:
!                 transform = getattr(element, "_as_immutable", None)
!                 if transform is None:
!                     raise # re-raise the TypeError exception we caught
!                 data[transform()] = value
  
  
--- 320,349 ----
              return
  
          value = True
! 
!         if type(iterable) in (list, tuple, xrange):
!             # Optimized: we know that __iter__() and next() can't
!             # raise TypeError, so we can move 'try:' out of the loop.
!             it = iter(iterable)
!             while True:
!                 try:
!                     for element in it:
!                         data[element] = value
!                     return
!                 except TypeError:
!                     transform = getattr(element, "_as_immutable", None)
!                     if transform is None:
!                         raise # re-raise the TypeError exception we caught
!                     data[transform()] = value
!         else:
!             # Safe: only catch TypeError where intended
!             for element in iterable:
!                 try:
                      data[element] = value
!                 except TypeError:
!                     transform = getattr(element, "_as_immutable", None)
!                     if transform is None:
!                         raise # re-raise the TypeError exception we caught
!                     data[transform()] = value