1. Wolfgang Scherer
  2. WebHelpersWs

Commits

wolf...@gmx.de  committed 93af082

Testsuite completes successfully with both Python2 and Python3 without any modifications (especially without 2to3).

  • Participants
  • Parent commits 1613373
  • Branches trunk

Comments (0)

Files changed (25)

File tests/test_containers.py

View file
 
 from util import raises
 
+from webhelpers.compat import *
 from webhelpers.containers import DumbObject
 from webhelpers.containers import defaultdict as webhelpers_containers_defaultdict
 from webhelpers.containers import distribute, get_many
     eq_(d2.default_factory, None)
     try:
         d2[15]
-    except KeyError, err:
+    except KeyError:
+        import sys
+        (t, err, tb) = sys.exc_info()
+        del(tb)
         eq_(err.args, (15,))
     else:
         message = "d2[15] didn't raise KeyError"
     try:
         f = open(tfn, "w+")
         try:
-            print >>f, d1
-            print >>f, d2
+            printf(d1, **{'file': f})
+            printf(d2, **{'file': f})
             f.seek(0)
             eq_(f.readline(), repr(d1) + "\n")
             eq_(f.readline(), repr(d2) + "\n")

File tests/test_converters.py

View file
 # -*- coding: utf-8 -*-
 from nose.tools import eq_
 
+from webhelpers.compat import *
 from webhelpers.html import HTML, literal
 from webhelpers.html.converters import *
 from webhelpers.html.converters import _get_markdown_module
 [Michel Fortin][] has ported to Markdown to PHP.
     """
     if _markdown.version_info >= (2, 0):
-        expected =  literal(u'<h2>Introduction</h2>\n<p>Markdown is a text-to-HTML conversion tool for web writers.</p>\n<h2>Acknowledgements <a id="acknowledgements" /></h2>\n<p>[Michel Fortin][] has ported to Markdown to PHP.</p>')
+        expected =  literal(ucs('<h2>Introduction</h2>\n<p>Markdown is a text-to-HTML conversion tool for web writers.</p>\n<h2>Acknowledgements <a id="acknowledgements" /></h2>\n<p>[Michel Fortin][] has ported to Markdown to PHP.</p>'))
     else:
-        expected =  literal(u'<h2>Introduction</h2>\n<p>Markdown is a text-to-HTML conversion tool for web writers.\n</p>\n\n<h2>Acknowledgements <a id="acknowledgements" /></h2>\n<p>[Michel Fortin][] has ported to Markdown to PHP.\n</p>')
+        expected =  literal(ucs('<h2>Introduction</h2>\n<p>Markdown is a text-to-HTML conversion tool for web writers.\n</p>\n\n<h2>Acknowledgements <a id="acknowledgements" /></h2>\n<p>[Michel Fortin][] has ported to Markdown to PHP.\n</p>'))
     eq_(markdown(markdown_text, markdown=_markdown), expected)
 
 def test_markdown_embedded_html():
     _markdown = _get_markdown_module()
     markdown_text = 'This text includes <script>Javascript</script> & stuff.'
     if _markdown.version_info >= (2, 0):
-        expected = literal(u'<p>This text includes &lt;script&gt;Javascript&lt;/script&gt; &amp; stuff.</p>')
+        expected = literal(ucs('<p>This text includes &lt;script&gt;Javascript&lt;/script&gt; &amp; stuff.</p>'))
     else:
-        expected = literal(u'<p>This text includes &lt;script&gt;Javascript&lt;/script&gt; &amp; stuff.\n</p>')
+        expected = literal(ucs('<p>This text includes &lt;script&gt;Javascript&lt;/script&gt; &amp; stuff.\n</p>'))
     eq_(markdown(markdown_text, safe_mode="escape", markdown=_markdown), expected)
 
 
 def test_nl2br():
-    eq_(u'A B<br />\nC D<br />\n<br />\nE F', nl2br("A B\nC D\r\n\r\nE F"))
+    eq_(ucs('A B<br />\nC D<br />\n<br />\nE F'), nl2br("A B\nC D\r\n\r\nE F"))
 
 def test_nl2br2():
-    eq_(u'&lt;strike&gt;W&lt;/strike&gt;<br />\nThe W', nl2br("<strike>W</strike>\nThe W"))
+    eq_(ucs('&lt;strike&gt;W&lt;/strike&gt;<br />\nThe W'), nl2br("<strike>W</strike>\nThe W"))
 
 def test_nl2br3():
-    eq_(u'<strike>W</strike><br />\nThe W', nl2br(literal("<strike>W</strike>\nThe W")))
+    eq_(ucs('<strike>W</strike><br />\nThe W'), nl2br(literal("<strike>W</strike>\nThe W")))
 
 def test_format_paragraphs1():
-    eq_(u"<p>crazy\n cross\n platform linebreaks</p>", format_paragraphs("crazy\r\n cross\r platform linebreaks"))
+    eq_(ucs("<p>crazy\n cross\n platform linebreaks</p>"), format_paragraphs("crazy\r\n cross\r platform linebreaks"))
 
 def test_format_paragraphs2():
-    eq_(u"<p>crazy<br />\n cross<br />\n platform linebreaks</p>", format_paragraphs("crazy\r\n cross\r platform linebreaks", True))
+    eq_(ucs("<p>crazy<br />\n cross<br />\n platform linebreaks</p>"), format_paragraphs("crazy\r\n cross\r platform linebreaks", True))
 
 def test_format_paragraphs3():
-    eq_(u"<p>A paragraph</p>\n\n<p>and another one!</p>", format_paragraphs("A paragraph\n\nand another one!"))
+    eq_(ucs("<p>A paragraph</p>\n\n<p>and another one!</p>"), format_paragraphs("A paragraph\n\nand another one!"))
 
 def test_format_paragraphs4():
-    eq_(u"<p>A paragraph<br />\n With a newline</p>", format_paragraphs("A paragraph\n With a newline", True))
+    eq_(ucs("<p>A paragraph<br />\n With a newline</p>"), format_paragraphs("A paragraph\n With a newline", True))
 
 def test_format_paragraphs5():
-    eq_(u"<p>A paragraph\n With a newline</p>", format_paragraphs("A paragraph\n With a newline", False))
+    eq_(ucs("<p>A paragraph\n With a newline</p>"), format_paragraphs("A paragraph\n With a newline", False))
 
 def test_format_paragraphs6():
-    eq_(u"<p>A paragraph\n With a newline</p>", format_paragraphs("A paragraph\n With a newline"))
+    eq_(ucs("<p>A paragraph\n With a newline</p>"), format_paragraphs("A paragraph\n With a newline"))
 
 def test_format_paragraphs7():
-    eq_(u"", format_paragraphs(None))
+    eq_(ucs(""), format_paragraphs(None))

File tests/test_escapes.py

View file
 from webhelpers.util import *
 
 def test_html_escape():
-    assert html_escape('foo') == 'foo'
-    assert html_escape('<this"that>') == '&lt;this&quot;that&gt;'
-    assert html_escape(u'\u1000') == '&#4096;'
+    assert html_escape('foo') == u8s('foo')
+    assert html_escape('<this"that>') == u8s('&lt;this&quot;that&gt;')
+    # portable u'\u1000', '\u1000'
+    uchar = eval(sformat(UCHAR_FMT,'\\', 0x1000))
+    assert html_escape(uchar) == u8s('&#4096;')
     class X:
         def __unicode__(self):
-            return u'<\u1000>'
-    assert html_escape(X()) == '&lt;&#4096;&gt;'
-    assert html_escape(1) == '1'
-    assert html_escape(None) == ''
+            return ucs('<' + uchar + '>')
+    assert html_escape(X()) == u8s('&lt;&#4096;&gt;')
+    assert html_escape(1) == u8s('1')
+    assert html_escape(None) == u8s('')

File tests/test_feedgenerator.py

View file
 import datetime
 
 from nose.tools import eq_
-
+from webhelpers.compat import *
 import webhelpers.feedgenerator as fg
 
 def test_simple_feed():
     pubdate = datetime.datetime(2009, 12, 18, 23, 45, 12)
     feed = fg.Rss201rev2Feed(
-        title=u"Poynter E-Media Tidbits",
-        link=u"http://www.poynter.org/column.asp?id=31",
-        description=u"A group weblog by the sharpest minds in online media/journalism/publishing.",
-        language=u"en",
+        title=ucs("Poynter E-Media Tidbits"),
+        link=ucs("http://www.poynter.org/column.asp?id=31"),
+        description=ucs("A group weblog by the sharpest minds in online media/journalism/publishing."),
+        language=ucs("en"),
     )
     feed.add_item(
         title="Hello", 
-        link=u"http://www.holovaty.com/test/",
+        link=ucs("http://www.holovaty.com/test/"),
         description="Testing.",  
         pubdate=pubdate)
     result = feed.writeString("utf-8")
 def test_escaping():
     pubdate = datetime.datetime(2009, 12, 18, 23, 45, 12)
     feed = fg.Rss201rev2Feed(
-        title=u"Poynter E-Media Tidbits",
-        link=u"http://www.poynter.org/column.asp?id=31",
-        description=u"A group weblog by the <em>sharpest</em> minds in online media & journalism.",
-        language=u"en",
+        title=ucs("Poynter E-Media Tidbits"),
+        link=ucs("http://www.poynter.org/column.asp?id=31"),
+        description=ucs("A group weblog by the <em>sharpest</em> minds in online media & journalism."),
+        language=ucs("en"),
     )
     feed.add_item(
         title="Hello", 
-        link=u"http://www.holovaty.com/test/",
+        link=ucs("http://www.holovaty.com/test/"),
         description="Testing.",  
         pubdate=pubdate)
     result = feed.writeString("utf-8")
 def test_geo_point_feed():
     pubdate = datetime.datetime(2009, 12, 18, 23, 45, 12)
     feed = fg.GeoAtom1Feed(
-        title=u"Poynter E-Media Tidbits",
-        link=u"http://www.poynter.org/column.asp?id=31",
-        description=u"A group weblog by the sharpest minds in online media/journalism/publishing.",
-        language=u"en",
+        title=ucs("Poynter E-Media Tidbits"),
+        link=ucs("http://www.poynter.org/column.asp?id=31"),
+        description=ucs("A group weblog by the sharpest minds in online media/journalism/publishing."),
+        language=ucs("en"),
     )
     feed.add_item(
         title="Hello", 
-        link=u"http://www.holovaty.com/test/",
+        link=ucs("http://www.holovaty.com/test/"),
         description="Testing.",  
         pubdate=pubdate,
         geometry=(-120.5, 50.5))
 def test_geo_point_feed_longitude_first():
     pubdate = datetime.datetime(2009, 12, 18, 23, 45, 12)
     feed = fg.GeoAtom1Feed(
-        title=u"Poynter E-Media Tidbits",
-        link=u"http://www.poynter.org/column.asp?id=31",
-        description=u"A group weblog by the sharpest minds in online media/journalism/publishing.",
-        language=u"en",
+        title=ucs("Poynter E-Media Tidbits"),
+        link=ucs("http://www.poynter.org/column.asp?id=31"),
+        description=ucs("A group weblog by the sharpest minds in online media/journalism/publishing."),
+        language=ucs("en"),
     )
     feed.is_input_latitude_first = False
     feed.add_item(
         title="Hello", 
-        link=u"http://www.holovaty.com/test/",
+        link=ucs("http://www.holovaty.com/test/"),
         description="Testing.",  
         pubdate=pubdate,
         geometry=(50.5, -120.5))

File tests/test_html.py

View file
 from nose.tools import eq_
 
+from webhelpers.compat import *
 from webhelpers.html import literal, lit_sub, escape, HTML
 
 def test_double_escape():
-    quoted = escape(u'This string is "quoted"')
-    eq_(quoted, u'This string is &#34;quoted&#34;')
+    quoted = escape(ucs('This string is "quoted"'))
+    eq_(quoted, ucs('This string is &#34;quoted&#34;'))
     dbl_quoted = escape(quoted)
     eq_(quoted, dbl_quoted)
 
 def test_literal():
-    lit = literal(u'This string <>')
-    other = literal(u'<other>')
-    eq_(u'This string <><other>', lit + other)
+    lit = literal(ucs('This string <>'))
+    other = literal(ucs('<other>'))
+    eq_(ucs('This string <><other>'), lit + other)
     assert type(lit + other) is literal
-    
-    eq_(u'&#34;<other>', '"' + other)
-    eq_(u'<other>&#34;', other + '"')
-    
+
+    eq_(ucs('&#34;<other>'), '"' + other)
+    eq_(ucs('<other>&#34;'), other + '"')
+
     mod = literal('<%s>ello')
-    eq_(u'<&lt;H&gt;>ello', mod % '<H>')
+    eq_(ucs('<&lt;H&gt;>ello'), mod % '<H>')
     assert type(mod % '<H>') is literal
     eq_(HTML('<a>'), '&lt;a&gt;')
     assert type(HTML('<a>')) is literal
 
 def test_literal_dict():
-    lit = literal(u'This string <>')
+    lit = literal(ucs('This string <>'))
     unq = 'This has <crap>'
     sub = literal('%s and %s')
-    eq_(u'This string <> and This has &lt;crap&gt;', sub % (lit, unq))
-    
+    eq_(ucs('This string <> and This has &lt;crap&gt;'), sub % (lit, unq))
+
     sub = literal('%(lit)s and %(lit)r')
-    eq_(u"This string <> and literal(u&#39;This string &lt;&gt;&#39;)", sub % dict(lit=lit))
+    try:
+        unicode('')
+        eq_(ucs("This string <> and literal(u&#39;This string &lt;&gt;&#39;)"), sub % dict(lit=lit))
+    except NameError:
+        eq_(ucs("This string <> and literal(&#39;This string &lt;&gt;&#39;)"), sub % dict(lit=lit))
+
     sub = literal('%(unq)r and %(unq)s')
-    eq_(u"&#39;This has &lt;crap&gt;&#39; and This has &lt;crap&gt;", sub % dict(unq=unq))
+    eq_(ucs("&#39;This has &lt;crap&gt;&#39; and This has &lt;crap&gt;"), sub % dict(unq=unq))
 
 def test_literal_mul():
-    lit = literal(u'<>')
-    eq_(u'<><><>', lit * 3)
+    lit = literal(ucs('<>'))
+    eq_(ucs('<><><>'), lit * 3)
     assert isinstance(lit*3, literal)
 
 def test_literal_join():
-    lit = literal(u'<>')
+    lit = literal(ucs('<>'))
     assert isinstance(lit.join(['f', 'a']), literal)
-    eq_(u'f<>a', lit.join(('f', 'a')))
+    eq_(ucs('f<>a'), lit.join(('f', 'a')))
 
 def test_literal_int():
-    lit = literal(u'<%i>')
-    eq_(u'<5>', lit % 5)
+    lit = literal(ucs('<%i>'))
+    eq_(ucs('<5>'), lit % 5)
 
 def test_html():
     a = HTML.a(href='http://mostlysafe\" <tag', c="Bad <script> tag")
-    eq_(a, u'<a href="http://mostlysafe&#34; &lt;tag">Bad &lt;script&gt; tag</a>')
-    
+    eq_(a, ucs('<a href="http://mostlysafe&#34; &lt;tag">Bad &lt;script&gt; tag</a>'))
+
     img = HTML.img(src='http://some/image.jpg')
-    eq_(img, u'<img src="http://some/image.jpg" />')
-    
+    eq_(img, ucs('<img src="http://some/image.jpg" />'))
+
     br = HTML.br()
-    eq_(u'<br />', br)
+    eq_(ucs('<br />'), br)
 
 def test_lit_re():
     lit = literal('This is a <string>')
     unlit = 'This is also a <string>'
-    
+
     result = lit_sub(r'<str', literal('<b'), lit)
-    eq_(u'This is a <bing>', escape(result))
-    
+    eq_(ucs('This is a <bing>'), escape(result))
+
     result = lit_sub(r'a <str', 'a <b> <b', unlit)
-    eq_(u'This is also a &lt;b&gt; &lt;bing&gt;', escape(result))
+    eq_(ucs('This is also a &lt;b&gt; &lt;bing&gt;'), escape(result))
 
 def test_unclosed_tag():
     result = HTML.form(_closed=False)
-    print result
-    eq_(u'<form>', result)
-    
+    print(result)
+    eq_(ucs('<form>'), result)
+
     result = HTML.form(_closed=False, action="hello")
-    eq_(u'<form action="hello">', result)
+    eq_(ucs('<form action="hello">'), result)
 
 def test_newline_arg():
-    eq_(HTML.a(),         literal(u'<a></a>'))
-    eq_(HTML.a(_nl=True), literal(u'<a>\n</a>\n'))
-    eq_(HTML.a(_closed=False),           literal(u'<a>'))
-    eq_(HTML.a(_closed=False, _nl=True), literal(u'<a>\n'))
-    eq_(HTML.a("A", "B", href="/"),      literal(u'<a href="/">AB</a>'))
-    eq_(HTML.a("A", "B", href="/", _nl=True), literal(u'<a href="/">\nA\nB\n</a>\n'))
+    eq_(HTML.a(),         literal(ucs('<a></a>')))
+    eq_(HTML.a(_nl=True), literal(ucs('<a>\n</a>\n')))
+    eq_(HTML.a(_closed=False),           literal(ucs('<a>')))
+    eq_(HTML.a(_closed=False, _nl=True), literal(ucs('<a>\n')))
+    eq_(HTML.a("A", "B", href="/"),      literal(ucs('<a href="/">AB</a>')))
+    eq_(HTML.a("A", "B", href="/", _nl=True), literal(ucs('<a href="/">\nA\nB\n</a>\n')))

File tests/test_modeltags.py

View file
 # -*- coding: utf-8 -*-
+from webhelpers.compat import *
+
 from util import WebHelpersTestCase
 import unittest
 
 
 class Holder(object):
     def __init__(self, settings):
-        for k,v in settings.iteritems():
+        for k,v in ditems(settings):
             setattr(self, k, v)
             
 class TestModelTagsHelperWithObject(WebHelpersTestCase):
     def test_check_box(self):
         self.assertEqual(
             self.m.checkbox("fulltime"),
-            u'<input checked="checked" id="fulltime" name="fulltime" type="checkbox" value="1" />',
+            ucs('<input checked="checked" id="fulltime" name="fulltime" type="checkbox" value="1" />'),
         )
 
     def test_hidden_field(self):
         self.assertEqual(
             self.m.hidden("name"),
-            u'<input id="name" name="name" type="hidden" value="Jim" />'
+            ucs('<input id="name" name="name" type="hidden" value="Jim" />')
         )
 
     def test_password_field(self):
         self.assertEqual(
             self.m.password('name'), 
-            u'<input id="name" name="name" type="password" value="Jim" />'
+            ucs('<input id="name" name="name" type="password" value="Jim" />')
         )
     def test_file_field(self):
         self.assertEqual(
             self.m.file('name'), 
-            u'<input id="name" name="name" type="file" value="Jim" />'
+            ucs('<input id="name" name="name" type="file" value="Jim" />')
         )
 
     def test_radio_button(self):
         self.assertEqual(
             self.m.radio("favcolor", "blue"),
-            u'<input checked="checked" id="favcolor_blue" name="favcolor" type="radio" value="blue" />'
+            ucs('<input checked="checked" id="favcolor_blue" name="favcolor" type="radio" value="blue" />')
         )
         
         self.assertEqual(
             self.m.radio("favcolor", "red"),
-            u'<input id="favcolor_red" name="favcolor" type="radio" value="red" />'
+            ucs('<input id="favcolor_red" name="favcolor" type="radio" value="red" />')
         )
 
 
     def test_text_area(self):
         self.assertEqual(
             self.m.textarea("longtext"),
-            u'<textarea id="longtext" name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>'
+            ucs('<textarea id="longtext" name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>')
         )
 
     def test_text_field(self):
         self.assertEqual(
             self.m.text("name"),
-            u'<input id="name" name="name" type="text" value="Jim" />'
+            ucs('<input id="name" name="name" type="text" value="Jim" />')
         )
     def test_select(self):
         self.assertEqual(
             self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
-            u'<select id="lang" name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
+            ucs('<select id="lang" name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>')
         )
 
 class TestModelTagsHelperWithDict(TestModelTagsHelperWithObject):
     def test_check_box(self):
         self.assertEqual(
             self.m.checkbox("fulltime"),
-            u'<input checked="checked" id="fulltime" name="fulltime" type="checkbox" value="1" />',
+            ucs('<input checked="checked" id="fulltime" name="fulltime" type="checkbox" value="1" />'),
         )
 
     def test_hidden_field(self):
         self.assertEqual(
             self.m.hidden("name"),
-            u'<input id="name" name="name" type="hidden" value="Jim" />'
+            ucs('<input id="name" name="name" type="hidden" value="Jim" />')
         )
 
     def test_password_field(self):
         self.assertEqual(
             self.m.password('name'), 
-            u'<input id="name" name="name" type="password" value="Jim" />'
+            ucs('<input id="name" name="name" type="password" value="Jim" />')
         )
     def test_file_field(self):
         self.assertEqual(
             self.m.file('name'), 
-            u'<input id="name" name="name" type="file" value="Jim" />'
+            ucs('<input id="name" name="name" type="file" value="Jim" />')
         )
 
     def test_radio_button(self):
         self.assertEqual(
             self.m.radio("favcolor", "blue"),
-            u'<input checked="checked" id="favcolor_blue" name="favcolor" type="radio" value="blue" />'
+            ucs('<input checked="checked" id="favcolor_blue" name="favcolor" type="radio" value="blue" />')
         )
 
         self.assertEqual(
             self.m.radio("favcolor", "red"),
-            u'<input id="favcolor_red" name="favcolor" type="radio" value="red" />'
+            ucs('<input id="favcolor_red" name="favcolor" type="radio" value="red" />')
         )
 
 
     def test_text_area(self):
         self.assertEqual(
             self.m.textarea("longtext"),
-            u'<textarea id="longtext" name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>'
+            ucs('<textarea id="longtext" name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>')
         )
 
     def test_text_field(self):
         self.assertEqual(
             self.m.text("name"),
-            u'<input id="name" name="name" type="text" value="Jim" />'
+            ucs('<input id="name" name="name" type="text" value="Jim" />')
         )
     def test_select(self):
         self.assertEqual(
             self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
-            u'<select id="lang" name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
+            ucs('<select id="lang" name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>')
         )
 
 class TestModelTagsHelperWithIdGeneration(TestModelTagsHelperWithObject):
     def test_check_box(self):
         self.assertEqual(
             self.m.checkbox("fulltime"),
-            u'<input checked="checked" id="person:fulltime" name="fulltime" type="checkbox" value="1" />',
+            ucs('<input checked="checked" id="person:fulltime" name="fulltime" type="checkbox" value="1" />'),
         )
 
     def test_hidden_field(self):
         self.assertEqual(
             self.m.hidden("name"),
-            u'<input id="person:name" name="name" type="hidden" value="Jim" />'
+            ucs('<input id="person:name" name="name" type="hidden" value="Jim" />')
         )
 
     def test_password_field(self):
         self.assertEqual(
             self.m.password('name'), 
-            u'<input id="person:name" name="name" type="password" value="Jim" />'
+            ucs('<input id="person:name" name="name" type="password" value="Jim" />')
         )
     def test_file_field(self):
         self.assertEqual(
             self.m.file('name'), 
-            u'<input id="person:name" name="name" type="file" value="Jim" />'
+            ucs('<input id="person:name" name="name" type="file" value="Jim" />')
         )
 
     def test_radio_button(self):
         self.assertEqual(
             self.m.radio("favcolor", "blue"),
-            u'<input checked="checked" id="person:favcolor_blue" name="favcolor" type="radio" value="blue" />'
+            ucs('<input checked="checked" id="person:favcolor_blue" name="favcolor" type="radio" value="blue" />')
         )
 
         self.assertEqual(
             self.m.radio("favcolor", "red"),
-            u'<input id="person:favcolor_red" name="favcolor" type="radio" value="red" />'
+            ucs('<input id="person:favcolor_red" name="favcolor" type="radio" value="red" />')
         )
 
 
     def test_text_area(self):
         self.assertEqual(
             self.m.textarea("longtext"),
-            u'<textarea id="person:longtext" name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>'
+            ucs('<textarea id="person:longtext" name="longtext">lorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\nlorem ipsum lorem ipsum\n</textarea>')
         )
 
     def test_text_field(self):
         self.assertEqual(
             self.m.text("name"),
-            u'<input id="person:name" name="name" type="text" value="Jim" />'
+            ucs('<input id="person:name" name="name" type="text" value="Jim" />')
         )
     def test_select(self):
         self.assertEqual(
             self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
-            u'<select id="person:lang" name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
+            ucs('<select id="person:lang" name="lang">\n<option selected="selected" value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>')
         )
 
 class TestModelTagsHelperWithoutObject(WebHelpersTestCase):
     def test_check_box(self):
         self.assertEqual(
             self.m.checkbox("fulltime"),
-            u'<input id="fulltime" name="fulltime" type="checkbox" value="1" />',
+            ucs('<input id="fulltime" name="fulltime" type="checkbox" value="1" />'),
         )
 
     def test_hidden_field(self):
         self.assertEqual(
             self.m.hidden("name"),
-            u'<input id="name" name="name" type="hidden" value="" />'
+            ucs('<input id="name" name="name" type="hidden" value="" />')
         )
 
     def test_password_field(self):
         self.assertEqual(
             self.m.password('name'), 
-            u'<input id="name" name="name" type="password" value="" />'
+            ucs('<input id="name" name="name" type="password" value="" />')
         )
     def test_file_field(self):
         self.assertEqual(
             self.m.file('name'), 
-            u'<input id="name" name="name" type="file" value="" />'
+            ucs('<input id="name" name="name" type="file" value="" />')
         )
 
     def test_radio_button(self):
         self.assertEqual(
             self.m.radio("favcolor", "blue"),
-            u'<input id="favcolor_blue" name="favcolor" type="radio" value="blue" />'
+            ucs('<input id="favcolor_blue" name="favcolor" type="radio" value="blue" />')
         )
         
         self.assertEqual(
             self.m.radio("favcolor", "red"),
-            u'<input id="favcolor_red" name="favcolor" type="radio" value="red" />'
+            ucs('<input id="favcolor_red" name="favcolor" type="radio" value="red" />')
         )
 
 
     def test_text_area(self):
         self.assertEqual(
             self.m.textarea("longtext"),
-            u'<textarea id="longtext" name="longtext"></textarea>'
+            ucs('<textarea id="longtext" name="longtext"></textarea>')
         )
 
     def test_text_field(self):
         self.assertEqual(
             self.m.text("name"),
-            u'<input id="name" name="name" type="text" value="" />'
+            ucs('<input id="name" name="name" type="text" value="" />')
         )
     def test_select(self):
         self.assertEqual(
             self.m.select("lang", [("en", "English"), ("de", "German"), ("jp", "Japanese")]),
-            u'<select id="lang" name="lang">\n<option value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>'
+            ucs('<select id="lang" name="lang">\n<option value="en">English</option>\n<option value="de">German</option>\n<option value="jp">Japanese</option>\n</select>')
         )        
 if __name__ == '__main__':
     suite = map(unittest.makeSuite, [

File tests/test_paginate.py

View file
-""""Test webhelpers.paginate package."""
+"""Test webhelpers.paginate package."""
 import sys
 import unittest
 
 import webhelpers.paginate as paginate
 from webhelpers.util import update_params
 
-
 def test_empty_list():
     """Test whether an empty list is handled correctly."""
     items = []
     purl = paginate.PageURL_WebOb(request, qualified=True)
     eq_(purl(2), "http://localhost:5000/articles?blah=boo&page=2")
 
-
 class UnsliceableSequence(object):
    def __init__(self, seq):
       self.l = seq
-   
+
    def __iter__(self):
        for i in self.l:
            yield i
    def __len__(self):
        return len(self.l)
 
-
 class UnsliceableSequence2(UnsliceableSequence):
    def __getitem__(self, key):
         raise TypeError("unhashable type")
     def test_unsliceable_sequence2(self):
         paginate.Page(UnsliceableSequence2(self.rng))
 
-
 class TestSQLAlchemyCollectionTypes(unittest.TestCase):
     def setUp(self):
         try:
         insert = notes.insert()
         records = [{"id": x} for x in range(1, 101)]
         engine.execute(insert, records)
-            
+
     def tearDown(self):
         import sqlalchemy as sa
         import sqlalchemy.orm as orm

File tests/test_tags.py

View file
 # -*- coding: utf-8 -*-
 
+from webhelpers.compat import *
+
 from nose.tools import eq_
 
 from webhelpers.html import HTML, literal
     def test_check_box(self):
         eq_(
             checkbox("admin"),
-            u'<input id="admin" name="admin" type="checkbox" value="1" />',
+            ucs('<input id="admin" name="admin" type="checkbox" value="1" />'),
         )
 
     def test_form(self):
         eq_(
             form(url="http://www.example.com"),
-            u'<form action="http://www.example.com" method="post">'
+            ucs('<form action="http://www.example.com" method="post">')
         )
         eq_(
             form(url="http://www.example.com", method='GET'),
-            u'<form action="http://www.example.com" method="GET">'
+            ucs('<form action="http://www.example.com" method="GET">')
         )
         eq_(
             form('/test/edit/1'),
-            u'<form action="/test/edit/1" method="post">'
+            ucs('<form action="/test/edit/1" method="post">')
         )
 
     def test_form_multipart(self):
         eq_(
             form(url='http://www.example.com', multipart=True),
-            u'<form action="http://www.example.com" enctype="multipart/form-data" method="post">'
+            ucs('<form action="http://www.example.com" enctype="multipart/form-data" method="post">')
         )
         
     def test_hidden_field(self):
         eq_(
             hidden("id", 3),
-            u'<input id="id" name="id" type="hidden" value="3" />'
+            ucs('<input id="id" name="id" type="hidden" value="3" />')
         )
 
     def test_hidden_field_alt(self):
         eq_(
             hidden("id", '3'),
-            u'<input id="id" name="id" type="hidden" value="3" />'
+            ucs('<input id="id" name="id" type="hidden" value="3" />')
         )
 
     def test_password_field(self):
         eq_(
             password("password"), 
-            u'<input id="password" name="password" type="password" />'
+            ucs('<input id="password" name="password" type="password" />')
         )
 
     def test_radio_button(self):
         eq_(
             radio("people", "justin"),
-            u'<input id="people_justin" name="people" type="radio" value="justin" />'
+            ucs('<input id="people_justin" name="people" type="radio" value="justin" />')
         )
         
         eq_(
             radio("num_people", 5),
-            u'<input id="num_people_5" name="num_people" type="radio" value="5" />'
+            ucs('<input id="num_people_5" name="num_people" type="radio" value="5" />')
         )
 
         eq_(
             radio("num_people", 5),
-            u'<input id="num_people_5" name="num_people" type="radio" value="5" />'
+            ucs('<input id="num_people_5" name="num_people" type="radio" value="5" />')
         )
         
         eq_(
             radio("gender", "m") + radio("gender", "f"),
-            u'<input id="gender_m" name="gender" type="radio" value="m" /><input id="gender_f" name="gender" type="radio" value="f" />'
+            ucs('<input id="gender_m" name="gender" type="radio" value="m" /><input id="gender_f" name="gender" type="radio" value="f" />')
         )
         
         eq_(
             radio("opinion", "-1") + radio("opinion", "1"),
-            u'<input id="opinion_-1" name="opinion" type="radio" value="-1" /><input id="opinion_1" name="opinion" type="radio" value="1" />'
+            ucs('<input id="opinion_-1" name="opinion" type="radio" value="-1" /><input id="opinion_1" name="opinion" type="radio" value="1" />')
         )
 
         eq_(
             radio("num_people", 5, checked=True),
-            u'<input checked="checked" id="num_people_5" name="num_people" type="radio" value="5" />'
+            ucs('<input checked="checked" id="num_people_5" name="num_people" type="radio" value="5" />')
         )
 
     def test_submit(self):
         eq_(
-            u'<input id="commit" name="commit" type="submit" value="Save changes" />',
+            ucs('<input id="commit" name="commit" type="submit" value="Save changes" />'),
             submit("commit", "Save changes")
         )
 
     def test_text_area(self):
         eq_(
             textarea("aa", ""),
-            u'<textarea id="aa" name="aa"></textarea>'
+            ucs('<textarea id="aa" name="aa"></textarea>')
         )
         eq_(
             textarea("aa", None),
-            u'<textarea id="aa" name="aa"></textarea>'
+            ucs('<textarea id="aa" name="aa"></textarea>')
         )
         eq_(
             textarea("aa", "Hello!"),
-            u'<textarea id="aa" name="aa">Hello!</textarea>'
+            ucs('<textarea id="aa" name="aa">Hello!</textarea>')
         )
 
     def test_text_area_size_string(self):
         eq_(
             textarea("body", "hello world", cols=20, rows=40),
-            u'<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>'
+            ucs('<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>')
         )
 
     def test_text_field(self):
         eq_(
             text("title", ""),
-            u'<input id="title" name="title" type="text" value="" />'
+            ucs('<input id="title" name="title" type="text" value="" />')
         )
         eq_(
             text("title", None),
-            u'<input id="title" name="title" type="text" />'
+            ucs('<input id="title" name="title" type="text" />')
         )
         eq_(
             text("title", "Hello!"),
-            u'<input id="title" name="title" type="text" value="Hello!" />'
+            ucs('<input id="title" name="title" type="text" value="Hello!" />')
         )
 
     def test_text_field_class_string(self):
         eq_(
             text( "title", "Hello!", class_= "admin"),
-            u'<input class="admin" id="title" name="title" type="text" value="Hello!" />'
+            ucs('<input class="admin" id="title" name="title" type="text" value="Hello!" />')
         )
 
     def test_boolean_options(self):
         eq_(     
             checkbox("admin", 1, True, disabled = True, readonly="yes"),
-            u'<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />'
+            ucs('<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />')
         )
         eq_(
             checkbox("admin", 1, True, disabled = False, readonly = None),
-            u'<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />'
+            ucs('<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />')
         )
 
     def test_multiple_id_bug(self):
         # Don't set multiple id attributes for 'id_' argument.
         eq_(
             text("spam", "pizza", id="eggs"),
-            u'<input id="eggs" name="spam" type="text" value="pizza" />')
+            ucs('<input id="eggs" name="spam" type="text" value="pizza" />'))
         eq_(
             text("spam", "pizza", id_="eggs"), 
-            u'<input id="eggs" name="spam" type="text" value="pizza" />')
+            ucs('<input id="eggs" name="spam" type="text" value="pizza" />'))
         eq_(
             select("spam", [1,2], [2], id="eggs"),
-            u'<select id="eggs" name="spam">\n<option selected="selected" value="2">2</option>\n</select>')
+            ucs('<select id="eggs" name="spam">\n<option selected="selected" value="2">2</option>\n</select>'))
         eq_(
             select("spam", [1,2], [2], id_="eggs"),
-            u'<select id="eggs" name="spam">\n<option selected="selected" value="2">2</option>\n</select>')
+            ucs('<select id="eggs" name="spam">\n<option selected="selected" value="2">2</option>\n</select>'))
 
     def test_id_and_id_(self):
         raises(TypeError, text, "spam", "pizza", id="fubar", id_="eggs")
     
 class TestLinkHelper(object):
     def test_link_tag_with_query(self):
-        eq_(u"<a href=\"http://www.example.com?q1=v1&amp;q2=v2\">Hello</a>", 
+        eq_(ucs("<a href=\"http://www.example.com?q1=v1&amp;q2=v2\">Hello</a>"), 
                link_to("Hello", "http://www.example.com?q1=v1&q2=v2"))
     
     def test_link_tag_with_query_and_no_name(self):
-        eq_(u"<a href=\"http://www.example.com?q1=v1&amp;q2=v2\">http://www.example.com?q1=v1&amp;q2=v2</a>", 
+        eq_(ucs("<a href=\"http://www.example.com?q1=v1&amp;q2=v2\">http://www.example.com?q1=v1&amp;q2=v2</a>"), 
                link_to(None, HTML.literal("http://www.example.com?q1=v1&amp;q2=v2")))
     
     def test_link_tag_with_custom_onclick(self):
-        eq_(u"<a href=\"http://www.example.com\" onclick=\"alert(&#39;yay!&#39;)\">Hello</a>", 
+        eq_(ucs("<a href=\"http://www.example.com\" onclick=\"alert(&#39;yay!&#39;)\">Hello</a>"), 
                link_to("Hello", "http://www.example.com", onclick="alert('yay!')"))
     
 
 class TestAssetTagHelper(object):
     def test_auto_discovery_link_tag(self):
-        eq_(literal(u'<link href="http://feed.com/feed.xml" rel="alternate" title="RSS" type="application/rss+xml" />'),
+        eq_(literal(ucs('<link href="http://feed.com/feed.xml" rel="alternate" title="RSS" type="application/rss+xml" />')),
                          auto_discovery_link('http://feed.com/feed.xml'))
         eq_('<link href="http://feed.com/feed.xml" rel="alternate" title="ATOM" type="application/atom+xml" />',
                          auto_discovery_link('http://feed.com/feed.xml', feed_type='atom'))
                          javascript_link('/js/pngfix.js', defer=True))
 
     def test_stylesheet_link_tag(self):
-        eq_(literal(u'<link href="/dir/file.css" media="all" rel="stylesheet" type="text/css" />'),
+        eq_(literal(ucs('<link href="/dir/file.css" media="all" rel="stylesheet" type="text/css" />')),
                          stylesheet_link('/dir/file.css', media='all'))
         eq_('<link href="style.css" media="all" rel="stylesheet" type="text/css" />',
                          stylesheet_link('style.css', media='all'))

File tests/test_text.py

View file
 # -*- coding: utf-8 -*-
+from webhelpers.compat import *
 from util import WebHelpersTestCase
 import unittest
 
                          excerpt('This is a beautiful? morning', 'beautiful', 5))
 
     def test_excerpt_with_utf8(self):
-        self.assertEqual(u"...fficiency could not be ...",
-                         excerpt(u"That's why efficiency could not be helped", 'could', 8))
+        self.assertEqual(ucs("...fficiency could not be ..."),
+                         excerpt(ucs("That's why efficiency could not be helped"), 'could', 8))
 
 
     def test_truncate(self):

File tests/test_tools.py

View file
 # -*- coding: utf-8 -*-
 # Relative import -- must be first in file to avoid ImportError
+from webhelpers.compat import *
 from util import WebHelpersTestCase
 
 import re
             }
 
         result_values = {}
-        for k, v in result_values_templates.iteritems():
+        for k, v in result_values_templates.items():
             result_values[k] = Template(v).substitute(raw_values)
 
         self.assertEqual(result_values["email_result"], auto_link(raw_values['email_raw'], 'email_addresses'))
-        self.assertEqual(u"hello %(email_result)s" % result_values, auto_link("hello %(email_raw)s" % raw_values, 'email_addresses'))
-        self.assertEqual(u"Go to %(link_result)s" % result_values, auto_link("Go to %(link_raw)s" % raw_values, 'urls'))
-        self.assertEqual(u"Go to %(link_raw)s" % raw_values, auto_link("Go to %(link_raw)s" % raw_values, 'email_addresses'))
-        self.assertEqual(u"Go to %(link_result)s and say hello to %(email_result)s" % result_values, auto_link("Go to %(link_raw)s and say hello to %(email_raw)s" % raw_values))
-        self.assertEqual(u"<p>Link %(link_result)s</p>" % result_values, auto_link(literal("<p>Link %(link_raw)s</p>") % raw_values))
-        self.assertEqual(u"<p>%(link_result)s Link</p>" % result_values, auto_link(literal("<p>%(link_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"<p>Link %(link_result_with_options)s</p>" % result_values, auto_link(literal("<p>Link %(link_raw)s</p>") % raw_values, 'all', target='_blank'))
-        self.assertEqual(u"Go to %(link_result)s." % result_values, auto_link("Go to %(link_raw)s." % raw_values))
-        self.assertEqual(u"<p>Go to %(link_result)s, then say hello to %(email_result)s.</p>" % result_values, auto_link(literal("<p>Go to %(link_raw)s, then say hello to %(email_raw)s.</p>") % raw_values))
-        self.assertEqual(u"Go to %(link2_result)s" % result_values, auto_link("Go to %(link2_raw)s" % raw_values, 'urls'))
-        self.assertEqual(u"Go to %(link2_raw)s" % raw_values, auto_link("Go to %(link2_raw)s" % raw_values, 'email_addresses'))
-        self.assertEqual(u"<p>Link %(link2_result)s</p>" % result_values, auto_link(literal("<p>Link %(link2_raw)s</p>") % raw_values))
-        self.assertEqual(u"<p>%(link2_result)s Link</p>" % result_values, auto_link(literal("<p>%(link2_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"Go to %(link2_result)s." % result_values, auto_link(literal("Go to %(link2_raw)s.") % raw_values))
-        self.assertEqual(u"<p>Say hello to %(email_result)s, then go to %(link2_result)s.</p>" % result_values, auto_link(literal("<p>Say hello to %(email_raw)s, then go to %(link2_raw)s.</p>") % raw_values))
-        self.assertEqual(u"Go to %(link3_result)s" % result_values, auto_link("Go to %(link3_raw)s" % raw_values, 'urls'))
-        self.assertEqual(u"Go to %(link3_raw)s" % raw_values, auto_link("Go to %(link3_raw)s" % raw_values, 'email_addresses'))
-        self.assertEqual(u"<p>Link %(link3_result)s</p>" % result_values, auto_link(literal("<p>Link %(link3_raw)s</p>") % raw_values))
-        self.assertEqual(u"<p>%(link3_result)s Link</p>" % result_values, auto_link(literal("<p>%(link3_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"Go to %(link3_result)s." % result_values, auto_link("Go to %(link3_raw)s." % raw_values))
-        self.assertEqual(u"<p>Go to %(link3_result)s. seriously, %(link3_result)s? i think I'll say hello to %(email_result)s. instead.</p>" % result_values, auto_link(literal("<p>Go to %(link3_raw)s. seriously, %(link3_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>") % raw_values))
-        self.assertEqual(u"<p>Link %(link4_result)s</p>" % result_values, auto_link(literal("<p>Link %(link4_raw)s</p>") % raw_values))
-        self.assertEqual(u"<p>%(link4_result)s Link</p>" % result_values, auto_link(literal("<p>%(link4_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"<p>%(link5_result)s Link</p>" % result_values, auto_link(literal("<p>%(link5_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"<p>%(link6_result)s Link</p>" % result_values, auto_link(literal("<p>%(link6_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"<p>%(link7_result)s Link</p>" % result_values, auto_link(literal("<p>%(link7_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"Go to %(link8_result)s" % result_values, auto_link("Go to %(link8_raw)s" % raw_values, 'urls'))
-        self.assertEqual(u"Go to %(link8_raw)s" % raw_values, auto_link("Go to %(link8_raw)s" % raw_values, 'email_addresses'))
-        self.assertEqual(u"<p>Link %(link8_result)s</p>" % result_values, auto_link(literal("<p>Link %(link8_raw)s</p>") % raw_values))
-        self.assertEqual(u"<p>%(link8_result)s Link</p>" % result_values, auto_link(literal("<p>%(link8_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"Go to %(link8_result)s." % result_values, auto_link("Go to %(link8_raw)s." % raw_values))
-        self.assertEqual(u"<p>Go to %(link8_result)s. seriously, %(link8_result)s? i think I'll say hello to %(email_result)s. instead.</p>" % result_values, auto_link(literal("<p>Go to %(link8_raw)s. seriously, %(link8_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>") % raw_values))
-        self.assertEqual(u"Go to %(link9_result)s" % result_values, auto_link("Go to %(link9_raw)s" % raw_values, 'urls'))
-        self.assertEqual(u"Go to %(link9_raw)s" % raw_values, auto_link("Go to %(link9_raw)s" % raw_values, 'email_addresses'))
-        self.assertEqual(u"<p>Link %(link9_result)s</p>" % result_values, auto_link(literal("<p>Link %(link9_raw)s</p>") % raw_values))
-        self.assertEqual(u"<p>%(link9_result)s Link</p>" % result_values, auto_link(literal("<p>%(link9_raw)s Link</p>") % raw_values))
-        self.assertEqual(u"Go to %(link9_result)s." % result_values, auto_link("Go to %(link9_raw)s." % raw_values))
-        self.assertEqual(u"<p>Go to %(link9_result)s. seriously, %(link9_result)s? i think I'll say hello to %(email_result)s. instead.</p>" % result_values, auto_link(literal("<p>Go to %(link9_raw)s. seriously, %(link9_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>") % raw_values))
-        self.assertEqual(u"", auto_link(None))
-        self.assertEqual(u"", auto_link(""))
+        self.assertEqual(ucs("hello %(email_result)s") % result_values, auto_link("hello %(email_raw)s" % raw_values, 'email_addresses'))
+        self.assertEqual(ucs("Go to %(link_result)s") % result_values, auto_link("Go to %(link_raw)s" % raw_values, 'urls'))
+        self.assertEqual(ucs("Go to %(link_raw)s") % raw_values, auto_link("Go to %(link_raw)s" % raw_values, 'email_addresses'))
+        self.assertEqual(ucs("Go to %(link_result)s and say hello to %(email_result)s") % result_values, auto_link("Go to %(link_raw)s and say hello to %(email_raw)s" % raw_values))
+        self.assertEqual(ucs("<p>Link %(link_result)s</p>") % result_values, auto_link(literal("<p>Link %(link_raw)s</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link_result)s Link</p>") % result_values, auto_link(literal("<p>%(link_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("<p>Link %(link_result_with_options)s</p>") % result_values, auto_link(literal("<p>Link %(link_raw)s</p>") % raw_values, 'all', target='_blank'))
+        self.assertEqual(ucs("Go to %(link_result)s.") % result_values, auto_link("Go to %(link_raw)s." % raw_values))
+        self.assertEqual(ucs("<p>Go to %(link_result)s, then say hello to %(email_result)s.</p>") % result_values, auto_link(literal("<p>Go to %(link_raw)s, then say hello to %(email_raw)s.</p>") % raw_values))
+        self.assertEqual(ucs("Go to %(link2_result)s") % result_values, auto_link("Go to %(link2_raw)s" % raw_values, 'urls'))
+        self.assertEqual(ucs("Go to %(link2_raw)s") % raw_values, auto_link("Go to %(link2_raw)s" % raw_values, 'email_addresses'))
+        self.assertEqual(ucs("<p>Link %(link2_result)s</p>") % result_values, auto_link(literal("<p>Link %(link2_raw)s</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link2_result)s Link</p>") % result_values, auto_link(literal("<p>%(link2_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("Go to %(link2_result)s.") % result_values, auto_link(literal("Go to %(link2_raw)s.") % raw_values))
+        self.assertEqual(ucs("<p>Say hello to %(email_result)s, then go to %(link2_result)s.</p>") % result_values, auto_link(literal("<p>Say hello to %(email_raw)s, then go to %(link2_raw)s.</p>") % raw_values))
+        self.assertEqual(ucs("Go to %(link3_result)s") % result_values, auto_link("Go to %(link3_raw)s" % raw_values, 'urls'))
+        self.assertEqual(ucs("Go to %(link3_raw)s") % raw_values, auto_link("Go to %(link3_raw)s" % raw_values, 'email_addresses'))
+        self.assertEqual(ucs("<p>Link %(link3_result)s</p>") % result_values, auto_link(literal("<p>Link %(link3_raw)s</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link3_result)s Link</p>") % result_values, auto_link(literal("<p>%(link3_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("Go to %(link3_result)s.") % result_values, auto_link("Go to %(link3_raw)s." % raw_values))
+        self.assertEqual(ucs("<p>Go to %(link3_result)s. seriously, %(link3_result)s? i think I'll say hello to %(email_result)s. instead.</p>") % result_values, auto_link(literal("<p>Go to %(link3_raw)s. seriously, %(link3_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>") % raw_values))
+        self.assertEqual(ucs("<p>Link %(link4_result)s</p>") % result_values, auto_link(literal("<p>Link %(link4_raw)s</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link4_result)s Link</p>") % result_values, auto_link(literal("<p>%(link4_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link5_result)s Link</p>") % result_values, auto_link(literal("<p>%(link5_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link6_result)s Link</p>") % result_values, auto_link(literal("<p>%(link6_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link7_result)s Link</p>") % result_values, auto_link(literal("<p>%(link7_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("Go to %(link8_result)s") % result_values, auto_link("Go to %(link8_raw)s" % raw_values, 'urls'))
+        self.assertEqual(ucs("Go to %(link8_raw)s") % raw_values, auto_link("Go to %(link8_raw)s" % raw_values, 'email_addresses'))
+        self.assertEqual(ucs("<p>Link %(link8_result)s</p>") % result_values, auto_link(literal("<p>Link %(link8_raw)s</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link8_result)s Link</p>") % result_values, auto_link(literal("<p>%(link8_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("Go to %(link8_result)s.") % result_values, auto_link("Go to %(link8_raw)s." % raw_values))
+        self.assertEqual(ucs("<p>Go to %(link8_result)s. seriously, %(link8_result)s? i think I'll say hello to %(email_result)s. instead.</p>") % result_values, auto_link(literal("<p>Go to %(link8_raw)s. seriously, %(link8_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>") % raw_values))
+        self.assertEqual(ucs("Go to %(link9_result)s") % result_values, auto_link("Go to %(link9_raw)s" % raw_values, 'urls'))
+        self.assertEqual(ucs("Go to %(link9_raw)s") % raw_values, auto_link("Go to %(link9_raw)s" % raw_values, 'email_addresses'))
+        self.assertEqual(ucs("<p>Link %(link9_result)s</p>") % result_values, auto_link(literal("<p>Link %(link9_raw)s</p>") % raw_values))
+        self.assertEqual(ucs("<p>%(link9_result)s Link</p>") % result_values, auto_link(literal("<p>%(link9_raw)s Link</p>") % raw_values))
+        self.assertEqual(ucs("Go to %(link9_result)s.") % result_values, auto_link("Go to %(link9_raw)s." % raw_values))
+        self.assertEqual(ucs("<p>Go to %(link9_result)s. seriously, %(link9_result)s? i think I'll say hello to %(email_result)s. instead.</p>") % result_values, auto_link(literal("<p>Go to %(link9_raw)s. seriously, %(link9_raw)s? i think I'll say hello to %(email_raw)s. instead.</p>") % raw_values))
+        self.assertEqual(ucs(""), auto_link(None))
+        self.assertEqual(ucs(""), auto_link(""))
         # Failing test: PylonsHQ bug #657
-        #self.assertEqual(u'&lt;<a href="http://www.google.com">www.google.com</a>&gt;', auto_link("<www.google.com>"))
+        #self.assertEqual(ucs('&lt;<a href="http://www.google.com">www.google.com</a>&gt;'), auto_link("<www.google.com>"))
 
     def test_strip_links(self):
         self.assertEqual("on my mind", strip_links("<a href='almost'>on my mind</a>"))
 
 class TestURLHelper(WebHelpersTestCase):
     def test_button_to_with_straight_url(self):
-        self.assertEqual(u"<form action=\"http://www.example.com\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", 
+        self.assertEqual(ucs("<form action=\"http://www.example.com\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"Hello\" /></div></form>"), 
                button_to("Hello", "http://www.example.com"))
 
     def test_button_to_with_query(self):
-        self.assertEqual(u"<form action=\"http://www.example.com/q1=v1&amp;q2=v2\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", 
+        self.assertEqual(ucs("<form action=\"http://www.example.com/q1=v1&amp;q2=v2\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"Hello\" /></div></form>"), 
                button_to("Hello", "http://www.example.com/q1=v1&q2=v2"))
 
     def test_button_to_with_escaped_query(self):
-        self.assertEqual(u"<form action=\"http://www.example.com/q1=v1&amp;q2=v2\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
+        self.assertEqual(ucs("<form action=\"http://www.example.com/q1=v1&amp;q2=v2\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"Hello\" /></div></form>"),
                          button_to("Hello", "http://www.example.com/q1=v1&q2=v2"))
     
     def test_button_to_with_query_and_no_name(self):
-        self.assertEqual(u"<form action=\"http://www.example.com?q1=v1&amp;q2=v2\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"http://www.example.com?q1=v1&amp;q2=v2\" /></div></form>", 
+        self.assertEqual(ucs("<form action=\"http://www.example.com?q1=v1&amp;q2=v2\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"http://www.example.com?q1=v1&amp;q2=v2\" /></div></form>"), 
                button_to(None, "http://www.example.com?q1=v1&q2=v2"))
     
     def test_button_to_enabled_disabled(self):
-        self.assertEqual(u"<form action=\"http://www.example.com\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
+        self.assertEqual(ucs("<form action=\"http://www.example.com\" class=\"button-to\" method=\"POST\"><div><input type=\"submit\" value=\"Hello\" /></div></form>"),
                button_to("Hello", "http://www.example.com", disabled=False))
-        self.assertEqual(u"<form action=\"http://www.example.com\" class=\"button-to\" method=\"POST\"><div><input disabled=\"disabled\" type=\"submit\" value=\"Hello\" /></div></form>",
+        self.assertEqual(ucs("<form action=\"http://www.example.com\" class=\"button-to\" method=\"POST\"><div><input disabled=\"disabled\" type=\"submit\" value=\"Hello\" /></div></form>"),
                button_to("Hello", "http://www.example.com", disabled=True))
     
     def test_button_to_with_method_delete(self):
-        self.assertEqual(u"<form action=\"http://www.example.com\" class=\"button-to\" method=\"POST\"><div><input id=\"_method\" name=\"_method\" type=\"hidden\" value=\"DELETE\" /><input type=\"submit\" value=\"Hello\" /></div></form>", 
+        self.assertEqual(ucs("<form action=\"http://www.example.com\" class=\"button-to\" method=\"POST\"><div><input id=\"_method\" name=\"_method\" type=\"hidden\" value=\"DELETE\" /><input type=\"submit\" value=\"Hello\" /></div></form>"), 
             button_to("Hello", "http://www.example.com", method='DELETE'))
-        self.assertEqual(u"<form action=\"http://www.example.com\" class=\"button-to\" method=\"post\"><div><input id=\"_method\" name=\"_method\" type=\"hidden\" value=\"delete\" /><input type=\"submit\" value=\"Hello\" /></div></form>", 
+        self.assertEqual(ucs("<form action=\"http://www.example.com\" class=\"button-to\" method=\"post\"><div><input id=\"_method\" name=\"_method\" type=\"hidden\" value=\"delete\" /><input type=\"submit\" value=\"Hello\" /></div></form>"), 
             button_to("Hello", "http://www.example.com", method='delete'))
 
     def test_button_to_with_method_get(self):
-        self.assertEqual(u"<form action=\"http://www.example.com\" class=\"button-to\" method=\"get\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
+        self.assertEqual(ucs("<form action=\"http://www.example.com\" class=\"button-to\" method=\"get\"><div><input type=\"submit\" value=\"Hello\" /></div></form>"),
             button_to("Hello", "http://www.example.com", method='get'))
-        self.assertEqual(u"<form action=\"http://www.example.com\" class=\"button-to\" method=\"GET\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
+        self.assertEqual(ucs("<form action=\"http://www.example.com\" class=\"button-to\" method=\"GET\"><div><input type=\"submit\" value=\"Hello\" /></div></form>"),
             button_to("Hello", "http://www.example.com", method='GET'))
 
     def test_button_to_with_img(self):
-        self.assertEqual(u'<form action="/content/edit/3" class="button-to" method="POST"><div><input alt="Edit" src="/images/icon_delete.gif" type="image" value="Edit" /></div></form>',
+        self.assertEqual(ucs('<form action="/content/edit/3" class="button-to" method="POST"><div><input alt="Edit" src="/images/icon_delete.gif" type="image" value="Edit" /></div></form>'),
                          button_to("Edit", '/content/edit/3', type='image', src='/images/icon_delete.gif'))
-        self.assertEqual(u'<form action="/content/submit/3" class="button-to" method="POST"><div><input alt="Complete the form" src="submit.png" type="image" value="Submit" /></div></form>',
+        self.assertEqual(ucs('<form action="/content/submit/3" class="button-to" method="POST"><div><input alt="Complete the form" src="submit.png" type="image" value="Submit" /></div></form>'),
                          button_to("Submit", '/content/submit/3', type='image', src='submit.png', alt='Complete the form'))
 
     def test_mail_to(self):
-        self.assertEqual(u'<a href="mailto:justin@example.com">justin@example.com</a>', mail_to("justin@example.com"))
-        self.assertEqual(u'<a href="mailto:justin@example.com">Justin Example</a>', mail_to("justin@example.com", "Justin Example"))
-        self.assertEqual(u'<a class="admin" href="mailto:justin@example.com">Justin Example</a>',
+        self.assertEqual(ucs('<a href="mailto:justin@example.com">justin@example.com</a>'), mail_to("justin@example.com"))
+        self.assertEqual(ucs('<a href="mailto:justin@example.com">Justin Example</a>'), mail_to("justin@example.com", "Justin Example"))
+        self.assertEqual(ucs('<a class="admin" href="mailto:justin@example.com">Justin Example</a>'),
                          mail_to("justin@example.com", "Justin Example", class_="admin"))
 
     def test_mail_to_with_javascript(self):
-        self.assertEqual(u"<script type=\"text/javascript\">\n//<![CDATA[\neval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))\n//]]>\n</script>", mail_to("me@domain.com", "My email", encode = "javascript"))
+        self.assertEqual(ucs("<script type=\"text/javascript\">\n//<![CDATA[\neval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))\n//]]>\n</script>"), mail_to("me@domain.com", "My email", encode = "javascript"))
 
     def test_mail_to_with_options(self):
-        self.assertEqual(u'<a href="mailto:me@example.com?cc=ccaddress%40example.com&amp;bcc=bccaddress%40example.com&amp;subject=This%20is%20an%20example%20email&amp;body=This%20is%20the%20body%20of%20the%20message.">My email</a>',
+        self.assertEqual(ucs('<a href="mailto:me@example.com?cc=ccaddress%40example.com&amp;bcc=bccaddress%40example.com&amp;subject=This%20is%20an%20example%20email&amp;body=This%20is%20the%20body%20of%20the%20message.">My email</a>'),
             mail_to("me@example.com", "My email", cc="ccaddress@example.com",
                     bcc="bccaddress@example.com", subject="This is an example email",
                     body="This is the body of the message."))
 
     def test_mail_to_with_img(self):
-        self.assertEqual(u'<a href="mailto:feedback@example.com"><img src="/feedback.png" /></a>',
+        self.assertEqual(ucs('<a href="mailto:feedback@example.com"><img src="/feedback.png" /></a>'),
                         mail_to('feedback@example.com', HTML.literal('<img src="/feedback.png" />')))
 
     def test_mail_to_with_hex(self):
-        self.assertEqual(u"<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>",
+        self.assertEqual(ucs("<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>"),
                          mail_to("me@domain.com", "My email", encode = "hex"))
-        self.assertEqual(u"<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#64;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;</a>",
+        self.assertEqual(ucs("<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#64;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;</a>"),
                          mail_to("me@domain.com", None, encode = "hex"))
 
     def test_mail_to_with_replace_options(self):
-        self.assertEqual(u'<a href="mailto:wolfgang@stufenlos.net">wolfgang(at)stufenlos(dot)net</a>',
+        self.assertEqual(ucs('<a href="mailto:wolfgang@stufenlos.net">wolfgang(at)stufenlos(dot)net</a>'),
                         mail_to("wolfgang@stufenlos.net", None, replace_at="(at)", replace_dot="(dot)"))
-        self.assertEqual(u"<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#40;&#97;&#116;&#41;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;</a>",
+        self.assertEqual(ucs("<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#40;&#97;&#116;&#41;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;</a>"),
                          mail_to("me@domain.com", None, encode = "hex", replace_at = "(at)"))
-        self.assertEqual(u"<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>",
+        self.assertEqual(ucs("<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>"),
                          mail_to("me@domain.com", "My email", encode = "hex", replace_at = "(at)"))
-        self.assertEqual(u"<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#40;&#97;&#116;&#41;&#100;&#111;&#109;&#97;&#105;&#110;&#40;&#100;&#111;&#116;&#41;&#99;&#111;&#109;</a>",
+        self.assertEqual(ucs("<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#40;&#97;&#116;&#41;&#100;&#111;&#109;&#97;&#105;&#110;&#40;&#100;&#111;&#116;&#41;&#99;&#111;&#109;</a>"),
                          mail_to("me@domain.com", None, encode = "hex", replace_at = "(at)", replace_dot = "(dot)"))
-        self.assertEqual(u"<script type=\"text/javascript\">\n//<![CDATA[\neval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))\n//]]>\n</script>",
+        self.assertEqual(ucs("<script type=\"text/javascript\">\n//<![CDATA[\neval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))\n//]]>\n</script>"),
                          mail_to("me@domain.com", "My email", encode = "javascript", replace_at = "(at)", replace_dot = "(dot)"))
 
 
                 style="color:red"))
 
     def test_highlight_literal(self):
-        eq_(literal(u'The &lt;red&gt; c<strong class="highlight">at</strong>.'),
+        eq_(literal(ucs('The &lt;red&gt; c<strong class="highlight">at</strong>.')),
             highlight("The <red> cat.", "at"))
-        eq_(literal(u'The <red> c<strong class="highlight">at</strong>.'),
+        eq_(literal(ucs('The <red> c<strong class="highlight">at</strong>.')),
             highlight(literal("The <red> cat."), "at"))
 
     def test_highlight_legacy_highlighter(self):
 
 class TestStripTagsHelper(WebHelpersTestCase):
     def test_compare_strip_tags_to_sanitize(self):
-        text = u'I <i>really</i> like <script language="javascript">NEFARIOUS CODE</script> steak!'
+        text = ucs('I <i>really</i> like <script language="javascript">NEFARIOUS CODE</script> steak!')
         eq_(strip_tags(text), render.sanitize(text))
 
 if __name__ == '__main__':

File webhelpers/compat.py

View file
 except ImportError:
     from urllib.parse import quote as url_escape
 
+try:
+    import html.parser as htmlparser
+except ImportError:
+    import HTMLParser as htmlparser
+
+HTMLParser = htmlparser.HTMLParser
+
+try:
+    import htmlentitydefs
+except ImportError:
+    import html.entities as htmlentitydefs
+
+try:
+    from cStringIO import StringIO as BytesIO, StringIO
+except ImportError:
+    try:
+        from StringIO import StringIO as BytesIO, StringIO
+    except ImportError:
+        from io import BytesIO, StringIO
+
 # :ide: COMPILE: Run with python3 w/o args
 # . (progn (save-buffer) (compile (concat "python3 ./" (file-name-nondirectory (buffer-file-name)) " ")))
 

File webhelpers/constants.py

View file
 """Place names and other constants often used in web forms.
 """
 
-# hide from 2to3
-exec('''
-def isstring(obj):
-    return isinstance(obj, basestring)
-''')
-try:
-    isstring("")
-except NameError:
-    def isstring(obj):
-        return isinstance(obj, str) or isinstance(obj, bytes)
-
-
-def _ucs(string, charset=None):                            # ||:fnc:||
-    return unicode(string, charset or 'utf-8')
-try:
-    _ucs("")
-except NameError:
-    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
-
-uc_type = type(_ucs(b""))
-
-def ucs(value, charset=None):                              # ||:fnc:||
-    """\
-    Convert `value` to unicode string using charset or UTF-8, if
-    `value` is a string.
-
-    If `value` is not a string, or if it is already a unicode string,
-    return it unmodified.
-    """
-    if isstring(value) and not isinstance(value, uc_type):
-        return _ucs(value, charset)
-    return value
-
-def u8s(string):                                           # ||:fnc:||
-    """\
-    Convert `string` to UTF-8-encoded byte string, if `string` is a
-    unicode string.
-
-    If `string` is not a unicode string, return it unmodified.
-    """
-    if isinstance(string, uc_type):
-        return string.encode('utf-8')
-    return string
-
-def nts(string):                                           # ||:fnc:||
-    """\
-    Convert string to native string, if applicable.
-
-    Python2 native strings are byte strings, while Python3 native
-    strings are unicode.
-    """
-    # for python3, unicode strings have type str
-    if isinstance(string, str):
-        return string
-    # for python2, encode unicode strings to utf-8 strings
-    if isinstance(string, uc_type):
-        return string.encode('utf-8')
-    if isstring(string):
-        try:
-            return str(string.decode('utf-8'))
-        except UnicodeDecodeError:
-            #return str(string.decode('latin1'))
-            pass
-    return string
-
-# Aliases (old py_b.strings)
-# uc = ucs
-# utf8str = u8s
-# nativestr = nts
-
+from webhelpers.compat import *
 
 def uk_counties():
     """\

File webhelpers/feedgenerator.py

View file
 # Copyright (c) Django Software Foundation and individual contributors.
 # All rights reserved.
-# 
+#
 # Redistribution and use in source and binary forms, with or without modification,
 # are permitted provided that the following conditions are met:
-# 
-#     1. Redistributions of source code must retain the above copyright notice, 
+#
+#     1. Redistributions of source code must retain the above copyright notice,
 #        this list of conditions and the following disclaimer.
-#     
-#     2. Redistributions in binary form must reproduce the above copyright 
+#
+#     2. Redistributions in binary form must reproduce the above copyright
 #        notice, this list of conditions and the following disclaimer in the
 #        documentation and/or other materials provided with the distribution.
-# 
+#
 #     3. Neither the name of Django nor the names of its contributors may be used
 #        to endorse or promote products derived from this software without
 #        specific prior written permission.
-# 
+#
 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 # WebHelpers changes from original:
 # ---------------------------------
 # - Combine ``django.utils.feedgenerator`` and ``django.contrib.gis.feeds``.
-# - Change imports: 
+# - Change imports:
 #     * ``SimpleXMLGenerator`` and ``iri_to_uri`` are in ``webhelpers.util``.
 #     * Add local copy of ``force_unicode``.
-# - Delete parts that depend on Django's ORM system: ``Feed`` class, 
+# - Delete parts that depend on Django's ORM system: ``Feed`` class,
 #   ``BaseFeed`` and ``FeedDoesNotExist`` imports.
 # - Delete ``to_unicode`` lambdas and ``force_unicode`` import; these seem
 #   unnecessary.
 # - "if geom is not None" syntax fix in ``GeoFeedMixin.get_georss_element()``.
 # - Add a ``Geometry`` class for passing geometries to the Geo classes.
 # - ``GeoFeedMixin`` docstring.
-# - Add a dummy version attribute to ``RssFeed`` base class. 
+# - Add a dummy version attribute to ``RssFeed`` base class.
 #   ``RssFeed._version = # "?"`` This avoids AttributeError when instantiating
 #   ``RssFeed`` directly, although it's obviously invalid RSS.
 
 
 import re
 import datetime
+from webhelpers.compat import *
+from webhelpers.compat import StringIO
 from webhelpers.util import SimplerXMLGenerator, iri_to_uri
 
-
-# hide from 2to3
-exec('''
-def isstring(obj):
-    return isinstance(obj, basestring)
-''')
-try:
-    isstring("")
-except NameError:
-    def isstring(obj):
-        return isinstance(obj, str) or isinstance(obj, bytes)
-
-
-def _ucs(string, charset=None):                            # ||:fnc:||
-    return unicode(string, charset or 'utf-8')
-try:
-    _ucs("")
-except NameError:
-    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
-
-uc_type = type(_ucs(b""))
-
-def ucs(value, charset=None):                              # ||:fnc:||
-    """\
-    Convert `value` to unicode string using charset or UTF-8, if
-    `value` is a string.
-
-    If `value` is not a string, or if it is already a unicode string,
-    return it unmodified.
-    """
-    if isstring(value) and not isinstance(value, uc_type):
-        return _ucs(value, charset)
-    return value
-
-def u8s(string):                                           # ||:fnc:||
-    """\
-    Convert `string` to UTF-8-encoded byte string, if `string` is a
-    unicode string.
-
-    If `string` is not a unicode string, return it unmodified.
-    """
-    if isinstance(string, uc_type):
-        return string.encode('utf-8')
-    return string
-
-def nts(string):                                           # ||:fnc:||
-    """\
-    Convert string to native string, if applicable.
-
-    Python2 native strings are byte strings, while Python3 native
-    strings are unicode.
-    """
-    # for python3, unicode strings have type str
-    if isinstance(string, str):
-        return string
-    # for python2, encode unicode strings to utf-8 strings
-    if isinstance(string, uc_type):
-        return string.encode('utf-8')
-    if isstring(string):
-        try:
-            return str(string.decode('utf-8'))
-        except UnicodeDecodeError:
-            #return str(string.decode('latin1'))
-            pass
-    return string
-
 #### The following code comes from ``django.utils.feedgenerator`` ####
 
 def rfc2822_date(date):
         """
         Returns the feed in the given encoding as a string.
         """
-        from StringIO import StringIO
         s = StringIO()
         self.write(s, encoding)
         return s.getvalue()
             handler.addQuickElement(ucs("category"), cat)
         if self.feed['feed_copyright'] is not None:
             handler.addQuickElement(ucs("copyright"), self.feed['feed_copyright'])
-        handler.addQuickElement(ucs("lastBuildDate"), rfc2822_date(self.latest_post_date()).decode('utf-8'))
+        handler.addQuickElement(ucs("lastBuildDate"), ucs(rfc2822_date(self.latest_post_date()), 'utf-8'))
         if self.feed['ttl'] is not None:
             handler.addQuickElement(ucs("ttl"), self.feed['ttl'])
 
             handler.addQuickElement(ucs("dc:creator"), item["author_name"], {"xmlns:dc": ucs("http://purl.org/dc/elements/1.1/")})
 
         if item['pubdate'] is not None:
-            handler.addQuickElement(ucs("pubDate"), rfc2822_date(item['pubdate']).decode('utf-8'))
+            handler.addQuickElement(ucs("pubDate"), ucs(rfc2822_date(item['pubdate']), 'utf-8'))
         if item['comments'] is not None:
             handler.addQuickElement(ucs("comments"), item['comments'])
         if item['unique_id'] is not None:
         if self.feed['feed_url'] is not None:
             handler.addQuickElement(ucs("link"), "", {ucs("rel"): ucs("self"), ucs("href"): self.feed['feed_url']})
         handler.addQuickElement(ucs("id"), self.feed['id'])
-        handler.addQuickElement(ucs("updated"), rfc3339_date(self.latest_post_date()).decode('utf-8'))
+        handler.addQuickElement(ucs("updated"), ucs(rfc3339_date(self.latest_post_date()), 'utf-8'))
         if self.feed['author_name'] is not None:
             handler.startElement(ucs("author"), {})
             handler.addQuickElement(ucs("name"), self.feed['author_name'])
         handler.addQuickElement(ucs("title"), item['title'])
         handler.addQuickElement(ucs("link"), ucs(""), {ucs("href"): item['link'], ucs("rel"): ucs("alternate")})
         if item['pubdate'] is not None:
-            handler.addQuickElement(ucs("updated"), rfc3339_date(item['pubdate']).decode('utf-8'))
-            handler.addQuickElement(ucs("published"), rfc3339_date(item['pubdate']).decode('utf-8'))
+            handler.addQuickElement(ucs("updated"), ucs(rfc3339_date(item['pubdate']), 'utf-8'))
+            handler.addQuickElement(ucs("published"), ucs(rfc3339_date(item['pubdate']), 'utf-8'))
 
         # Author information.
         if item['author_name'] is not None:
     Subclasses recognize a ``geometry`` keyword argument to ``.add_item()``.
     The value may be any of several types:
 
-    * a 2-element tuple or list of floats representing latitude/longitude: 
+    * a 2-element tuple or list of floats representing latitude/longitude:
       ``(X, Y)``.  This is called a "point".
 
     * a 4-element tuple or list of floats representing a box:
                 # Getting the lower-case geometry type.
                 gtype = str(geom.geom_type).lower()
                 if gtype == 'point':
-                    self.add_georss_point(handler, geom.coords, w3c_geo=w3c_geo) 
+                    self.add_georss_point(handler, geom.coords, w3c_geo=w3c_geo)
                 else:
                     if w3c_geo: raise ValueError('W3C Geo only supports Point geometries.')
                     # For formatting consistent w/the GeoRSS simple standard:
        ring.
 
     The constructor does not check its argument types.
-      
+
     This class was created for WebHelpers based on the interface expected by
     ``GeoFeedMixin.add_georss_element()``.  The class is untested. Please send
     us feedback on whether it works for you.

File webhelpers/html/builder.py

View file
 """HTML/XHTML tag builder
 
-HTML Builder provides: 
+HTML Builder provides:
 
-* an ``HTML`` object that creates (X)HTML tags in a Pythonic way.  
+* an ``HTML`` object that creates (X)HTML tags in a Pythonic way.
 
-* a ``literal`` class used to mark strings containing intentional HTML markup. 
+* a ``literal`` class used to mark strings containing intentional HTML markup.
 
 * a smart ``escape()`` function that preserves literals but
   escapes other strings that may accidentally contain markup characters ("<",
 escaping single-quotes for security, and adding new methods to
 ``literal``. **literal** is now a subclass of ``markupsafe.Markup``.
 **escape** is ``markupsafe.escape_silent``. (The latter does not exist yet in
-MarkupSafe 0.9.3, but WebHelpers itself converts None to "" in the meantime). 
+MarkupSafe 0.9.3, but WebHelpers itself converts None to "" in the meantime).
 
 Single-quote escaping affects HTML attributes that are written like this:
 *alt='Some text.'* rather than the normal *alt="Some text."*  If the text is a
 issues but may in the following edge cases.  (A) The ``force`` argument to
 ``escape()`` is gone. We doubt it was ever used. (B) The default encoding of
 ``literal()`` is "ascii" instead of "utf-8". (C) Double quotes are escaped as
-"&#34;" instead of "&quot;". Single quotes are escaped as "&#39;". 
+"&#34;" instead of "&quot;". Single quotes are escaped as "&#39;".
 
 When ``literal`` is used in a mixed expression containing both literals and
 ordinary strings, it tries hard to escape the strings and return a literal.
     Same as ``literal`` but concatenates multiple arguments.
 
 ``HTML.comment(*strings)``
-    Escape and concatenate the strings, and wrap the result in an HTML 
+    Escape and concatenate the strings, and wrap the result in an HTML
     comment.
 
 ``HTML.tag(tag, *content, **attrs)``
     (notably "class"), append an underscore.  If an attribute value is
     ``None``, the attribute is not inserted.  Two special keyword args are
     recognized:
-    
+
     ``c``
         Specifies the content.  This cannot be combined with content in
         positional args.  The purpose of this argument is to position the
 
     Example:
 
-    >>> HTML.tag("a", href="http://www.yahoo.com", name=None, 
+    >>> HTML.tag("a", href="http://www.yahoo.com", name=None,
     ... c="Click Here")
     literal(u'<a href="http://www.yahoo.com">Click Here</a>')
 
 
 """
 import re
-from urllib import quote as url_escape
-from UserDict import DictMixin
 
-
-# hide from 2to3
-exec('''
-def isstring(obj):
-    return isinstance(obj, basestring)
-''')
-try:
-    isstring("")
-except NameError:
-    def isstring(obj):
-        return isinstance(obj, str) or isinstance(obj, bytes)
-
-
-def _ucs(string, charset=None):                            # ||:fnc:||
-    return unicode(string, charset or 'utf-8')
-try:
-    _ucs("")
-except NameError:
-    _ucs = lambda s, c=None: s.decode(c or 'utf-8')
-
-uc_type = type(_ucs(b""))
-
-def ucs(value, charset=None):                              # ||:fnc:||
-    """\
-    Convert `value` to unicode string using charset or UTF-8, if
-    `value` is a string.
-
-    If `value` is not a string, or if it is already a unicode string,
-    return it unmodified.
-    """
-    if isstring(value) and not isinstance(value, uc_type):
-        return _ucs(value, charset)
-    return value
-
-def u8s(string):                                           # ||:fnc:||
-    """\
-    Convert `string` to UTF-8-encoded byte string, if `string` is a
-    unicode string.
-
-    If `string` is not a unicode string, return it unmodified.
-    """
-    if isinstance(string, uc_type):
-        return string.encode('utf-8')
-    return string
-
-def nts(string):                                           # ||:fnc:||
-    """\
-    Convert string to native string, if applicable.
-
-    Python2 native strings are byte strings, while Python3 native
-    strings are unicode.
-    """
-    # for python3, unicode strings have type str
-    if isinstance(string, str):
-        return string
-    # for python2, encode unicode strings to utf-8 strings
-    if isinstance(string, uc_type):
-        return string.encode('utf-8')
-    if isstring(string):
-        try:
-            return str(string.decode('utf-8'))
-        except UnicodeDecodeError:
-            #return str(string.decode('latin1'))
-            pass
-    return string
+from webhelpers.compat import *
+from webhelpers.compat import url_escape
+from webhelpers.compat import DictMixin
 
 import markupsafe
 try:
 
 class literal(markupsafe.Markup):
     """Represents an HTML literal.
-    
-    This subclass of unicode has a ``.__html__()`` method that is 
+
+    This subclass of unicode has a ``.__html__()`` method that is
     detected by the ``escape()`` function.
-    
-    Also, if you add another string to this string, the other string 
+
+    Also, if you add another string to this string, the other string
     will be quoted and you will get back another literal object.  Also
     ``literal(...) % obj`` will quote any value(s) from ``obj``.  If
     you do something like ``literal(...) + literal(...)``, neither
     Changed in WebHelpers 1.2: the implementation is now now a subclass of
     ``markupsafe.Markup``.  This brings some new methods: ``.escape`` (class
     method), ``.unescape``, and ``.striptags``.
-    
+
     """
     __slots__ = ()
 
 
 __all__ = ["HTML", "escape", "literal", "url_escape", "lit_sub"]
 
-# Not included in __all__ because for specialized purposes only: 
+# Not included in __all__ because for specialized purposes only:
 # "format_attrs".
 
 class UnfinishedTag(object):
-    
+
     """Represents an unfinished or empty tag."""
-    
+
     def __init__(self, tag):
         """Initialize with the tag name."""
         self._tag = tag
 
 
 class UnfinishedComment(object):
-    
+
     """Represents an unfinished or empty comment."""
-    
+
     def __call__(self, *args):
         """Create the HTML comment."""
         return literal('<!--%s-->' % ''.join([str(x) for x in args]))
-        
+
     def __html__(self):
         """Return the HTML escaped tag."""
         raise NotImplementedError(
 
 
 class UnfinishedLiteral(object):
-    
+
     """Represent an unfinished literal value."""
-    
+
     def __call__(self, *args):
         """Return the literal HTML."""
         return literal(*args)
 
 
 class HTMLBuilder(object):
-    
+
     """Base HTML object."""
-    
+
     comment = UnfinishedComment()
     literal = UnfinishedLiteral()
-    
+
     def __getattr__(self, attr):
         """Generate the tag for the given attribute name."""
         if attr.startswith('_'):
     def tag(self, tag, *args, **kw):
         return make_tag(tag, *args, **kw)
 
-    def cdata(self, *content): 
+    def cdata(self, *content):
         """Wrap the content in a "<![CDATA[ ... ]]>" section.
 
-        The content will not be escaped because CDATA itself is an 
+        The content will not be escaped because CDATA itself is an
         escaping syntax.
         """
         # _CDATA_START and _CDATA_END are defined at end of module.
 
 
 def make_tag(tag, *args, **kw):
-    if kw.has_key("c"):
+    if "c" in kw:
         assert not args, "The special 'c' keyword argument cannot be used "\
 "in conjunction with non-keyword arguments"
         args = kw.pop("c")
     literal(u'')
     """
     strings = [ucs(' %s="%s"') % (_attr_decode(attr), escape(value))
-        for attr, value in sorted(attrs.iteritems())
+        for attr, value in sorted(ditems(attrs))
         if value is not None]
     return literal("".join(strings))
 
 NL = literal(ucs("\n"))
 EMPTY = literal(ucs(""))
 BR = HTML.br(_nl=True)
-_CDATA_START = literal(ucs("<![CDATA[")) 
+_CDATA_START = literal(ucs("<![CDATA["))
 _CDATA_END = literal(ucs("]]>"))

File webhelpers/html/converters.py

View file
 """
 import re
 
+from webhelpers.compat import *
 from webhelpers.html import HTML, escape, literal, lit_sub
 import webhelpers.textile as textile
 
     <img />,  <br /> and <hr /> for proper XHTML output.
     
     """
-    texer = textile.Textiler(text)
-    return literal(texer.process(sanitize=sanitize))
+    texer = textile.Textiler(ucs(text))
+    return literal(ucs(texer.process(sanitize=sanitize)))
 
 def nl2br(text):
     """Insert a <br /> before each newline.

File webhelpers/html/render.py

View file
 """An HTML-to-text formatter and HTML sanitizer.
 """
 
-from HTMLParser import HTMLParser
-import htmlentitydefs
+from webhelpers.compat import HTMLParser
+from webhelpers.compat import htmlentitydefs
+
 import re
 import textwrap
 

File webhelpers/html/tags.py

View file
 """Helpers that produce simple HTML tags.
 
 Most helpers have an ``**attrs`` argument to specify additional HTML
-attributes.  A trailing underscore in the name will be deleted; this is 
+attributes.  A trailing underscore in the name will be deleted; this is
 especially important for attributes that are identical to Python keywords;
 e.g., ``class_``.  Some helpers handle certain keywords specially; these are
 noted in the helpers' docstrings.
 import os
 import re
 import urllib
-import urlparse
-
+from webhelpers.compat import *
+from webhelpers.compat import urlparse
 from webhelpers import containers
 from webhelpers.html import escape, HTML, literal, url_escape
 import webhelpers.media as media
 
 __all__ = [
            # Form tags
-           "form", "end_form", 
-           "text", "textarea", "hidden", "file", "password", 
+           "form", "end_form",
+           "text", "textarea", "hidden", "file", "password",
            "checkbox", "radio", "submit",
            "select", "Options", "Option", "OptGroup",
            "ModelTags", "title", "required_legend",
 
     You must close the form yourself by calling ``end_form()`` or outputting
     </form>.
-    
+
     Options:
 
     ``method``
-        The method to use when submitting the form, usually either 
+        The method to use when submitting the form, usually either
         "GET" or "POST". If "PUT", "DELETE", or another verb is used, a
         hidden input with name _method is added to simulate the verb
         over POST.
-    
+
     ``multipart``
         If set to True, the enctype is set to "multipart/form-data".
         You must set it to true when uploading files, or the browser will
         the same key in WebOb MultiDict objects.)
 
     Because input tags must be placed in a block tag rather than directly
-    inside the form, all hidden fields will be put in a 
+    inside the form, all hidden fields will be put in a
     '<div style="display:none">'.  The style prevents the <div> from being
     displayed or affecting the layout.
 
     literal(u'<form action="/submit" method="get">')
     >>> form("/submit", method="put")
     literal(u'<form action="/submit" method="post"><div style="display:none">\\n<input name="_method" type="hidden" value="put" />\\n</div>\\n')
-    >>> form("/submit", "post", multipart=True) 
+    >>> form("/submit", "post", multipart=True)
     literal(u'<form action="/submit" enctype="multipart/form-data" method="post">')
 
     Changed in WebHelpers 1.0b2: add <div> and ``hidden_fields`` arg.
 
 def end_form():
     """Output "</form>".
-    
+
     Example::
 
         >>> end_form()
 
 def text(name, value=None, id=NotGiven, type="text", **attrs):
     """Create a standard text field.
-    
+
     ``value`` is a string, the content of the text field.
 
     ``id`` is the HTML ID attribute, and should be passed as a keyword
     for HTML 5 input fields that don't have their own helper; e.g.,
     "search", "email", "date".
 
-    
+
     Options:
-    
+
     * ``disabled`` - If set to True, the user will not be able to use
         this input.
     * ``size`` - The number of visible characters that will fit in the
         input.
     * ``maxlength`` - The maximum number of characters that the browser
         will allow the user to enter.
-    
+
     The remaining keyword args will be standard HTML attributes for the tag.
 
     Example, a text input field::
 
         >>> text("color", type="color")
         literal(u'<input id="color" name="color" type="color" />')
-    
+
     """
     _set_input_attrs(attrs, type, name, value)
     _set_id_attr(attrs, id, name)
 
 def file(name, value=None, id=NotGiven, **attrs):
     """Create a file upload field.
-    
-    If you are using file uploads then you will also need to set the 
+
+    If you are using file uploads then you will also need to set the
     multipart option for the form.
 
     Example::
 
         >>> file('myfile')
         literal(u'<input id="myfile" name="myfile" type="file" />')
-    
+
     """
     _set_input_attrs(attrs, "file", name, value)
     _set_id_attr(attrs, id, name)
 
 def password(name, value=None, id=NotGiven, **attrs):
     """Create a password field.
-    
+
     Takes the same options as ``text()``.
-    
+
     """
     _set_input_attrs(attrs, "password", name, value)
     _set_id_attr(attrs, id, name)
 
 def textarea(name, content="", id=NotGiven, **attrs):
     """Create a text input area.
-    
+
     Example::
-    
+
         >>> textarea("body", "", cols=25, rows=10)
         literal(u'<textarea cols="25" id="body" name="body" rows="10"></textarea>')
-    
+
     """
     attrs["name"] = name
     _set_id_attr(attrs, id, name)
     webhelpers.containers.distribute().
 
     Example::
-    
+
         >>> checkbox("hi")
         literal(u'<input id="hi" name="hi" type="checkbox" value="1" />')
     """
     ``checked`` -- true if the button should be initially pressed.
 
     ``label`` -- a text label to display to the right of the button.
-    
-    The id of the radio button will be set to the name + '_' + value to 
+
+    The id of the radio button will be set to the name + '_' + value to
     ensure its uniqueness.  An ``id`` keyword arg overrides this.  (Note
     that this behavior is unique to the ``radio()`` helper.)
-    
+
     To arrange multiple radio buttons in a group, see
     webhelpers.containers.distribute().
     """
     * ``multiple`` -- if true, this control will allow multiple
        selections.
 
-    * ``prompt`` -- if specified, an extra option will be prepended to the 
+    * ``prompt`` -- if specified, an extra option will be prepended to the
       list: ("", ``prompt``).  This is intended for those "Please choose ..."
       pseudo-options.  Its value is "", equivalent to not making a selection.
 
     Any other keyword args will become HTML attributes for the <select>.
 
     Examples (call, result)::
-    
+
         >>> select("currency", "$", [["$", "Dollar"], ["DKK", "Kroner"]])
         literal(u'<select id="currency" name="currency">\\n<option selected="selected" value="$">Dollar</option>\\n<option value="DKK">Kroner</option>\\n</select>')
         >>> select("cc", "MasterCard", [ "VISA", "MasterCard" ], id="cc", class_="blue")
         literal(u'<select id="privacy" name="privacy">\\n<option value="1">Private</option>\\n<option value="2">Semi-public</option>\\n<option selected="selected" value="3">Public</option>\\n</select>')
         >>> select("recipients", None, [([("u1", "User1"), ("u2", "User2")], "Users"), ([("g1", "Group1"), ("g2", "Group2")], "Groups")])
         literal(u'<select id="recipients" name="recipients">\\n<optgroup label="Users">\\n<option value="u1">User1</option>\\n<option value="u2">User2</option>\\n</optgroup>\\n<optgroup label="Groups">\\n<option value="g1">Group1</option>\\n<option value="g2">Group2</option>\\n</optgroup>\\n</select>')
-        
+
     """
     _set_id_attr(attrs, id, name)
     attrs["name"] = name
     convert_boolean_attrs(attrs, ["multiple"])
     # Accept None as selected_values meaning that no option is selected
+    check_types = [int]
+    try:
+        check_types.append(long)
+    except NameError:
+        pass
     if select