MCPcopy Index your code
hub / github.com/python/cpython / do_test_remove_with_clear

Method do_test_remove_with_clear

Lib/test/test_xml_etree.py:2857–2942  ·  view source on GitHub ↗
(self, *, raises)

Source from the content-addressed store, hash-verified

2855 self.do_test_remove_with_clear(raises=False)
2856
2857 def do_test_remove_with_clear(self, *, raises):
2858
2859 # Until the discrepency between "del root[:]" and "root.clear()" is
2860 # resolved, we need to keep two tests. Previously, using "del root[:]"
2861 # did not crash with the reproducer of gh-126033 while "root.clear()"
2862 # did.
2863
2864 class E(ET.Element):
2865 """Local class to be able to mock E.__eq__ for introspection."""
2866
2867 class X(E):
2868 def __eq__(self, o):
2869 del root[:]
2870 return not raises
2871
2872 class Y(E):
2873 def __eq__(self, o):
2874 root.clear()
2875 return not raises
2876
2877 if raises:
2878 get_checker_context = lambda: self.assertRaises(ValueError)
2879 else:
2880 get_checker_context = nullcontext
2881
2882 self.assertIs(E.__eq__, object.__eq__)
2883
2884 for Z, side_effect in [(X, 'del root[:]'), (Y, 'root.clear()')]:
2885 self.enterContext(self.subTest(side_effect=side_effect))
2886
2887 # test removing R() from [U()]
2888 for R, U, description in [
2889 (E, Z, "remove missing E() from [Z()]"),
2890 (Z, E, "remove missing Z() from [E()]"),
2891 (Z, Z, "remove missing Z() from [Z()]"),
2892 ]:
2893 with self.subTest(description):
2894 root = E('top')
2895 root.extend([U('one')])
2896 with get_checker_context():
2897 root.remove(R('missing'))
2898
2899 # test removing R() from [U(), V()]
2900 cases = self.cases_for_remove_missing_with_mutations(E, Z)
2901 for R, U, V, description in cases:
2902 with self.subTest(description):
2903 root = E('top')
2904 root.extend([U('one'), V('two')])
2905 with get_checker_context():
2906 root.remove(R('missing'))
2907
2908 # Test removing root[0] from [Z()].
2909 #
2910 # Since we call root.remove() with root[0], Z.__eq__()
2911 # will not be called (we branch on the fast Py_EQ path).
2912 with self.subTest("remove root[0] from [Z()]"):
2913 root = E('top')
2914 root.append(Z('rem'))

Calls 15

equal_wrapperFunction · 0.85
is_python_implementationFunction · 0.85
enterContextMethod · 0.80
assert_not_calledMethod · 0.80
EClass · 0.70
RFunction · 0.70
VClass · 0.70
ZClass · 0.70
UClass · 0.50
assertRaisesMethod · 0.45

Tested by

no test coverage detected