PyXR

c:\python24\lib \ xml \ dom \ minicompat.py



0001 """Python version compatibility support for minidom."""
0002 
0003 # This module should only be imported using "import *".
0004 #
0005 # The following names are defined:
0006 #
0007 #   isinstance    -- version of the isinstance() function that accepts
0008 #                    tuples as the second parameter regardless of the
0009 #                    Python version
0010 #
0011 #   NodeList      -- lightest possible NodeList implementation
0012 #
0013 #   EmptyNodeList -- lightest possible NodeList that is guarateed to
0014 #                    remain empty (immutable)
0015 #
0016 #   StringTypes   -- tuple of defined string types
0017 #
0018 #   GetattrMagic  -- base class used to make _get_<attr> be magically
0019 #                    invoked when available
0020 #   defproperty   -- function used in conjunction with GetattrMagic;
0021 #                    using these together is needed to make them work
0022 #                    as efficiently as possible in both Python 2.2+
0023 #                    and older versions.  For example:
0024 #
0025 #                        class MyClass(GetattrMagic):
0026 #                            def _get_myattr(self):
0027 #                                return something
0028 #
0029 #                        defproperty(MyClass, "myattr",
0030 #                                    "return some value")
0031 #
0032 #                    For Python 2.2 and newer, this will construct a
0033 #                    property object on the class, which avoids
0034 #                    needing to override __getattr__().  It will only
0035 #                    work for read-only attributes.
0036 #
0037 #                    For older versions of Python, inheriting from
0038 #                    GetattrMagic will use the traditional
0039 #                    __getattr__() hackery to achieve the same effect,
0040 #                    but less efficiently.
0041 #
0042 #                    defproperty() should be used for each version of
0043 #                    the relevant _get_<property>() function.
0044 #
0045 #   NewStyle      -- base class to cause __slots__ to be honored in
0046 #                    the new world
0047 #
0048 #   True, False   -- only for Python 2.2 and earlier
0049 
0050 __all__ = ["NodeList", "EmptyNodeList", "NewStyle",
0051            "StringTypes", "defproperty", "GetattrMagic"]
0052 
0053 import xml.dom
0054 
0055 try:
0056     unicode
0057 except NameError:
0058     StringTypes = type(''),
0059 else:
0060     StringTypes = type(''), type(unicode(''))
0061 
0062 
0063 # define True and False only if not defined as built-ins
0064 try:
0065     True
0066 except NameError:
0067     True = 1
0068     False = 0
0069     __all__.extend(["True", "False"])
0070 
0071 
0072 try:
0073     isinstance('', StringTypes)
0074 except TypeError:
0075     #
0076     # Wrap isinstance() to make it compatible with the version in
0077     # Python 2.2 and newer.
0078     #
0079     _isinstance = isinstance
0080     def isinstance(obj, type_or_seq):
0081         try:
0082             return _isinstance(obj, type_or_seq)
0083         except TypeError:
0084             for t in type_or_seq:
0085                 if _isinstance(obj, t):
0086                     return 1
0087             return 0
0088     __all__.append("isinstance")
0089 
0090 
0091 if list is type([]):
0092     class NodeList(list):
0093         __slots__ = ()
0094 
0095         def item(self, index):
0096             if 0 <= index < len(self):
0097                 return self[index]
0098 
0099         def _get_length(self):
0100             return len(self)
0101 
0102         def _set_length(self, value):
0103             raise xml.dom.NoModificationAllowedErr(
0104                 "attempt to modify read-only attribute 'length'")
0105 
0106         length = property(_get_length, _set_length,
0107                           doc="The number of nodes in the NodeList.")
0108 
0109         def __getstate__(self):
0110             return list(self)
0111 
0112         def __setstate__(self, state):
0113             self[:] = state
0114 
0115     class EmptyNodeList(tuple):
0116         __slots__ = ()
0117 
0118         def __add__(self, other):
0119             NL = NodeList()
0120             NL.extend(other)
0121             return NL
0122 
0123         def __radd__(self, other):
0124             NL = NodeList()
0125             NL.extend(other)
0126             return NL
0127 
0128         def item(self, index):
0129             return None
0130 
0131         def _get_length(self):
0132             return 0
0133 
0134         def _set_length(self, value):
0135             raise xml.dom.NoModificationAllowedErr(
0136                 "attempt to modify read-only attribute 'length'")
0137 
0138         length = property(_get_length, _set_length,
0139                           doc="The number of nodes in the NodeList.")
0140 
0141 else:
0142     def NodeList():
0143         return []
0144 
0145     def EmptyNodeList():
0146         return []
0147 
0148 
0149 try:
0150     property
0151 except NameError:
0152     def defproperty(klass, name, doc):
0153         # taken care of by the base __getattr__()
0154         pass
0155 
0156     class GetattrMagic:
0157         def __getattr__(self, key):
0158             if key.startswith("_"):
0159                 raise AttributeError, key
0160 
0161             try:
0162                 get = getattr(self, "_get_" + key)
0163             except AttributeError:
0164                 raise AttributeError, key
0165             return get()
0166 
0167     class NewStyle:
0168         pass
0169 
0170 else:
0171     def defproperty(klass, name, doc):
0172         get = getattr(klass, ("_get_" + name)).im_func
0173         def set(self, value, name=name):
0174             raise xml.dom.NoModificationAllowedErr(
0175                 "attempt to modify read-only attribute " + repr(name))
0176         assert not hasattr(klass, "_set_" + name), \
0177                "expected not to find _set_" + name
0178         prop = property(get, set, doc=doc)
0179         setattr(klass, name, prop)
0180 
0181     class GetattrMagic:
0182         pass
0183 
0184     NewStyle = object
0185 

Generated by PyXR 0.9.4
SourceForge.net Logo