1. Pypy
  2. Untitled project
  3. pypy

Commits

Philip Jenvey  committed d0acdb6 Merge

merge upstream

  • Participants
  • Parent commits ed6f1ae, a178d3a
  • Branches default

Comments (0)

Files changed (2)

File pypy/objspace/std/setobject.py

View file
  • Ignore whitespace
 
     def _intersect_base(self, w_set, w_other):
         if self is w_other.strategy:
-            strategy = w_set.strategy
-            storage = strategy._intersect_unwrapped(w_set, w_other)
+            strategy = self
+            if w_set.length() > w_other.length():
+                # swap operants
+                storage = self._intersect_unwrapped(w_other, w_set)
+            else:
+                storage = self._intersect_unwrapped(w_set, w_other)
         elif not w_set.strategy.may_contain_equal_elements(w_other.strategy):
             strategy = self.space.fromcache(EmptySetStrategy)
             storage = strategy.get_empty_storage()
         else:
             strategy = self.space.fromcache(ObjectSetStrategy)
-            storage = self._intersect_wrapped(w_set, w_other)
+            if w_set.length() > w_other.length():
+                # swap operants
+                storage = w_other.strategy._intersect_wrapped(w_other, w_set)
+            else:
+                storage = self._intersect_wrapped(w_set, w_other)
         return storage, strategy
 
     def _intersect_wrapped(self, w_set, w_other):
         return self.erase(result)
 
     def intersect(self, w_set, w_other):
-        if w_set.length() > w_other.length():
-            return w_other.intersect(w_set)
-
         storage, strategy = self._intersect_base(w_set, w_other)
         return w_set.from_storage_and_strategy(storage, strategy)
 

File pypy/objspace/std/test/test_setobject.py

View file
  • Ignore whitespace
         # getting a RuntimeError because iterating over the old storage
         # gives us 1, but 1 is not in the set any longer.
         raises(RuntimeError, list, it)
+
+    def test_intersect_frozenset_set(self):
+        # worked before
+        assert type(frozenset([2]) & set([1, 2])) is frozenset
+        # did not work before because of an optimization that swaps both
+        # operands when the first set is larger than the second
+        assert type(frozenset([1, 2]) & set([2])) is frozenset