Edgewall Software

Changeset 1079 for trunk/genshi/input.py


Ignore:
Timestamp:
Nov 12, 2009, 12:36:14 PM (14 years ago)
Author:
cmlenz
Message:

Add a couple of fallback imports for Python 3.0.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/genshi/input.py

    r1077 r1079  
    1717
    1818from itertools import chain
     19try:
     20    import htmlentitydefs as entities
     21    import HTMLParser as html
     22except ImportError:
     23    from html import entities
     24    from html import parser as html
     25try:
     26    from StringIO import StringIO
     27    BytesIO = StringIO
     28except ImportError:
     29    from io import BytesIO, StringIO
    1930from xml.parsers import expat
    20 import HTMLParser as html
    21 import htmlentitydefs
    22 from StringIO import StringIO
    2331
    2432from genshi.core import Attrs, QName, Stream, stripentities
     
    8997
    9098    _entitydefs = ['<!ENTITY %s "&#%d;">' % (name, value) for name, value in
    91                    htmlentitydefs.name2codepoint.items()]
     99                   entities.name2codepoint.items()]
    92100    _external_dtd = '\n'.join(_entitydefs)
    93101
     
    170178    def _build_foreign(self, context, base, sysid, pubid):
    171179        parser = self.expat.ExternalEntityParserCreate(context)
    172         parser.ParseFile(StringIO(self._external_dtd))
     180        parser.ParseFile(BytesIO(self._external_dtd))
    173181        return 1
    174182
     
    238246            # deal with undefined entities
    239247            try:
    240                 text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])
     248                text = unichr(entities.name2codepoint[text[1:-1]])
    241249                self._enqueue(TEXT, text)
    242250            except KeyError:
     
    268276    :raises ParseError: if the XML text is not well-formed
    269277    """
    270     return Stream(list(XMLParser(StringIO(text))))
     278    return Stream(list(XMLParser(BytesIO(text))))
    271279
    272280
     
    388396    def handle_entityref(self, name):
    389397        try:
    390             text = unichr(htmlentitydefs.name2codepoint[name])
     398            text = unichr(entities.name2codepoint[name])
    391399        except KeyError:
    392400            text = '&%s;' % name
     
    422430                        fails
    423431    """
    424     return Stream(list(HTMLParser(StringIO(text), encoding=encoding)))
     432    return Stream(list(HTMLParser(BytesIO(text), encoding=encoding)))
    425433
    426434def _coalesce(stream):
Note: See TracChangeset for help on using the changeset viewer.