|
18 | 18 | import textwrap |
19 | 19 | import types |
20 | 20 | import unittest |
| 21 | +import unittest.mock as mock |
21 | 22 | import warnings |
22 | 23 | import weakref |
23 | 24 |
|
| 25 | +from contextlib import nullcontext |
24 | 26 | from functools import partial |
25 | 27 | from itertools import product, islice |
26 | 28 | from test import support |
|
121 | 123 | </foo> |
122 | 124 | """ |
123 | 125 |
|
| 126 | +def is_python_implementation(): |
| 127 | + assert ET is not None, "ET must be initialized" |
| 128 | + assert pyET is not None, "pyET must be initialized" |
| 129 | + return ET is pyET |
| 130 | + |
| 131 | + |
| 132 | +def equal_wrapper(cls): |
| 133 | + """Mock cls.__eq__ to check whether it has been called or not. |
| 134 | +
|
| 135 | + The behaviour of cls.__eq__ (side-effects included) is left as is. |
| 136 | + """ |
| 137 | + eq = cls.__eq__ |
| 138 | + return mock.patch.object(cls, "__eq__", autospec=True, wraps=eq) |
| 139 | + |
| 140 | + |
124 | 141 | def checkwarnings(*filters, quiet=False): |
125 | 142 | def decorator(test): |
126 | 143 | def newtest(*args, **kwargs): |
@@ -2562,6 +2579,7 @@ def test_pickle_issue18997(self): |
2562 | 2579 |
|
2563 | 2580 |
|
2564 | 2581 | class BadElementTest(ElementTestCase, unittest.TestCase): |
| 2582 | + |
2565 | 2583 | def test_extend_mutable_list(self): |
2566 | 2584 | class X: |
2567 | 2585 | @property |
@@ -2600,18 +2618,168 @@ class Y(X, ET.Element): |
2600 | 2618 | e = ET.Element('foo') |
2601 | 2619 | e.extend(L) |
2602 | 2620 |
|
2603 | | - def test_remove_with_mutating(self): |
2604 | | - class X(ET.Element): |
| 2621 | + def test_remove_with_clear_assume_missing(self): |
| 2622 | + # gh-126033: Check that a concurrent clear() for an assumed-to-be |
| 2623 | + # missing element does not make the interpreter crash. |
| 2624 | + self.do_test_remove_with_clear(raises=True) |
| 2625 | + |
| 2626 | + def test_remove_with_clear_assume_existing(self): |
| 2627 | + # gh-126033: Check that a concurrent clear() for an assumed-to-be |
| 2628 | + # existing element does not make the interpreter crash. |
| 2629 | + self.do_test_remove_with_clear(raises=False) |
| 2630 | + |
| 2631 | + def do_test_remove_with_clear(self, *, raises): |
| 2632 | + |
| 2633 | + # Until the discrepency between "del root[:]" and "root.clear()" is |
| 2634 | + # resolved, we need to keep two tests. Previously, using "del root[:]" |
| 2635 | + # did not crash with the reproducer of gh-126033 while "root.clear()" |
| 2636 | + # did. |
| 2637 | + |
| 2638 | + class E(ET.Element): |
| 2639 | + """Local class to be able to mock E.__eq__ for introspection.""" |
| 2640 | + |
| 2641 | + class X(E): |
2605 | 2642 | def __eq__(self, o): |
2606 | | - del e[:] |
2607 | | - return False |
2608 | | - e = ET.Element('foo') |
2609 | | - e.extend([X('bar')]) |
2610 | | - self.assertRaises(ValueError, e.remove, ET.Element('baz')) |
| 2643 | + del root[:] |
| 2644 | + return not raises |
2611 | 2645 |
|
2612 | | - e = ET.Element('foo') |
2613 | | - e.extend([ET.Element('bar')]) |
2614 | | - self.assertRaises(ValueError, e.remove, X('baz')) |
| 2646 | + class Y(E): |
| 2647 | + def __eq__(self, o): |
| 2648 | + root.clear() |
| 2649 | + return not raises |
| 2650 | + |
| 2651 | + if raises: |
| 2652 | + get_checker_context = lambda: self.assertRaises(ValueError) |
| 2653 | + else: |
| 2654 | + get_checker_context = nullcontext |
| 2655 | + |
| 2656 | + self.assertIs(E.__eq__, object.__eq__) |
| 2657 | + |
| 2658 | + for Z, side_effect in [(X, 'del root[:]'), (Y, 'root.clear()')]: |
| 2659 | + self.enterContext(self.subTest(side_effect=side_effect)) |
| 2660 | + |
| 2661 | + # test removing R() from [U()] |
| 2662 | + for R, U, description in [ |
| 2663 | + (E, Z, "remove missing E() from [Z()]"), |
| 2664 | + (Z, E, "remove missing Z() from [E()]"), |
| 2665 | + (Z, Z, "remove missing Z() from [Z()]"), |
| 2666 | + ]: |
| 2667 | + with self.subTest(description): |
| 2668 | + root = E('top') |
| 2669 | + root.extend([U('one')]) |
| 2670 | + with get_checker_context(): |
| 2671 | + root.remove(R('missing')) |
| 2672 | + |
| 2673 | + # test removing R() from [U(), V()] |
| 2674 | + cases = self.cases_for_remove_missing_with_mutations(E, Z) |
| 2675 | + for R, U, V, description in cases: |
| 2676 | + with self.subTest(description): |
| 2677 | + root = E('top') |
| 2678 | + root.extend([U('one'), V('two')]) |
| 2679 | + with get_checker_context(): |
| 2680 | + root.remove(R('missing')) |
| 2681 | + |
| 2682 | + # Test removing root[0] from [Z()]. |
| 2683 | + # |
| 2684 | + # Since we call root.remove() with root[0], Z.__eq__() |
| 2685 | + # will not be called (we branch on the fast Py_EQ path). |
| 2686 | + with self.subTest("remove root[0] from [Z()]"): |
| 2687 | + root = E('top') |
| 2688 | + root.append(Z('rem')) |
| 2689 | + with equal_wrapper(E) as f, equal_wrapper(Z) as g: |
| 2690 | + root.remove(root[0]) |
| 2691 | + f.assert_not_called() |
| 2692 | + g.assert_not_called() |
| 2693 | + |
| 2694 | + # Test removing root[1] (of type R) from [U(), R()]. |
| 2695 | + is_special = is_python_implementation() and raises and Z is Y |
| 2696 | + if is_python_implementation() and raises and Z is Y: |
| 2697 | + # In pure Python, using root.clear() sets the children |
| 2698 | + # list to [] without calling list.clear(). |
| 2699 | + # |
| 2700 | + # For this reason, the call to root.remove() first |
| 2701 | + # checks root[0] and sets the children list to [] |
| 2702 | + # since either root[0] or root[1] is an evil element. |
| 2703 | + # |
| 2704 | + # Since checking root[1] still uses the old reference |
| 2705 | + # to the children list, PyObject_RichCompareBool() branches |
| 2706 | + # to the fast Py_EQ path and Y.__eq__() is called exactly |
| 2707 | + # once (when checking root[0]). |
| 2708 | + continue |
| 2709 | + else: |
| 2710 | + cases = self.cases_for_remove_existing_with_mutations(E, Z) |
| 2711 | + for R, U, description in cases: |
| 2712 | + with self.subTest(description): |
| 2713 | + root = E('top') |
| 2714 | + root.extend([U('one'), R('rem')]) |
| 2715 | + with get_checker_context(): |
| 2716 | + root.remove(root[1]) |
| 2717 | + |
| 2718 | + def test_remove_with_mutate_root_assume_missing(self): |
| 2719 | + # gh-126033: Check that a concurrent mutation for an assumed-to-be |
| 2720 | + # missing element does not make the interpreter crash. |
| 2721 | + self.do_test_remove_with_mutate_root(raises=True) |
| 2722 | + |
| 2723 | + def test_remove_with_mutate_root_assume_existing(self): |
| 2724 | + # gh-126033: Check that a concurrent mutation for an assumed-to-be |
| 2725 | + # existing element does not make the interpreter crash. |
| 2726 | + self.do_test_remove_with_mutate_root(raises=False) |
| 2727 | + |
| 2728 | + def do_test_remove_with_mutate_root(self, *, raises): |
| 2729 | + E = ET.Element |
| 2730 | + |
| 2731 | + class Z(E): |
| 2732 | + def __eq__(self, o): |
| 2733 | + del root[0] |
| 2734 | + return not raises |
| 2735 | + |
| 2736 | + if raises: |
| 2737 | + get_checker_context = lambda: self.assertRaises(ValueError) |
| 2738 | + else: |
| 2739 | + get_checker_context = nullcontext |
| 2740 | + |
| 2741 | + # test removing R() from [U(), V()] |
| 2742 | + cases = self.cases_for_remove_missing_with_mutations(E, Z) |
| 2743 | + for R, U, V, description in cases: |
| 2744 | + with self.subTest(description): |
| 2745 | + root = E('top') |
| 2746 | + root.extend([U('one'), V('two')]) |
| 2747 | + with get_checker_context(): |
| 2748 | + root.remove(R('missing')) |
| 2749 | + |
| 2750 | + # test removing root[1] (of type R) from [U(), R()] |
| 2751 | + cases = self.cases_for_remove_existing_with_mutations(E, Z) |
| 2752 | + for R, U, description in cases: |
| 2753 | + with self.subTest(description): |
| 2754 | + root = E('top') |
| 2755 | + root.extend([U('one'), R('rem')]) |
| 2756 | + with get_checker_context(): |
| 2757 | + root.remove(root[1]) |
| 2758 | + |
| 2759 | + def cases_for_remove_missing_with_mutations(self, E, Z): |
| 2760 | + # Cases for removing R() from [U(), V()]. |
| 2761 | + # The case U = V = R = E is not interesting as there is no mutation. |
| 2762 | + for U, V in [(E, Z), (Z, E), (Z, Z)]: |
| 2763 | + description = (f"remove missing {E.__name__}() from " |
| 2764 | + f"[{U.__name__}(), {V.__name__}()]") |
| 2765 | + yield E, U, V, description |
| 2766 | + |
| 2767 | + for U, V in [(E, E), (E, Z), (Z, E), (Z, Z)]: |
| 2768 | + description = (f"remove missing {Z.__name__}() from " |
| 2769 | + f"[{U.__name__}(), {V.__name__}()]") |
| 2770 | + yield Z, U, V, description |
| 2771 | + |
| 2772 | + def cases_for_remove_existing_with_mutations(self, E, Z): |
| 2773 | + # Cases for removing root[1] (of type R) from [U(), R()]. |
| 2774 | + # The case U = R = E is not interesting as there is no mutation. |
| 2775 | + for U, R, description in [ |
| 2776 | + (E, Z, "remove root[1] from [E(), Z()]"), |
| 2777 | + (Z, E, "remove root[1] from [Z(), E()]"), |
| 2778 | + (Z, Z, "remove root[1] from [Z(), Z()]"), |
| 2779 | + ]: |
| 2780 | + description = (f"remove root[1] (of type {R.__name__}) " |
| 2781 | + f"from [{U.__name__}(), {R.__name__}()]") |
| 2782 | + yield R, U, description |
2615 | 2783 |
|
2616 | 2784 | @support.infinite_recursion(25) |
2617 | 2785 | def test_recursive_repr(self): |
|
0 commit comments