diff options
| author | Stefan Behnel <stefan_ml@behnel.de> | 2022-07-15 08:40:19 +0200 |
|---|---|---|
| committer | Stefan Behnel <stefan_ml@behnel.de> | 2022-07-15 08:40:19 +0200 |
| commit | ff82753bd09408ecfd6cacf7da67257495dc1335 (patch) | |
| tree | aed0b0af0f6dc27ff7af0981eb060be9b9da4085 /src | |
| parent | 05636df54bff39bb6017e8fa6b979458f61a7bd9 (diff) | |
| download | python-lxml-ff82753bd09408ecfd6cacf7da67257495dc1335.tar.gz | |
LP#1981760: Register "Element.attrib" as collections.abc.MutableMapping.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lxml/etree.pyx | 5 | ||||
| -rw-r--r-- | src/lxml/tests/test_etree.py | 12 |
2 files changed, 16 insertions, 1 deletions
diff --git a/src/lxml/etree.pyx b/src/lxml/etree.pyx index 95dd21ee..c0d236bd 100644 --- a/src/lxml/etree.pyx +++ b/src/lxml/etree.pyx @@ -87,6 +87,7 @@ from itertools import islice cdef object ITER_EMPTY = iter(()) +cdef object MutableMapping try: from collections.abc import MutableMapping # Py3.3+ except ImportError: @@ -113,7 +114,7 @@ class _ImmutableMapping(MutableMapping): iterkeys = itervalues = iteritems = __iter__ cdef object IMMUTABLE_EMPTY_MAPPING = _ImmutableMapping() -del MutableMapping, _ImmutableMapping +del _ImmutableMapping # the rules @@ -2568,6 +2569,8 @@ cdef class _Attrib: return NotImplemented return python.PyObject_RichCompare(one, other, op) +MutableMapping.register(_Attrib) + @cython.final @cython.internal diff --git a/src/lxml/tests/test_etree.py b/src/lxml/tests/test_etree.py index 3e52258e..8bf82c08 100644 --- a/src/lxml/tests/test_etree.py +++ b/src/lxml/tests/test_etree.py @@ -241,6 +241,18 @@ class ETreeOnlyTestCase(HelperTestCase): a[0].clear(keep_tail=True) self.assertEqual(_bytes('<a aa="A"><b/>B2<c ca="C">C1</c>C2</a>'), tostring(a)) + def test_attrib_is_Mapping(self): + try: + from collections.abc import Mapping, MutableMapping + except ImportError: + from collections import Mapping, MutableMapping # Py2 + + Element = self.etree.Element + root = Element("root") + + self.assertTrue(isinstance(root.attrib, Mapping)) + self.assertTrue(isinstance(root.attrib, MutableMapping)) + def test_attribute_has_key(self): # ET in Py 3.x has no "attrib.has_key()" method XML = self.etree.XML |
