From: INADA Naoki Date: Mon, 25 Aug 2014 05:56:49 +0000 (+0900) Subject: Replace tornado.util.bytes_type to bytes X-Git-Tag: v4.1.0b1~102^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6596d55a4595d5234a1898531756176eefb5755;p=thirdparty%2Ftornado.git Replace tornado.util.bytes_type to bytes --- diff --git a/demos/s3server/s3server.py b/demos/s3server/s3server.py index 87816c328..5a4805694 100644 --- a/demos/s3server/s3server.py +++ b/demos/s3server/s3server.py @@ -42,7 +42,6 @@ from tornado import escape 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.""" @@ -87,7 +86,7 @@ class BaseRequestHandler(web.RequestHandler): ''.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)) diff --git a/maint/test/websocket/server.py b/maint/test/websocket/server.py index 305bd7468..602cc506d 100644 --- a/maint/test/websocket/server.py +++ b/maint/test/websocket/server.py @@ -2,7 +2,6 @@ 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 @@ -10,7 +9,7 @@ define('port', default=9000) 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 {} diff --git a/tornado/auth.py b/tornado/auth.py index 7bd3fa1ed..c503442a5 100644 --- a/tornado/auth.py +++ b/tornado/auth.py @@ -76,7 +76,7 @@ from tornado import escape 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 @@ -1112,7 +1112,7 @@ class FacebookMixin(object): 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?" + diff --git a/tornado/curl_httpclient.py b/tornado/curl_httpclient.py index 2ed6fcf61..9c2aeb9f2 100644 --- a/tornado/curl_httpclient.py +++ b/tornado/curl_httpclient.py @@ -32,7 +32,6 @@ from tornado import stack_context from tornado.escape import utf8, native_str from tornado.httpclient import HTTPResponse, HTTPError, AsyncHTTPClient, main -from tornado.util import bytes_type class CurlAsyncHTTPClient(AsyncHTTPClient): @@ -307,7 +306,7 @@ def _curl_setup_request(curl, request, buffer, headers): 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 diff --git a/tornado/escape.py b/tornado/escape.py index 48fa673c1..24be2264b 100644 --- a/tornado/escape.py +++ b/tornado/escape.py @@ -25,7 +25,7 @@ from __future__ import absolute_import, division, print_function, with_statement 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 @@ -187,7 +187,7 @@ else: return encoded -_UTF8_TYPES = (bytes_type, type(None)) +_UTF8_TYPES = (bytes, type(None)) def utf8(value): @@ -215,7 +215,7 @@ def to_unicode(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) ) @@ -246,7 +246,7 @@ def to_basestring(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) ) @@ -264,7 +264,7 @@ def recursive_unicode(obj): 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 diff --git a/tornado/httputil.py b/tornado/httputil.py index c472998fa..126db40e3 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -33,7 +33,7 @@ import time 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 @@ -379,7 +379,7 @@ class HTTPServerRequest(object): 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): diff --git a/tornado/iostream.py b/tornado/iostream.py index 3726a768e..94fbb9476 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -39,7 +39,7 @@ from tornado import ioloop 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 @@ -324,7 +324,7 @@ class BaseIOStream(object): .. 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. diff --git a/tornado/template.py b/tornado/template.py index 4dcec5d5f..3882ed02a 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -199,7 +199,7 @@ import threading 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 @@ -261,7 +261,7 @@ class Template(object): "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('.', '_'), diff --git a/tornado/test/escape_test.py b/tornado/test/escape_test.py index 9abc74803..7cc81abf2 100644 --- a/tornado/test/escape_test.py +++ b/tornado/test/escape_test.py @@ -5,7 +5,7 @@ from __future__ import absolute_import, division, print_function, with_statement 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 = [ @@ -212,6 +212,6 @@ class EscapeTestCase(unittest.TestCase): # 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") diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index 104ed632c..123b2cfd4 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -20,7 +20,7 @@ from tornado import netutil 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 @@ -287,7 +287,7 @@ Transfer-Encoding: chunked 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) diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 5d1b780e3..24056338a 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -14,7 +14,7 @@ from tornado.netutil import ssl_options_to_context 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 @@ -293,10 +293,10 @@ class TypeCheckHandler(RequestHandler): # 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): @@ -354,7 +354,7 @@ class HTTPServerTest(AsyncHTTPTestCase): # 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( diff --git a/tornado/test/log_test.py b/tornado/test/log_test.py index ee832c541..6fad5ca02 100644 --- a/tornado/test/log_test.py +++ b/tornado/test/log_test.py @@ -29,7 +29,7 @@ from tornado.escape import utf8 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 @@ -96,7 +96,7 @@ class LogFormatterTest(unittest.TestCase): 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"))) diff --git a/tornado/test/template_test.py b/tornado/test/template_test.py index 6d8b624eb..ac1fbbd04 100644 --- a/tornado/test/template_test.py +++ b/tornado/test/template_test.py @@ -7,7 +7,7 @@ import traceback 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): @@ -374,7 +374,7 @@ raw: {% raw name %}""", "{% 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): diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index 3e7176cef..1ad3794cb 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -10,7 +10,7 @@ from tornado.simple_httpclient import SimpleAsyncHTTPClient 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 @@ -302,7 +302,7 @@ class EchoHandler(RequestHandler): 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): @@ -370,10 +370,10 @@ class TypeCheckHandler(RequestHandler): 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) @@ -399,7 +399,7 @@ class TypeCheckHandler(RequestHandler): 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: @@ -409,7 +409,7 @@ class DecodeArgHandler(RequestHandler): 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] diff --git a/tornado/util.py b/tornado/util.py index b6e06c678..c86fe983a 100644 --- a/tornado/util.py +++ b/tornado/util.py @@ -115,13 +115,11 @@ def import_object(name): 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 @@ -237,7 +235,7 @@ class Configurable(object): 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) diff --git a/tornado/web.py b/tornado/web.py index aa7c0134a..afc72facb 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -84,7 +84,7 @@ from tornado.log import access_log, app_log, gen_log 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: @@ -341,7 +341,7 @@ class RequestHandler(object): _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') @@ -649,7 +649,7 @@ class RequestHandler(object): 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) @@ -674,7 +674,7 @@ class RequestHandler(object): 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) @@ -683,7 +683,7 @@ class RequestHandler(object): 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) @@ -2169,7 +2169,7 @@ class StaticFileHandler(RequestHandler): 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) @@ -2340,7 +2340,7 @@ class StaticFileHandler(RequestHandler): """ 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: @@ -2715,7 +2715,7 @@ class TemplateModule(UIModule): 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) @@ -2727,7 +2727,7 @@ class TemplateModule(UIModule): 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) @@ -2832,7 +2832,7 @@ class URLSpec(object): 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) diff --git a/tornado/websocket.py b/tornado/websocket.py index f14077edd..c414f7655 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -36,7 +36,7 @@ from tornado.iostream import StreamClosedError 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 @@ -651,7 +651,7 @@ class WebSocketProtocol13(WebSocketProtocol): 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: @@ -664,7 +664,7 @@ class WebSocketProtocol13(WebSocketProtocol): 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): diff --git a/tornado/wsgi.py b/tornado/wsgi.py index 05d1e7495..f3aa66503 100644 --- a/tornado/wsgi.py +++ b/tornado/wsgi.py @@ -41,7 +41,7 @@ from tornado import httputil 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: @@ -55,7 +55,7 @@ except ImportError: # 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): @@ -63,7 +63,7 @@ if str is unicode_type: 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):