changeset 859:fbe34d12acde

More bits of 2to3 related cleanup.
author cmlenz
date Thu, 12 Nov 2009 17:01:52 +0000
parents df860bdad9ca
children 61d37796da98
files genshi/input.py genshi/template/interpolation.py genshi/template/plugin.py genshi/util.py
diffstat 4 files changed, 21 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/input.py
+++ b/genshi/input.py
@@ -16,26 +16,19 @@
 """
 
 from itertools import chain
-try:
-    import htmlentitydefs as entities
-    import HTMLParser as html
-except ImportError:
-    from html import entities
-    from html import parser as html
-try:
-    from StringIO import StringIO
-    BytesIO = StringIO
-except ImportError:
-    from io import BytesIO, StringIO
+import htmlentitydefs as entities
+import HTMLParser as html
+from StringIO import StringIO
 from xml.parsers import expat
 
 from genshi.core import Attrs, QName, Stream, stripentities
-from genshi.core import START, END, XML_DECL, DOCTYPE, TEXT, START_NS, END_NS, \
-                        START_CDATA, END_CDATA, PI, COMMENT
+from genshi.core import START, END, XML_DECL, DOCTYPE, TEXT, START_NS, \
+                        END_NS, START_CDATA, END_CDATA, PI, COMMENT
 
 __all__ = ['ET', 'ParseError', 'XMLParser', 'XML', 'HTMLParser', 'HTML']
 __docformat__ = 'restructuredtext en'
 
+
 def ET(element):
     """Convert a given ElementTree element to a markup stream.
     
@@ -177,7 +170,7 @@
 
     def _build_foreign(self, context, base, sysid, pubid):
         parser = self.expat.ExternalEntityParserCreate(context)
-        parser.ParseFile(BytesIO(self._external_dtd))
+        parser.ParseFile(StringIO(self._external_dtd))
         return 1
 
     def _enqueue(self, kind, data=None, pos=None):
@@ -275,7 +268,7 @@
     :return: the parsed XML event stream
     :raises ParseError: if the XML text is not well-formed
     """
-    return Stream(list(XMLParser(BytesIO(text))))
+    return Stream(list(XMLParser(StringIO(text))))
 
 
 class HTMLParser(html.HTMLParser, object):
@@ -429,7 +422,8 @@
     :raises ParseError: if the HTML text is not well-formed, and error recovery
                         fails
     """
-    return Stream(list(HTMLParser(BytesIO(text), encoding=encoding)))
+    return Stream(list(HTMLParser(StringIO(text), encoding=encoding)))
+
 
 def _coalesce(stream):
     """Coalesces adjacent TEXT events into a single event."""
--- a/genshi/template/interpolation.py
+++ b/genshi/template/interpolation.py
@@ -36,6 +36,7 @@
     PseudoToken
 ))
 
+
 def interpolate(text, filepath=None, lineno=-1, offset=0, lookup='strict'):
     """Parse the given string and extract expressions.
     
@@ -45,9 +46,9 @@
     
     >>> for kind, data, pos in interpolate("hey ${foo}bar"):
     ...     print('%s %r' % (kind, data))
-    TEXT u'hey '
+    TEXT 'hey '
     EXPR Expression('foo')
-    TEXT u'bar'
+    TEXT 'bar'
     
     :param text: the text to parse
     :param filepath: absolute path to the file in which the text was found
@@ -68,7 +69,7 @@
     for is_expr, chunk in chain(lex(text, pos, filepath), [(True, '')]):
         if is_expr:
             if textbuf:
-                yield TEXT, u''.join(textbuf), textpos
+                yield TEXT, ''.join(textbuf), textpos
                 del textbuf[:]
                 textpos = None
             if chunk:
@@ -91,6 +92,7 @@
         else:
             pos[2] += len(chunk)
 
+
 def lex(text, textpos, filepath):
     offset = pos = 0
     end = len(text)
--- a/genshi/template/plugin.py
+++ b/genshi/template/plugin.py
@@ -72,7 +72,7 @@
             raise ConfigurationError('Invalid value for allow_exec "%s"' %
                                      options.get('genshi.allow_exec'))
 
-        self.loader = TemplateLoader(filter(None, search_path),
+        self.loader = TemplateLoader([p for p in search_path if p],
                                      auto_reload=auto_reload,
                                      max_cache_size=max_cache_size,
                                      default_class=self.template_class,
--- a/genshi/util.py
+++ b/genshi/util.py
@@ -13,10 +13,7 @@
 
 """Various utility classes and functions."""
 
-try:
-    import htmlentitydefs as entities
-except ImportError:
-    from html import entities
+import htmlentitydefs as entities
 import re
 
 __docformat__ = 'restructuredtext en'
@@ -251,12 +248,11 @@
 
 
 def stringrepr(string):
-    slen = len(string)
     ascii = string.encode('ascii', 'backslashreplace')
-    r = "'" +  ascii.replace("'", "\\'") + "'"
-    if (unicode is not str) and (len(ascii) > len(string)):
-        return 'u' + r
-    return r
+    quoted = "'" +  ascii.replace("'", "\\'") + "'"
+    if len(ascii) > len(string):
+        return 'u' + quoted
+    return quoted
 
 
 # Compatibility fallback implementations for older Python versions
Copyright (C) 2012-2017 Edgewall Software