Commits

Philip Jenvey committed a052f15

special case nan containment

Comments (0)

Files changed (2)

pypy/objspace/std/listobject.py

     def _safe_find(self, w_list, obj, start, stop):
         l = self.unerase(w_list.lstorage)
         for i in range(start, min(stop, len(l))):
-            val = l[i]
-            if val == obj:
+            if l[i] == obj:
                 return i
         raise ValueError
 
         if reverse:
             l.reverse()
 
+    def _safe_find(self, w_list, obj, start, stop):
+        from rpython.rlib.rfloat import isnan
+        if not isnan(obj):
+            return AbstractUnwrappedStrategy._safe_find(self, w_list, obj,
+                                                        start, stop)
+        # unwrapped nan != nan, finding it requires more effort
+        l = self.unerase(w_list.lstorage)
+        for i in range(start, min(stop, len(l))):
+            if isnan(l[i]):
+                return i
+        raise ValueError
+
 
 class StringListStrategy(AbstractUnwrappedStrategy, ListStrategy):
     _none_value = None

pypy/objspace/std/test/test_listobject.py

         non_list = NonList()
         assert [] != non_list
 
+    def test_nan_containment(self):
+        nan = float('nan')
+        assert nan in [nan]
+
 
 class AppTestForRangeLists(AppTestW_ListObject):
     spaceconfig = {"objspace.std.withrangelist": True}