blob: 1244500259d1a6812aec23f8040c46784160700f [file] [log] [blame]
Andrew Kuchling98a0d062013-11-12 10:25:15 -05001"""Python version compatibility support for minidom.
2
3This module contains internal implementation details and
4should not be imported; use xml.dom.minidom instead.
5"""
Martin v. Löwisfc5fec72003-01-25 15:11:07 +00006
7# This module should only be imported using "import *".
8#
9# The following names are defined:
10#
Martin v. Löwisfc5fec72003-01-25 15:11:07 +000011# NodeList -- lightest possible NodeList implementation
12#
Ezio Melotti13925002011-03-16 11:05:33 +020013# EmptyNodeList -- lightest possible NodeList that is guaranteed to
Martin v. Löwisfc5fec72003-01-25 15:11:07 +000014# remain empty (immutable)
15#
16# StringTypes -- tuple of defined string types
17#
Martin v. Löwisfc5fec72003-01-25 15:11:07 +000018# defproperty -- function used in conjunction with GetattrMagic;
19# using these together is needed to make them work
20# as efficiently as possible in both Python 2.2+
21# and older versions. For example:
22#
23# class MyClass(GetattrMagic):
24# def _get_myattr(self):
25# return something
26#
27# defproperty(MyClass, "myattr",
28# "return some value")
29#
30# For Python 2.2 and newer, this will construct a
31# property object on the class, which avoids
32# needing to override __getattr__(). It will only
33# work for read-only attributes.
34#
35# For older versions of Python, inheriting from
36# GetattrMagic will use the traditional
37# __getattr__() hackery to achieve the same effect,
38# but less efficiently.
39#
40# defproperty() should be used for each version of
41# the relevant _get_<property>() function.
Martin v. Löwisfc5fec72003-01-25 15:11:07 +000042
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043__all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]
Martin v. Löwisfc5fec72003-01-25 15:11:07 +000044
Thomas Wouters0e3f5912006-08-11 14:57:12 +000045import xml.dom
Martin v. Löwisfc5fec72003-01-25 15:11:07 +000046
Walter Dörwald5de48bd2007-06-11 21:38:39 +000047StringTypes = (str,)
Martin v. Löwisfc5fec72003-01-25 15:11:07 +000048
49
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050class NodeList(list):
51 __slots__ = ()
52
53 def item(self, index):
54 if 0 <= index < len(self):
55 return self[index]
56
57 def _get_length(self):
58 return len(self)
59
60 def _set_length(self, value):
61 raise xml.dom.NoModificationAllowedErr(
62 "attempt to modify read-only attribute 'length'")
63
64 length = property(_get_length, _set_length,
65 doc="The number of nodes in the NodeList.")
66
67 def __getstate__(self):
68 return list(self)
69
70 def __setstate__(self, state):
71 self[:] = state
72
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074class EmptyNodeList(tuple):
75 __slots__ = ()
76
77 def __add__(self, other):
78 NL = NodeList()
79 NL.extend(other)
80 return NL
81
82 def __radd__(self, other):
83 NL = NodeList()
84 NL.extend(other)
85 return NL
86
87 def item(self, index):
88 return None
89
90 def _get_length(self):
91 return 0
92
93 def _set_length(self, value):
94 raise xml.dom.NoModificationAllowedErr(
95 "attempt to modify read-only attribute 'length'")
96
97 length = property(_get_length, _set_length,
98 doc="The number of nodes in the NodeList.")
Martin v. Löwisfc5fec72003-01-25 15:11:07 +000099
100
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101def defproperty(klass, name, doc):
Christian Heimes4a22b5d2007-11-25 09:39:14 +0000102 get = getattr(klass, ("_get_" + name))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103 def set(self, value, name=name):
104 raise xml.dom.NoModificationAllowedErr(
105 "attempt to modify read-only attribute " + repr(name))
106 assert not hasattr(klass, "_set_" + name), \
107 "expected not to find _set_" + name
108 prop = property(get, set, doc=doc)
109 setattr(klass, name, prop)