From e575d7b52c634bf480e6abc21d90f5d82294c220 Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sat, 7 Jul 2018 00:19:38 -0400 Subject: [PATCH] locale,netutil,process: Minor deprecation/py3 updates --- tornado/locale.py | 17 +++------------- tornado/netutil.py | 48 ++++++++++++++++------------------------------ tornado/process.py | 16 +++------------- 3 files changed, 23 insertions(+), 58 deletions(-) diff --git a/tornado/locale.py b/tornado/locale.py index d45172f3b..2931010ce 100644 --- a/tornado/locale.py +++ b/tornado/locale.py @@ -44,14 +44,12 @@ from __future__ import absolute_import, division, print_function import codecs import csv import datetime -from io import BytesIO import numbers import os import re from tornado import escape from tornado.log import gen_log -from tornado.util import PY3 from tornado._locale_data import LOCALE_NAMES @@ -148,18 +146,9 @@ def load_translations(directory, encoding=None): # in most cases but is common with CSV files because Excel # cannot read utf-8 files without a BOM. encoding = 'utf-8-sig' - if PY3: - # python 3: csv.reader requires a file open in text mode. - # Force utf8 to avoid dependence on $LANG environment variable. - f = open(full_path, "r", encoding=encoding) - else: - # python 2: csv can only handle byte strings (in ascii-compatible - # encodings), which we decode below. Transcode everything into - # utf8 before passing it to csv.reader. - f = BytesIO() - with codecs.open(full_path, "r", encoding=encoding) as infile: - f.write(escape.utf8(infile.read())) - f.seek(0) + # python 3: csv.reader requires a file open in text mode. + # Specify an encoding to avoid dependence on $LANG environment variable. + f = open(full_path, "r", encoding=encoding) _translations[locale] = {} for i, row in enumerate(csv.reader(f)): if not row or len(row) < 2: diff --git a/tornado/netutil.py b/tornado/netutil.py index e63683ad7..c7682be3a 100644 --- a/tornado/netutil.py +++ b/tornado/netutil.py @@ -21,39 +21,25 @@ import errno import os import sys import socket +import ssl import stat from tornado.concurrent import dummy_executor, run_on_executor from tornado import gen from tornado.ioloop import IOLoop from tornado.platform.auto import set_close_exec -from tornado.util import PY3, Configurable, errno_from_exception - -try: - import ssl -except ImportError: - # ssl is not available on Google App Engine - ssl = None - -if PY3: - xrange = range - -if ssl is not None: - # Note that the naming of ssl.Purpose is confusing; the purpose - # of a context is to authentiate the opposite side of the connection. - _client_ssl_defaults = ssl.create_default_context( - ssl.Purpose.SERVER_AUTH) - _server_ssl_defaults = ssl.create_default_context( - ssl.Purpose.CLIENT_AUTH) - if hasattr(ssl, 'OP_NO_COMPRESSION'): - # See netutil.ssl_options_to_context - _client_ssl_defaults.options |= ssl.OP_NO_COMPRESSION - _server_ssl_defaults.options |= ssl.OP_NO_COMPRESSION -else: - # Google App Engine - _client_ssl_defaults = dict(cert_reqs=None, - ca_certs=None) - _server_ssl_defaults = {} +from tornado.util import Configurable, errno_from_exception + +# Note that the naming of ssl.Purpose is confusing; the purpose +# of a context is to authentiate the opposite side of the connection. +_client_ssl_defaults = ssl.create_default_context( + ssl.Purpose.SERVER_AUTH) +_server_ssl_defaults = ssl.create_default_context( + ssl.Purpose.CLIENT_AUTH) +if hasattr(ssl, 'OP_NO_COMPRESSION'): + # See netutil.ssl_options_to_context + _client_ssl_defaults.options |= ssl.OP_NO_COMPRESSION + _server_ssl_defaults.options |= ssl.OP_NO_COMPRESSION # ThreadedResolver runs getaddrinfo on a thread. If the hostname is unicode, # getaddrinfo attempts to import encodings.idna. If this is done at @@ -241,7 +227,7 @@ def add_accept_handler(sock, callback): # Instead, we use the (default) listen backlog as a rough # heuristic for the number of connections we can reasonably # accept at once. - for i in xrange(_DEFAULT_BACKLOG): + for i in range(_DEFAULT_BACKLOG): if removed[0]: # The socket was probably closed return @@ -321,7 +307,7 @@ class Resolver(Configurable): def configurable_default(cls): return DefaultExecutorResolver - def resolve(self, host, port, family=socket.AF_UNSPEC, callback=None): + def resolve(self, host, port, family=socket.AF_UNSPEC): """Resolves an address. The ``host`` argument is a string which may be a hostname or a @@ -339,9 +325,9 @@ class Resolver(Configurable): .. versionchanged:: 4.4 Standardized all implementations to raise `IOError`. - .. deprecated:: 5.1 - The ``callback`` argument is deprecated and will be removed in 6.0. + .. versionchanged:: 6.0 The ``callback`` argument was removed. Use the returned awaitable object instead. + """ raise NotImplementedError() diff --git a/tornado/process.py b/tornado/process.py index 122fd7e14..34dd2e084 100644 --- a/tornado/process.py +++ b/tornado/process.py @@ -34,7 +34,7 @@ from tornado.iostream import PipeIOStream from tornado.log import gen_log from tornado.platform.auto import set_close_exec from tornado import stack_context -from tornado.util import errno_from_exception, PY3 +from tornado.util import errno_from_exception try: import multiprocessing @@ -42,18 +42,8 @@ except ImportError: # Multiprocessing is not available on Google App Engine. multiprocessing = None -if PY3: - long = int - # Re-export this exception for convenience. -try: - CalledProcessError = subprocess.CalledProcessError -except AttributeError: - # The subprocess module exists in Google App Engine, but is empty. - # This module isn't very useful in that case, but it should - # at least be importable. - if 'APPENGINE_RUNTIME' not in os.environ: - raise +CalledProcessError = subprocess.CalledProcessError def cpu_count(): @@ -80,7 +70,7 @@ def _reseed_random(): # random.seed (at least as of python 2.6). If os.urandom is not # available, we mix in the pid in addition to a timestamp. try: - seed = long(hexlify(os.urandom(16)), 16) + seed = int(hexlify(os.urandom(16)), 16) except NotImplementedError: seed = int(time.time() * 1000) ^ os.getpid() random.seed(seed) -- 2.47.2