from tornado import escape
from tornado.httputil import url_concat
from tornado.log import gen_log
-from tornado.util import bytes_type, b, u, unicode_type
+from tornado.util import bytes_type, u, unicode_type
try:
import urlparse # py2
from tornado.log import gen_log
from tornado.netutil import TCPServer
from tornado import stack_context
-from tornado.util import b, bytes_type
+from tornado.util import bytes_type
try:
import ssl # Python 2.6+
from tornado.escape import native_str, parse_qs_bytes, utf8
from tornado.log import gen_log
-from tornado.util import b, ObjectDict
+from tornado.util import ObjectDict
try:
from httplib import responses # py2
>>> for (k,v) in sorted(h.get_all()):
... print('%s: %s' % (k,v))
- ...
+ ...
Content-Type: text/html
Set-Cookie: A=B
Set-Cookie: C=D
from tornado import ioloop
from tornado.log import gen_log, app_log
from tornado import stack_context
-from tornado.util import b, bytes_type
+from tornado.util import bytes_type
try:
import ssl # Python 2.6+
import socket
from tornado.platform import interface
-from tornado.util import b
class Waker(interface.Waker):
import os
from tornado.platform import interface
-from tornado.util import b
def set_close_exec(fd):
from tornado.netutil import Resolver
from tornado.log import gen_log
from tornado import stack_context
-from tornado.util import b, GzipDecompressor
+from tornado.util import GzipDecompressor
import base64
import collections
from tornado.auth import OpenIdMixin, OAuthMixin, OAuth2Mixin, TwitterMixin
from tornado.escape import json_decode
from tornado.testing import AsyncHTTPTestCase
-from tornado.util import b, u
+from tornado.util import u
from tornado.web import RequestHandler, Application, asynchronous
from tornado.iostream import IOStream
from tornado.netutil import TCPServer
from tornado.testing import AsyncTestCase, LogTrapTestCase, get_unused_port
-from tornado.util import b
class CapServer(TCPServer):
def handle_stream(self, stream, address):
import tornado.escape
from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
-from tornado.util import b, u, unicode_type
+from tornado.util import u, unicode_type
from tornado.test.util import unittest
linkify_tests = [
from tornado.httpclient import AsyncHTTPClient
from tornado.log import app_log
from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog
-from tornado.util import b
from tornado.web import Application, RequestHandler, asynchronous
from tornado import gen
from tornado.stack_context import ExceptionStackContext, NullContext
from tornado.testing import AsyncHTTPTestCase, bind_unused_port
from tornado.test.util import unittest
-from tornado.util import b, u, bytes_type
+from tornado.util import u, bytes_type
from tornado.web import Application, RequestHandler, url
sock, port = bind_unused_port()
with closing(sock):
def write_response(stream, request_data):
- stream.write(b("""\
+ stream.write(b"""\
HTTP/1.1 200 OK
Transfer-Encoding: chunked
2
0
-""").replace(b"\n", b"\r\n"), callback=stream.close)
+""".replace(b"\n", b"\r\n"), callback=stream.close)
def accept_callback(conn, address):
# fake an HTTP server using chunked encoding where the final chunks
from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.testing import AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase, ExpectLog
from tornado.test.util import unittest
-from tornado.util import b, u, bytes_type
+from tornado.util import u, bytes_type
from tornado.web import Application, RequestHandler, asynchronous
import datetime
import os
from tornado.log import gen_log
from tornado.testing import ExpectLog
from tornado.test.util import unittest
-from tornado.util import b
import logging
class MultipartFormDataTest(unittest.TestCase):
def test_file_upload(self):
- data = b("""\
+ data = b"""\
--1234
Content-Disposition: form-data; name="files"; filename="ab.txt"
Foo
---1234--""").replace(b"\n", b"\r\n")
+--1234--""".replace(b"\n", b"\r\n")
args = {}
files = {}
parse_multipart_form_data(b"1234", data, args, files)
def test_unquoted_names(self):
# quotes are optional unless special characters are present
- data = b("""\
+ data = b"""\
--1234
Content-Disposition: form-data; name=files; filename=ab.txt
Foo
---1234--""").replace(b"\n", b"\r\n")
+--1234--""".replace(b"\n", b"\r\n")
args = {}
files = {}
parse_multipart_form_data(b"1234", data, args, files)
self.assertEqual(file["body"], b"Foo")
def test_boundary_starts_and_ends_with_quotes(self):
- data = b('''\
+ data = b'''\
--1234
Content-Disposition: form-data; name="files"; filename="ab.txt"
Foo
---1234--''').replace(b"\n", b"\r\n")
+--1234--'''.replace(b"\n", b"\r\n")
args = {}
files = {}
parse_multipart_form_data(b'"1234"', data, args, files)
self.assertEqual(file["body"], b"Foo")
def test_missing_headers(self):
- data = b('''\
+ data = b'''\
--1234
Foo
---1234--''').replace(b"\n", b"\r\n")
+--1234--'''.replace(b"\n", b"\r\n")
args = {}
files = {}
with ExpectLog(gen_log, "multipart/form-data missing headers"):
self.assertEqual(files, {})
def test_invalid_content_disposition(self):
- data = b('''\
+ data = b'''\
--1234
Content-Disposition: invalid; name="files"; filename="ab.txt"
Foo
---1234--''').replace(b"\n", b"\r\n")
+--1234--'''.replace(b"\n", b"\r\n")
args = {}
files = {}
with ExpectLog(gen_log, "Invalid multipart/form-data"):
self.assertEqual(files, {})
def test_line_does_not_end_with_correct_line_break(self):
- data = b('''\
+ data = b'''\
--1234
Content-Disposition: form-data; name="files"; filename="ab.txt"
-Foo--1234--''').replace(b"\n", b"\r\n")
+Foo--1234--'''.replace(b"\n", b"\r\n")
args = {}
files = {}
with ExpectLog(gen_log, "Invalid multipart/form-data"):
self.assertEqual(files, {})
def test_content_disposition_header_without_name_parameter(self):
- data = b("""\
+ data = b"""\
--1234
Content-Disposition: form-data; filename="ab.txt"
Foo
---1234--""").replace(b"\n", b"\r\n")
+--1234--""".replace(b"\n", b"\r\n")
args = {}
files = {}
with ExpectLog(gen_log, "multipart/form-data value missing name"):
# The spec requires that data after the final boundary be ignored.
# http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
# In practice, some libraries include an extra CRLF after the boundary.
- data = b("""\
+ data = b"""\
--1234
Content-Disposition: form-data; name="files"; filename="ab.txt"
Foo
--1234--
-""").replace(b"\n", b"\r\n")
+""".replace(b"\n", b"\r\n")
args = {}
files = {}
parse_multipart_form_data(b"1234", data, args, files)
from tornado.stack_context import NullContext
from tornado.testing import AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase, bind_unused_port, ExpectLog
from tornado.test.util import unittest, skipIfNonUnix
-from tornado.util import b
from tornado.web import RequestHandler, Application
import errno
import logging
import tornado.locale
from tornado.escape import utf8
from tornado.test.util import unittest
-from tornado.util import b, u, unicode_type
+from tornado.util import u, unicode_type
class TranslationLoaderTest(unittest.TestCase):
from tornado.log import LogFormatter, define_logging_options, enable_pretty_logging
from tornado.options import OptionParser
from tornado.test.util import unittest
-from tornado.util import b, u, bytes_type, basestring_type
+from tornado.util import u, bytes_type, basestring_type
@contextlib.contextmanager
def ignore_bytes_warning():
from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.testing import bind_unused_port, ExpectLog, AsyncTestCase
from tornado.test.util import unittest, skipIfNonUnix
-from tornado.util import b
from tornado.web import RequestHandler, Application
from tornado.test import httpclient_test
from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, bind_unused_port, ExpectLog
from tornado.test.util import unittest
-from tornado.util import b
from tornado.web import RequestHandler, Application, asynchronous, url
from tornado.stack_context import StackContext, wrap, NullContext
from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog
from tornado.test.util import unittest
-from tornado.util import b
from tornado.web import asynchronous, Application, RequestHandler
import contextlib
import functools
from tornado.template import Template, DictLoader, ParseError, Loader
from tornado.testing import ExpectLog
from tornado.test.util import unittest
-from tornado.util import b, u, bytes_type, ObjectDict, unicode_type
+from tornado.util import u, bytes_type, ObjectDict, unicode_type
class TemplateTest(unittest.TestCase):
b"Bobby <table>s")
self.assertEqual(loader.load("include.html").generate(name=name),
- b("escaped: Bobby <table>s\n"
- "unescaped: Bobby <table>s\n"
- "default: Bobby <table>s\n"))
+ b"escaped: Bobby <table>s\n"
+ b"unescaped: Bobby <table>s\n"
+ b"default: Bobby <table>s\n")
def test_default_on(self):
loader = DictLoader(self.templates, autoescape="xhtml_escape")
b"Bobby <table>s")
self.assertEqual(loader.load("include.html").generate(name=name),
- b("escaped: Bobby <table>s\n"
- "unescaped: Bobby <table>s\n"
- "default: Bobby <table>s\n"))
+ b"escaped: Bobby <table>s\n"
+ b"unescaped: Bobby <table>s\n"
+ b"default: Bobby <table>s\n")
def test_unextended_block(self):
loader = DictLoader(self.templates)
def render(name):
return loader.load(name).generate(name='<>&"')
self.assertEqual(render("raw_expression.html"),
- b("expr: <>&"\n"
- "raw: <>&\""))
+ b"expr: <>&"\n"
+ b"raw: <>&\"")
def test_custom_escape(self):
loader = DictLoader({"foo.py":
self.assertEqual(render("foo.py", "<html>"),
b"s = '<html>'\n")
self.assertEqual(render("foo.py", "';sys.exit()"),
- b("""s = "';sys.exit()"\n"""))
+ b"""s = "';sys.exit()"\n""")
self.assertEqual(render("foo.py", ["not a string"]),
- b("""s = "['not a string']"\n"""))
+ b"""s = "['not a string']"\n""")
class TemplateLoaderTest(unittest.TestCase):
import sys
from tornado.escape import utf8
-from tornado.util import raise_exc_info, Configurable, u, b
+from tornado.util import raise_exc_info, Configurable, u
from tornado.test.util import unittest
from tornado.template import DictLoader
from tornado.testing import AsyncHTTPTestCase, ExpectLog
from tornado.test.util import unittest
-from tornado.util import b, u, bytes_type, ObjectDict, unicode_type
+from tornado.util import u, bytes_type, ObjectDict, unicode_type
from tornado.web import RequestHandler, authenticated, Application, asynchronous, url, HTTPError, StaticFileHandler, _create_signature, create_signed_value, ErrorHandler
import binascii
# this string base64-encodes to '12345678'
handler.set_secure_cookie('foo', binascii.a2b_hex(b'd76df8e7aefc'))
cookie = handler._cookies['foo']
- match = re.match(b(r'12345678\|([0-9]+)\|([0-9a-f]+)'), cookie)
+ match = re.match(br'12345678\|([0-9]+)\|([0-9a-f]+)', cookie)
self.assertTrue(match)
timestamp = match.group(1)
sig = match.group(2)
def test_uimodule_unescaped(self):
response = self.fetch("/linkify")
self.assertEqual(response.body,
- b("<a href=\"http://example.com\">http://example.com</a>"))
+ b"<a href=\"http://example.com\">http://example.com</a>")
def test_uimodule_resources(self):
response = self.fetch("/uimodule_resources")
- self.assertEqual(response.body, b("""\
+ self.assertEqual(response.body, b"""\
<html><head><link href="/base.css" type="text/css" rel="stylesheet"/><link href="/foo.css" type="text/css" rel="stylesheet"/>
<style type="text/css">
.entry { margin-bottom: 1em; }
//]]>
</script>
<script src="/analytics.js"/>
-</body></html>"""))
+</body></html>""")
def test_optional_path(self):
self.assertEqual(self.fetch_json("/optional_path/foo"),
from tornado.escape import json_decode
from tornado.test.httpserver_test import TypeCheckHandler
from tornado.testing import AsyncHTTPTestCase
-from tornado.util import b, u
+from tornado.util import u
from tornado.web import RequestHandler
from tornado.wsgi import WSGIApplication, WSGIContainer
obj = __import__('.'.join(parts[:-1]), None, None, [parts[-1]], 0)
return getattr(obj, parts[-1])
-# Fake byte literal support: In python 2.6+, you can say b"foo" to get
-# a byte literal (str in 2.x, bytes in 3.x). There's no way to do this
-# in a way that supports 2.5, though, so we need a function wrapper
-# to convert our string literals. b() should only be applied to literal
-# latin1 strings. Once we drop support for 2.5, we can remove this function
-# and just use byte literals.
-#
# Fake unicode literal support: Python 3.2 doesn't have the u'' marker for
# literal strings, and alternative solutions like "from __future__ import
# unicode_literals" have other problems (see PEP 414). u() can be applied
# to ascii strings that include \u escapes (but they must not contain
# literal non-ascii characters).
if type('') is not type(b''):
- def b(s):
- return s.encode('latin1')
def u(s):
return s
bytes_type = bytes
unicode_type = str
basestring_type = str
else:
- def b(s):
- return s
def u(s):
return s.decode('unicode_escape')
bytes_type = str
from tornado import stack_context
from tornado import template
from tornado.escape import utf8, _unicode
-from tornado.util import b, bytes_type, import_object, ObjectDict, raise_exc_info, unicode_type
+from tornado.util import bytes_type, import_object, ObjectDict, raise_exc_info, unicode_type
try:
from io import BytesIO # python 3
# If \n is allowed into the header, it is possible to inject
# additional headers or split the request. Also cap length to
# prevent obviously erroneous values.
- if len(value) > 4000 or re.search(b(r"[\x00-\x1f]"), value):
+ if len(value) > 4000 or re.search(br"[\x00-\x1f]", value):
raise ValueError("Unsafe header value %r", value)
return value
assert isinstance(status, int) and 300 <= status <= 399
self.set_status(status)
# Remove whitespace
- url = re.sub(b(r"[\x00-\x20]+"), "", utf8(url))
+ url = re.sub(br"[\x00-\x20]+", "", utf8(url))
self.set_header("Location", urlparse.urljoin(utf8(self.request.uri),
url))
self.finish()
import tornado.web
from tornado.log import gen_log, app_log
-from tornado.util import bytes_type, b
+from tornado.util import bytes_type
class WebSocketHandler(tornado.web.RequestHandler):
from tornado.log import access_log
from tornado import web
from tornado.escape import native_str, utf8, parse_qs_bytes
-from tornado.util import b, bytes_type, unicode_type
+from tornado.util import bytes_type, unicode_type
try:
from io import BytesIO # python 3
``error`` attribute when closing it.
* Python 2.5 is no longer supported.
* Installation under Python 3 no longer uses ``2to3``.
+* `tornado.util.b` (which was only intended for internal use) is gone.