from tornado import httpserver
from tornado import ioloop
from tornado import web
-from tornado.util import bytes_type
def start(port, root_directory="/tmp/s3", bucket_depth=0):
"""Starts the mock S3 server on the given port at the given path."""
''.join(parts))
def _render_parts(self, value, parts=[]):
- if isinstance(value, (unicode, bytes_type)):
+ if isinstance(value, (unicode, bytes)):
parts.append(escape.xhtml_escape(value))
elif isinstance(value, int) or isinstance(value, long):
parts.append(str(value))
from tornado.ioloop import IOLoop
from tornado.options import define, options, parse_command_line
-from tornado.util import bytes_type
from tornado.websocket import WebSocketHandler
from tornado.web import Application
class EchoHandler(WebSocketHandler):
def on_message(self, message):
- self.write_message(message, binary=isinstance(message, bytes_type))
+ self.write_message(message, binary=isinstance(message, bytes))
def get_compression_options(self):
return {}
from tornado.httputil import url_concat
from tornado.log import gen_log
from tornado.stack_context import ExceptionStackContext
-from tornado.util import bytes_type, u, unicode_type, ArgReplacer
+from tornado.util import u, unicode_type, ArgReplacer
try:
import urlparse # py2
args["cancel_url"] = urlparse.urljoin(
self.request.full_url(), cancel_uri)
if extended_permissions:
- if isinstance(extended_permissions, (unicode_type, bytes_type)):
+ if isinstance(extended_permissions, (unicode_type, bytes)):
extended_permissions = [extended_permissions]
args["req_perms"] = ",".join(extended_permissions)
self.redirect("http://www.facebook.com/login.php?" +
from tornado.escape import utf8, native_str
from tornado.httpclient import HTTPResponse, HTTPError, AsyncHTTPClient, main
-from tornado.util import bytes_type
class CurlAsyncHTTPClient(AsyncHTTPClient):
write_function = request.streaming_callback
else:
write_function = buffer.write
- if bytes_type is str: # py2
+ if bytes is str: # py2
curl.setopt(pycurl.WRITEFUNCTION, write_function)
else: # py3
# Upstream pycurl doesn't support py3, but ubuntu 12.10 includes
import re
import sys
-from tornado.util import bytes_type, unicode_type, basestring_type, u
+from tornado.util import unicode_type, basestring_type, u
try:
from urllib.parse import parse_qs as _parse_qs # py3
return encoded
-_UTF8_TYPES = (bytes_type, type(None))
+_UTF8_TYPES = (bytes, type(None))
def utf8(value):
"""
if isinstance(value, _TO_UNICODE_TYPES):
return value
- if not isinstance(value, bytes_type):
+ if not isinstance(value, bytes):
raise TypeError(
"Expected bytes, unicode, or None; got %r" % type(value)
)
"""
if isinstance(value, _BASESTRING_TYPES):
return value
- if not isinstance(value, bytes_type):
+ if not isinstance(value, bytes):
raise TypeError(
"Expected bytes, unicode, or None; got %r" % type(value)
)
return list(recursive_unicode(i) for i in obj)
elif isinstance(obj, tuple):
return tuple(recursive_unicode(i) for i in obj)
- elif isinstance(obj, bytes_type):
+ elif isinstance(obj, bytes):
return to_unicode(obj)
else:
return obj
from tornado.escape import native_str, parse_qs_bytes, utf8
from tornado.log import gen_log
-from tornado.util import ObjectDict, bytes_type
+from tornado.util import ObjectDict
try:
import Cookie # py2
Use ``request.connection`` and the `.HTTPConnection` methods
to write the response.
"""
- assert isinstance(chunk, bytes_type)
+ assert isinstance(chunk, bytes)
self.connection.write(chunk, callback=callback)
def finish(self):
from tornado.log import gen_log, app_log
from tornado.netutil import ssl_wrap_socket, ssl_match_hostname, SSLCertificateError
from tornado import stack_context
-from tornado.util import bytes_type, errno_from_exception
+from tornado.util import errno_from_exception
try:
from tornado.platform.posix import _set_nonblocking
.. versionchanged:: 4.0
Now returns a `.Future` if no callback is given.
"""
- assert isinstance(data, bytes_type)
+ assert isinstance(data, bytes)
self._check_closed()
# We use bool(_write_buffer) as a proxy for write_buffer_size>0,
# so never put empty strings in the buffer.
from tornado import escape
from tornado.log import app_log
-from tornado.util import bytes_type, ObjectDict, exec_in, unicode_type
+from tornado.util import ObjectDict, exec_in, unicode_type
try:
from cStringIO import StringIO # py2
"linkify": escape.linkify,
"datetime": datetime,
"_tt_utf8": escape.utf8, # for internal use
- "_tt_string_types": (unicode_type, bytes_type),
+ "_tt_string_types": (unicode_type, bytes),
# __name__ and __loader__ allow the traceback mechanism to find
# the generated source code.
"__name__": self.name.replace('.', '_'),
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 u, unicode_type, bytes_type
+from tornado.util import u, unicode_type
from tornado.test.util import unittest
linkify_tests = [
# convert automatically if they are utf8; on python 3 byte strings
# are not allowed.
self.assertEqual(json_decode(json_encode(u("\u00e9"))), u("\u00e9"))
- if bytes_type is str:
+ if bytes is str:
self.assertEqual(json_decode(json_encode(utf8(u("\u00e9")))), u("\u00e9"))
self.assertRaises(UnicodeDecodeError, json_encode, b"\xe9")
from tornado.stack_context import ExceptionStackContext, NullContext
from tornado.testing import AsyncHTTPTestCase, bind_unused_port, gen_test, ExpectLog
from tornado.test.util import unittest, skipOnTravis
-from tornado.util import u, bytes_type
+from tornado.util import u
from tornado.web import Application, RequestHandler, url
def test_types(self):
response = self.fetch("/hello")
- self.assertEqual(type(response.body), bytes_type)
+ self.assertEqual(type(response.body), bytes)
self.assertEqual(type(response.headers["Content-Type"]), str)
self.assertEqual(type(response.code), int)
self.assertEqual(type(response.effective_url), str)
from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.testing import AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase, ExpectLog, gen_test
from tornado.test.util import unittest, skipOnTravis
-from tornado.util import u, bytes_type
+from tornado.util import u
from tornado.web import Application, RequestHandler, asynchronous, stream_request_body
from contextlib import closing
import datetime
# secure cookies
self.check_type('arg_key', list(self.request.arguments.keys())[0], str)
- self.check_type('arg_value', list(self.request.arguments.values())[0][0], bytes_type)
+ self.check_type('arg_value', list(self.request.arguments.values())[0][0], bytes)
def post(self):
- self.check_type('body', self.request.body, bytes_type)
+ self.check_type('body', self.request.body, bytes)
self.write(self.errors)
def get(self):
# if the data is not utf8. On python 2 parse_qs will work,
# but then the recursive_unicode call in EchoHandler will
# fail.
- if str is bytes_type:
+ if str is bytes:
return
with ExpectLog(gen_log, 'Invalid x-www-form-urlencoded body'):
response = self.fetch(
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 u, bytes_type, basestring_type
+from tornado.util import u, basestring_type
@contextlib.contextmanager
def test_utf8_logging(self):
self.logger.error(u("\u00e9").encode("utf8"))
- if issubclass(bytes_type, basestring_type):
+ if issubclass(bytes, basestring_type):
# on python 2, utf8 byte strings (and by extension ascii byte
# strings) are passed through as-is.
self.assertEqual(self.get_output(), utf8(u("\u00e9")))
from tornado.escape import utf8, native_str, to_unicode
from tornado.template import Template, DictLoader, ParseError, Loader
from tornado.test.util import unittest
-from tornado.util import u, bytes_type, ObjectDict, unicode_type
+from tornado.util import u, ObjectDict, unicode_type
class TemplateTest(unittest.TestCase):
"{% autoescape py_escape %}s = {{ name }}\n"})
def py_escape(s):
- self.assertEqual(type(s), bytes_type)
+ self.assertEqual(type(s), bytes)
return repr(native_str(s))
def render(template, name):
from tornado.template import DictLoader
from tornado.testing import AsyncHTTPTestCase, ExpectLog, gen_test
from tornado.test.util import unittest
-from tornado.util import u, bytes_type, ObjectDict, unicode_type
+from tornado.util import u, ObjectDict, unicode_type
from tornado.web import RequestHandler, authenticated, Application, asynchronous, url, HTTPError, StaticFileHandler, _create_signature_v1, create_signed_value, decode_signed_value, ErrorHandler, UIModule, MissingArgumentError, stream_request_body, Finish
import binascii
if type(key) != str:
raise Exception("incorrect type for key: %r" % type(key))
for value in self.request.arguments[key]:
- if type(value) != bytes_type:
+ if type(value) != bytes:
raise Exception("incorrect type for value: %r" %
type(value))
for value in self.get_arguments(key):
if list(self.cookies.keys()) != ['asdf']:
raise Exception("unexpected values for cookie keys: %r" %
self.cookies.keys())
- self.check_type('get_secure_cookie', self.get_secure_cookie('asdf'), bytes_type)
+ self.check_type('get_secure_cookie', self.get_secure_cookie('asdf'), bytes)
self.check_type('get_cookie', self.get_cookie('asdf'), str)
- self.check_type('xsrf_token', self.xsrf_token, bytes_type)
+ self.check_type('xsrf_token', self.xsrf_token, bytes)
self.check_type('xsrf_form_html', self.xsrf_form_html(), str)
self.check_type('reverse_url', self.reverse_url('typecheck', 'foo'), str)
class DecodeArgHandler(RequestHandler):
def decode_argument(self, value, name=None):
- if type(value) != bytes_type:
+ if type(value) != bytes:
raise Exception("unexpected type for value: %r" % type(value))
# use self.request.arguments directly to avoid recursion
if 'encoding' in self.request.arguments:
def get(self, arg):
def describe(s):
- if type(s) == bytes_type:
+ if type(s) == bytes:
return ["bytes", native_str(binascii.b2a_hex(s))]
elif type(s) == unicode_type:
return ["unicode", s]
if type('') is not type(b''):
def u(s):
return s
- bytes_type = bytes
unicode_type = str
basestring_type = str
else:
def u(s):
return s.decode('unicode_escape')
- bytes_type = str
unicode_type = unicode
basestring_type = basestring
some parameters.
"""
base = cls.configurable_base()
- if isinstance(impl, (unicode_type, bytes_type)):
+ if isinstance(impl, (unicode_type, bytes)):
impl = import_object(impl)
if impl is not None and not issubclass(impl, cls):
raise ValueError("Invalid subclass of %s" % cls)
from tornado import stack_context
from tornado import template
from tornado.escape import utf8, _unicode
-from tornado.util import bytes_type, import_object, ObjectDict, raise_exc_info, unicode_type, _websocket_mask
+from tornado.util import import_object, ObjectDict, raise_exc_info, unicode_type, _websocket_mask
try:
_INVALID_HEADER_CHAR_RE = re.compile(br"[\x00-\x1f]")
def _convert_header_value(self, value):
- if isinstance(value, bytes_type):
+ if isinstance(value, bytes):
pass
elif isinstance(value, unicode_type):
value = value.encode('utf-8')
raise RuntimeError("Cannot write() after finish(). May be caused "
"by using async operations without the "
"@asynchronous decorator.")
- if not isinstance(chunk, (bytes_type, unicode_type, dict)):
+ if not isinstance(chunk, (bytes, unicode_type, dict)):
raise TypeError("write() only accepts bytes, unicode, and dict objects")
if isinstance(chunk, dict):
chunk = escape.json_encode(chunk)
js_embed.append(utf8(embed_part))
file_part = module.javascript_files()
if file_part:
- if isinstance(file_part, (unicode_type, bytes_type)):
+ if isinstance(file_part, (unicode_type, bytes)):
js_files.append(file_part)
else:
js_files.extend(file_part)
css_embed.append(utf8(embed_part))
file_part = module.css_files()
if file_part:
- if isinstance(file_part, (unicode_type, bytes_type)):
+ if isinstance(file_part, (unicode_type, bytes)):
css_files.append(file_part)
else:
css_files.extend(file_part)
if include_body:
content = self.get_content(self.absolute_path, start, end)
- if isinstance(content, bytes_type):
+ if isinstance(content, bytes):
content = [content]
for chunk in content:
self.write(chunk)
"""
data = cls.get_content(abspath)
hasher = hashlib.md5()
- if isinstance(data, bytes_type):
+ if isinstance(data, bytes):
hasher.update(data)
else:
for chunk in data:
def javascript_files(self):
result = []
for f in self._get_resources("javascript_files"):
- if isinstance(f, (unicode_type, bytes_type)):
+ if isinstance(f, (unicode_type, bytes)):
result.append(f)
else:
result.extend(f)
def css_files(self):
result = []
for f in self._get_resources("css_files"):
- if isinstance(f, (unicode_type, bytes_type)):
+ if isinstance(f, (unicode_type, bytes)):
result.append(f)
else:
result.extend(f)
return self._path
converted_args = []
for a in args:
- if not isinstance(a, (unicode_type, bytes_type)):
+ if not isinstance(a, (unicode_type, bytes)):
a = str(a)
converted_args.append(escape.url_escape(utf8(a), plus=False))
return self._path % tuple(converted_args)
from tornado.log import gen_log, app_log
from tornado import simple_httpclient
from tornado.tcpclient import TCPClient
-from tornado.util import bytes_type, _websocket_mask
+from tornado.util import _websocket_mask
try:
from urllib.parse import urlparse # py2
else:
opcode = 0x1
message = tornado.escape.utf8(message)
- assert isinstance(message, bytes_type)
+ assert isinstance(message, bytes)
self._message_bytes_out += len(message)
flags = 0
if self._compressor:
def write_ping(self, data):
"""Send ping frame."""
- assert isinstance(data, bytes_type)
+ assert isinstance(data, bytes)
self._write_frame(True, 0x9, data)
def _receive_frame(self):
from tornado.log import access_log
from tornado import web
from tornado.escape import native_str
-from tornado.util import bytes_type, unicode_type
+from tornado.util import unicode_type
try:
# here to minimize the temptation to use them in non-wsgi contexts.
if str is unicode_type:
def to_wsgi_str(s):
- assert isinstance(s, bytes_type)
+ assert isinstance(s, bytes)
return s.decode('latin1')
def from_wsgi_str(s):
return s.encode('latin1')
else:
def to_wsgi_str(s):
- assert isinstance(s, bytes_type)
+ assert isinstance(s, bytes)
return s
def from_wsgi_str(s):