1 """Python version compatibility support for minidom."""
3 # This module should only be imported using "import *".
5 # The following names are defined:
7 # isinstance -- version of the isinstance() function that accepts
8 # tuples as the second parameter regardless of the
11 # NodeList -- lightest possible NodeList implementation
13 # EmptyNodeList -- lightest possible NodeList that is guarateed to
14 # remain empty (immutable)
16 # StringTypes -- tuple of defined string types
18 # GetattrMagic -- base class used to make _get_<attr> be magically
19 # invoked when available
20 # defproperty -- function used in conjunction with GetattrMagic;
21 # using these together is needed to make them work
22 # as efficiently as possible in both Python 2.2+
23 # and older versions. For example:
25 # class MyClass(GetattrMagic):
26 # def _get_myattr(self):
29 # defproperty(MyClass, "myattr",
30 # "return some value")
32 # For Python 2.2 and newer, this will construct a
33 # property object on the class, which avoids
34 # needing to override __getattr__(). It will only
35 # work for read-only attributes.
37 # For older versions of Python, inheriting from
38 # GetattrMagic will use the traditional
39 # __getattr__() hackery to achieve the same effect,
40 # but less efficiently.
42 # defproperty() should be used for each version of
43 # the relevant _get_<property>() function.
45 # NewStyle -- base class to cause __slots__ to be honored in
48 # True, False -- only for Python 2.2 and earlier
50 __all__
= ["NodeList", "EmptyNodeList", "NewStyle",
51 "StringTypes", "defproperty", "GetattrMagic"]
58 StringTypes
= type(''),
60 StringTypes
= type(''), type(unicode(''))
63 # define True and False only if not defined as built-ins
69 __all__
.extend(["True", "False"])
73 isinstance('', StringTypes
)
76 # Wrap isinstance() to make it compatible with the version in
77 # Python 2.2 and newer.
79 _isinstance
= isinstance
80 def isinstance(obj
, type_or_seq
):
82 return _isinstance(obj
, type_or_seq
)
85 if _isinstance(obj
, t
):
88 __all__
.append("isinstance")
95 def item(self
, index
):
96 if 0 <= index
< len(self
):
99 def _get_length(self
):
102 def _set_length(self
, value
):
103 raise xml
.dom
.NoModificationAllowedErr(
104 "attempt to modify read-only attribute 'length'")
106 length
= property(_get_length
, _set_length
,
107 doc
="The number of nodes in the NodeList.")
109 def __getstate__(self
):
112 def __setstate__(self
, state
):
115 class EmptyNodeList(tuple):
118 def __add__(self
, other
):
123 def __radd__(self
, other
):
128 def item(self
, index
):
131 def _get_length(self
):
134 def _set_length(self
, value
):
135 raise xml
.dom
.NoModificationAllowedErr(
136 "attempt to modify read-only attribute 'length'")
138 length
= property(_get_length
, _set_length
,
139 doc
="The number of nodes in the NodeList.")
152 def defproperty(klass
, name
, doc
):
153 # taken care of by the base __getattr__()
157 def __getattr__(self
, key
):
158 if key
.startswith("_"):
159 raise AttributeError, key
162 get
= getattr(self
, "_get_" + key
)
163 except AttributeError:
164 raise AttributeError, key
171 def defproperty(klass
, name
, doc
):
172 get
= getattr(klass
, ("_get_" + name
)).im_func
173 def set(self
, value
, name
=name
):
174 raise xml
.dom
.NoModificationAllowedErr(
175 "attempt to modify read-only attribute " + repr(name
))
176 assert not hasattr(klass
, "_set_" + name
), \
177 "expected not to find _set_" + name
178 prop
= property(get
, set, doc
=doc
)
179 setattr(klass
, name
, prop
)