ยปCore Development>Code coverage>Lib/xml/dom/minicompat.py

Python code coverage for Lib/xml/dom/minicompat.py

#countcontent
1n/a"""Python version compatibility support for minidom.
2n/a
3n/aThis module contains internal implementation details and
4n/ashould not be imported; use xml.dom.minidom instead.
5n/a"""
6n/a
7n/a# This module should only be imported using "import *".
8n/a#
9n/a# The following names are defined:
10n/a#
11n/a# NodeList -- lightest possible NodeList implementation
12n/a#
13n/a# EmptyNodeList -- lightest possible NodeList that is guaranteed to
14n/a# remain empty (immutable)
15n/a#
16n/a# StringTypes -- tuple of defined string types
17n/a#
18n/a# defproperty -- function used in conjunction with GetattrMagic;
19n/a# using these together is needed to make them work
20n/a# as efficiently as possible in both Python 2.2+
21n/a# and older versions. For example:
22n/a#
23n/a# class MyClass(GetattrMagic):
24n/a# def _get_myattr(self):
25n/a# return something
26n/a#
27n/a# defproperty(MyClass, "myattr",
28n/a# "return some value")
29n/a#
30n/a# For Python 2.2 and newer, this will construct a
31n/a# property object on the class, which avoids
32n/a# needing to override __getattr__(). It will only
33n/a# work for read-only attributes.
34n/a#
35n/a# For older versions of Python, inheriting from
36n/a# GetattrMagic will use the traditional
37n/a# __getattr__() hackery to achieve the same effect,
38n/a# but less efficiently.
39n/a#
40n/a# defproperty() should be used for each version of
41n/a# the relevant _get_<property>() function.
42n/a
43n/a__all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]
44n/a
45n/aimport xml.dom
46n/a
47n/aStringTypes = (str,)
48n/a
49n/a
50n/aclass NodeList(list):
51n/a __slots__ = ()
52n/a
53n/a def item(self, index):
54n/a if 0 <= index < len(self):
55n/a return self[index]
56n/a
57n/a def _get_length(self):
58n/a return len(self)
59n/a
60n/a def _set_length(self, value):
61n/a raise xml.dom.NoModificationAllowedErr(
62n/a "attempt to modify read-only attribute 'length'")
63n/a
64n/a length = property(_get_length, _set_length,
65n/a doc="The number of nodes in the NodeList.")
66n/a
67n/a # For backward compatibility
68n/a def __setstate__(self, state):
69n/a if state is None:
70n/a state = []
71n/a self[:] = state
72n/a
73n/a
74n/aclass EmptyNodeList(tuple):
75n/a __slots__ = ()
76n/a
77n/a def __add__(self, other):
78n/a NL = NodeList()
79n/a NL.extend(other)
80n/a return NL
81n/a
82n/a def __radd__(self, other):
83n/a NL = NodeList()
84n/a NL.extend(other)
85n/a return NL
86n/a
87n/a def item(self, index):
88n/a return None
89n/a
90n/a def _get_length(self):
91n/a return 0
92n/a
93n/a def _set_length(self, value):
94n/a raise xml.dom.NoModificationAllowedErr(
95n/a "attempt to modify read-only attribute 'length'")
96n/a
97n/a length = property(_get_length, _set_length,
98n/a doc="The number of nodes in the NodeList.")
99n/a
100n/a
101n/adef defproperty(klass, name, doc):
102n/a get = getattr(klass, ("_get_" + name))
103n/a def set(self, value, name=name):
104n/a raise xml.dom.NoModificationAllowedErr(
105n/a "attempt to modify read-only attribute " + repr(name))
106n/a assert not hasattr(klass, "_set_" + name), \
107n/a "expected not to find _set_" + name
108n/a prop = property(get, set, doc=doc)
109n/a setattr(klass, name, prop)