"""Python version compatibility support for minidom."""
# This module should only be imported using "import *".
# The following names are defined:
# isinstance -- version of the isinstance() function that accepts
# tuples as the second parameter regardless of the
# NodeList -- lightest possible NodeList implementation
# EmptyNodeList -- lightest possible NodeList that is guarateed to
# remain empty (immutable)
# StringTypes -- tuple of defined string types
# GetattrMagic -- base class used to make _get_<attr> be magically
# defproperty -- function used in conjunction with GetattrMagic;
# using these together is needed to make them work
# as efficiently as possible in both Python 2.2+
# and older versions. For example:
# class MyClass(GetattrMagic):
# defproperty(MyClass, "myattr",
# For Python 2.2 and newer, this will construct a
# property object on the class, which avoids
# needing to override __getattr__(). It will only
# work for read-only attributes.
# For older versions of Python, inheriting from
# GetattrMagic will use the traditional
# __getattr__() hackery to achieve the same effect,
# defproperty() should be used for each version of
# the relevant _get_<property>() function.
# NewStyle -- base class to cause __slots__ to be honored in
# True, False -- only for Python 2.2 and earlier
__all__
= ["NodeList", "EmptyNodeList", "NewStyle",
"StringTypes", "defproperty", "GetattrMagic"]
StringTypes
= type(''), type(unicode(''))
# define True and False only if not defined as built-ins
__all__
.extend(["True", "False"])
isinstance('', StringTypes
)
# Wrap isinstance() to make it compatible with the version in
def isinstance(obj
, type_or_seq
):
return _isinstance(obj
, type_or_seq
)
__all__
.append("isinstance")
if 0 <= index
< len(self
):
def _set_length(self
, value
):
raise xml
.dom
.NoModificationAllowedErr(
"attempt to modify read-only attribute 'length'")
length
= property(_get_length
, _set_length
,
doc
="The number of nodes in the NodeList.")
def __setstate__(self
, state
):
class EmptyNodeList(tuple):
def __add__(self
, other
):
def __radd__(self
, other
):
def _set_length(self
, value
):
raise xml
.dom
.NoModificationAllowedErr(
"attempt to modify read-only attribute 'length'")
length
= property(_get_length
, _set_length
,
doc
="The number of nodes in the NodeList.")
def defproperty(klass
, name
, doc
):
# taken care of by the base __getattr__()
def __getattr__(self
, key
):
raise AttributeError, key
get
= getattr(self
, "_get_" + key
)
raise AttributeError, key
def defproperty(klass
, name
, doc
):
get
= getattr(klass
, ("_get_" + name
)).im_func
def set(self
, value
, name
=name
):
raise xml
.dom
.NoModificationAllowedErr(
"attempt to modify read-only attribute " + repr(name
))
assert not hasattr(klass
, "_set_" + name
), \
"expected not to find _set_" + name
prop
= property(get
, set, doc
=doc
)
setattr(klass
, name
, prop
)