Commit 9ea179fa authored by Fred Drake's avatar Fred Drake

remove much of the Python-version compatibility cruft; the minimum Python

version this should support is Python 2.3
parent 8038163e
...@@ -59,7 +59,7 @@ _typeinfo_map = { ...@@ -59,7 +59,7 @@ _typeinfo_map = {
"NMTOKENS": minidom.TypeInfo(None, "nmtokens"), "NMTOKENS": minidom.TypeInfo(None, "nmtokens"),
} }
class ElementInfo(NewStyle): class ElementInfo(object):
__slots__ = '_attr_info', '_model', 'tagName' __slots__ = '_attr_info', '_model', 'tagName'
def __init__(self, tagName, model=None): def __init__(self, tagName, model=None):
...@@ -460,7 +460,7 @@ class ExpatBuilder: ...@@ -460,7 +460,7 @@ class ExpatBuilder:
# where allowed. # where allowed.
_ALLOWED_FILTER_RETURNS = (FILTER_ACCEPT, FILTER_REJECT, FILTER_SKIP) _ALLOWED_FILTER_RETURNS = (FILTER_ACCEPT, FILTER_REJECT, FILTER_SKIP)
class FilterVisibilityController(NewStyle): class FilterVisibilityController(object):
"""Wrapper around a DOMBuilderFilter which implements the checks """Wrapper around a DOMBuilderFilter which implements the checks
to make the whatToShow filter attribute work.""" to make the whatToShow filter attribute work."""
...@@ -518,7 +518,7 @@ class FilterVisibilityController(NewStyle): ...@@ -518,7 +518,7 @@ class FilterVisibilityController(NewStyle):
} }
class FilterCrutch(NewStyle): class FilterCrutch(object):
__slots__ = '_builder', '_level', '_old_start', '_old_end' __slots__ = '_builder', '_level', '_old_start', '_old_end'
def __init__(self, builder): def __init__(self, builder):
...@@ -908,7 +908,7 @@ class InternalSubsetExtractor(ExpatBuilder): ...@@ -908,7 +908,7 @@ class InternalSubsetExtractor(ExpatBuilder):
raise ParseEscape() raise ParseEscape()
def parse(file, namespaces=1): def parse(file, namespaces=True):
"""Parse a document, returning the resulting Document node. """Parse a document, returning the resulting Document node.
'file' may be either a file name or an open file object. 'file' may be either a file name or an open file object.
...@@ -929,7 +929,7 @@ def parse(file, namespaces=1): ...@@ -929,7 +929,7 @@ def parse(file, namespaces=1):
return result return result
def parseString(string, namespaces=1): def parseString(string, namespaces=True):
"""Parse a document from a string, returning the resulting """Parse a document from a string, returning the resulting
Document node. Document node.
""" """
...@@ -940,7 +940,7 @@ def parseString(string, namespaces=1): ...@@ -940,7 +940,7 @@ def parseString(string, namespaces=1):
return builder.parseString(string) return builder.parseString(string)
def parseFragment(file, context, namespaces=1): def parseFragment(file, context, namespaces=True):
"""Parse a fragment of a document, given the context from which it """Parse a fragment of a document, given the context from which it
was originally extracted. context should be the parent of the was originally extracted. context should be the parent of the
node(s) which are in the fragment. node(s) which are in the fragment.
...@@ -963,7 +963,7 @@ def parseFragment(file, context, namespaces=1): ...@@ -963,7 +963,7 @@ def parseFragment(file, context, namespaces=1):
return result return result
def parseFragmentString(string, context, namespaces=1): def parseFragmentString(string, context, namespaces=True):
"""Parse a fragment of a document from a string, given the context """Parse a fragment of a document from a string, given the context
from which it was originally extracted. context should be the from which it was originally extracted. context should be the
parent of the node(s) which are in the fragment. parent of the node(s) which are in the fragment.
......
...@@ -4,10 +4,6 @@ ...@@ -4,10 +4,6 @@
# #
# The following names are defined: # The following names are defined:
# #
# isinstance -- version of the isinstance() function that accepts
# tuples as the second parameter regardless of the
# Python version
#
# NodeList -- lightest possible NodeList implementation # NodeList -- lightest possible NodeList implementation
# #
# EmptyNodeList -- lightest possible NodeList that is guarateed to # EmptyNodeList -- lightest possible NodeList that is guarateed to
...@@ -15,8 +11,6 @@ ...@@ -15,8 +11,6 @@
# #
# StringTypes -- tuple of defined string types # StringTypes -- tuple of defined string types
# #
# GetattrMagic -- base class used to make _get_<attr> be magically
# invoked when available
# defproperty -- function used in conjunction with GetattrMagic; # defproperty -- function used in conjunction with GetattrMagic;
# using these together is needed to make them work # using these together is needed to make them work
# as efficiently as possible in both Python 2.2+ # as efficiently as possible in both Python 2.2+
...@@ -41,14 +35,8 @@ ...@@ -41,14 +35,8 @@
# #
# defproperty() should be used for each version of # defproperty() should be used for each version of
# the relevant _get_<property>() function. # the relevant _get_<property>() function.
#
# NewStyle -- base class to cause __slots__ to be honored in
# the new world
#
# True, False -- only for Python 2.2 and earlier
__all__ = ["NodeList", "EmptyNodeList", "NewStyle", __all__ = ["NodeList", "EmptyNodeList", "StringTypes", "defproperty"]
"StringTypes", "defproperty", "GetattrMagic"]
import xmlcore.dom import xmlcore.dom
...@@ -60,125 +48,62 @@ else: ...@@ -60,125 +48,62 @@ else:
StringTypes = type(''), type(unicode('')) StringTypes = type(''), type(unicode(''))
# define True and False only if not defined as built-ins class NodeList(list):
try: __slots__ = ()
True
except NameError:
True = 1
False = 0
__all__.extend(["True", "False"])
def item(self, index):
if 0 <= index < len(self):
return self[index]
try: def _get_length(self):
isinstance('', StringTypes) return len(self)
except TypeError:
#
# Wrap isinstance() to make it compatible with the version in
# Python 2.2 and newer.
#
_isinstance = isinstance
def isinstance(obj, type_or_seq):
try:
return _isinstance(obj, type_or_seq)
except TypeError:
for t in type_or_seq:
if _isinstance(obj, t):
return 1
return 0
__all__.append("isinstance")
if list is type([]):
class NodeList(list):
__slots__ = ()
def item(self, index):
if 0 <= index < len(self):
return self[index]
def _get_length(self):
return len(self)
def _set_length(self, value):
raise xmlcore.dom.NoModificationAllowedErr(
"attempt to modify read-only attribute 'length'")
length = property(_get_length, _set_length,
doc="The number of nodes in the NodeList.")
def __getstate__(self):
return list(self)
def __setstate__(self, state):
self[:] = state
class EmptyNodeList(tuple):
__slots__ = ()
def __add__(self, other):
NL = NodeList()
NL.extend(other)
return NL
def __radd__(self, other):
NL = NodeList()
NL.extend(other)
return NL
def item(self, index):
return None
def _get_length(self):
return 0
def _set_length(self, value):
raise xmlcore.dom.NoModificationAllowedErr(
"attempt to modify read-only attribute 'length'")
length = property(_get_length, _set_length,
doc="The number of nodes in the NodeList.")
else: def _set_length(self, value):
def NodeList(): raise xml.dom.NoModificationAllowedErr(
return [] "attempt to modify read-only attribute 'length'")
def EmptyNodeList(): length = property(_get_length, _set_length,
return [] doc="The number of nodes in the NodeList.")
def __getstate__(self):
return list(self)
try: def __setstate__(self, state):
property self[:] = state
except NameError:
def defproperty(klass, name, doc):
# taken care of by the base __getattr__()
pass
class GetattrMagic: class EmptyNodeList(tuple):
def __getattr__(self, key): __slots__ = ()
if key.startswith("_"):
raise AttributeError, key
try: def __add__(self, other):
get = getattr(self, "_get_" + key) NL = NodeList()
except AttributeError: NL.extend(other)
raise AttributeError, key return NL
return get()
class NewStyle: def __radd__(self, other):
pass NL = NodeList()
NL.extend(other)
return NL
else: def item(self, index):
def defproperty(klass, name, doc): return None
get = getattr(klass, ("_get_" + name)).im_func
def set(self, value, name=name): def _get_length(self):
raise xmlcore.dom.NoModificationAllowedErr( return 0
"attempt to modify read-only attribute " + repr(name))
assert not hasattr(klass, "_set_" + name), \ def _set_length(self, value):
"expected not to find _set_" + name raise xml.dom.NoModificationAllowedErr(
prop = property(get, set, doc=doc) "attempt to modify read-only attribute 'length'")
setattr(klass, name, prop)
length = property(_get_length, _set_length,
class GetattrMagic: doc="The number of nodes in the NodeList.")
pass
NewStyle = object 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)
...@@ -31,7 +31,7 @@ _nodeTypes_with_children = (xmlcore.dom.Node.ELEMENT_NODE, ...@@ -31,7 +31,7 @@ _nodeTypes_with_children = (xmlcore.dom.Node.ELEMENT_NODE,
xmlcore.dom.Node.ENTITY_REFERENCE_NODE) xmlcore.dom.Node.ENTITY_REFERENCE_NODE)
class Node(xmlcore.dom.Node, GetattrMagic): class Node(xmlcore.dom.Node):
namespaceURI = None # this is non-null only for elements and attributes namespaceURI = None # this is non-null only for elements and attributes
parentNode = None parentNode = None
ownerDocument = None ownerDocument = None
...@@ -459,7 +459,7 @@ defproperty(Attr, "localName", doc="Namespace-local name of this attribute.") ...@@ -459,7 +459,7 @@ defproperty(Attr, "localName", doc="Namespace-local name of this attribute.")
defproperty(Attr, "schemaType", doc="Schema type for this attribute.") defproperty(Attr, "schemaType", doc="Schema type for this attribute.")
class NamedNodeMap(NewStyle, GetattrMagic): class NamedNodeMap(object):
"""The attribute list is a transient interface to the underlying """The attribute list is a transient interface to the underlying
dictionaries. Mutations here will change the underlying element's dictionaries. Mutations here will change the underlying element's
dictionary. dictionary.
...@@ -613,7 +613,7 @@ defproperty(NamedNodeMap, "length", ...@@ -613,7 +613,7 @@ defproperty(NamedNodeMap, "length",
AttributeList = NamedNodeMap AttributeList = NamedNodeMap
class TypeInfo(NewStyle): class TypeInfo(object):
__slots__ = 'namespace', 'name' __slots__ = 'namespace', 'name'
def __init__(self, namespace, name): def __init__(self, namespace, name):
...@@ -1146,7 +1146,7 @@ class CDATASection(Text): ...@@ -1146,7 +1146,7 @@ class CDATASection(Text):
writer.write("<![CDATA[%s]]>" % self.data) writer.write("<![CDATA[%s]]>" % self.data)
class ReadOnlySequentialNamedNodeMap(NewStyle, GetattrMagic): class ReadOnlySequentialNamedNodeMap(object):
__slots__ = '_seq', __slots__ = '_seq',
def __init__(self, seq=()): def __init__(self, seq=()):
...@@ -1418,7 +1418,7 @@ class DOMImplementation(DOMImplementationLS): ...@@ -1418,7 +1418,7 @@ class DOMImplementation(DOMImplementationLS):
def _create_document(self): def _create_document(self):
return Document() return Document()
class ElementInfo(NewStyle): class ElementInfo(object):
"""Object that represents content-model information for an element. """Object that represents content-model information for an element.
This implementation is not expected to be used in practice; DOM This implementation is not expected to be used in practice; DOM
......
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
import copy import copy
import xmlcore.dom import xmlcore.dom
from xmlcore.dom.minicompat import *
from xmlcore.dom.NodeFilter import NodeFilter from xmlcore.dom.NodeFilter import NodeFilter
...@@ -211,7 +209,7 @@ def _name_xform(name): ...@@ -211,7 +209,7 @@ def _name_xform(name):
return name.lower().replace('-', '_') return name.lower().replace('-', '_')
class DOMEntityResolver(NewStyle): class DOMEntityResolver(object):
__slots__ = '_opener', __slots__ = '_opener',
def resolveEntity(self, publicId, systemId): def resolveEntity(self, publicId, systemId):
...@@ -255,7 +253,7 @@ class DOMEntityResolver(NewStyle): ...@@ -255,7 +253,7 @@ class DOMEntityResolver(NewStyle):
return param.split("=", 1)[1].lower() return param.split("=", 1)[1].lower()
class DOMInputSource(NewStyle): class DOMInputSource(object):
__slots__ = ('byteStream', 'characterStream', 'stringData', __slots__ = ('byteStream', 'characterStream', 'stringData',
'encoding', 'publicId', 'systemId', 'baseURI') 'encoding', 'publicId', 'systemId', 'baseURI')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment