Skip to content

Commit ca14860

Browse files
committed
Replace tornado.web._unicode with tornado.escape._unicode.
Add a test case where this matters (when the argument is None)
1 parent 02ce53b commit ca14860

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

tornado/test/web_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ class UIModuleResourceHandler(RequestHandler):
244244
def get(self):
245245
self.render("page.html", entries=[1,2])
246246

247+
class OptionalPathHandler(RequestHandler):
248+
def get(self, path):
249+
self.write({"path": path})
250+
247251
class WebTest(AsyncHTTPTestCase, LogTrapTestCase):
248252
def get_app(self):
249253
loader = DictLoader({
@@ -264,11 +268,17 @@ def get_app(self):
264268
url("/decode_arg_kw/(?P<arg>.*)", DecodeArgHandler),
265269
url("/linkify", LinkifyHandler),
266270
url("/uimodule_resources", UIModuleResourceHandler),
271+
url("/optional_path/(.+)?", OptionalPathHandler),
267272
]
268273
return Application(urls,
269274
template_loader=loader,
270275
autoescape="xhtml_escape")
271276

277+
def fetch_json(self, *args, **kwargs):
278+
response = self.fetch(*args, **kwargs)
279+
response.rethrow()
280+
return json_decode(response.body)
281+
272282
def test_types(self):
273283
response = self.fetch("/typecheck/asdf?foo=bar",
274284
headers={"Cookie": "cook=ie"})
@@ -329,3 +339,9 @@ def test_uimodule_resources(self):
329339
</script>
330340
<script src="/analytics.js"/>
331341
</body></html>"""))
342+
343+
def test_optional_path(self):
344+
self.assertEqual(self.fetch_json("/optional_path/foo"),
345+
{u"path": u"foo"})
346+
self.assertEqual(self.fetch_json("/optional_path/"),
347+
{u"path": None})

tornado/web.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def get(self):
7979
from tornado import locale
8080
from tornado import stack_context
8181
from tornado import template
82-
from tornado.escape import utf8
82+
from tornado.escape import utf8, _unicode
8383
from tornado.util import b, bytes_type
8484

8585
try:
@@ -1751,16 +1751,6 @@ def reverse(self, *args):
17511751
url = URLSpec
17521752

17531753

1754-
def _unicode(s):
1755-
if isinstance(s, bytes_type):
1756-
try:
1757-
return s.decode("utf-8")
1758-
except UnicodeDecodeError:
1759-
raise HTTPError(400, "Non-utf8 argument")
1760-
assert isinstance(s, unicode)
1761-
return s
1762-
1763-
17641754
def _time_independent_equals(a, b):
17651755
if len(a) != len(b):
17661756
return False

0 commit comments

Comments
 (0)