|
65 | 65 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__RMUL__;
|
66 | 66 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
|
67 | 67 |
|
68 |
| -import java.util.ConcurrentModificationException; |
69 | 68 | import java.util.Iterator;
|
70 | 69 | import java.util.List;
|
71 | 70 |
|
@@ -467,20 +466,34 @@ public abstract static class DequeRemoveNode extends PythonBinaryBuiltinNode {
|
467 | 466 |
|
468 | 467 | @Specialization
|
469 | 468 | @TruffleBoundary
|
470 |
| - Object doGeneric(PDeque self, Object value) { |
| 469 | + Object doGeneric(PDeque self, Object value, |
| 470 | + @CachedLibrary(limit = "3") PythonObjectLibrary itemLib) { |
471 | 471 | PythonObjectLibrary valueLib = PythonObjectLibrary.getFactory().getUncached(value);
|
472 |
| - PythonObjectLibrary itemLib = PythonObjectLibrary.getUncached(); |
473 |
| - Iterator<Object> iterator = self.data.iterator(); |
474 |
| - try { |
475 |
| - while (iterator.hasNext()) { |
476 |
| - Object item = iterator.next(); |
477 |
| - if (itemLib.equals(item, value, valueLib)) { |
478 |
| - iterator.remove(); |
| 472 | + // CPython captures the size before iteration |
| 473 | + int n = self.getSize(); |
| 474 | + for (int i = 0; i < n; i++) { |
| 475 | + try { |
| 476 | + boolean result = itemLib.equals(self.peekLeft(), value, valueLib); |
| 477 | + if (n != self.getSize()) { |
| 478 | + throw PRaiseNode.raiseUncached(this, IndexError, "deque mutated during remove()."); |
| 479 | + } |
| 480 | + if (result) { |
| 481 | + Object removed = self.popLeft(); |
| 482 | + assert removed != null; |
| 483 | + DequeRotateNode.doRight(self, i); |
479 | 484 | return PNone.NONE;
|
| 485 | + } else { |
| 486 | + // this is basically 'DequeRotateNode.doLeft(self, -1)' |
| 487 | + self.append(self.popLeft()); |
480 | 488 | }
|
| 489 | + } catch (PException e) { |
| 490 | + /* |
| 491 | + * In case of an error during comparison, we need to restore the original deque |
| 492 | + * by rotating. |
| 493 | + */ |
| 494 | + DequeRotateNode.doRight(self, i); |
| 495 | + throw e; |
481 | 496 | }
|
482 |
| - } catch (ConcurrentModificationException e) { |
483 |
| - throw PRaiseNode.raiseUncached(this, IndexError, "deque mutated during remove()."); |
484 | 497 | }
|
485 | 498 | throw PRaiseNode.raiseUncached(this, ValueError, "deque.remove(x): x not in deque");
|
486 | 499 | }
|
|
0 commit comments