Merged default into 3.4 branch.  3.4 branch is now effectively 3.4.1rc1.
diff --git a/.hgeol b/.hgeol
index aad79c2..2919f76 100644
--- a/.hgeol
+++ b/.hgeol
@@ -38,6 +38,8 @@
 
 Lib/venv/scripts/nt/* = BIN
 
+Lib/test/coding20731.py = BIN
+
 # All other files (which presumably are human-editable) are "native".
 # This must be the last rule!
 
diff --git a/.hgignore b/.hgignore
index 6128c2b..91f02fd 100644
--- a/.hgignore
+++ b/.hgignore
@@ -24,11 +24,6 @@
 reflog.txt$
 tags$
 Lib/plat-mac/errors.rsrc.df.rsrc
-Doc/tools/sphinx/
-Doc/tools/docutils/
-Doc/tools/jinja/
-Doc/tools/jinja2/
-Doc/tools/pygments/
 Misc/python.pc
 Misc/python-config.sh$
 Modules/Setup$
diff --git a/.hgtags b/.hgtags
index 460d724..9c3af09 100644
--- a/.hgtags
+++ b/.hgtags
@@ -120,6 +120,9 @@
 c3896275c0f61b2510a6c7e6c458a750359a91b8 v3.3.3
 fa92f5f940c6c0d839d7f0611e4b717606504a3c v3.3.4rc1
 7ff62415e4263c432c8acf6e424224209211eadb v3.3.4
+9ec811df548ed154a9bf9815383a916d6df31b98 v3.3.5rc1
+ca5635efe090f78806188ac2758f9948596aa8b2 v3.3.5rc2
+62cf4e77f78564714e7ea3d4bf1479ca1fbd0758 v3.3.5
 46535f65e7f3bcdcf176f36d34bc1fed719ffd2b v3.4.0a1
 9265a2168e2cb2a84785d8717792acc661e6b692 v3.4.0a2
 dd9cdf90a5073510877e9dd5112f8e6cf20d5e89 v3.4.0a3
diff --git a/Doc/Makefile b/Doc/Makefile
index 82f5bef..d85fb94 100644
--- a/Doc/Makefile
+++ b/Doc/Makefile
@@ -5,7 +5,7 @@
 
 # You can set these variables from the command line.
 PYTHON       = python
-SVNROOT      = http://svn.python.org/projects
+SPHINXBUILD  = sphinx-build
 SPHINXOPTS   =
 PAPER        =
 SOURCES      =
@@ -14,14 +14,13 @@
 ALLSPHINXOPTS = -b $(BUILDER) -d build/doctrees -D latex_paper_size=$(PAPER) \
                 $(SPHINXOPTS) . build/$(BUILDER) $(SOURCES)
 
-.PHONY: help checkout update build html htmlhelp latex text changes linkcheck \
+.PHONY: help build html htmlhelp latex text changes linkcheck \
 	suspicious coverage doctest pydoc-topics htmlview clean dist check serve \
 	autobuild-dev autobuild-stable
 
 help:
 	@echo "Please use \`make <target>' where <target> is one of"
 	@echo "  clean      to remove build files"
-	@echo "  update     to update build tools"
 	@echo "  html       to make standalone HTML files"
 	@echo "  htmlhelp   to make HTML files and a HTML help project"
 	@echo "  latex      to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@@ -37,30 +36,8 @@
 	@echo "  check      to run a check for frequent markup errors"
 	@echo "  serve      to serve the documentation on the localhost (8000)"
 
-# Note: if you update versions here, do the same in make.bat and README.txt
-checkout:
-	@if [ ! -d tools/sphinx ]; then \
-	  echo "Checking out Sphinx..."; \
-	  svn checkout $(SVNROOT)/external/Sphinx-1.2/sphinx tools/sphinx; \
-	fi
-	@if [ ! -d tools/docutils ]; then \
-	  echo "Checking out Docutils..."; \
-	  svn checkout $(SVNROOT)/external/docutils-0.11/docutils tools/docutils; \
-	fi
-	@if [ ! -d tools/jinja2 ]; then \
-	  echo "Checking out Jinja..."; \
-	  svn checkout $(SVNROOT)/external/Jinja-2.3.1/jinja2 tools/jinja2; \
-	fi
-	@if [ ! -d tools/pygments ]; then \
-	  echo "Checking out Pygments..."; \
-	  svn checkout $(SVNROOT)/external/Pygments-1.6/pygments tools/pygments; \
-	fi
-
-update: clean checkout
-
-build: checkout
-	mkdir -p build/$(BUILDER) build/doctrees
-	$(PYTHON) tools/sphinx-build.py $(ALLSPHINXOPTS)
+build:
+	$(SPHINXBUILD) $(ALLSPHINXOPTS)
 	@echo
 
 html: BUILDER = html
@@ -120,10 +97,6 @@
 
 clean:
 	-rm -rf build/*
-	-rm -rf tools/sphinx
-	-rm -rf tools/pygments
-	-rm -rf tools/jinja2
-	-rm -rf tools/docutils
 
 dist:
 	rm -rf dist
@@ -184,7 +157,6 @@
 
 # for development releases: always build
 autobuild-dev:
-	make update
 	make dist SPHINXOPTS='-A daily=1 -A versionswitcher=1'
 	-make suspicious
 
diff --git a/Doc/README.txt b/Doc/README.txt
index 4a157d5..f4f6d81 100644
--- a/Doc/README.txt
+++ b/Doc/README.txt
@@ -3,36 +3,34 @@
 
 This directory contains the reStructuredText (reST) sources to the Python
 documentation.  You don't need to build them yourself, prebuilt versions are
-available at http://docs.python.org/download/.
+available at <http://docs.python.org/download/>.
 
 Documentation on the authoring Python documentation, including information about
 both style and markup, is available in the "Documenting Python" chapter of the
-developers guide (http://docs.python.org/devguide/documenting.html).
-There's also a chapter intended to point out differences to
-those familiar with the previous docs written in LaTeX.
+developers guide <http://docs.python.org/devguide/documenting.html>.
 
 
 Building the docs
 =================
 
-You need to have Python 2.4 or higher installed; the toolset used to build the
-docs is written in Python.  It is called *Sphinx*, it is not included in this
-tree, but maintained separately.  Also needed are the docutils, supplying the
-base markup that Sphinx uses, Jinja, a templating engine, and optionally
-Pygments, a code highlighter.
+You need to have Sphinx <http://sphinx-doc.org/> installed; it is the toolset
+used to build the docs.  It is not included in this tree, but maintained
+separately and available from PyPI <http://pypi.python.org/pypi/Sphinx>.
 
 
 Using make
 ----------
 
-Luckily, a Makefile has been prepared so that on Unix, provided you have
-installed Python and Subversion, you can just run ::
+A Makefile has been prepared so that on Unix, provided you have installed
+Sphinx, you can just run ::
 
    make html
 
-to check out the necessary toolset in the `tools/` subdirectory and build the
-HTML output files.  To view the generated HTML, point your favorite browser at
-the top-level index `build/html/index.html` after running "make".
+to build the HTML output files.  To view the generated HTML, point your favorite
+browser at the top-level index `build/html/index.html` after running "make".
+
+On Windows, we try to emulate the Makefile as closely as possible with a
+``make.bat`` file.
 
 To use a Python interpreter that's not called ``python``, use the standard
 way to set Makefile variables, using e.g. ::
@@ -74,43 +72,21 @@
    `tools/sphinxext/pyspecific.py` -- pydoc needs these to show topic and
    keyword help.
 
-A "make update" updates the Subversion checkouts in `tools/`.
+ * "suspicious", which checks the parsed markup for text that looks like
+   malformed and thus unconverted reST.
 
 
 Without make
 ------------
 
-You'll need to install the Sphinx package, either by checking it out via ::
+Install the Sphinx package and its dependencies from PyPI.
 
-   svn co http://svn.python.org/projects/external/Sphinx-1.0.7/sphinx tools/sphinx
+Then, from the ``Docs`` directory, run ::
 
-or by installing it from PyPI.
+   sphinx-build -b<builder> . build/<builder>
 
-Then, you need to install Docutils, either by checking it out via ::
-
-   svn co http://svn.python.org/projects/external/docutils-0.6/docutils tools/docutils
-
-or by installing it from http://docutils.sf.net/.
-
-You also need Jinja2, either by checking it out via ::
-
-   svn co http://svn.python.org/projects/external/Jinja-2.3.1/jinja2 tools/jinja2
-
-or by installing it from PyPI.
-
-You can optionally also install Pygments, either as a checkout via ::
-
-   svn co http://svn.python.org/projects/external/Pygments-1.3.1/pygments tools/pygments
-
-or from PyPI at http://pypi.python.org/pypi/Pygments.
-
-
-Then, make an output directory, e.g. under `build/`, and run ::
-
-   python tools/sphinx-build.py -b<builder> . build/<outputdirectory>
-
-where `<builder>` is one of html, text, latex, or htmlhelp (for explanations see
-the make targets above).
+where ``<builder>`` is one of html, text, latex, or htmlhelp (for explanations
+see the make targets above).
 
 
 Contributing
diff --git a/Doc/library/asyncio-subprocess.rst b/Doc/library/asyncio-subprocess.rst
index 861bcc0..44ef504 100644
--- a/Doc/library/asyncio-subprocess.rst
+++ b/Doc/library/asyncio-subprocess.rst
@@ -107,6 +107,8 @@
          The data read is buffered in memory, so do not use this method if the
          data size is large or unlimited.
 
+      This method is a :ref:`coroutine <coroutine>`.
+
    .. method:: kill()
 
       Kills the child. On Posix OSs the function sends :py:data:`SIGKILL` to
@@ -129,11 +131,13 @@
       to the child. On Windows the Win32 API function
       :c:func:`TerminateProcess` is called to stop the child.
 
-   .. method:: wait(self):
+   .. method:: wait():
 
       Wait for child process to terminate.  Set and return :attr:`returncode`
       attribute.
 
+      This method is a :ref:`coroutine <coroutine>`.
+
 
 Example
 -------
diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst
index d4cdfd3..8ece400 100644
--- a/Doc/library/http.client.rst
+++ b/Doc/library/http.client.rst
@@ -451,11 +451,25 @@
 
 .. method:: HTTPConnection.set_tunnel(host, port=None, headers=None)
 
-   Set the host and the port for HTTP Connect Tunnelling. Normally used when it
-   is required to a HTTPS Connection through a proxy server.
+   Set the host and the port for HTTP Connect Tunnelling. This allows running
+   the connection through a proxy server.
 
-   The headers argument should be a mapping of extra HTTP headers to send
-   with the CONNECT request.
+   The host and port arguments specify the endpoint of the tunneled connection
+   (i.e. the address included in the CONNECT request, *not* the address of the
+   proxy server).
+
+   The headers argument should be a mapping of extra HTTP headers to send with
+   the CONNECT request.
+
+   For example, to tunnel through a HTTPS proxy server running locally on port
+   8080, we would pass the address of the proxy to the :class:`HTTPSConnection`
+   constructor, and the address of the host that we eventually want to reach to
+   the :meth:`~HTTPConnection.set_tunnel` method::
+
+      >>> import http.client
+      >>> conn = http.client.HTTPSConnection("localhost", 8080)
+      >>> conn.set_tunnel("www.python.org")
+      >>> conn.request("HEAD","/index.html")
 
    .. versionadded:: 3.2
 
diff --git a/Doc/make.bat b/Doc/make.bat
index d6f7074..0b5b56e 100644
--- a/Doc/make.bat
+++ b/Doc/make.bat
@@ -1,7 +1,6 @@
 @@echo off
 setlocal
 
-set SVNROOT=http://svn.python.org/projects
 if "%PYTHON%" EQU "" set PYTHON=py -2
 if "%HTMLHELP%" EQU "" set HTMLHELP=%ProgramFiles%\HTML Help Workshop\hhc.exe
 if "%DISTVERSION%" EQU "" for /f "usebackq" %%v in (`%PYTHON% tools/sphinxext/patchlevel.py`) do set DISTVERSION=%%v
@@ -14,15 +13,11 @@
 if "%1" EQU "suspicious" goto build
 if "%1" EQU "linkcheck" goto build
 if "%1" EQU "changes" goto build
-if "%1" EQU "checkout" goto checkout
-if "%1" EQU "update" goto update
 
 :help
 set this=%~n0
 echo HELP
 echo.
-echo %this% checkout
-echo %this% update
 echo %this% html
 echo %this% htmlhelp
 echo %this% latex
@@ -33,20 +28,6 @@
 echo.
 goto end
 
-:checkout
-svn co %SVNROOT%/external/Sphinx-1.2/sphinx tools/sphinx
-svn co %SVNROOT%/external/docutils-0.11/docutils tools/docutils
-svn co %SVNROOT%/external/Jinja-2.3.1/jinja2 tools/jinja2
-svn co %SVNROOT%/external/Pygments-1.6/pygments tools/pygments
-goto end
-
-:update
-svn update tools/sphinx
-svn update tools/docutils
-svn update tools/jinja2
-svn update tools/pygments
-goto end
-
 :build
 if not exist build mkdir build
 if not exist build\%1 mkdir build\%1
diff --git a/Doc/tools/sphinx-build.py b/Doc/tools/sphinx-build.py
deleted file mode 100644
index a446dfa..0000000
--- a/Doc/tools/sphinx-build.py
+++ /dev/null
@@ -1,28 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-    Sphinx - Python documentation toolchain
-    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-    :copyright: 2007-2010 by Georg Brandl.
-    :license: Python license.
-"""
-
-import sys
-import warnings
-
-# Get rid of UserWarnings reported by pkg_resources.
-warnings.filterwarnings('ignore', category=UserWarning, module='jinja2')
-
-if __name__ == '__main__':
-
-    if sys.version_info[:3] < (2, 4, 0) or sys.version_info[:3] > (3, 0, 0):
-        sys.stderr.write("""\
-Error: Sphinx needs to be executed with Python 2.4 or newer (not 3.0 though).
-(If you run this from the Makefile, you can set the PYTHON variable
-to the path of an alternative interpreter executable, e.g.,
-``make html PYTHON=python2.5``).
-""")
-        sys.exit(1)
-
-    from sphinx import main
-    sys.exit(main(sys.argv))
diff --git a/Doc/tools/sphinxext/pyspecific.py b/Doc/tools/sphinxext/pyspecific.py
index 4e41446..31d8c06 100644
--- a/Doc/tools/sphinxext/pyspecific.py
+++ b/Doc/tools/sphinxext/pyspecific.py
@@ -16,6 +16,7 @@
 
 import sphinx
 from sphinx.util.nodes import split_explicit_title
+from sphinx.util.compat import Directive
 from sphinx.writers.html import HTMLTranslator
 from sphinx.writers.latex import LaTeXTranslator
 from sphinx.locale import versionlabels
@@ -27,7 +28,9 @@
     Body.enum.converters['lowerroman'] = \
     Body.enum.converters['upperroman'] = lambda x: None
 
-if sphinx.__version__[:3] < '1.2':
+SPHINX11 = sphinx.__version__[:3] < '1.2'
+
+if SPHINX11:
     # monkey-patch HTML translator to give versionmodified paragraphs a class
     def new_visit_versionmodified(self, node):
         self.body.append(self.starttag(node, 'p', CLASS=node['type']))
@@ -88,8 +91,6 @@
 
 # Support for marking up implementation details
 
-from sphinx.util.compat import Directive
-
 class ImplementationDetail(Directive):
 
     has_content = True
@@ -142,10 +143,6 @@
 
 # Support for documenting version of removal in deprecations
 
-from sphinx.locale import versionlabels
-from sphinx.util.compat import Directive
-
-
 class DeprecatedRemoved(Directive):
     has_content = True
     required_arguments = 2
@@ -171,16 +168,16 @@
             messages = []
         if self.content:
             self.state.nested_parse(self.content, self.content_offset, node)
-        if len(node):
             if isinstance(node[0], nodes.paragraph) and node[0].rawsource:
                 content = nodes.inline(node[0].rawsource, translatable=True)
                 content.source = node[0].source
                 content.line = node[0].line
                 content += node[0].children
                 node[0].replace_self(nodes.paragraph('', '', content))
-            node[0].insert(0, nodes.inline('', '%s: ' % text,
-                                           classes=['versionmodified']))
-        else:
+            if not SPHINX11:
+                node[0].insert(0, nodes.inline('', '%s: ' % text,
+                                               classes=['versionmodified']))
+        elif not SPHINX11:
             para = nodes.paragraph('', '',
                 nodes.inline('', '%s.' % text, classes=['versionmodified']))
             node.append(para)
@@ -188,6 +185,9 @@
         env.note_versionchange('deprecated', version[0], node, self.lineno)
         return [node] + messages
 
+# for Sphinx < 1.2
+versionlabels['deprecated-removed'] = DeprecatedRemoved._label
+
 
 # Support for including Misc/NEWS
 
diff --git a/Include/objimpl.h b/Include/objimpl.h
index 9a27ec3..3f21b70 100644
--- a/Include/objimpl.h
+++ b/Include/objimpl.h
@@ -265,7 +265,7 @@
 #define _PyGCHead_REFS(g) ((g)->gc.gc_refs >> _PyGC_REFS_SHIFT)
 #define _PyGCHead_SET_REFS(g, v) do { \
     (g)->gc.gc_refs = ((g)->gc.gc_refs & ~_PyGC_REFS_MASK) \
-        | (v << _PyGC_REFS_SHIFT); \
+        | (((size_t)(v)) << _PyGC_REFS_SHIFT);             \
     } while (0)
 #define _PyGCHead_DECREF(g) ((g)->gc.gc_refs -= 1 << _PyGC_REFS_SHIFT)
 
diff --git a/Include/pytime.h b/Include/pytime.h
index 52902f5..b0fc6d0 100644
--- a/Include/pytime.h
+++ b/Include/pytime.h
@@ -53,10 +53,19 @@
      (tv_end.tv_usec - tv_start.tv_usec) * 0.000001)
 
 #ifndef Py_LIMITED_API
+
+typedef enum {
+    /* Round towards zero. */
+    _PyTime_ROUND_DOWN=0,
+    /* Round away from zero. */
+    _PyTime_ROUND_UP
+} _PyTime_round_t;
+
 /* Convert a number of seconds, int or float, to time_t. */
 PyAPI_FUNC(int) _PyTime_ObjectToTime_t(
     PyObject *obj,
-    time_t *sec);
+    time_t *sec,
+    _PyTime_round_t);
 
 /* Convert a time_t to a PyLong. */
 PyAPI_FUNC(PyObject *) _PyLong_FromTime_t(
@@ -72,7 +81,8 @@
 PyAPI_FUNC(int) _PyTime_ObjectToTimeval(
     PyObject *obj,
     time_t *sec,
-    long *usec);
+    long *usec,
+    _PyTime_round_t);
 
 /* Convert a number of seconds, int or float, to a timespec structure.
    nsec is in the range [0; 999999999] and rounded towards zero.
@@ -80,7 +90,8 @@
 PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
     PyObject *obj,
     time_t *sec,
-    long *nsec);
+    long *nsec,
+    _PyTime_round_t);
 #endif
 
 /* Dummy to force linking. */
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index aa42745..367c5fb 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -136,6 +136,8 @@
 
     def add_reader(self, fd, callback, *args):
         """Add a reader callback."""
+        if self._selector is None:
+            raise RuntimeError('Event loop is closed')
         handle = events.Handle(callback, args, self)
         try:
             key = self._selector.get_key(fd)
@@ -151,6 +153,8 @@
 
     def remove_reader(self, fd):
         """Remove a reader callback."""
+        if self._selector is None:
+            return False
         try:
             key = self._selector.get_key(fd)
         except KeyError:
@@ -171,6 +175,8 @@
 
     def add_writer(self, fd, callback, *args):
         """Add a writer callback.."""
+        if self._selector is None:
+            raise RuntimeError('Event loop is closed')
         handle = events.Handle(callback, args, self)
         try:
             key = self._selector.get_key(fd)
@@ -186,6 +192,8 @@
 
     def remove_writer(self, fd):
         """Remove a writer callback."""
+        if self._selector is None:
+            return False
         try:
             key = self._selector.get_key(fd)
         except KeyError:
@@ -702,8 +710,7 @@
         if self._buffer:
             try:
                 n = self._sock.send(self._buffer)
-            except (BlockingIOError, InterruptedError,
-                    ssl.SSLWantWriteError):
+            except (BlockingIOError, InterruptedError, ssl.SSLWantWriteError):
                 n = 0
             except ssl.SSLWantReadError:
                 n = 0
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 19fa654..0967e7e 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -325,7 +325,7 @@
                         'Task got bad yield: {!r}'.format(result)))
         finally:
             self.__class__._current_tasks.pop(self._loop)
-        self = None
+            self = None  # Needed to break cycles when an exception occurs.
 
     def _wakeup(self, future):
         try:
diff --git a/Lib/asyncio/test_utils.py b/Lib/asyncio/test_utils.py
index dd87789..9c3656a 100644
--- a/Lib/asyncio/test_utils.py
+++ b/Lib/asyncio/test_utils.py
@@ -11,8 +11,7 @@
 import tempfile
 import threading
 import time
-import unittest
-import unittest.mock
+from unittest import mock
 
 from http.server import HTTPServer
 from wsgiref.simple_server import WSGIRequestHandler, WSGIServer
@@ -22,10 +21,11 @@
 except ImportError:  # pragma: no cover
     ssl = None
 
-from . import tasks
 from . import base_events
 from . import events
+from . import futures
 from . import selectors
+from . import tasks
 
 
 if sys.platform == 'win32':  # pragma: no cover
@@ -53,18 +53,14 @@
         gen.close()
 
 
-def run_until(loop, pred, timeout=None):
-    if timeout is not None:
-        deadline = time.time() + timeout
+def run_until(loop, pred, timeout=30):
+    deadline = time.time() + timeout
     while not pred():
         if timeout is not None:
             timeout = deadline - time.time()
             if timeout <= 0:
-                return False
-            loop.run_until_complete(tasks.sleep(timeout, loop=loop))
-        else:
-            run_briefly(loop)
-    return True
+                raise futures.TimeoutError()
+        loop.run_until_complete(tasks.sleep(0.001, loop=loop))
 
 
 def run_once(loop):
@@ -362,7 +358,7 @@
 
 
 def MockCallback(**kwargs):
-    return unittest.mock.Mock(spec=['__call__'], **kwargs)
+    return mock.Mock(spec=['__call__'], **kwargs)
 
 
 class MockPattern(str):
diff --git a/Lib/asyncio/windows_events.py b/Lib/asyncio/windows_events.py
index 60fb589..19f2588 100644
--- a/Lib/asyncio/windows_events.py
+++ b/Lib/asyncio/windows_events.py
@@ -213,7 +213,7 @@
         else:
             ov.ReadFile(conn.fileno(), nbytes)
 
-        def finish(trans, key, ov):
+        def finish_recv(trans, key, ov):
             try:
                 return ov.getresult()
             except OSError as exc:
@@ -222,7 +222,7 @@
                 else:
                     raise
 
-        return self._register(ov, conn, finish)
+        return self._register(ov, conn, finish_recv)
 
     def send(self, conn, buf, flags=0):
         self._register_with_iocp(conn)
@@ -232,7 +232,7 @@
         else:
             ov.WriteFile(conn.fileno(), buf)
 
-        def finish(trans, key, ov):
+        def finish_send(trans, key, ov):
             try:
                 return ov.getresult()
             except OSError as exc:
@@ -241,7 +241,7 @@
                 else:
                     raise
 
-        return self._register(ov, conn, finish)
+        return self._register(ov, conn, finish_send)
 
     def accept(self, listener):
         self._register_with_iocp(listener)
@@ -300,17 +300,17 @@
         ov = _overlapped.Overlapped(NULL)
         ov.ConnectNamedPipe(pipe.fileno())
 
-        def finish(trans, key, ov):
+        def finish_accept_pipe(trans, key, ov):
             ov.getresult()
             return pipe
 
-        return self._register(ov, pipe, finish)
+        return self._register(ov, pipe, finish_accept_pipe)
 
     def connect_pipe(self, address):
         ov = _overlapped.Overlapped(NULL)
         ov.WaitNamedPipeAndConnect(address, self._iocp, ov.address)
 
-        def finish(err, handle, ov):
+        def finish_connect_pipe(err, handle, ov):
             # err, handle were arguments passed to PostQueuedCompletionStatus()
             # in a function run in a thread pool.
             if err == _overlapped.ERROR_SEM_TIMEOUT:
@@ -323,7 +323,7 @@
             else:
                 return windows_utils.PipeHandle(handle)
 
-        return self._register(ov, None, finish, wait_for_post=True)
+        return self._register(ov, None, finish_connect_pipe, wait_for_post=True)
 
     def wait_for_handle(self, handle, timeout=None):
         if timeout is None:
@@ -339,7 +339,7 @@
             handle, self._iocp, ov.address, ms)
         f = _WaitHandleFuture(wh, loop=self._loop)
 
-        def finish(trans, key, ov):
+        def finish_wait_for_handle(trans, key, ov):
             if not f.cancelled():
                 try:
                     _overlapped.UnregisterWait(wh)
@@ -355,7 +355,7 @@
             return (_winapi.WaitForSingleObject(handle, 0) ==
                     _winapi.WAIT_OBJECT_0)
 
-        self._cache[ov.address] = (f, ov, None, finish)
+        self._cache[ov.address] = (f, ov, None, finish_wait_for_handle)
         return f
 
     def _register_with_iocp(self, obj):
diff --git a/Lib/asyncio/windows_utils.py b/Lib/asyncio/windows_utils.py
index aa1c064..2a196cc 100644
--- a/Lib/asyncio/windows_utils.py
+++ b/Lib/asyncio/windows_utils.py
@@ -36,12 +36,25 @@
 
     Origin: https://gist.github.com/4325783, by Geert Jansen.  Public domain.
     """
+    if family == socket.AF_INET:
+        host = '127.0.0.1'
+    elif family == socket.AF_INET6:
+        host = '::1'
+    else:
+        raise ValueError("Ony AF_INET and AF_INET6 socket address families "
+                         "are supported")
+    if type != socket.SOCK_STREAM:
+        raise ValueError("Only SOCK_STREAM socket type is supported")
+    if proto != 0:
+        raise ValueError("Only protocol zero is supported")
+
     # We create a connected TCP socket. Note the trick with setblocking(0)
     # that prevents us from having to create a thread.
     lsock = socket.socket(family, type, proto)
-    lsock.bind(('localhost', 0))
+    lsock.bind((host, 0))
     lsock.listen(1)
-    addr, port = lsock.getsockname()
+    # On IPv6, ignore flow_info and scope_id
+    addr, port = lsock.getsockname()[:2]
     csock = socket.socket(family, type, proto)
     csock.setblocking(False)
     try:
diff --git a/Lib/base64.py b/Lib/base64.py
index 573f76d..ad154ae 100755
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -9,7 +9,6 @@
 import re
 import struct
 import binascii
-import itertools
 
 
 __all__ = [
diff --git a/Lib/copy.py b/Lib/copy.py
index d26bcdb..bb8840e 100644
--- a/Lib/copy.py
+++ b/Lib/copy.py
@@ -110,7 +110,7 @@
 def _copy_immutable(x):
     return x
 for t in (type(None), int, float, bool, str, tuple,
-          frozenset, type, range,
+          bytes, frozenset, type, range,
           types.BuiltinFunctionType, type(Ellipsis),
           types.FunctionType, weakref.ref):
     d[t] = _copy_immutable
diff --git a/Lib/ctypes/test/test_bitfields.py b/Lib/ctypes/test/test_bitfields.py
index c89ee34..77de606 100644
--- a/Lib/ctypes/test/test_bitfields.py
+++ b/Lib/ctypes/test/test_bitfields.py
@@ -207,7 +207,7 @@
         class X(Structure):
             _fields_ = [("a", c_byte, 4),
                         ("b", c_int, 32)]
-        self.assertEqual(sizeof(X), sizeof(c_int)*2)
+        self.assertEqual(sizeof(X), alignment(c_int)+sizeof(c_int))
 
     def test_mixed_3(self):
         class X(Structure):
diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py
index 61b9fe7..87613ad 100644
--- a/Lib/ctypes/test/test_structures.py
+++ b/Lib/ctypes/test/test_structures.py
@@ -83,7 +83,7 @@
         class Y(Structure):
             _fields_ = [("x", c_char * 3),
                         ("y", c_int)]
-        self.assertEqual(alignment(Y), calcsize("i"))
+        self.assertEqual(alignment(Y), alignment(c_int))
         self.assertEqual(sizeof(Y), calcsize("3si"))
 
         class SI(Structure):
@@ -175,13 +175,6 @@
         self.assertEqual(sizeof(X), 10)
         self.assertEqual(X.b.offset, 2)
 
-        class X(Structure):
-            _fields_ = [("a", c_byte),
-                        ("b", c_longlong)]
-            _pack_ = 4
-        self.assertEqual(sizeof(X), 12)
-        self.assertEqual(X.b.offset, 4)
-
         import struct
         longlong_size = struct.calcsize("q")
         longlong_align = struct.calcsize("bq") - longlong_size
@@ -189,9 +182,16 @@
         class X(Structure):
             _fields_ = [("a", c_byte),
                         ("b", c_longlong)]
+            _pack_ = 4
+        self.assertEqual(sizeof(X), min(4, longlong_align) + longlong_size)
+        self.assertEqual(X.b.offset, min(4, longlong_align))
+
+        class X(Structure):
+            _fields_ = [("a", c_byte),
+                        ("b", c_longlong)]
             _pack_ = 8
 
-        self.assertEqual(sizeof(X), longlong_align + longlong_size)
+        self.assertEqual(sizeof(X), min(8, longlong_align) + longlong_size)
         self.assertEqual(X.b.offset, min(8, longlong_align))
 
 
diff --git a/Lib/distutils/core.py b/Lib/distutils/core.py
index c811d5b..2bfe66a 100644
--- a/Lib/distutils/core.py
+++ b/Lib/distutils/core.py
@@ -11,7 +11,6 @@
 
 from distutils.debug import DEBUG
 from distutils.errors import *
-from distutils.util import grok_environment_error
 
 # Mainly import these so setup scripts can "from distutils.core import" them.
 from distutils.dist import Distribution
@@ -150,13 +149,11 @@
         except KeyboardInterrupt:
             raise SystemExit("interrupted")
         except OSError as exc:
-            error = grok_environment_error(exc)
-
             if DEBUG:
-                sys.stderr.write(error + "\n")
+                sys.stderr.write("error: %s\n" % (exc,))
                 raise
             else:
-                raise SystemExit(error)
+                raise SystemExit("error: %s" % (exc,))
 
         except (DistutilsError,
                 CCompilerError) as msg:
diff --git a/Lib/distutils/dir_util.py b/Lib/distutils/dir_util.py
index 2b35aa3..9879b0d 100644
--- a/Lib/distutils/dir_util.py
+++ b/Lib/distutils/dir_util.py
@@ -2,7 +2,7 @@
 
 Utility functions for manipulating directories and directory trees."""
 
-import os, sys
+import os
 import errno
 from distutils.errors import DistutilsFileError, DistutilsInternalError
 from distutils import log
@@ -182,7 +182,6 @@
     Any errors are ignored (apart from being reported to stdout if 'verbose'
     is true).
     """
-    from distutils.util import grok_environment_error
     global _path_created
 
     if verbose >= 1:
@@ -199,8 +198,7 @@
             if abspath in _path_created:
                 del _path_created[abspath]
         except OSError as exc:
-            log.warn(grok_environment_error(
-                    exc, "error removing %s: " % directory))
+            log.warn("error removing %s: %s", directory, exc)
 
 def ensure_relative(path):
     """Take the full path 'path', and make it a relative path.
diff --git a/Lib/distutils/spawn.py b/Lib/distutils/spawn.py
index b1c5a44..d9cf950 100644
--- a/Lib/distutils/spawn.py
+++ b/Lib/distutils/spawn.py
@@ -10,6 +10,7 @@
 import os
 
 from distutils.errors import DistutilsPlatformError, DistutilsExecError
+from distutils.debug import DEBUG
 from distutils import log
 
 def spawn(cmd, search_path=1, verbose=0, dry_run=0):
@@ -28,10 +29,15 @@
     Raise DistutilsExecError if running the program fails in any way; just
     return on success.
     """
+    # cmd is documented as a list, but just in case some code passes a tuple
+    # in, protect our %-formatting code against horrible death
+    cmd = list(cmd)
     if os.name == 'posix':
         _spawn_posix(cmd, search_path, dry_run=dry_run)
     elif os.name == 'nt':
         _spawn_nt(cmd, search_path, dry_run=dry_run)
+    elif os.name == 'os2':
+        _spawn_os2(cmd, search_path, dry_run=dry_run)
     else:
         raise DistutilsPlatformError(
               "don't know how to spawn programs on platform '%s'" % os.name)
@@ -65,12 +71,16 @@
             rc = os.spawnv(os.P_WAIT, executable, cmd)
         except OSError as exc:
             # this seems to happen when the command isn't found
+            if not DEBUG:
+                cmd = executable
             raise DistutilsExecError(
-                  "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
+                  "command %r failed: %s" % (cmd, exc.args[-1]))
         if rc != 0:
             # and this reflects the command running but failing
+            if not DEBUG:
+                cmd = executable
             raise DistutilsExecError(
-                  "command '%s' failed with exit status %d" % (cmd[0], rc))
+                  "command %r failed with exit status %d" % (cmd, rc))
 
 if sys.platform == 'darwin':
     from distutils import sysconfig
@@ -81,8 +91,9 @@
     log.info(' '.join(cmd))
     if dry_run:
         return
+    executable = cmd[0]
     exec_fn = search_path and os.execvp or os.execv
-    exec_args = [cmd[0], cmd]
+    env = None
     if sys.platform == 'darwin':
         global _cfg_target, _cfg_target_split
         if _cfg_target is None:
@@ -103,17 +114,23 @@
             env = dict(os.environ,
                        MACOSX_DEPLOYMENT_TARGET=cur_target)
             exec_fn = search_path and os.execvpe or os.execve
-            exec_args.append(env)
     pid = os.fork()
     if pid == 0: # in the child
         try:
-            exec_fn(*exec_args)
+            if env is None:
+                exec_fn(executable, cmd)
+            else:
+                exec_fn(executable, cmd, env)
         except OSError as e:
-            sys.stderr.write("unable to execute %s: %s\n"
-                             % (cmd[0], e.strerror))
+            if not DEBUG:
+                cmd = executable
+            sys.stderr.write("unable to execute %r: %s\n"
+                             % (cmd, e.strerror))
             os._exit(1)
 
-        sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
+        if not DEBUG:
+            cmd = executable
+        sys.stderr.write("unable to execute %r for unknown reasons" % cmd)
         os._exit(1)
     else: # in the parent
         # Loop until the child either exits or is terminated by a signal
@@ -125,26 +142,34 @@
                 import errno
                 if exc.errno == errno.EINTR:
                     continue
+                if not DEBUG:
+                    cmd = executable
                 raise DistutilsExecError(
-                      "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
+                      "command %r failed: %s" % (cmd, exc.args[-1]))
             if os.WIFSIGNALED(status):
+                if not DEBUG:
+                    cmd = executable
                 raise DistutilsExecError(
-                      "command '%s' terminated by signal %d"
-                      % (cmd[0], os.WTERMSIG(status)))
+                      "command %r terminated by signal %d"
+                      % (cmd, os.WTERMSIG(status)))
             elif os.WIFEXITED(status):
                 exit_status = os.WEXITSTATUS(status)
                 if exit_status == 0:
                     return   # hey, it succeeded!
                 else:
+                    if not DEBUG:
+                        cmd = executable
                     raise DistutilsExecError(
-                          "command '%s' failed with exit status %d"
-                          % (cmd[0], exit_status))
+                          "command %r failed with exit status %d"
+                          % (cmd, exit_status))
             elif os.WIFSTOPPED(status):
                 continue
             else:
+                if not DEBUG:
+                    cmd = executable
                 raise DistutilsExecError(
-                      "unknown error executing '%s': termination status %d"
-                      % (cmd[0], status))
+                      "unknown error executing %r: termination status %d"
+                      % (cmd, status))
 
 def find_executable(executable, path=None):
     """Tries to find 'executable' in the directories listed in 'path'.
diff --git a/Lib/distutils/tests/test_util.py b/Lib/distutils/tests/test_util.py
index 548865c..4e9d79b 100644
--- a/Lib/distutils/tests/test_util.py
+++ b/Lib/distutils/tests/test_util.py
@@ -8,7 +8,8 @@
 from distutils.errors import DistutilsPlatformError, DistutilsByteCompileError
 from distutils.util import (get_platform, convert_path, change_root,
                             check_environ, split_quoted, strtobool,
-                            rfc822_escape, byte_compile)
+                            rfc822_escape, byte_compile,
+                            grok_environment_error)
 from distutils import util # used to patch _environ_checked
 from distutils.sysconfig import get_config_vars
 from distutils import sysconfig
@@ -285,6 +286,13 @@
         finally:
             sys.dont_write_bytecode = old_dont_write_bytecode
 
+    def test_grok_environment_error(self):
+        # test obsolete function to ensure backward compat (#4931)
+        exc = IOError("Unable to find batch file")
+        msg = grok_environment_error(exc)
+        self.assertEqual(msg, "error: Unable to find batch file")
+
+
 def test_suite():
     return unittest.makeSuite(UtilTestCase)
 
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py
index efb3834..5adcac5 100644
--- a/Lib/distutils/util.py
+++ b/Lib/distutils/util.py
@@ -207,25 +207,10 @@
 
 
 def grok_environment_error (exc, prefix="error: "):
-    """Generate a useful error message from an OSError
-    exception object.  Handles Python 1.5.1 and 1.5.2 styles, and
-    does what it can to deal with exception objects that don't have a
-    filename (which happens when the error is due to a two-file operation,
-    such as 'rename()' or 'link()'.  Returns the error message as a string
-    prefixed with 'prefix'.
-    """
-    # check for Python 1.5.2-style {IO,OS}Error exception objects
-    if hasattr(exc, 'filename') and hasattr(exc, 'strerror'):
-        if exc.filename:
-            error = prefix + "%s: %s" % (exc.filename, exc.strerror)
-        else:
-            # two-argument functions in posix module don't
-            # include the filename in the exception object!
-            error = prefix + "%s" % exc.strerror
-    else:
-        error = prefix + str(exc.args[-1])
-
-    return error
+    # Function kept for backward compatibility.
+    # Used to try clever things with EnvironmentErrors,
+    # but nowadays str(exception) produces good messages.
+    return prefix + str(exc)
 
 
 # Needed by 'split_quoted()'
diff --git a/Lib/email/message.py b/Lib/email/message.py
index 88b5fa3..b4bc8cb 100644
--- a/Lib/email/message.py
+++ b/Lib/email/message.py
@@ -203,7 +203,11 @@
         if self._payload is None:
             self._payload = [payload]
         else:
-            self._payload.append(payload)
+            try:
+                self._payload.append(payload)
+            except AttributeError:
+                raise TypeError("Attach is not valid on a message with a"
+                                " non-multipart payload")
 
     def get_payload(self, i=None, decode=False):
         """Return a reference to the payload.
diff --git a/Lib/encodings/base64_codec.py b/Lib/encodings/base64_codec.py
index 881d1ba..8e7703b 100644
--- a/Lib/encodings/base64_codec.py
+++ b/Lib/encodings/base64_codec.py
@@ -1,7 +1,6 @@
 """Python 'base64_codec' Codec - base64 content transfer encoding.
 
-This codec de/encodes from bytes to bytes and is therefore usable with
-bytes.transform() and bytes.untransform().
+This codec de/encodes from bytes to bytes.
 
 Written by Marc-Andre Lemburg (mal@lemburg.com).
 """
diff --git a/Lib/encodings/hex_codec.py b/Lib/encodings/hex_codec.py
index f2ed0a7..9fb1072 100644
--- a/Lib/encodings/hex_codec.py
+++ b/Lib/encodings/hex_codec.py
@@ -1,7 +1,6 @@
 """Python 'hex_codec' Codec - 2-digit hex content transfer encoding.
 
-This codec de/encodes from bytes to bytes and is therefore usable with
-bytes.transform() and bytes.untransform().
+This codec de/encodes from bytes to bytes.
 
 Written by Marc-Andre Lemburg (mal@lemburg.com).
 """
diff --git a/Lib/encodings/quopri_codec.py b/Lib/encodings/quopri_codec.py
index 70f7083..0533dbe 100644
--- a/Lib/encodings/quopri_codec.py
+++ b/Lib/encodings/quopri_codec.py
@@ -1,7 +1,6 @@
 """Codec for quoted-printable encoding.
 
-This codec de/encodes from bytes to bytes and is therefore usable with
-bytes.transform() and bytes.untransform().
+This codec de/encodes from bytes to bytes.
 """
 
 import codecs
diff --git a/Lib/encodings/rot_13.py b/Lib/encodings/rot_13.py
index fff9153..1f2f47b 100755
--- a/Lib/encodings/rot_13.py
+++ b/Lib/encodings/rot_13.py
@@ -1,8 +1,7 @@
 #!/usr/bin/env python
 """ Python Character Mapping Codec for ROT13.
 
-This codec de/encodes from str to str and is therefore usable with
-str.transform() and str.untransform().
+This codec de/encodes from str to str.
 
 Written by Marc-Andre Lemburg (mal@lemburg.com).
 """
diff --git a/Lib/encodings/uu_codec.py b/Lib/encodings/uu_codec.py
index e3269e4..1454095 100644
--- a/Lib/encodings/uu_codec.py
+++ b/Lib/encodings/uu_codec.py
@@ -1,7 +1,6 @@
 """Python 'uu_codec' Codec - UU content transfer encoding.
 
-This codec de/encodes from bytes to bytes and is therefore usable with
-bytes.transform() and bytes.untransform().
+This codec de/encodes from bytes to bytes.
 
 Written by Marc-Andre Lemburg (mal@lemburg.com). Some details were
 adapted from uu.py which was written by Lance Ellinghouse and
diff --git a/Lib/encodings/zlib_codec.py b/Lib/encodings/zlib_codec.py
index 4c81ca1..95908a4 100644
--- a/Lib/encodings/zlib_codec.py
+++ b/Lib/encodings/zlib_codec.py
@@ -1,7 +1,6 @@
 """Python 'zlib_codec' Codec - zlib compression encoding.
 
-This codec de/encodes from bytes to bytes and is therefore usable with
-bytes.transform() and bytes.untransform().
+This codec de/encodes from bytes to bytes.
 
 Written by Marc-Andre Lemburg (mal@lemburg.com).
 """
diff --git a/Lib/gzip.py b/Lib/gzip.py
index 8d21fe4..f934d4f 100644
--- a/Lib/gzip.py
+++ b/Lib/gzip.py
@@ -96,7 +96,7 @@
             self._read -= len(prepend)
             return
         else:
-            self._buffer = self._buffer[read:] + prepend
+            self._buffer = self._buffer[self._read:] + prepend
         self._length = len(self._buffer)
         self._read = 0
 
diff --git a/Lib/idlelib/GrepDialog.py b/Lib/idlelib/GrepDialog.py
index c0074e2..f73d70a 100644
--- a/Lib/idlelib/GrepDialog.py
+++ b/Lib/idlelib/GrepDialog.py
@@ -98,7 +98,7 @@
     def findfiles(self, dir, base, rec):
         try:
             names = os.listdir(dir or os.curdir)
-        except OSerror as msg:
+        except OSError as msg:
             print(msg)
             return []
         list = []
diff --git a/Lib/idlelib/MultiCall.py b/Lib/idlelib/MultiCall.py
index 477db2e..25105dc 100644
--- a/Lib/idlelib/MultiCall.py
+++ b/Lib/idlelib/MultiCall.py
@@ -111,6 +111,8 @@
             except tkinter.TclError as e:
                 if e.args[0] == APPLICATION_GONE:
                     pass
+                else:
+                    raise
 
 # An int in range(1 << len(_modifiers)) represents a combination of modifiers
 # (if the least significent bit is on, _modifiers[0] is on, and so on).
@@ -244,6 +246,8 @@
             except tkinter.TclError as e:
                 if e.args[0] == APPLICATION_GONE:
                     break
+                else:
+                    raise
 
 # define the list of event types to be handled by MultiEvent. the order is
 # compatible with the definition of event type constants.
@@ -411,6 +415,8 @@
                         except tkinter.TclError as e:
                             if e.args[0] == APPLICATION_GONE:
                                 break
+                            else:
+                                raise
 
     _multicall_dict[widget] = MultiCall
     return MultiCall
diff --git a/Lib/idlelib/idle_test/README.txt b/Lib/idlelib/idle_test/README.txt
index a8d4dcb..6b92483 100644
--- a/Lib/idlelib/idle_test/README.txt
+++ b/Lib/idlelib/idle_test/README.txt
@@ -41,9 +41,10 @@
 screen, gui tests must be 'guarded' by "requires('gui')" in a setUp
 function or method. This will typically be setUpClass.
 
-All gui objects must be destroyed by the end of the test, perhaps in a tearDown
-function. Creating the Tk root directly in a setUp allows a reference to be saved
-so it can be properly destroyed in the corresponding tearDown. 
+To avoid interfering with other gui tests, all gui objects must be destroyed
+and deleted by the end of the test.  If a widget, such as a Tk root, is created
+in a setUpX function, destroy it in the corresponding tearDownX.  For module
+and class attributes, also delete the widget.
 ---
     @classmethod
     def setUpClass(cls):
@@ -53,6 +54,7 @@
     @classmethod
     def tearDownClass(cls):
         cls.root.destroy()
+        del cls.root
 ---
 
 Support.requires('gui') returns true if it is either called in a main module
@@ -105,4 +107,4 @@
 To run an individual Testcase or test method, extend the dotted name given to
 unittest on the command line. (But gui tests will not this way.)
 
-python -m unittest -v idlelib.idle_test.text_xyz.Test_case.test_meth
+python -m unittest -v idlelib.idle_test.test_xyz.Test_case.test_meth
diff --git a/Lib/idlelib/idle_test/test_formatparagraph.py b/Lib/idlelib/idle_test/test_formatparagraph.py
index 818c9d4..f4a7c2d 100644
--- a/Lib/idlelib/idle_test/test_formatparagraph.py
+++ b/Lib/idlelib/idle_test/test_formatparagraph.py
@@ -277,6 +277,9 @@
     @classmethod
     def tearDownClass(cls):
         cls.root.destroy()
+        del cls.root
+        del cls.text
+        del cls.formatter
 
     def test_short_line(self):
         self.text.insert('1.0', "Short line\n")
diff --git a/Lib/idlelib/idle_test/test_idlehistory.py b/Lib/idlelib/idle_test/test_idlehistory.py
index b27db91..d7c3d70 100644
--- a/Lib/idlelib/idle_test/test_idlehistory.py
+++ b/Lib/idlelib/idle_test/test_idlehistory.py
@@ -80,6 +80,7 @@
     @classmethod
     def tearDownClass(cls):
         cls.root.destroy()
+        del cls.root
 
     def fetch_test(self, reverse, line, prefix, index, *, bell=False):
         # Perform one fetch as invoked by Alt-N or Alt-P
diff --git a/Lib/idlelib/idle_test/test_searchengine.py b/Lib/idlelib/idle_test/test_searchengine.py
index fdf38bd..129a5a3 100644
--- a/Lib/idlelib/idle_test/test_searchengine.py
+++ b/Lib/idlelib/idle_test/test_searchengine.py
@@ -64,6 +64,7 @@
 ##    @classmethod
 ##    def tearDownClass(cls):
 ##        cls.root.destroy()
+##        del cls.root
 
     def test_get_selection(self):
         # text = Text(master=self.root)
@@ -219,6 +220,7 @@
 ##    @classmethod
 ##    def tearDownClass(cls):
 ##        cls.root.destroy()
+##        del cls.root
 
     def test_search(self):
         Equal = self.assertEqual
@@ -261,6 +263,7 @@
 ##    @classmethod
 ##    def tearDownClass(cls):
 ##        cls.root.destroy()
+##        del cls.root
 
     @classmethod
     def setUpClass(cls):
diff --git a/Lib/idlelib/idle_test/test_text.py b/Lib/idlelib/idle_test/test_text.py
index 367bf38..5ac2fd7 100644
--- a/Lib/idlelib/idle_test/test_text.py
+++ b/Lib/idlelib/idle_test/test_text.py
@@ -221,6 +221,7 @@
     @classmethod
     def tearDownClass(cls):
         cls.root.destroy()
+        del cls.root
 
 
 if __name__ == '__main__':
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index dd71347..54df39a 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -2155,6 +2155,18 @@
         if self._prefixlen == (self._max_prefixlen - 1):
             self.hosts = self.__iter__
 
+    def hosts(self):
+        """Generate Iterator over usable hosts in a network.
+
+          This is like __iter__ except it doesn't return the
+          Subnet-Router anycast address.
+
+        """
+        network = int(self.network_address)
+        broadcast = int(self.broadcast_address)
+        for x in range(network + 1, broadcast + 1):
+            yield self._address_class(x)
+
     @property
     def is_site_local(self):
         """Test if the address is reserved for site-local.
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index bd9b994..478c5af 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1,4 +1,4 @@
-# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
+# Copyright 2001-2014 by Vinay Sajip. All Rights Reserved.
 #
 # Permission to use, copy, modify, and distribute this software and its
 # documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
 Logging package for Python. Based on PEP 282 and comments thereto in
 comp.lang.python.
 
-Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2014 Vinay Sajip. All Rights Reserved.
 
 To use, simply 'import logging' and log away!
 """
@@ -42,6 +42,7 @@
 
 __author__  = "Vinay Sajip <vinay_sajip@red-dove.com>"
 __status__  = "production"
+# The following module attributes are no longer updated.
 __version__ = "0.5.1.2"
 __date__    = "07 February 2010"
 
@@ -902,8 +903,15 @@
                     sys.stderr.write('Logged from file %s, line %s\n' % (
                                      record.filename, record.lineno))
                 # Issue 18671: output logging message and arguments
-                sys.stderr.write('Message: %r\n'
-                                 'Arguments: %s\n' % (record.msg, record.args))
+                try:
+                    sys.stderr.write('Message: %r\n'
+                                     'Arguments: %s\n' % (record.msg,
+                                                          record.args))
+                except Exception:
+                    sys.stderr.write('Unable to print the message and arguments'
+                                     ' - possible formatting error.\nUse the'
+                                     ' traceback above to help find the error.\n'
+                                    )
             except OSError: #pragma: no cover
                 pass    # see issue 5971
             finally:
diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py
index b19941e..cc5b8cc 100644
--- a/Lib/modulefinder.py
+++ b/Lib/modulefinder.py
@@ -1,6 +1,7 @@
 """Find modules used by a script, using introspection."""
 
 import dis
+import importlib._bootstrap
 import importlib.machinery
 import marshal
 import os
@@ -287,11 +288,12 @@
         if type == imp.PY_SOURCE:
             co = compile(fp.read()+'\n', pathname, 'exec')
         elif type == imp.PY_COMPILED:
-            if fp.read(4) != imp.get_magic():
-                self.msgout(2, "raise ImportError: Bad magic number", pathname)
-                raise ImportError("Bad magic number in %s" % pathname)
-            fp.read(4)
-            co = marshal.load(fp)
+            try:
+                marshal_data = importlib._bootstrap._validate_bytecode_header(fp.read())
+            except ImportError as exc:
+                self.msgout(2, "raise ImportError: " + str(exc), pathname)
+                raise
+            co = marshal.loads(marshal_data)
         else:
             co = None
         m = self.add_module(fqname)
diff --git a/Lib/ntpath.py b/Lib/ntpath.py
index de7a247..af3fb87 100644
--- a/Lib/ntpath.py
+++ b/Lib/ntpath.py
@@ -377,6 +377,7 @@
         percent = b'%'
         brace = b'{'
         dollar = b'$'
+        environ = getattr(os, 'environb', None)
     else:
         if '$' not in path and '%' not in path:
             return path
@@ -386,6 +387,7 @@
         percent = '%'
         brace = '{'
         dollar = '$'
+        environ = os.environ
     res = path[:0]
     index = 0
     pathlen = len(path)
@@ -414,14 +416,13 @@
                     index = pathlen - 1
                 else:
                     var = path[:index]
-                    if isinstance(path, bytes):
-                        var = var.decode('ascii')
-                    if var in os.environ:
-                        value = os.environ[var]
-                    else:
-                        value = '%' + var + '%'
-                    if isinstance(path, bytes):
-                        value = value.encode('ascii')
+                    try:
+                        if environ is None:
+                            value = os.fsencode(os.environ[os.fsdecode(var)])
+                        else:
+                            value = environ[var]
+                    except KeyError:
+                        value = percent + var + percent
                     res += value
         elif c == dollar:  # variable or '$$'
             if path[index + 1:index + 2] == dollar:
@@ -435,39 +436,40 @@
                         index = path.index(b'}')
                     else:
                         index = path.index('}')
-                    var = path[:index]
-                    if isinstance(path, bytes):
-                        var = var.decode('ascii')
-                    if var in os.environ:
-                        value = os.environ[var]
-                    else:
-                        value = '${' + var + '}'
-                    if isinstance(path, bytes):
-                        value = value.encode('ascii')
-                    res += value
                 except ValueError:
                     if isinstance(path, bytes):
                         res += b'${' + path
                     else:
                         res += '${' + path
                     index = pathlen - 1
+                else:
+                    var = path[:index]
+                    try:
+                        if environ is None:
+                            value = os.fsencode(os.environ[os.fsdecode(var)])
+                        else:
+                            value = environ[var]
+                    except KeyError:
+                        if isinstance(path, bytes):
+                            value = b'${' + var + b'}'
+                        else:
+                            value = '${' + var + '}'
+                    res += value
             else:
-                var = ''
+                var = path[:0]
                 index += 1
                 c = path[index:index + 1]
                 while c and c in varchars:
-                    if isinstance(path, bytes):
-                        var += c.decode('ascii')
-                    else:
-                        var += c
+                    var += c
                     index += 1
                     c = path[index:index + 1]
-                if var in os.environ:
-                    value = os.environ[var]
-                else:
-                    value = '$' + var
-                if isinstance(path, bytes):
-                    value = value.encode('ascii')
+                try:
+                    if environ is None:
+                        value = os.fsencode(os.environ[os.fsdecode(var)])
+                    else:
+                        value = environ[var]
+                except KeyError:
+                    value = dollar + var
                 res += value
                 if c:
                     index -= 1
diff --git a/Lib/posixpath.py b/Lib/posixpath.py
index 492c415..3e13239 100644
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -279,6 +279,7 @@
         search = _varprogb.search
         start = b'{'
         end = b'}'
+        environ = getattr(os, 'environb', None)
     else:
         if '$' not in path:
             return path
@@ -288,6 +289,7 @@
         search = _varprog.search
         start = '{'
         end = '}'
+        environ = os.environ
     i = 0
     while True:
         m = search(path, i)
@@ -297,18 +299,18 @@
         name = m.group(1)
         if name.startswith(start) and name.endswith(end):
             name = name[1:-1]
-        if isinstance(name, bytes):
-            name = str(name, 'ASCII')
-        if name in os.environ:
+        try:
+            if environ is None:
+                value = os.fsencode(os.environ[os.fsdecode(name)])
+            else:
+                value = environ[name]
+        except KeyError:
+            i = j
+        else:
             tail = path[j:]
-            value = os.environ[name]
-            if isinstance(path, bytes):
-                value = value.encode('ASCII')
             path = path[:i] + value
             i = len(path)
             path += tail
-        else:
-            i = j
     return path
 
 
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index c4838d7..505b7cb 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1256,9 +1256,12 @@
                         doc = getdoc(value)
                     else:
                         doc = None
-                    push(self.docother(
-                        getattr(object, name, None) or homecls.__dict__[name],
-                        name, mod, maxlen=70, doc=doc) + '\n')
+                    try:
+                        obj = getattr(object, name)
+                    except AttributeError:
+                        obj = homecls.__dict__[name]
+                    push(self.docother(obj, name, mod, maxlen=70, doc=doc) +
+                         '\n')
             return attrs
 
         attrs = [(name, kind, cls, value)
diff --git a/Lib/shutil.py b/Lib/shutil.py
index 9a6a040..0cd6ec4 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -483,7 +483,8 @@
 def _basename(path):
     # A basename() variant which first strips the trailing slash, if present.
     # Thus we always get the last component of the path, even for directories.
-    return os.path.basename(path.rstrip(os.path.sep))
+    sep = os.path.sep + (os.path.altsep or '')
+    return os.path.basename(path.rstrip(sep))
 
 def move(src, dst):
     """Recursively move a file or directory to another location. This is
diff --git a/Lib/sqlite3/test/factory.py b/Lib/sqlite3/test/factory.py
index 0314ebd..1013755 100644
--- a/Lib/sqlite3/test/factory.py
+++ b/Lib/sqlite3/test/factory.py
@@ -112,6 +112,7 @@
         self.con.row_factory = sqlite.Row
         row = self.con.execute("select 1 as a, 2 as b").fetchone()
         t = tuple(row)
+        self.assertEqual(t, (row['a'], row['b']))
 
     def CheckSqliteRowAsDict(self):
         """Checks if the row object can be correctly converted to a dictionary"""
diff --git a/Lib/sqlite3/test/hooks.py b/Lib/sqlite3/test/hooks.py
index 60f7605..ede0bec 100644
--- a/Lib/sqlite3/test/hooks.py
+++ b/Lib/sqlite3/test/hooks.py
@@ -162,7 +162,7 @@
             create table bar (a, b)
             """)
         second_count = len(progress_calls)
-        self.assertGreater(first_count, second_count)
+        self.assertGreaterEqual(first_count, second_count)
 
     def CheckCancelOperation(self):
         """
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 26d8de5..d629bdf 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -738,6 +738,9 @@
 
 
 class Popen(object):
+
+    _child_created = False  # Set here since __del__ checks it
+
     def __init__(self, args, bufsize=-1, executable=None,
                  stdin=None, stdout=None, stderr=None,
                  preexec_fn=None, close_fds=_PLATFORM_DEFAULT_CLOSE_FDS,
@@ -748,7 +751,6 @@
         """Create new Popen instance."""
         _cleanup()
 
-        self._child_created = False
         self._input = None
         self._communication_started = False
         if bufsize is None:
@@ -890,11 +892,8 @@
         # Wait for the process to terminate, to avoid zombies.
         self.wait()
 
-    def __del__(self, _maxsize=sys.maxsize, _active=_active):
-        # If __init__ hasn't had a chance to execute (e.g. if it
-        # was passed an undeclared keyword argument), we don't
-        # have a _child_created attribute at all.
-        if not getattr(self, '_child_created', False):
+    def __del__(self, _maxsize=sys.maxsize):
+        if not self._child_created:
             # We didn't get to successfully create a child process.
             return
         # In case the child hasn't been waited on, check if it's done.
@@ -1187,7 +1186,15 @@
                     try:
                         self.stdin.write(input)
                     except OSError as e:
-                        if e.errno != errno.EPIPE:
+                        if e.errno == errno.EPIPE:
+                            # communicate() should ignore pipe full error
+                            pass
+                        elif (e.errno == errno.EINVAL
+                              and self.poll() is not None):
+                            # Issue #19612: stdin.write() fails with EINVAL
+                            # if the process already exited before the write
+                            pass
+                        else:
                             raise
                 self.stdin.close()
 
@@ -1446,7 +1453,7 @@
                 _WTERMSIG=os.WTERMSIG, _WIFEXITED=os.WIFEXITED,
                 _WEXITSTATUS=os.WEXITSTATUS):
             # This method is called (indirectly) by __del__, so it cannot
-            # refer to anything outside of its local scope."""
+            # refer to anything outside of its local scope.
             if _WIFSIGNALED(sts):
                 self.returncode = -_WTERMSIG(sts)
             elif _WIFEXITED(sts):
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 06d3ca9..8eb57fe 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -3651,7 +3651,7 @@
         _multiprocessing.sem_unlink(name1)
         p.terminate()
         p.wait()
-        time.sleep(1.0)
+        time.sleep(2.0)
         with self.assertRaises(OSError) as ctx:
             _multiprocessing.sem_unlink(name2)
         # docs say it should be ENOENT, but OSX seems to give EINVAL
diff --git a/Lib/test/coding20731.py b/Lib/test/coding20731.py
new file mode 100644
index 0000000..b0e227a
--- /dev/null
+++ b/Lib/test/coding20731.py
@@ -0,0 +1,4 @@
+#coding:latin1

+

+

+

diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index 17cbccb..c1c831f 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -1373,10 +1373,9 @@
     try:
         import zipimport
     except ImportError:
-        zsc = zdc = None # Run unmodified on platforms without zipimport support
+        zdc = None # Run unmodified on platforms without zipimport support
     else:
         zdc = zipimport._zip_directory_cache.copy()
-        zsc = zipimport._zip_stat_cache.copy()
     abcs = {}
     for abc in [getattr(collections.abc, a) for a in collections.abc.__all__]:
         if not isabstract(abc):
@@ -1395,7 +1394,7 @@
     sys.stderr.flush()
     for i in range(repcount):
         indirect_test()
-        alloc_after, rc_after = dash_R_cleanup(fs, ps, pic, zdc, zsc, abcs)
+        alloc_after, rc_after = dash_R_cleanup(fs, ps, pic, zdc, abcs)
         sys.stderr.write('.')
         sys.stderr.flush()
         if i >= nwarmup:
@@ -1429,7 +1428,7 @@
             failed = True
     return failed
 
-def dash_R_cleanup(fs, ps, pic, zdc, zsc, abcs):
+def dash_R_cleanup(fs, ps, pic, zdc, abcs):
     import gc, copyreg
     import _strptime, linecache
     import urllib.parse, urllib.request, mimetypes, doctest
@@ -1455,8 +1454,6 @@
     else:
         zipimport._zip_directory_cache.clear()
         zipimport._zip_directory_cache.update(zdc)
-        zipimport._zip_stat_cache.clear()
-        zipimport._zip_stat_cache.update(zsc)
 
     # clear type cache
     sys._clear_type_cache()
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
index 784a39f..340ca67 100644
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -6,8 +6,8 @@
 import sys
 import time
 import unittest
-import unittest.mock
-from test.support import find_unused_port, IPV6_ENABLED
+from unittest import mock
+from test.support import IPV6_ENABLED
 
 import asyncio
 from asyncio import base_events
@@ -15,7 +15,7 @@
 from asyncio import test_utils
 
 
-MOCK_ANY = unittest.mock.ANY
+MOCK_ANY = mock.ANY
 PY34 = sys.version_info >= (3, 4)
 
 
@@ -23,11 +23,11 @@
 
     def setUp(self):
         self.loop = base_events.BaseEventLoop()
-        self.loop._selector = unittest.mock.Mock()
+        self.loop._selector = mock.Mock()
         asyncio.set_event_loop(None)
 
     def test_not_implemented(self):
-        m = unittest.mock.Mock()
+        m = mock.Mock()
         self.assertRaises(
             NotImplementedError,
             self.loop._make_socket_transport, m, m)
@@ -75,13 +75,13 @@
         self.assertFalse(self.loop._ready)
 
     def test_set_default_executor(self):
-        executor = unittest.mock.Mock()
+        executor = mock.Mock()
         self.loop.set_default_executor(executor)
         self.assertIs(executor, self.loop._default_executor)
 
     def test_getnameinfo(self):
-        sockaddr = unittest.mock.Mock()
-        self.loop.run_in_executor = unittest.mock.Mock()
+        sockaddr = mock.Mock()
+        self.loop.run_in_executor = mock.Mock()
         self.loop.getnameinfo(sockaddr)
         self.assertEqual(
             (None, socket.getnameinfo, sockaddr, 0),
@@ -111,7 +111,7 @@
         def cb(arg):
             calls.append(arg)
 
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
         self.loop.call_later(-1, cb, 'a')
         self.loop.call_later(-2, cb, 'b')
         test_utils.run_briefly(self.loop)
@@ -121,7 +121,7 @@
         def cb():
             self.loop.stop()
 
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
         delay = 0.1
 
         when = self.loop.time() + delay
@@ -163,7 +163,7 @@
             pass
         h = asyncio.Handle(cb, (), self.loop)
         f = asyncio.Future(loop=self.loop)
-        executor = unittest.mock.Mock()
+        executor = mock.Mock()
         executor.submit.return_value = f
 
         self.loop.set_default_executor(executor)
@@ -171,7 +171,7 @@
         res = self.loop.run_in_executor(None, h)
         self.assertIs(f, res)
 
-        executor = unittest.mock.Mock()
+        executor = mock.Mock()
         executor.submit.return_value = f
         res = self.loop.run_in_executor(executor, h)
         self.assertIs(f, res)
@@ -187,7 +187,7 @@
 
         h1.cancel()
 
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
         self.loop._scheduled.append(h1)
         self.loop._scheduled.append(h2)
         self.loop._run_once()
@@ -203,8 +203,8 @@
         self.loop.set_debug(False)
         self.assertFalse(self.loop.get_debug())
 
-    @unittest.mock.patch('asyncio.base_events.time')
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.time')
+    @mock.patch('asyncio.base_events.logger')
     def test__run_once_logging(self, m_logger, m_time):
         # Log to INFO level if timeout > 1.0 sec.
         idx = -1
@@ -219,7 +219,7 @@
 
         self.loop._scheduled.append(
             asyncio.TimerHandle(11.0, lambda: True, (), self.loop))
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
         self.loop._run_once()
         self.assertEqual(logging.INFO, m_logger.log.call_args[0][0])
 
@@ -242,7 +242,7 @@
         h = asyncio.TimerHandle(time.monotonic() - 1, cb, (self.loop,),
                                 self.loop)
 
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
         self.loop._scheduled.append(h)
         self.loop._run_once()
 
@@ -303,14 +303,14 @@
             asyncio.SubprocessProtocol, 'exit 0', bufsize=4096)
 
     def test_default_exc_handler_callback(self):
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
 
         def zero_error(fut):
             fut.set_result(True)
             1/0
 
         # Test call_soon (events.Handle)
-        with unittest.mock.patch('asyncio.base_events.logger') as log:
+        with mock.patch('asyncio.base_events.logger') as log:
             fut = asyncio.Future(loop=self.loop)
             self.loop.call_soon(zero_error, fut)
             fut.add_done_callback(lambda fut: self.loop.stop())
@@ -320,7 +320,7 @@
                 exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY))
 
         # Test call_later (events.TimerHandle)
-        with unittest.mock.patch('asyncio.base_events.logger') as log:
+        with mock.patch('asyncio.base_events.logger') as log:
             fut = asyncio.Future(loop=self.loop)
             self.loop.call_later(0.01, zero_error, fut)
             fut.add_done_callback(lambda fut: self.loop.stop())
@@ -330,7 +330,7 @@
                 exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY))
 
     def test_default_exc_handler_coro(self):
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
 
         @asyncio.coroutine
         def zero_error_coro():
@@ -338,7 +338,7 @@
             1/0
 
         # Test Future.__del__
-        with unittest.mock.patch('asyncio.base_events.logger') as log:
+        with mock.patch('asyncio.base_events.logger') as log:
             fut = asyncio.async(zero_error_coro(), loop=self.loop)
             fut.add_done_callback(lambda *args: self.loop.stop())
             self.loop.run_forever()
@@ -368,9 +368,9 @@
             self.loop.call_soon(zero_error)
             self.loop._run_once()
 
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
 
-        mock_handler = unittest.mock.Mock()
+        mock_handler = mock.Mock()
         self.loop.set_exception_handler(mock_handler)
         run_loop()
         mock_handler.assert_called_with(self.loop, {
@@ -382,7 +382,7 @@
         mock_handler.reset_mock()
 
         self.loop.set_exception_handler(None)
-        with unittest.mock.patch('asyncio.base_events.logger') as log:
+        with mock.patch('asyncio.base_events.logger') as log:
             run_loop()
             log.error.assert_called_with(
                         test_utils.MockPattern(
@@ -401,11 +401,11 @@
         def handler(loop, context):
             raise AttributeError('spam')
 
-        self.loop._process_events = unittest.mock.Mock()
+        self.loop._process_events = mock.Mock()
 
         self.loop.set_exception_handler(handler)
 
-        with unittest.mock.patch('asyncio.base_events.logger') as log:
+        with mock.patch('asyncio.base_events.logger') as log:
             run_loop()
             log.error.assert_called_with(
                 test_utils.MockPattern(
@@ -417,8 +417,8 @@
 
         class Loop(base_events.BaseEventLoop):
 
-            _selector = unittest.mock.Mock()
-            _process_events = unittest.mock.Mock()
+            _selector = mock.Mock()
+            _process_events = mock.Mock()
 
             def default_exception_handler(self, context):
                 nonlocal _context
@@ -435,7 +435,7 @@
             loop.call_soon(zero_error)
             loop._run_once()
 
-        with unittest.mock.patch('asyncio.base_events.logger') as log:
+        with mock.patch('asyncio.base_events.logger') as log:
             run_loop()
             log.error.assert_called_with(
                 'Exception in default exception handler',
@@ -446,7 +446,7 @@
 
         _context = None
         loop.set_exception_handler(custom_handler)
-        with unittest.mock.patch('asyncio.base_events.logger') as log:
+        with mock.patch('asyncio.base_events.logger') as log:
             run_loop()
             log.error.assert_called_with(
                 test_utils.MockPattern('Exception in default exception.*'
@@ -527,7 +527,7 @@
     def tearDown(self):
         self.loop.close()
 
-    @unittest.mock.patch('asyncio.base_events.socket')
+    @mock.patch('asyncio.base_events.socket')
     def test_create_connection_multiple_errors(self, m_socket):
 
         class MyProto(asyncio.Protocol):
@@ -592,7 +592,7 @@
             return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop)
 
         self.loop.getaddrinfo = getaddrinfo_task
-        self.loop.sock_connect = unittest.mock.Mock()
+        self.loop.sock_connect = mock.Mock()
         self.loop.sock_connect.side_effect = OSError
 
         coro = self.loop.create_connection(MyProto, 'example.com', 80)
@@ -609,7 +609,7 @@
             return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop)
 
         self.loop.getaddrinfo = getaddrinfo_task
-        self.loop.sock_connect = unittest.mock.Mock()
+        self.loop.sock_connect = mock.Mock()
         self.loop.sock_connect.side_effect = OSError
 
         coro = self.loop.create_connection(
@@ -617,7 +617,7 @@
         with self.assertRaises(OSError):
             self.loop.run_until_complete(coro)
 
-    @unittest.mock.patch('asyncio.base_events.socket')
+    @mock.patch('asyncio.base_events.socket')
     def test_create_connection_multiple_errors_local_addr(self, m_socket):
 
         def bind(addr):
@@ -637,7 +637,7 @@
             return asyncio.Task(getaddrinfo(*args, **kwds), loop=self.loop)
 
         self.loop.getaddrinfo = getaddrinfo_task
-        self.loop.sock_connect = unittest.mock.Mock()
+        self.loop.sock_connect = mock.Mock()
         self.loop.sock_connect.side_effect = OSError('Err2')
 
         coro = self.loop.create_connection(
@@ -669,7 +669,7 @@
             OSError, self.loop.run_until_complete, coro)
 
     def test_create_connection_ssl_server_hostname_default(self):
-        self.loop.getaddrinfo = unittest.mock.Mock()
+        self.loop.getaddrinfo = mock.Mock()
 
         def mock_getaddrinfo(*args, **kwds):
             f = asyncio.Future(loop=self.loop)
@@ -678,9 +678,9 @@
             return f
 
         self.loop.getaddrinfo.side_effect = mock_getaddrinfo
-        self.loop.sock_connect = unittest.mock.Mock()
+        self.loop.sock_connect = mock.Mock()
         self.loop.sock_connect.return_value = ()
-        self.loop._make_ssl_transport = unittest.mock.Mock()
+        self.loop._make_ssl_transport = mock.Mock()
 
         class _SelectorTransportMock:
             _sock = None
@@ -696,7 +696,7 @@
             return transport
 
         self.loop._make_ssl_transport.side_effect = mock_make_ssl_transport
-        ANY = unittest.mock.ANY
+        ANY = mock.ANY
         # First try the default server_hostname.
         self.loop._make_ssl_transport.reset_mock()
         coro = self.loop.create_connection(MyProto, 'python.org', 80, ssl=True)
@@ -775,13 +775,13 @@
         self.assertRaises(ValueError, self.loop.run_until_complete, fut)
 
     def test_create_server_no_getaddrinfo(self):
-        getaddrinfo = self.loop.getaddrinfo = unittest.mock.Mock()
+        getaddrinfo = self.loop.getaddrinfo = mock.Mock()
         getaddrinfo.return_value = []
 
         f = self.loop.create_server(MyProto, '0.0.0.0', 0)
         self.assertRaises(OSError, self.loop.run_until_complete, f)
 
-    @unittest.mock.patch('asyncio.base_events.socket')
+    @mock.patch('asyncio.base_events.socket')
     def test_create_server_cant_bind(self, m_socket):
 
         class Err(OSError):
@@ -790,14 +790,14 @@
         m_socket.getaddrinfo.return_value = [
             (2, 1, 6, '', ('127.0.0.1', 10100))]
         m_socket.getaddrinfo._is_coroutine = False
-        m_sock = m_socket.socket.return_value = unittest.mock.Mock()
+        m_sock = m_socket.socket.return_value = mock.Mock()
         m_sock.bind.side_effect = Err
 
         fut = self.loop.create_server(MyProto, '0.0.0.0', 0)
         self.assertRaises(OSError, self.loop.run_until_complete, fut)
         self.assertTrue(m_sock.close.called)
 
-    @unittest.mock.patch('asyncio.base_events.socket')
+    @mock.patch('asyncio.base_events.socket')
     def test_create_datagram_endpoint_no_addrinfo(self, m_socket):
         m_socket.getaddrinfo.return_value = []
         m_socket.getaddrinfo._is_coroutine = False
@@ -818,7 +818,7 @@
             AssertionError, self.loop.run_until_complete, coro)
 
     def test_create_datagram_endpoint_connect_err(self):
-        self.loop.sock_connect = unittest.mock.Mock()
+        self.loop.sock_connect = mock.Mock()
         self.loop.sock_connect.side_effect = OSError
 
         coro = self.loop.create_datagram_endpoint(
@@ -826,7 +826,7 @@
         self.assertRaises(
             OSError, self.loop.run_until_complete, coro)
 
-    @unittest.mock.patch('asyncio.base_events.socket')
+    @mock.patch('asyncio.base_events.socket')
     def test_create_datagram_endpoint_socket_err(self, m_socket):
         m_socket.getaddrinfo = socket.getaddrinfo
         m_socket.socket.side_effect = OSError
@@ -849,7 +849,7 @@
         self.assertRaises(
             ValueError, self.loop.run_until_complete, coro)
 
-    @unittest.mock.patch('asyncio.base_events.socket')
+    @mock.patch('asyncio.base_events.socket')
     def test_create_datagram_endpoint_setblk_err(self, m_socket):
         m_socket.socket.return_value.setblocking.side_effect = OSError
 
@@ -865,14 +865,14 @@
             asyncio.DatagramProtocol)
         self.assertRaises(ValueError, self.loop.run_until_complete, coro)
 
-    @unittest.mock.patch('asyncio.base_events.socket')
+    @mock.patch('asyncio.base_events.socket')
     def test_create_datagram_endpoint_cant_bind(self, m_socket):
         class Err(OSError):
             pass
 
         m_socket.AF_INET6 = socket.AF_INET6
         m_socket.getaddrinfo = socket.getaddrinfo
-        m_sock = m_socket.socket.return_value = unittest.mock.Mock()
+        m_sock = m_socket.socket.return_value = mock.Mock()
         m_sock.bind.side_effect = Err
 
         fut = self.loop.create_datagram_endpoint(
@@ -882,19 +882,19 @@
         self.assertTrue(m_sock.close.called)
 
     def test_accept_connection_retry(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.accept.side_effect = BlockingIOError()
 
         self.loop._accept_connection(MyProto, sock)
         self.assertFalse(sock.close.called)
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_accept_connection_exception(self, m_log):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         sock.accept.side_effect = OSError(errno.EMFILE, 'Too many open files')
-        self.loop.remove_reader = unittest.mock.Mock()
-        self.loop.call_later = unittest.mock.Mock()
+        self.loop.remove_reader = mock.Mock()
+        self.loop.call_later = mock.Mock()
 
         self.loop._accept_connection(MyProto, sock)
         self.assertTrue(m_log.error.called)
@@ -902,7 +902,7 @@
         self.loop.remove_reader.assert_called_with(10)
         self.loop.call_later.assert_called_with(constants.ACCEPT_RETRY_DELAY,
                                                 # self.loop._start_serving
-                                                unittest.mock.ANY,
+                                                mock.ANY,
                                                 MyProto, sock, None, None)
 
     def test_call_coroutine(self):
diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py
index d00af23..bafa875 100644
--- a/Lib/test/test_asyncio/test_events.py
+++ b/Lib/test/test_asyncio/test_events.py
@@ -20,12 +20,11 @@
 import time
 import errno
 import unittest
-import unittest.mock
+from unittest import mock
 from test import support  # find_unused_port, IPV6_ENABLED, TEST_HOME_DIR
 
 
 import asyncio
-from asyncio import events
 from asyncio import selector_events
 from asyncio import test_utils
 
@@ -57,6 +56,7 @@
 
 
 class MyBaseProto(asyncio.Protocol):
+    connected = None
     done = None
 
     def __init__(self, loop=None):
@@ -64,12 +64,15 @@
         self.state = 'INITIAL'
         self.nbytes = 0
         if loop is not None:
+            self.connected = asyncio.Future(loop=loop)
             self.done = asyncio.Future(loop=loop)
 
     def connection_made(self, transport):
         self.transport = transport
         assert self.state == 'INITIAL', self.state
         self.state = 'CONNECTED'
+        if self.connected:
+            self.connected.set_result(None)
 
     def data_received(self, data):
         assert self.state == 'CONNECTED', self.state
@@ -331,7 +334,8 @@
 
     def test_reader_callback(self):
         r, w = test_utils.socketpair()
-        bytes_read = []
+        r.setblocking(False)
+        bytes_read = bytearray()
 
         def reader():
             try:
@@ -341,37 +345,40 @@
                 # at least on Linux -- see man select.
                 return
             if data:
-                bytes_read.append(data)
+                bytes_read.extend(data)
             else:
                 self.assertTrue(self.loop.remove_reader(r.fileno()))
                 r.close()
 
         self.loop.add_reader(r.fileno(), reader)
         self.loop.call_soon(w.send, b'abc')
-        test_utils.run_briefly(self.loop)
+        test_utils.run_until(self.loop, lambda: len(bytes_read) >= 3)
         self.loop.call_soon(w.send, b'def')
-        test_utils.run_briefly(self.loop)
+        test_utils.run_until(self.loop, lambda: len(bytes_read) >= 6)
         self.loop.call_soon(w.close)
         self.loop.call_soon(self.loop.stop)
         self.loop.run_forever()
-        self.assertEqual(b''.join(bytes_read), b'abcdef')
+        self.assertEqual(bytes_read, b'abcdef')
 
     def test_writer_callback(self):
         r, w = test_utils.socketpair()
         w.setblocking(False)
-        self.loop.add_writer(w.fileno(), w.send, b'x'*(256*1024))
-        test_utils.run_briefly(self.loop)
 
-        def remove_writer():
-            self.assertTrue(self.loop.remove_writer(w.fileno()))
+        def writer(data):
+            w.send(data)
+            self.loop.stop()
 
-        self.loop.call_soon(remove_writer)
-        self.loop.call_soon(self.loop.stop)
+        data = b'x' * 1024
+        self.loop.add_writer(w.fileno(), writer, data)
         self.loop.run_forever()
+
+        self.assertTrue(self.loop.remove_writer(w.fileno()))
+        self.assertFalse(self.loop.remove_writer(w.fileno()))
+
         w.close()
-        data = r.recv(256*1024)
+        read = r.recv(len(data) * 2)
         r.close()
-        self.assertGreaterEqual(len(data), 200)
+        self.assertEqual(read, data)
 
     def _basetest_sock_client_ops(self, httpd, sock):
         sock.setblocking(False)
@@ -465,10 +472,10 @@
         self.assertFalse(self.loop.remove_signal_handler(signal.SIGKILL))
         # Now set a handler and handle it.
         self.loop.add_signal_handler(signal.SIGINT, my_handler)
-        test_utils.run_briefly(self.loop)
+
         os.kill(os.getpid(), signal.SIGINT)
-        test_utils.run_briefly(self.loop)
-        self.assertEqual(caught, 1)
+        test_utils.run_until(self.loop, lambda: caught)
+
         # Removing it should restore the default handler.
         self.assertTrue(self.loop.remove_signal_handler(signal.SIGINT))
         self.assertEqual(signal.getsignal(signal.SIGINT),
@@ -624,7 +631,7 @@
             self.assertIn(str(httpd.address), cm.exception.strerror)
 
     def test_create_server(self):
-        proto = MyProto()
+        proto = MyProto(self.loop)
         f = self.loop.create_server(lambda: proto, '0.0.0.0', 0)
         server = self.loop.run_until_complete(f)
         self.assertEqual(len(server.sockets), 1)
@@ -634,14 +641,11 @@
         client = socket.socket()
         client.connect(('127.0.0.1', port))
         client.sendall(b'xxx')
-        test_utils.run_briefly(self.loop)
-        test_utils.run_until(self.loop, lambda: proto is not None, 10)
-        self.assertIsInstance(proto, MyProto)
-        self.assertEqual('INITIAL', proto.state)
-        test_utils.run_briefly(self.loop)
+
+        self.loop.run_until_complete(proto.connected)
         self.assertEqual('CONNECTED', proto.state)
-        test_utils.run_until(self.loop, lambda: proto.nbytes > 0,
-                             timeout=10)
+
+        test_utils.run_until(self.loop, lambda: proto.nbytes > 0)
         self.assertEqual(3, proto.nbytes)
 
         # extra info is available
@@ -651,7 +655,7 @@
 
         # close connection
         proto.transport.close()
-        test_utils.run_briefly(self.loop)  # windows iocp
+        self.loop.run_until_complete(proto.done)
 
         self.assertEqual('CLOSED', proto.state)
 
@@ -673,27 +677,22 @@
 
     @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'No UNIX Sockets')
     def test_create_unix_server(self):
-        proto = MyProto()
+        proto = MyProto(loop=self.loop)
         server, path = self._make_unix_server(lambda: proto)
         self.assertEqual(len(server.sockets), 1)
 
         client = socket.socket(socket.AF_UNIX)
         client.connect(path)
         client.sendall(b'xxx')
-        test_utils.run_briefly(self.loop)
-        test_utils.run_until(self.loop, lambda: proto is not None, 10)
 
-        self.assertIsInstance(proto, MyProto)
-        self.assertEqual('INITIAL', proto.state)
-        test_utils.run_briefly(self.loop)
+        self.loop.run_until_complete(proto.connected)
         self.assertEqual('CONNECTED', proto.state)
-        test_utils.run_until(self.loop, lambda: proto.nbytes > 0,
-                             timeout=10)
+        test_utils.run_until(self.loop, lambda: proto.nbytes > 0)
         self.assertEqual(3, proto.nbytes)
 
         # close connection
         proto.transport.close()
-        test_utils.run_briefly(self.loop)  # windows iocp
+        self.loop.run_until_complete(proto.done)
 
         self.assertEqual('CLOSED', proto.state)
 
@@ -736,12 +735,10 @@
         client, pr = self.loop.run_until_complete(f_c)
 
         client.write(b'xxx')
-        test_utils.run_briefly(self.loop)
-        self.assertIsInstance(proto, MyProto)
-        test_utils.run_briefly(self.loop)
+        self.loop.run_until_complete(proto.connected)
         self.assertEqual('CONNECTED', proto.state)
-        test_utils.run_until(self.loop, lambda: proto.nbytes > 0,
-                             timeout=10)
+
+        test_utils.run_until(self.loop, lambda: proto.nbytes > 0)
         self.assertEqual(3, proto.nbytes)
 
         # extra info is available
@@ -775,12 +772,9 @@
         client, pr = self.loop.run_until_complete(f_c)
 
         client.write(b'xxx')
-        test_utils.run_briefly(self.loop)
-        self.assertIsInstance(proto, MyProto)
-        test_utils.run_briefly(self.loop)
+        self.loop.run_until_complete(proto.connected)
         self.assertEqual('CONNECTED', proto.state)
-        test_utils.run_until(self.loop, lambda: proto.nbytes > 0,
-                             timeout=10)
+        test_utils.run_until(self.loop, lambda: proto.nbytes > 0)
         self.assertEqual(3, proto.nbytes)
 
         # close connection
@@ -1045,15 +1039,9 @@
 
         self.assertEqual('INITIALIZED', client.state)
         transport.sendto(b'xxx')
-        for _ in range(1000):
-            if server.nbytes:
-                break
-            test_utils.run_briefly(self.loop)
+        test_utils.run_until(self.loop, lambda: server.nbytes)
         self.assertEqual(3, server.nbytes)
-        for _ in range(1000):
-            if client.nbytes:
-                break
-            test_utils.run_briefly(self.loop)
+        test_utils.run_until(self.loop, lambda: client.nbytes)
 
         # received
         self.assertEqual(8, client.nbytes)
@@ -1098,11 +1086,11 @@
         self.loop.run_until_complete(connect())
 
         os.write(wpipe, b'1')
-        test_utils.run_briefly(self.loop)
+        test_utils.run_until(self.loop, lambda: proto.nbytes >= 1)
         self.assertEqual(1, proto.nbytes)
 
         os.write(wpipe, b'2345')
-        test_utils.run_briefly(self.loop)
+        test_utils.run_until(self.loop, lambda: proto.nbytes >= 5)
         self.assertEqual(['INITIAL', 'CONNECTED'], proto.state)
         self.assertEqual(5, proto.nbytes)
 
@@ -1156,33 +1144,30 @@
     @unittest.skipUnless(sys.platform != 'win32',
                          "Don't support pipes for Windows")
     def test_write_pipe(self):
-        proto = MyWritePipeProto(loop=self.loop)
-        transport = None
-
         rpipe, wpipe = os.pipe()
         pipeobj = io.open(wpipe, 'wb', 1024)
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal transport
-            t, p = yield from self.loop.connect_write_pipe(
-                        lambda: proto, pipeobj)
-            self.assertIs(p, proto)
-            self.assertIs(t, proto.transport)
-            self.assertEqual('CONNECTED', proto.state)
-            transport = t
-
-        self.loop.run_until_complete(connect())
+        proto = MyWritePipeProto(loop=self.loop)
+        connect = self.loop.connect_write_pipe(lambda: proto, pipeobj)
+        transport, p = self.loop.run_until_complete(connect)
+        self.assertIs(p, proto)
+        self.assertIs(transport, proto.transport)
+        self.assertEqual('CONNECTED', proto.state)
 
         transport.write(b'1')
-        test_utils.run_briefly(self.loop)
-        data = os.read(rpipe, 1024)
+
+        data = bytearray()
+        def reader(data):
+            chunk = os.read(rpipe, 1024)
+            data += chunk
+            return len(data)
+
+        test_utils.run_until(self.loop, lambda: reader(data) >= 1)
         self.assertEqual(b'1', data)
 
         transport.write(b'2345')
-        test_utils.run_briefly(self.loop)
-        data = os.read(rpipe, 1024)
-        self.assertEqual(b'2345', data)
+        test_utils.run_until(self.loop, lambda: reader(data) >= 5)
+        self.assertEqual(b'12345', data)
         self.assertEqual('CONNECTED', proto.state)
 
         os.close(rpipe)
@@ -1198,23 +1183,14 @@
     @unittest.skipUnless(sys.platform != 'win32',
                          "Don't support pipes for Windows")
     def test_write_pipe_disconnect_on_close(self):
-        proto = MyWritePipeProto(loop=self.loop)
-        transport = None
-
         rsock, wsock = test_utils.socketpair()
         pipeobj = io.open(wsock.detach(), 'wb', 1024)
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal transport
-            t, p = yield from self.loop.connect_write_pipe(lambda: proto,
-                                                           pipeobj)
-            self.assertIs(p, proto)
-            self.assertIs(t, proto.transport)
-            self.assertEqual('CONNECTED', proto.state)
-            transport = t
-
-        self.loop.run_until_complete(connect())
+        proto = MyWritePipeProto(loop=self.loop)
+        connect = self.loop.connect_write_pipe(lambda: proto, pipeobj)
+        transport, p = self.loop.run_until_complete(connect)
+        self.assertIs(p, proto)
+        self.assertIs(transport, proto.transport)
         self.assertEqual('CONNECTED', proto.state)
 
         transport.write(b'1')
@@ -1232,33 +1208,32 @@
     # older than 10.6 (Snow Leopard)
     @support.requires_mac_ver(10, 6)
     def test_write_pty(self):
-        proto = MyWritePipeProto(loop=self.loop)
-        transport = None
-
         master, slave = os.openpty()
         slave_write_obj = io.open(slave, 'wb', 0)
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal transport
-            t, p = yield from self.loop.connect_write_pipe(lambda: proto,
-                                                           slave_write_obj)
-            self.assertIs(p, proto)
-            self.assertIs(t, proto.transport)
-            self.assertEqual('CONNECTED', proto.state)
-            transport = t
-
-        self.loop.run_until_complete(connect())
+        proto = MyWritePipeProto(loop=self.loop)
+        connect = self.loop.connect_write_pipe(lambda: proto, slave_write_obj)
+        transport, p = self.loop.run_until_complete(connect)
+        self.assertIs(p, proto)
+        self.assertIs(transport, proto.transport)
+        self.assertEqual('CONNECTED', proto.state)
 
         transport.write(b'1')
-        test_utils.run_briefly(self.loop)
-        data = os.read(master, 1024)
+
+        data = bytearray()
+        def reader(data):
+            chunk = os.read(master, 1024)
+            data += chunk
+            return len(data)
+
+        test_utils.run_until(self.loop, lambda: reader(data) >= 1,
+                             timeout=10)
         self.assertEqual(b'1', data)
 
         transport.write(b'2345')
-        test_utils.run_briefly(self.loop)
-        data = os.read(master, 1024)
-        self.assertEqual(b'2345', data)
+        test_utils.run_until(self.loop, lambda: reader(data) >= 5,
+                             timeout=10)
+        self.assertEqual(b'12345', data)
         self.assertEqual('CONNECTED', proto.state)
 
         os.close(master)
@@ -1352,6 +1327,30 @@
                     self.assertIn('address must be resolved',
                                   str(cm.exception))
 
+    def test_remove_fds_after_closing(self):
+        loop = self.create_event_loop()
+        callback = lambda: None
+        r, w = test_utils.socketpair()
+        self.addCleanup(r.close)
+        self.addCleanup(w.close)
+        loop.add_reader(r, callback)
+        loop.add_writer(w, callback)
+        loop.close()
+        self.assertFalse(loop.remove_reader(r))
+        self.assertFalse(loop.remove_writer(w))
+
+    def test_add_fds_after_closing(self):
+        loop = self.create_event_loop()
+        callback = lambda: None
+        r, w = test_utils.socketpair()
+        self.addCleanup(r.close)
+        self.addCleanup(w.close)
+        loop.close()
+        with self.assertRaises(RuntimeError):
+            loop.add_reader(r, callback)
+        with self.assertRaises(RuntimeError):
+            loop.add_writer(w, callback)
+
 
 class SubprocessTestsMixin:
 
@@ -1370,20 +1369,13 @@
             self.assertEqual(-signal.SIGKILL, returncode)
 
     def test_subprocess_exec(self):
-        proto = None
-        transp = None
-
         prog = os.path.join(os.path.dirname(__file__), 'echo.py')
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_exec(
-                functools.partial(MySubprocessProtocol, self.loop),
-                sys.executable, prog)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_exec(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        sys.executable, prog)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
         self.assertEqual('CONNECTED', proto.state)
 
@@ -1396,20 +1388,13 @@
         self.assertEqual(b'Python The Winner', proto.data[1])
 
     def test_subprocess_interactive(self):
-        proto = None
-        transp = None
-
         prog = os.path.join(os.path.dirname(__file__), 'echo.py')
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_exec(
-                functools.partial(MySubprocessProtocol, self.loop),
-                sys.executable, prog)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_exec(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        sys.executable, prog)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
         self.assertEqual('CONNECTED', proto.state)
 
@@ -1430,18 +1415,11 @@
         self.check_terminated(proto.returncode)
 
     def test_subprocess_shell(self):
-        proto = None
-        transp = None
-
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_shell(
-                functools.partial(MySubprocessProtocol, self.loop),
-                'echo Python')
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_shell(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        'echo Python')
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
 
         transp.get_pipe_transport(0).close()
@@ -1452,33 +1430,20 @@
         self.assertEqual(proto.data[2], b'')
 
     def test_subprocess_exitcode(self):
-        proto = None
-
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto
-            transp, proto = yield from self.loop.subprocess_shell(
-                functools.partial(MySubprocessProtocol, self.loop),
-                'exit 7', stdin=None, stdout=None, stderr=None)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_shell(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        'exit 7', stdin=None, stdout=None, stderr=None)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.completed)
         self.assertEqual(7, proto.returncode)
 
     def test_subprocess_close_after_finish(self):
-        proto = None
-        transp = None
-
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_shell(
-                functools.partial(MySubprocessProtocol, self.loop),
-                'exit 7', stdin=None, stdout=None, stderr=None)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_shell(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        'exit 7', stdin=None, stdout=None, stderr=None)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.assertIsNone(transp.get_pipe_transport(0))
         self.assertIsNone(transp.get_pipe_transport(1))
         self.assertIsNone(transp.get_pipe_transport(2))
@@ -1487,20 +1452,13 @@
         self.assertIsNone(transp.close())
 
     def test_subprocess_kill(self):
-        proto = None
-        transp = None
-
         prog = os.path.join(os.path.dirname(__file__), 'echo.py')
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_exec(
-                functools.partial(MySubprocessProtocol, self.loop),
-                sys.executable, prog)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_exec(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        sys.executable, prog)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
 
         transp.kill()
@@ -1508,20 +1466,13 @@
         self.check_killed(proto.returncode)
 
     def test_subprocess_terminate(self):
-        proto = None
-        transp = None
-
         prog = os.path.join(os.path.dirname(__file__), 'echo.py')
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_exec(
-                functools.partial(MySubprocessProtocol, self.loop),
-                sys.executable, prog)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_exec(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        sys.executable, prog)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
 
         transp.terminate()
@@ -1530,20 +1481,13 @@
 
     @unittest.skipIf(sys.platform == 'win32', "Don't have SIGHUP")
     def test_subprocess_send_signal(self):
-        proto = None
-        transp = None
-
         prog = os.path.join(os.path.dirname(__file__), 'echo.py')
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_exec(
-                functools.partial(MySubprocessProtocol, self.loop),
-                sys.executable, prog)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_exec(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        sys.executable, prog)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
 
         transp.send_signal(signal.SIGHUP)
@@ -1551,20 +1495,13 @@
         self.assertEqual(-signal.SIGHUP, proto.returncode)
 
     def test_subprocess_stderr(self):
-        proto = None
-        transp = None
-
         prog = os.path.join(os.path.dirname(__file__), 'echo2.py')
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_exec(
-                functools.partial(MySubprocessProtocol, self.loop),
-                sys.executable, prog)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_exec(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        sys.executable, prog)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
 
         stdin = transp.get_pipe_transport(0)
@@ -1578,20 +1515,13 @@
         self.assertEqual(0, proto.returncode)
 
     def test_subprocess_stderr_redirect_to_stdout(self):
-        proto = None
-        transp = None
-
         prog = os.path.join(os.path.dirname(__file__), 'echo2.py')
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_exec(
-                functools.partial(MySubprocessProtocol, self.loop),
-                sys.executable, prog, stderr=subprocess.STDOUT)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_exec(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        sys.executable, prog, stderr=subprocess.STDOUT)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
 
         stdin = transp.get_pipe_transport(0)
@@ -1608,20 +1538,13 @@
         self.assertEqual(0, proto.returncode)
 
     def test_subprocess_close_client_stream(self):
-        proto = None
-        transp = None
-
         prog = os.path.join(os.path.dirname(__file__), 'echo3.py')
 
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto, transp
-            transp, proto = yield from self.loop.subprocess_exec(
-                functools.partial(MySubprocessProtocol, self.loop),
-                sys.executable, prog)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        connect = self.loop.subprocess_exec(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        sys.executable, prog)
+        transp, proto = self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.connected)
 
         stdin = transp.get_pipe_transport(0)
@@ -1647,20 +1570,13 @@
         self.check_terminated(proto.returncode)
 
     def test_subprocess_wait_no_same_group(self):
-        proto = None
-        transp = None
-
-        @asyncio.coroutine
-        def connect():
-            nonlocal proto
-            # start the new process in a new session
-            transp, proto = yield from self.loop.subprocess_shell(
-                functools.partial(MySubprocessProtocol, self.loop),
-                'exit 7', stdin=None, stdout=None, stderr=None,
-                start_new_session=True)
-            self.assertIsInstance(proto, MySubprocessProtocol)
-
-        self.loop.run_until_complete(connect())
+        # start the new process in a new session
+        connect = self.loop.subprocess_shell(
+                        functools.partial(MySubprocessProtocol, self.loop),
+                        'exit 7', stdin=None, stdout=None, stderr=None,
+                        start_new_session=True)
+        _, proto = yield self.loop.run_until_complete(connect)
+        self.assertIsInstance(proto, MySubprocessProtocol)
         self.loop.run_until_complete(proto.completed)
         self.assertEqual(7, proto.returncode)
 
@@ -1741,6 +1657,9 @@
         def test_create_datagram_endpoint(self):
             raise unittest.SkipTest(
                 "IocpEventLoop does not have create_datagram_endpoint()")
+
+        def test_remove_fds_after_closing(self):
+            raise unittest.SkipTest("IocpEventLoop does not have add_reader()")
 else:
     from asyncio import selectors
 
@@ -1812,7 +1731,7 @@
             return args
 
         args = ()
-        h = asyncio.Handle(callback, args, unittest.mock.Mock())
+        h = asyncio.Handle(callback, args, mock.Mock())
         self.assertIs(h._callback, callback)
         self.assertIs(h._args, args)
         self.assertFalse(h._cancelled)
@@ -1844,15 +1763,15 @@
         def callback():
             raise ValueError()
 
-        m_loop = unittest.mock.Mock()
-        m_loop.call_exception_handler = unittest.mock.Mock()
+        m_loop = mock.Mock()
+        m_loop.call_exception_handler = mock.Mock()
 
         h = asyncio.Handle(callback, (), m_loop)
         h._run()
 
         m_loop.call_exception_handler.assert_called_with({
             'message': test_utils.MockPattern('Exception in callback.*'),
-            'exception': unittest.mock.ANY,
+            'exception': mock.ANY,
             'handle': h
         })
 
@@ -1862,7 +1781,7 @@
     def test_hash(self):
         when = time.monotonic()
         h = asyncio.TimerHandle(when, lambda: False, (),
-                                unittest.mock.Mock())
+                                mock.Mock())
         self.assertEqual(hash(h), hash(when))
 
     def test_timer(self):
@@ -1871,7 +1790,7 @@
 
         args = ()
         when = time.monotonic()
-        h = asyncio.TimerHandle(when, callback, args, unittest.mock.Mock())
+        h = asyncio.TimerHandle(when, callback, args, mock.Mock())
         self.assertIs(h._callback, callback)
         self.assertIs(h._args, args)
         self.assertFalse(h._cancelled)
@@ -1887,10 +1806,10 @@
 
         self.assertRaises(AssertionError,
                           asyncio.TimerHandle, None, callback, args,
-                          unittest.mock.Mock())
+                          mock.Mock())
 
     def test_timer_comparison(self):
-        loop = unittest.mock.Mock()
+        loop = mock.Mock()
 
         def callback(*args):
             return args
@@ -1935,7 +1854,7 @@
 class AbstractEventLoopTests(unittest.TestCase):
 
     def test_not_implemented(self):
-        f = unittest.mock.Mock()
+        f = mock.Mock()
         loop = asyncio.AbstractEventLoop()
         self.assertRaises(
             NotImplementedError, loop.run_forever)
@@ -1995,13 +1914,13 @@
             NotImplementedError, loop.remove_signal_handler, 1)
         self.assertRaises(
             NotImplementedError, loop.connect_read_pipe, f,
-            unittest.mock.sentinel.pipe)
+            mock.sentinel.pipe)
         self.assertRaises(
             NotImplementedError, loop.connect_write_pipe, f,
-            unittest.mock.sentinel.pipe)
+            mock.sentinel.pipe)
         self.assertRaises(
             NotImplementedError, loop.subprocess_shell, f,
-            unittest.mock.sentinel)
+            mock.sentinel)
         self.assertRaises(
             NotImplementedError, loop.subprocess_exec, f)
 
@@ -2009,7 +1928,7 @@
 class ProtocolsAbsTests(unittest.TestCase):
 
     def test_empty(self):
-        f = unittest.mock.Mock()
+        f = mock.Mock()
         p = asyncio.Protocol()
         self.assertIsNone(p.connection_made(f))
         self.assertIsNone(p.connection_lost(f))
@@ -2055,7 +1974,7 @@
     def test_get_event_loop_calls_set_event_loop(self):
         policy = asyncio.DefaultEventLoopPolicy()
 
-        with unittest.mock.patch.object(
+        with mock.patch.object(
                 policy, "set_event_loop",
                 wraps=policy.set_event_loop) as m_set_event_loop:
 
@@ -2073,7 +1992,7 @@
         policy.set_event_loop(None)
         self.assertRaises(AssertionError, policy.get_event_loop)
 
-    @unittest.mock.patch('asyncio.events.threading.current_thread')
+    @mock.patch('asyncio.events.threading.current_thread')
     def test_get_event_loop_thread(self, m_current_thread):
 
         def f():
diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py
index f2b81dd..399e8f4 100644
--- a/Lib/test/test_asyncio/test_futures.py
+++ b/Lib/test/test_asyncio/test_futures.py
@@ -3,7 +3,7 @@
 import concurrent.futures
 import threading
 import unittest
-import unittest.mock
+from unittest import mock
 
 import asyncio
 from asyncio import test_utils
@@ -174,20 +174,20 @@
         self.assertRaises(AssertionError, test)
         fut.cancel()
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_tb_logger_abandoned(self, m_log):
         fut = asyncio.Future(loop=self.loop)
         del fut
         self.assertFalse(m_log.error.called)
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_tb_logger_result_unretrieved(self, m_log):
         fut = asyncio.Future(loop=self.loop)
         fut.set_result(42)
         del fut
         self.assertFalse(m_log.error.called)
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_tb_logger_result_retrieved(self, m_log):
         fut = asyncio.Future(loop=self.loop)
         fut.set_result(42)
@@ -195,7 +195,7 @@
         del fut
         self.assertFalse(m_log.error.called)
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_tb_logger_exception_unretrieved(self, m_log):
         fut = asyncio.Future(loop=self.loop)
         fut.set_exception(RuntimeError('boom'))
@@ -203,7 +203,7 @@
         test_utils.run_briefly(self.loop)
         self.assertTrue(m_log.error.called)
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_tb_logger_exception_retrieved(self, m_log):
         fut = asyncio.Future(loop=self.loop)
         fut.set_exception(RuntimeError('boom'))
@@ -211,7 +211,7 @@
         del fut
         self.assertFalse(m_log.error.called)
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_tb_logger_exception_result_retrieved(self, m_log):
         fut = asyncio.Future(loop=self.loop)
         fut.set_exception(RuntimeError('boom'))
@@ -236,7 +236,7 @@
         f2 = asyncio.wrap_future(f1)
         self.assertIs(f1, f2)
 
-    @unittest.mock.patch('asyncio.futures.events')
+    @mock.patch('asyncio.futures.events')
     def test_wrap_future_use_global_loop(self, m_events):
         def run(arg):
             return (arg, threading.get_ident())
diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py
index 0975f49..f542463 100644
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -1,7 +1,7 @@
 """Tests for lock.py"""
 
 import unittest
-import unittest.mock
+from unittest import mock
 import re
 
 import asyncio
@@ -27,7 +27,7 @@
         self.loop.close()
 
     def test_ctor_loop(self):
-        loop = unittest.mock.Mock()
+        loop = mock.Mock()
         lock = asyncio.Lock(loop=loop)
         self.assertIs(lock._loop, loop)
 
@@ -250,7 +250,7 @@
         self.loop.close()
 
     def test_ctor_loop(self):
-        loop = unittest.mock.Mock()
+        loop = mock.Mock()
         ev = asyncio.Event(loop=loop)
         self.assertIs(ev._loop, loop)
 
@@ -275,7 +275,7 @@
         self.assertTrue(repr(ev).endswith('[set]>'))
         self.assertTrue(RGX_REPR.match(repr(ev)))
 
-        ev._waiters.append(unittest.mock.Mock())
+        ev._waiters.append(mock.Mock())
         self.assertTrue('waiters:1' in repr(ev))
         self.assertTrue(RGX_REPR.match(repr(ev)))
 
@@ -386,7 +386,7 @@
         self.loop.close()
 
     def test_ctor_loop(self):
-        loop = unittest.mock.Mock()
+        loop = mock.Mock()
         cond = asyncio.Condition(loop=loop)
         self.assertIs(cond._loop, loop)
 
@@ -644,11 +644,11 @@
         self.loop.run_until_complete(cond.acquire())
         self.assertTrue('locked' in repr(cond))
 
-        cond._waiters.append(unittest.mock.Mock())
+        cond._waiters.append(mock.Mock())
         self.assertTrue('waiters:1' in repr(cond))
         self.assertTrue(RGX_REPR.match(repr(cond)))
 
-        cond._waiters.append(unittest.mock.Mock())
+        cond._waiters.append(mock.Mock())
         self.assertTrue('waiters:2' in repr(cond))
         self.assertTrue(RGX_REPR.match(repr(cond)))
 
@@ -688,7 +688,7 @@
         self.loop.close()
 
     def test_ctor_loop(self):
-        loop = unittest.mock.Mock()
+        loop = mock.Mock()
         sem = asyncio.Semaphore(loop=loop)
         self.assertIs(sem._loop, loop)
 
@@ -717,11 +717,11 @@
         self.assertTrue('waiters' not in repr(sem))
         self.assertTrue(RGX_REPR.match(repr(sem)))
 
-        sem._waiters.append(unittest.mock.Mock())
+        sem._waiters.append(mock.Mock())
         self.assertTrue('waiters:1' in repr(sem))
         self.assertTrue(RGX_REPR.match(repr(sem)))
 
-        sem._waiters.append(unittest.mock.Mock())
+        sem._waiters.append(mock.Mock())
         self.assertTrue('waiters:2' in repr(sem))
         self.assertTrue(RGX_REPR.match(repr(sem)))
 
diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py
index 0892069..5bf24a4 100644
--- a/Lib/test/test_asyncio/test_proactor_events.py
+++ b/Lib/test/test_asyncio/test_proactor_events.py
@@ -2,7 +2,7 @@
 
 import socket
 import unittest
-import unittest.mock
+from unittest import mock
 
 import asyncio
 from asyncio.proactor_events import BaseProactorEventLoop
@@ -16,10 +16,10 @@
 
     def setUp(self):
         self.loop = test_utils.TestLoop()
-        self.proactor = unittest.mock.Mock()
+        self.proactor = mock.Mock()
         self.loop._proactor = self.proactor
         self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
-        self.sock = unittest.mock.Mock(socket.socket)
+        self.sock = mock.Mock(socket.socket)
 
     def test_ctor(self):
         fut = asyncio.Future(loop=self.loop)
@@ -56,7 +56,7 @@
 
         self.assertRaises(AssertionError, tr._loop_reading, res)
 
-        tr.close = unittest.mock.Mock()
+        tr.close = mock.Mock()
         tr._read_fut = res
         tr._loop_reading(res)
         self.assertFalse(self.loop._proactor.recv.called)
@@ -67,7 +67,7 @@
         err = self.loop._proactor.recv.side_effect = ConnectionAbortedError()
 
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-        tr._fatal_error = unittest.mock.Mock()
+        tr._fatal_error = mock.Mock()
         tr._loop_reading()
         tr._fatal_error.assert_called_with(
                             err,
@@ -78,7 +78,7 @@
 
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
         tr._closing = True
-        tr._fatal_error = unittest.mock.Mock()
+        tr._fatal_error = mock.Mock()
         tr._loop_reading()
         self.assertFalse(tr._fatal_error.called)
 
@@ -86,7 +86,7 @@
         self.loop._proactor.recv.side_effect = ConnectionAbortedError()
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
         tr._closing = False
-        tr._fatal_error = unittest.mock.Mock()
+        tr._fatal_error = mock.Mock()
         tr._loop_reading()
         self.assertTrue(tr._fatal_error.called)
 
@@ -95,8 +95,8 @@
 
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
         tr._closing = False
-        tr._fatal_error = unittest.mock.Mock()
-        tr._force_close = unittest.mock.Mock()
+        tr._fatal_error = mock.Mock()
+        tr._force_close = mock.Mock()
         tr._loop_reading()
         self.assertFalse(tr._fatal_error.called)
         tr._force_close.assert_called_with(err)
@@ -105,7 +105,7 @@
         err = self.loop._proactor.recv.side_effect = (OSError())
 
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-        tr._fatal_error = unittest.mock.Mock()
+        tr._fatal_error = mock.Mock()
         tr._loop_reading()
         tr._fatal_error.assert_called_with(
                             err,
@@ -113,7 +113,7 @@
 
     def test_write(self):
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-        tr._loop_writing = unittest.mock.Mock()
+        tr._loop_writing = mock.Mock()
         tr.write(b'data')
         self.assertEqual(tr._buffer, None)
         tr._loop_writing.assert_called_with(data=b'data')
@@ -125,8 +125,8 @@
 
     def test_write_more(self):
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-        tr._write_fut = unittest.mock.Mock()
-        tr._loop_writing = unittest.mock.Mock()
+        tr._write_fut = mock.Mock()
+        tr._loop_writing = mock.Mock()
         tr.write(b'data')
         self.assertEqual(tr._buffer, b'data')
         self.assertFalse(tr._loop_writing.called)
@@ -139,11 +139,11 @@
         self.loop._proactor.send.return_value.add_done_callback.\
             assert_called_with(tr._loop_writing)
 
-    @unittest.mock.patch('asyncio.proactor_events.logger')
+    @mock.patch('asyncio.proactor_events.logger')
     def test_loop_writing_err(self, m_log):
         err = self.loop._proactor.send.side_effect = OSError()
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-        tr._fatal_error = unittest.mock.Mock()
+        tr._fatal_error = mock.Mock()
         tr._buffer = [b'da', b'ta']
         tr._loop_writing()
         tr._fatal_error.assert_called_with(
@@ -182,7 +182,7 @@
 
     def test_abort(self):
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-        tr._force_close = unittest.mock.Mock()
+        tr._force_close = mock.Mock()
         tr.abort()
         tr._force_close.assert_called_with(None)
 
@@ -201,7 +201,7 @@
 
     def test_close_write_fut(self):
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-        tr._write_fut = unittest.mock.Mock()
+        tr._write_fut = mock.Mock()
         tr.close()
         test_utils.run_briefly(self.loop)
         self.assertFalse(self.protocol.connection_lost.called)
@@ -213,10 +213,10 @@
         test_utils.run_briefly(self.loop)
         self.assertFalse(self.protocol.connection_lost.called)
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_fatal_error(self, m_logging):
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
-        tr._force_close = unittest.mock.Mock()
+        tr._force_close = mock.Mock()
         tr._fatal_error(None)
         self.assertTrue(tr._force_close.called)
         self.assertTrue(m_logging.error.called)
@@ -224,8 +224,8 @@
     def test_force_close(self):
         tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
         tr._buffer = [b'data']
-        read_fut = tr._read_fut = unittest.mock.Mock()
-        write_fut = tr._write_fut = unittest.mock.Mock()
+        read_fut = tr._read_fut = mock.Mock()
+        write_fut = tr._write_fut = mock.Mock()
         tr._force_close(None)
 
         read_fut.cancel.assert_called_with()
@@ -346,10 +346,10 @@
 class BaseProactorEventLoopTests(unittest.TestCase):
 
     def setUp(self):
-        self.sock = unittest.mock.Mock(socket.socket)
-        self.proactor = unittest.mock.Mock()
+        self.sock = mock.Mock(socket.socket)
+        self.proactor = mock.Mock()
 
-        self.ssock, self.csock = unittest.mock.Mock(), unittest.mock.Mock()
+        self.ssock, self.csock = mock.Mock(), mock.Mock()
 
         class EventLoop(BaseProactorEventLoop):
             def _socketpair(s):
@@ -357,11 +357,11 @@
 
         self.loop = EventLoop(self.proactor)
 
-    @unittest.mock.patch.object(BaseProactorEventLoop, 'call_soon')
-    @unittest.mock.patch.object(BaseProactorEventLoop, '_socketpair')
+    @mock.patch.object(BaseProactorEventLoop, 'call_soon')
+    @mock.patch.object(BaseProactorEventLoop, '_socketpair')
     def test_ctor(self, socketpair, call_soon):
         ssock, csock = socketpair.return_value = (
-            unittest.mock.Mock(), unittest.mock.Mock())
+            mock.Mock(), mock.Mock())
         loop = BaseProactorEventLoop(self.proactor)
         self.assertIs(loop._ssock, ssock)
         self.assertIs(loop._csock, csock)
@@ -377,7 +377,7 @@
         self.assertIsNone(self.loop._csock)
 
     def test_close(self):
-        self.loop._close_self_pipe = unittest.mock.Mock()
+        self.loop._close_self_pipe = mock.Mock()
         self.loop.close()
         self.assertTrue(self.loop._close_self_pipe.called)
         self.assertTrue(self.proactor.close.called)
@@ -418,7 +418,7 @@
             self.loop._loop_self_reading)
 
     def test_loop_self_reading_fut(self):
-        fut = unittest.mock.Mock()
+        fut = mock.Mock()
         self.loop._loop_self_reading(fut)
         self.assertTrue(fut.result.called)
         self.proactor.recv.assert_called_with(self.ssock, 4096)
@@ -426,7 +426,7 @@
             self.loop._loop_self_reading)
 
     def test_loop_self_reading_exception(self):
-        self.loop.close = unittest.mock.Mock()
+        self.loop.close = mock.Mock()
         self.proactor.recv.side_effect = OSError()
         self.assertRaises(OSError, self.loop._loop_self_reading)
         self.assertTrue(self.loop.close.called)
@@ -438,10 +438,10 @@
     def test_process_events(self):
         self.loop._process_events([])
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_create_server(self, m_log):
-        pf = unittest.mock.Mock()
-        call_soon = self.loop.call_soon = unittest.mock.Mock()
+        pf = mock.Mock()
+        call_soon = self.loop.call_soon = mock.Mock()
 
         self.loop._start_serving(pf, self.sock)
         self.assertTrue(call_soon.called)
@@ -452,10 +452,10 @@
         self.proactor.accept.assert_called_with(self.sock)
 
         # conn
-        fut = unittest.mock.Mock()
-        fut.result.return_value = (unittest.mock.Mock(), unittest.mock.Mock())
+        fut = mock.Mock()
+        fut.result.return_value = (mock.Mock(), mock.Mock())
 
-        make_tr = self.loop._make_socket_transport = unittest.mock.Mock()
+        make_tr = self.loop._make_socket_transport = mock.Mock()
         loop(fut)
         self.assertTrue(fut.result.called)
         self.assertTrue(make_tr.called)
@@ -467,8 +467,8 @@
         self.assertTrue(m_log.error.called)
 
     def test_create_server_cancel(self):
-        pf = unittest.mock.Mock()
-        call_soon = self.loop.call_soon = unittest.mock.Mock()
+        pf = mock.Mock()
+        call_soon = self.loop.call_soon = mock.Mock()
 
         self.loop._start_serving(pf, self.sock)
         loop = call_soon.call_args[0][0]
@@ -480,7 +480,7 @@
         self.assertTrue(self.sock.close.called)
 
     def test_stop_serving(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         self.loop._stop_serving(sock)
         self.assertTrue(sock.close.called)
         self.proactor._stop_serving.assert_called_with(sock)
diff --git a/Lib/test/test_asyncio/test_queues.py b/Lib/test/test_asyncio/test_queues.py
index fc2bf46..f79fee2 100644
--- a/Lib/test/test_asyncio/test_queues.py
+++ b/Lib/test/test_asyncio/test_queues.py
@@ -1,7 +1,7 @@
 """Tests for queues.py"""
 
 import unittest
-import unittest.mock
+from unittest import mock
 
 import asyncio
 from asyncio import test_utils
@@ -72,7 +72,7 @@
         self.assertTrue('_queue=[1]' in fn(q))
 
     def test_ctor_loop(self):
-        loop = unittest.mock.Mock()
+        loop = mock.Mock()
         q = asyncio.Queue(loop=loop)
         self.assertIs(q._loop, loop)
 
diff --git a/Lib/test/test_asyncio/test_selector_events.py b/Lib/test/test_asyncio/test_selector_events.py
index 247df9e..964b2e8 100644
--- a/Lib/test/test_asyncio/test_selector_events.py
+++ b/Lib/test/test_asyncio/test_selector_events.py
@@ -1,13 +1,12 @@
 """Tests for selector_events.py"""
 
-import collections
 import errno
 import gc
 import pprint
 import socket
 import sys
 import unittest
-import unittest.mock
+from unittest import mock
 try:
     import ssl
 except ImportError:
@@ -23,14 +22,14 @@
 from asyncio.selector_events import _SelectorDatagramTransport
 
 
-MOCK_ANY = unittest.mock.ANY
+MOCK_ANY = mock.ANY
 
 
 class TestBaseSelectorEventLoop(BaseSelectorEventLoop):
 
     def _make_self_pipe(self):
-        self._ssock = unittest.mock.Mock()
-        self._csock = unittest.mock.Mock()
+        self._ssock = mock.Mock()
+        self._csock = mock.Mock()
         self._internal_fds += 1
 
 
@@ -41,34 +40,34 @@
 class BaseSelectorEventLoopTests(unittest.TestCase):
 
     def setUp(self):
-        selector = unittest.mock.Mock()
+        selector = mock.Mock()
         self.loop = TestBaseSelectorEventLoop(selector)
 
     def test_make_socket_transport(self):
-        m = unittest.mock.Mock()
-        self.loop.add_reader = unittest.mock.Mock()
+        m = mock.Mock()
+        self.loop.add_reader = mock.Mock()
         transport = self.loop._make_socket_transport(m, asyncio.Protocol())
         self.assertIsInstance(transport, _SelectorSocketTransport)
 
     @unittest.skipIf(ssl is None, 'No ssl module')
     def test_make_ssl_transport(self):
-        m = unittest.mock.Mock()
-        self.loop.add_reader = unittest.mock.Mock()
-        self.loop.add_writer = unittest.mock.Mock()
-        self.loop.remove_reader = unittest.mock.Mock()
-        self.loop.remove_writer = unittest.mock.Mock()
+        m = mock.Mock()
+        self.loop.add_reader = mock.Mock()
+        self.loop.add_writer = mock.Mock()
+        self.loop.remove_reader = mock.Mock()
+        self.loop.remove_writer = mock.Mock()
         waiter = asyncio.Future(loop=self.loop)
         transport = self.loop._make_ssl_transport(
             m, asyncio.Protocol(), m, waiter)
         self.assertIsInstance(transport, _SelectorSslTransport)
 
-    @unittest.mock.patch('asyncio.selector_events.ssl', None)
+    @mock.patch('asyncio.selector_events.ssl', None)
     def test_make_ssl_transport_without_ssl_error(self):
-        m = unittest.mock.Mock()
-        self.loop.add_reader = unittest.mock.Mock()
-        self.loop.add_writer = unittest.mock.Mock()
-        self.loop.remove_reader = unittest.mock.Mock()
-        self.loop.remove_writer = unittest.mock.Mock()
+        m = mock.Mock()
+        self.loop.add_reader = mock.Mock()
+        self.loop.add_writer = mock.Mock()
+        self.loop.remove_reader = mock.Mock()
+        self.loop.remove_writer = mock.Mock()
         with self.assertRaises(RuntimeError):
             self.loop._make_ssl_transport(m, m, m, m)
 
@@ -77,10 +76,10 @@
         ssock.fileno.return_value = 7
         csock = self.loop._csock
         csock.fileno.return_value = 1
-        remove_reader = self.loop.remove_reader = unittest.mock.Mock()
+        remove_reader = self.loop.remove_reader = mock.Mock()
 
         self.loop._selector.close()
-        self.loop._selector = selector = unittest.mock.Mock()
+        self.loop._selector = selector = mock.Mock()
         self.loop.close()
         self.assertIsNone(self.loop._selector)
         self.assertIsNone(self.loop._csock)
@@ -96,7 +95,7 @@
     def test_close_no_selector(self):
         ssock = self.loop._ssock
         csock = self.loop._csock
-        remove_reader = self.loop.remove_reader = unittest.mock.Mock()
+        remove_reader = self.loop.remove_reader = mock.Mock()
 
         self.loop._selector.close()
         self.loop._selector = None
@@ -126,15 +125,15 @@
         self.assertRaises(OSError, self.loop._write_to_self)
 
     def test_sock_recv(self):
-        sock = unittest.mock.Mock()
-        self.loop._sock_recv = unittest.mock.Mock()
+        sock = mock.Mock()
+        self.loop._sock_recv = mock.Mock()
 
         f = self.loop.sock_recv(sock, 1024)
         self.assertIsInstance(f, asyncio.Future)
         self.loop._sock_recv.assert_called_with(f, False, sock, 1024)
 
     def test__sock_recv_canceled_fut(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
 
         f = asyncio.Future(loop=self.loop)
         f.cancel()
@@ -143,30 +142,30 @@
         self.assertFalse(sock.recv.called)
 
     def test__sock_recv_unregister(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
 
         f = asyncio.Future(loop=self.loop)
         f.cancel()
 
-        self.loop.remove_reader = unittest.mock.Mock()
+        self.loop.remove_reader = mock.Mock()
         self.loop._sock_recv(f, True, sock, 1024)
         self.assertEqual((10,), self.loop.remove_reader.call_args[0])
 
     def test__sock_recv_tryagain(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         sock.recv.side_effect = BlockingIOError
 
-        self.loop.add_reader = unittest.mock.Mock()
+        self.loop.add_reader = mock.Mock()
         self.loop._sock_recv(f, False, sock, 1024)
         self.assertEqual((10, self.loop._sock_recv, f, True, sock, 1024),
                          self.loop.add_reader.call_args[0])
 
     def test__sock_recv_exception(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         err = sock.recv.side_effect = OSError()
 
@@ -174,8 +173,8 @@
         self.assertIs(err, f.exception())
 
     def test_sock_sendall(self):
-        sock = unittest.mock.Mock()
-        self.loop._sock_sendall = unittest.mock.Mock()
+        sock = mock.Mock()
+        self.loop._sock_sendall = mock.Mock()
 
         f = self.loop.sock_sendall(sock, b'data')
         self.assertIsInstance(f, asyncio.Future)
@@ -184,8 +183,8 @@
             self.loop._sock_sendall.call_args[0])
 
     def test_sock_sendall_nodata(self):
-        sock = unittest.mock.Mock()
-        self.loop._sock_sendall = unittest.mock.Mock()
+        sock = mock.Mock()
+        self.loop._sock_sendall = mock.Mock()
 
         f = self.loop.sock_sendall(sock, b'')
         self.assertIsInstance(f, asyncio.Future)
@@ -194,7 +193,7 @@
         self.assertFalse(self.loop._sock_sendall.called)
 
     def test__sock_sendall_canceled_fut(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
 
         f = asyncio.Future(loop=self.loop)
         f.cancel()
@@ -203,23 +202,23 @@
         self.assertFalse(sock.send.called)
 
     def test__sock_sendall_unregister(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
 
         f = asyncio.Future(loop=self.loop)
         f.cancel()
 
-        self.loop.remove_writer = unittest.mock.Mock()
+        self.loop.remove_writer = mock.Mock()
         self.loop._sock_sendall(f, True, sock, b'data')
         self.assertEqual((10,), self.loop.remove_writer.call_args[0])
 
     def test__sock_sendall_tryagain(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         sock.send.side_effect = BlockingIOError
 
-        self.loop.add_writer = unittest.mock.Mock()
+        self.loop.add_writer = mock.Mock()
         self.loop._sock_sendall(f, False, sock, b'data')
         self.assertEqual(
             (10, self.loop._sock_sendall, f, True, sock, b'data'),
@@ -227,11 +226,11 @@
 
     def test__sock_sendall_interrupted(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         sock.send.side_effect = InterruptedError
 
-        self.loop.add_writer = unittest.mock.Mock()
+        self.loop.add_writer = mock.Mock()
         self.loop._sock_sendall(f, False, sock, b'data')
         self.assertEqual(
             (10, self.loop._sock_sendall, f, True, sock, b'data'),
@@ -239,7 +238,7 @@
 
     def test__sock_sendall_exception(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         err = sock.send.side_effect = OSError()
 
@@ -247,7 +246,7 @@
         self.assertIs(f.exception(), err)
 
     def test__sock_sendall(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
 
         f = asyncio.Future(loop=self.loop)
         sock.fileno.return_value = 10
@@ -258,13 +257,13 @@
         self.assertIsNone(f.result())
 
     def test__sock_sendall_partial(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
 
         f = asyncio.Future(loop=self.loop)
         sock.fileno.return_value = 10
         sock.send.return_value = 2
 
-        self.loop.add_writer = unittest.mock.Mock()
+        self.loop.add_writer = mock.Mock()
         self.loop._sock_sendall(f, False, sock, b'data')
         self.assertFalse(f.done())
         self.assertEqual(
@@ -272,13 +271,13 @@
             self.loop.add_writer.call_args[0])
 
     def test__sock_sendall_none(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
 
         f = asyncio.Future(loop=self.loop)
         sock.fileno.return_value = 10
         sock.send.return_value = 0
 
-        self.loop.add_writer = unittest.mock.Mock()
+        self.loop.add_writer = mock.Mock()
         self.loop._sock_sendall(f, False, sock, b'data')
         self.assertFalse(f.done())
         self.assertEqual(
@@ -286,8 +285,8 @@
             self.loop.add_writer.call_args[0])
 
     def test_sock_connect(self):
-        sock = unittest.mock.Mock()
-        self.loop._sock_connect = unittest.mock.Mock()
+        sock = mock.Mock()
+        self.loop._sock_connect = mock.Mock()
 
         f = self.loop.sock_connect(sock, ('127.0.0.1', 8080))
         self.assertIsInstance(f, asyncio.Future)
@@ -298,7 +297,7 @@
     def test__sock_connect(self):
         f = asyncio.Future(loop=self.loop)
 
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
 
         self.loop._sock_connect(f, False, sock, ('127.0.0.1', 8080))
@@ -307,7 +306,7 @@
         self.assertTrue(sock.connect.called)
 
     def test__sock_connect_canceled_fut(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
 
         f = asyncio.Future(loop=self.loop)
         f.cancel()
@@ -316,24 +315,24 @@
         self.assertFalse(sock.connect.called)
 
     def test__sock_connect_unregister(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
 
         f = asyncio.Future(loop=self.loop)
         f.cancel()
 
-        self.loop.remove_writer = unittest.mock.Mock()
+        self.loop.remove_writer = mock.Mock()
         self.loop._sock_connect(f, True, sock, ('127.0.0.1', 8080))
         self.assertEqual((10,), self.loop.remove_writer.call_args[0])
 
     def test__sock_connect_tryagain(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         sock.getsockopt.return_value = errno.EAGAIN
 
-        self.loop.add_writer = unittest.mock.Mock()
-        self.loop.remove_writer = unittest.mock.Mock()
+        self.loop.add_writer = mock.Mock()
+        self.loop.remove_writer = mock.Mock()
 
         self.loop._sock_connect(f, True, sock, ('127.0.0.1', 8080))
         self.assertEqual(
@@ -343,17 +342,17 @@
 
     def test__sock_connect_exception(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         sock.getsockopt.return_value = errno.ENOTCONN
 
-        self.loop.remove_writer = unittest.mock.Mock()
+        self.loop.remove_writer = mock.Mock()
         self.loop._sock_connect(f, True, sock, ('127.0.0.1', 8080))
         self.assertIsInstance(f.exception(), OSError)
 
     def test_sock_accept(self):
-        sock = unittest.mock.Mock()
-        self.loop._sock_accept = unittest.mock.Mock()
+        sock = mock.Mock()
+        self.loop._sock_accept = mock.Mock()
 
         f = self.loop.sock_accept(sock)
         self.assertIsInstance(f, asyncio.Future)
@@ -363,9 +362,9 @@
     def test__sock_accept(self):
         f = asyncio.Future(loop=self.loop)
 
-        conn = unittest.mock.Mock()
+        conn = mock.Mock()
 
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         sock.accept.return_value = conn, ('127.0.0.1', 1000)
 
@@ -375,7 +374,7 @@
         self.assertEqual((False,), conn.setblocking.call_args[0])
 
     def test__sock_accept_canceled_fut(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
 
         f = asyncio.Future(loop=self.loop)
         f.cancel()
@@ -384,23 +383,23 @@
         self.assertFalse(sock.accept.called)
 
     def test__sock_accept_unregister(self):
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
 
         f = asyncio.Future(loop=self.loop)
         f.cancel()
 
-        self.loop.remove_reader = unittest.mock.Mock()
+        self.loop.remove_reader = mock.Mock()
         self.loop._sock_accept(f, True, sock)
         self.assertEqual((10,), self.loop.remove_reader.call_args[0])
 
     def test__sock_accept_tryagain(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         sock.accept.side_effect = BlockingIOError
 
-        self.loop.add_reader = unittest.mock.Mock()
+        self.loop.add_reader = mock.Mock()
         self.loop._sock_accept(f, False, sock)
         self.assertEqual(
             (10, self.loop._sock_accept, f, True, sock),
@@ -408,7 +407,7 @@
 
     def test__sock_accept_exception(self):
         f = asyncio.Future(loop=self.loop)
-        sock = unittest.mock.Mock()
+        sock = mock.Mock()
         sock.fileno.return_value = 10
         err = sock.accept.side_effect = OSError()
 
@@ -428,8 +427,8 @@
         self.assertIsNone(w)
 
     def test_add_reader_existing(self):
-        reader = unittest.mock.Mock()
-        writer = unittest.mock.Mock()
+        reader = mock.Mock()
+        writer = mock.Mock()
         self.loop._selector.get_key.return_value = selectors.SelectorKey(
             1, 1, selectors.EVENT_WRITE, (reader, writer))
         cb = lambda: True
@@ -445,7 +444,7 @@
         self.assertEqual(writer, w)
 
     def test_add_reader_existing_writer(self):
-        writer = unittest.mock.Mock()
+        writer = mock.Mock()
         self.loop._selector.get_key.return_value = selectors.SelectorKey(
             1, 1, selectors.EVENT_WRITE, (None, writer))
         cb = lambda: True
@@ -467,8 +466,8 @@
         self.assertTrue(self.loop._selector.unregister.called)
 
     def test_remove_reader_read_write(self):
-        reader = unittest.mock.Mock()
-        writer = unittest.mock.Mock()
+        reader = mock.Mock()
+        writer = mock.Mock()
         self.loop._selector.get_key.return_value = selectors.SelectorKey(
             1, 1, selectors.EVENT_READ | selectors.EVENT_WRITE,
             (reader, writer))
@@ -498,8 +497,8 @@
         self.assertEqual(cb, w._callback)
 
     def test_add_writer_existing(self):
-        reader = unittest.mock.Mock()
-        writer = unittest.mock.Mock()
+        reader = mock.Mock()
+        writer = mock.Mock()
         self.loop._selector.get_key.return_value = selectors.SelectorKey(
             1, 1, selectors.EVENT_READ, (reader, writer))
         cb = lambda: True
@@ -522,8 +521,8 @@
         self.assertTrue(self.loop._selector.unregister.called)
 
     def test_remove_writer_read_write(self):
-        reader = unittest.mock.Mock()
-        writer = unittest.mock.Mock()
+        reader = mock.Mock()
+        writer = mock.Mock()
         self.loop._selector.get_key.return_value = selectors.SelectorKey(
             1, 1, selectors.EVENT_READ | selectors.EVENT_WRITE,
             (reader, writer))
@@ -541,10 +540,10 @@
             self.loop.remove_writer(1))
 
     def test_process_events_read(self):
-        reader = unittest.mock.Mock()
+        reader = mock.Mock()
         reader._cancelled = False
 
-        self.loop._add_callback = unittest.mock.Mock()
+        self.loop._add_callback = mock.Mock()
         self.loop._process_events(
             [(selectors.SelectorKey(
                 1, 1, selectors.EVENT_READ, (reader, None)),
@@ -553,10 +552,10 @@
         self.loop._add_callback.assert_called_with(reader)
 
     def test_process_events_read_cancelled(self):
-        reader = unittest.mock.Mock()
+        reader = mock.Mock()
         reader.cancelled = True
 
-        self.loop.remove_reader = unittest.mock.Mock()
+        self.loop.remove_reader = mock.Mock()
         self.loop._process_events(
             [(selectors.SelectorKey(
                 1, 1, selectors.EVENT_READ, (reader, None)),
@@ -564,10 +563,10 @@
         self.loop.remove_reader.assert_called_with(1)
 
     def test_process_events_write(self):
-        writer = unittest.mock.Mock()
+        writer = mock.Mock()
         writer._cancelled = False
 
-        self.loop._add_callback = unittest.mock.Mock()
+        self.loop._add_callback = mock.Mock()
         self.loop._process_events(
             [(selectors.SelectorKey(1, 1, selectors.EVENT_WRITE,
                                     (None, writer)),
@@ -575,9 +574,9 @@
         self.loop._add_callback.assert_called_with(writer)
 
     def test_process_events_write_cancelled(self):
-        writer = unittest.mock.Mock()
+        writer = mock.Mock()
         writer.cancelled = True
-        self.loop.remove_writer = unittest.mock.Mock()
+        self.loop.remove_writer = mock.Mock()
 
         self.loop._process_events(
             [(selectors.SelectorKey(1, 1, selectors.EVENT_WRITE,
@@ -591,7 +590,7 @@
     def setUp(self):
         self.loop = test_utils.TestLoop()
         self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
-        self.sock = unittest.mock.Mock(socket.socket)
+        self.sock = mock.Mock(socket.socket)
         self.sock.fileno.return_value = 7
 
     def test_ctor(self):
@@ -602,7 +601,7 @@
 
     def test_abort(self):
         tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
-        tr._force_close = unittest.mock.Mock()
+        tr._force_close = mock.Mock()
 
         tr.abort()
         tr._force_close.assert_called_with(None)
@@ -632,8 +631,8 @@
     def test_force_close(self):
         tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
         tr._buffer.extend(b'1')
-        self.loop.add_reader(7, unittest.mock.sentinel)
-        self.loop.add_writer(7, unittest.mock.sentinel)
+        self.loop.add_reader(7, mock.sentinel)
+        self.loop.add_writer(7, mock.sentinel)
         tr._force_close(None)
 
         self.assertTrue(tr._closing)
@@ -646,11 +645,11 @@
         self.assertFalse(self.loop.readers)
         self.assertEqual(1, self.loop.remove_reader_count[7])
 
-    @unittest.mock.patch('asyncio.log.logger.error')
+    @mock.patch('asyncio.log.logger.error')
     def test_fatal_error(self, m_exc):
         exc = OSError()
         tr = _SelectorTransport(self.loop, self.sock, self.protocol, None)
-        tr._force_close = unittest.mock.Mock()
+        tr._force_close = mock.Mock()
         tr._fatal_error(exc)
 
         m_exc.assert_called_with(
@@ -682,7 +681,7 @@
     def setUp(self):
         self.loop = test_utils.TestLoop()
         self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
-        self.sock = unittest.mock.Mock(socket.socket)
+        self.sock = mock.Mock(socket.socket)
         self.sock_fd = self.sock.fileno.return_value = 7
 
     def test_ctor(self):
@@ -724,7 +723,7 @@
     def test_read_ready_eof(self):
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
-        transport.close = unittest.mock.Mock()
+        transport.close = mock.Mock()
 
         self.sock.recv.return_value = b''
         transport._read_ready()
@@ -735,7 +734,7 @@
     def test_read_ready_eof_keep_open(self):
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
-        transport.close = unittest.mock.Mock()
+        transport.close = mock.Mock()
 
         self.sock.recv.return_value = b''
         self.protocol.eof_received.return_value = True
@@ -744,45 +743,45 @@
         self.protocol.eof_received.assert_called_with()
         self.assertFalse(transport.close.called)
 
-    @unittest.mock.patch('logging.exception')
+    @mock.patch('logging.exception')
     def test_read_ready_tryagain(self, m_exc):
         self.sock.recv.side_effect = BlockingIOError
 
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._read_ready()
 
         self.assertFalse(transport._fatal_error.called)
 
-    @unittest.mock.patch('logging.exception')
+    @mock.patch('logging.exception')
     def test_read_ready_tryagain_interrupted(self, m_exc):
         self.sock.recv.side_effect = InterruptedError
 
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._read_ready()
 
         self.assertFalse(transport._fatal_error.called)
 
-    @unittest.mock.patch('logging.exception')
+    @mock.patch('logging.exception')
     def test_read_ready_conn_reset(self, m_exc):
         err = self.sock.recv.side_effect = ConnectionResetError()
 
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
-        transport._force_close = unittest.mock.Mock()
+        transport._force_close = mock.Mock()
         transport._read_ready()
         transport._force_close.assert_called_with(err)
 
-    @unittest.mock.patch('logging.exception')
+    @mock.patch('logging.exception')
     def test_read_ready_err(self, m_exc):
         err = self.sock.recv.side_effect = OSError()
 
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._read_ready()
 
         transport._fatal_error.assert_called_with(
@@ -891,14 +890,14 @@
         self.loop.assert_writer(7, transport._write_ready)
         self.assertEqual(list_to_buffer([b'data']), transport._buffer)
 
-    @unittest.mock.patch('asyncio.selector_events.logger')
+    @mock.patch('asyncio.selector_events.logger')
     def test_write_exception(self, m_log):
         err = self.sock.send.side_effect = OSError()
 
         data = b'data'
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport.write(data)
         transport._fatal_error.assert_called_with(
                                    err,
@@ -1002,17 +1001,17 @@
 
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._buffer.extend(b'data')
         transport._write_ready()
         transport._fatal_error.assert_called_with(
                                    err,
                                    'Fatal write error on socket transport')
 
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.base_events.logger')
     def test_write_ready_exception_and_close(self, m_log):
         self.sock.send.side_effect = OSError()
-        remove_writer = self.loop.remove_writer = unittest.mock.Mock()
+        remove_writer = self.loop.remove_writer = mock.Mock()
 
         transport = _SelectorSocketTransport(
             self.loop, self.sock, self.protocol)
@@ -1053,11 +1052,11 @@
     def setUp(self):
         self.loop = test_utils.TestLoop()
         self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
-        self.sock = unittest.mock.Mock(socket.socket)
+        self.sock = mock.Mock(socket.socket)
         self.sock.fileno.return_value = 7
-        self.sslsock = unittest.mock.Mock()
+        self.sslsock = mock.Mock()
         self.sslsock.fileno.return_value = 1
-        self.sslcontext = unittest.mock.Mock()
+        self.sslcontext = mock.Mock()
         self.sslcontext.wrap_socket.return_value = self.sslsock
 
     def _make_one(self, create_waiter=None):
@@ -1162,7 +1161,7 @@
         transport.write(b'data')
         self.assertEqual(transport._conn_lost, 2)
 
-    @unittest.mock.patch('asyncio.selector_events.logger')
+    @mock.patch('asyncio.selector_events.logger')
     def test_write_exception(self, m_log):
         transport = self._make_one()
         transport._conn_lost = 1
@@ -1182,11 +1181,11 @@
         self.assertEqual((b'data',), self.protocol.data_received.call_args[0])
 
     def test_read_ready_write_wants_read(self):
-        self.loop.add_writer = unittest.mock.Mock()
+        self.loop.add_writer = mock.Mock()
         self.sslsock.recv.side_effect = BlockingIOError
         transport = self._make_one()
         transport._write_wants_read = True
-        transport._write_ready = unittest.mock.Mock()
+        transport._write_ready = mock.Mock()
         transport._buffer.extend(b'data')
         transport._read_ready()
 
@@ -1198,7 +1197,7 @@
     def test_read_ready_recv_eof(self):
         self.sslsock.recv.return_value = b''
         transport = self._make_one()
-        transport.close = unittest.mock.Mock()
+        transport.close = mock.Mock()
         transport._read_ready()
         transport.close.assert_called_with()
         self.protocol.eof_received.assert_called_with()
@@ -1206,7 +1205,7 @@
     def test_read_ready_recv_conn_reset(self):
         err = self.sslsock.recv.side_effect = ConnectionResetError()
         transport = self._make_one()
-        transport._force_close = unittest.mock.Mock()
+        transport._force_close = mock.Mock()
         transport._read_ready()
         transport._force_close.assert_called_with(err)
 
@@ -1226,8 +1225,8 @@
         self.assertFalse(self.protocol.data_received.called)
 
     def test_read_ready_recv_write(self):
-        self.loop.remove_reader = unittest.mock.Mock()
-        self.loop.add_writer = unittest.mock.Mock()
+        self.loop.remove_reader = mock.Mock()
+        self.loop.add_writer = mock.Mock()
         self.sslsock.recv.side_effect = ssl.SSLWantWriteError
         transport = self._make_one()
         transport._read_ready()
@@ -1241,7 +1240,7 @@
     def test_read_ready_recv_exc(self):
         err = self.sslsock.recv.side_effect = OSError()
         transport = self._make_one()
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._read_ready()
         transport._fatal_error.assert_called_with(
                                    err,
@@ -1313,7 +1312,7 @@
         transport = self._make_one()
         transport._buffer = list_to_buffer([b'data'])
 
-        self.loop.remove_writer = unittest.mock.Mock()
+        self.loop.remove_writer = mock.Mock()
         self.sslsock.send.side_effect = ssl.SSLWantReadError
         transport._write_ready()
         self.assertFalse(self.protocol.data_received.called)
@@ -1325,7 +1324,7 @@
 
         transport = self._make_one()
         transport._buffer = list_to_buffer([b'data'])
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._write_ready()
         transport._fatal_error.assert_called_with(
                                    err,
@@ -1333,11 +1332,11 @@
         self.assertEqual(list_to_buffer(), transport._buffer)
 
     def test_write_ready_read_wants_write(self):
-        self.loop.add_reader = unittest.mock.Mock()
+        self.loop.add_reader = mock.Mock()
         self.sslsock.send.side_effect = BlockingIOError
         transport = self._make_one()
         transport._read_wants_write = True
-        transport._read_ready = unittest.mock.Mock()
+        transport._read_ready = mock.Mock()
         transport._write_ready()
 
         self.assertFalse(transport._read_wants_write)
@@ -1374,11 +1373,11 @@
 
 class SelectorSslWithoutSslTransportTests(unittest.TestCase):
 
-    @unittest.mock.patch('asyncio.selector_events.ssl', None)
+    @mock.patch('asyncio.selector_events.ssl', None)
     def test_ssl_transport_requires_ssl_module(self):
-        Mock = unittest.mock.Mock
+        Mock = mock.Mock
         with self.assertRaises(RuntimeError):
-            transport = _SelectorSslTransport(Mock(), Mock(), Mock(), Mock())
+            _SelectorSslTransport(Mock(), Mock(), Mock(), Mock())
 
 
 class SelectorDatagramTransportTests(unittest.TestCase):
@@ -1386,7 +1385,7 @@
     def setUp(self):
         self.loop = test_utils.TestLoop()
         self.protocol = test_utils.make_test_protocol(asyncio.DatagramProtocol)
-        self.sock = unittest.mock.Mock(spec_set=socket.socket)
+        self.sock = mock.Mock(spec_set=socket.socket)
         self.sock.fileno.return_value = 7
 
     def test_read_ready(self):
@@ -1404,7 +1403,7 @@
             self.loop, self.sock, self.protocol)
 
         self.sock.recvfrom.side_effect = BlockingIOError
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._read_ready()
 
         self.assertFalse(transport._fatal_error.called)
@@ -1414,7 +1413,7 @@
             self.loop, self.sock, self.protocol)
 
         err = self.sock.recvfrom.side_effect = RuntimeError()
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._read_ready()
 
         transport._fatal_error.assert_called_with(
@@ -1426,7 +1425,7 @@
             self.loop, self.sock, self.protocol)
 
         err = self.sock.recvfrom.side_effect = OSError()
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._read_ready()
 
         self.assertFalse(transport._fatal_error.called)
@@ -1518,14 +1517,14 @@
         self.assertEqual(
             [(b'data', ('0.0.0.0', 12345))], list(transport._buffer))
 
-    @unittest.mock.patch('asyncio.selector_events.logger')
+    @mock.patch('asyncio.selector_events.logger')
     def test_sendto_exception(self, m_log):
         data = b'data'
         err = self.sock.sendto.side_effect = RuntimeError()
 
         transport = _SelectorDatagramTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport.sendto(data, ())
 
         self.assertTrue(transport._fatal_error.called)
@@ -1549,7 +1548,7 @@
 
         transport = _SelectorDatagramTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport.sendto(data, ())
 
         self.assertEqual(transport._conn_lost, 0)
@@ -1562,7 +1561,7 @@
 
         transport = _SelectorDatagramTransport(
             self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport.sendto(data)
 
         self.assertFalse(transport._fatal_error.called)
@@ -1643,7 +1642,7 @@
 
         transport = _SelectorDatagramTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._buffer.append((b'data', ()))
         transport._sendto_ready()
 
@@ -1656,7 +1655,7 @@
 
         transport = _SelectorDatagramTransport(
             self.loop, self.sock, self.protocol)
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._buffer.append((b'data', ()))
         transport._sendto_ready()
 
@@ -1667,14 +1666,14 @@
 
         transport = _SelectorDatagramTransport(
             self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
-        transport._fatal_error = unittest.mock.Mock()
+        transport._fatal_error = mock.Mock()
         transport._buffer.append((b'data', ()))
         transport._sendto_ready()
 
         self.assertFalse(transport._fatal_error.called)
         self.assertTrue(self.protocol.error_received.called)
 
-    @unittest.mock.patch('asyncio.base_events.logger.error')
+    @mock.patch('asyncio.base_events.logger.error')
     def test_fatal_error_connected(self, m_exc):
         transport = _SelectorDatagramTransport(
             self.loop, self.sock, self.protocol, ('0.0.0.0', 1))
diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py
index ca792f2..031499e 100644
--- a/Lib/test/test_asyncio/test_streams.py
+++ b/Lib/test/test_asyncio/test_streams.py
@@ -1,10 +1,9 @@
 """Tests for streams.py."""
 
-import functools
 import gc
 import socket
 import unittest
-import unittest.mock
+from unittest import mock
 try:
     import ssl
 except ImportError:
@@ -29,7 +28,7 @@
         self.loop.close()
         gc.collect()
 
-    @unittest.mock.patch('asyncio.streams.events')
+    @mock.patch('asyncio.streams.events')
     def test_ctor_global_loop(self, m_events):
         stream = asyncio.StreamReader()
         self.assertIs(stream._loop, m_events.get_event_loop.return_value)
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index 6d03dc7..ced3431 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -830,7 +830,7 @@
                 v = yield from f
                 self.assertEqual(v, 'a')
 
-        res = loop.run_until_complete(asyncio.Task(foo(), loop=loop))
+        loop.run_until_complete(asyncio.Task(foo(), loop=loop))
 
     def test_as_completed_reverse_wait(self):
 
@@ -964,13 +964,9 @@
         loop = test_utils.TestLoop(gen)
         self.addCleanup(loop.close)
 
-        sleepfut = None
-
         @asyncio.coroutine
         def sleep(dt):
-            nonlocal sleepfut
-            sleepfut = asyncio.sleep(dt, loop=loop)
-            yield from sleepfut
+            yield from asyncio.sleep(dt, loop=loop)
 
         @asyncio.coroutine
         def doit():
diff --git a/Lib/test/test_asyncio/test_transports.py b/Lib/test/test_asyncio/test_transports.py
index 4c64526..cfbdf3e 100644
--- a/Lib/test/test_asyncio/test_transports.py
+++ b/Lib/test/test_asyncio/test_transports.py
@@ -1,7 +1,7 @@
 """Tests for transports.py."""
 
 import unittest
-import unittest.mock
+from unittest import mock
 
 import asyncio
 from asyncio import transports
@@ -23,7 +23,7 @@
 
     def test_writelines(self):
         transport = asyncio.Transport()
-        transport.write = unittest.mock.Mock()
+        transport.write = mock.Mock()
 
         transport.writelines([b'line1',
                               bytearray(b'line2'),
@@ -70,7 +70,7 @@
                 return 512
 
         transport = MyTransport()
-        transport._protocol = unittest.mock.Mock()
+        transport._protocol = mock.Mock()
 
         self.assertFalse(transport._protocol_paused)
 
diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py
index 9e489c2..cc74383 100644
--- a/Lib/test/test_asyncio/test_unix_events.py
+++ b/Lib/test/test_asyncio/test_unix_events.py
@@ -13,7 +13,7 @@
 import tempfile
 import threading
 import unittest
-import unittest.mock
+from unittest import mock
 
 if sys.platform == 'win32':
     raise unittest.SkipTest('UNIX only')
@@ -25,7 +25,7 @@
 from asyncio import unix_events
 
 
-MOCK_ANY = unittest.mock.ANY
+MOCK_ANY = mock.ANY
 
 
 @unittest.skipUnless(signal, 'Signals are not supported')
@@ -48,15 +48,15 @@
         self.loop._handle_signal(signal.NSIG + 1, ())
 
     def test_handle_signal_cancelled_handler(self):
-        h = asyncio.Handle(unittest.mock.Mock(), (),
-                           loop=unittest.mock.Mock())
+        h = asyncio.Handle(mock.Mock(), (),
+                           loop=mock.Mock())
         h.cancel()
         self.loop._signal_handlers[signal.NSIG + 1] = h
-        self.loop.remove_signal_handler = unittest.mock.Mock()
+        self.loop.remove_signal_handler = mock.Mock()
         self.loop._handle_signal(signal.NSIG + 1, ())
         self.loop.remove_signal_handler.assert_called_with(signal.NSIG + 1)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.unix_events.signal')
     def test_add_signal_handler_setup_error(self, m_signal):
         m_signal.NSIG = signal.NSIG
         m_signal.set_wakeup_fd.side_effect = ValueError
@@ -66,7 +66,7 @@
             self.loop.add_signal_handler,
             signal.SIGINT, lambda: True)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.unix_events.signal')
     def test_add_signal_handler(self, m_signal):
         m_signal.NSIG = signal.NSIG
 
@@ -76,7 +76,7 @@
         self.assertIsInstance(h, asyncio.Handle)
         self.assertEqual(h._callback, cb)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.unix_events.signal')
     def test_add_signal_handler_install_error(self, m_signal):
         m_signal.NSIG = signal.NSIG
 
@@ -94,8 +94,8 @@
             self.loop.add_signal_handler,
             signal.SIGINT, lambda: True)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.base_events.logger')
     def test_add_signal_handler_install_error2(self, m_logging, m_signal):
         m_signal.NSIG = signal.NSIG
 
@@ -111,8 +111,8 @@
         self.assertFalse(m_logging.info.called)
         self.assertEqual(1, m_signal.set_wakeup_fd.call_count)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.base_events.logger')
     def test_add_signal_handler_install_error3(self, m_logging, m_signal):
         class Err(OSError):
             errno = errno.EINVAL
@@ -126,7 +126,7 @@
         self.assertFalse(m_logging.info.called)
         self.assertEqual(2, m_signal.set_wakeup_fd.call_count)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.unix_events.signal')
     def test_remove_signal_handler(self, m_signal):
         m_signal.NSIG = signal.NSIG
 
@@ -139,7 +139,7 @@
         self.assertEqual(
             (signal.SIGHUP, m_signal.SIG_DFL), m_signal.signal.call_args[0])
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.unix_events.signal')
     def test_remove_signal_handler_2(self, m_signal):
         m_signal.NSIG = signal.NSIG
         m_signal.SIGINT = signal.SIGINT
@@ -156,8 +156,8 @@
             (signal.SIGINT, m_signal.default_int_handler),
             m_signal.signal.call_args[0])
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
-    @unittest.mock.patch('asyncio.base_events.logger')
+    @mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.base_events.logger')
     def test_remove_signal_handler_cleanup_error(self, m_logging, m_signal):
         m_signal.NSIG = signal.NSIG
         self.loop.add_signal_handler(signal.SIGHUP, lambda: True)
@@ -167,7 +167,7 @@
         self.loop.remove_signal_handler(signal.SIGHUP)
         self.assertTrue(m_logging.info)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.unix_events.signal')
     def test_remove_signal_handler_error(self, m_signal):
         m_signal.NSIG = signal.NSIG
         self.loop.add_signal_handler(signal.SIGHUP, lambda: True)
@@ -177,7 +177,7 @@
         self.assertRaises(
             OSError, self.loop.remove_signal_handler, signal.SIGHUP)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.unix_events.signal')
     def test_remove_signal_handler_error2(self, m_signal):
         m_signal.NSIG = signal.NSIG
         self.loop.add_signal_handler(signal.SIGHUP, lambda: True)
@@ -189,7 +189,7 @@
         self.assertRaises(
             RuntimeError, self.loop.remove_signal_handler, signal.SIGHUP)
 
-    @unittest.mock.patch('asyncio.unix_events.signal')
+    @mock.patch('asyncio.unix_events.signal')
     def test_close(self, m_signal):
         m_signal.NSIG = signal.NSIG
 
@@ -291,16 +291,16 @@
     def setUp(self):
         self.loop = test_utils.TestLoop()
         self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
-        self.pipe = unittest.mock.Mock(spec_set=io.RawIOBase)
+        self.pipe = mock.Mock(spec_set=io.RawIOBase)
         self.pipe.fileno.return_value = 5
 
-        fcntl_patcher = unittest.mock.patch('fcntl.fcntl')
+        fcntl_patcher = mock.patch('fcntl.fcntl')
         fcntl_patcher.start()
         self.addCleanup(fcntl_patcher.stop)
 
-        fstat_patcher = unittest.mock.patch('os.fstat')
+        fstat_patcher = mock.patch('os.fstat')
         m_fstat = fstat_patcher.start()
-        st = unittest.mock.Mock()
+        st = mock.Mock()
         st.st_mode = stat.S_IFIFO
         m_fstat.return_value = st
         self.addCleanup(fstat_patcher.stop)
@@ -319,7 +319,7 @@
         test_utils.run_briefly(self.loop)
         self.assertIsNone(fut.result())
 
-    @unittest.mock.patch('os.read')
+    @mock.patch('os.read')
     def test__read_ready(self, m_read):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -329,7 +329,7 @@
         m_read.assert_called_with(5, tr.max_size)
         self.protocol.data_received.assert_called_with(b'data')
 
-    @unittest.mock.patch('os.read')
+    @mock.patch('os.read')
     def test__read_ready_eof(self, m_read):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -342,7 +342,7 @@
         self.protocol.eof_received.assert_called_with()
         self.protocol.connection_lost.assert_called_with(None)
 
-    @unittest.mock.patch('os.read')
+    @mock.patch('os.read')
     def test__read_ready_blocked(self, m_read):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -353,14 +353,14 @@
         test_utils.run_briefly(self.loop)
         self.assertFalse(self.protocol.data_received.called)
 
-    @unittest.mock.patch('asyncio.log.logger.error')
-    @unittest.mock.patch('os.read')
+    @mock.patch('asyncio.log.logger.error')
+    @mock.patch('os.read')
     def test__read_ready_error(self, m_read, m_logexc):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
         err = OSError()
         m_read.side_effect = err
-        tr._close = unittest.mock.Mock()
+        tr._close = mock.Mock()
         tr._read_ready()
 
         m_read.assert_called_with(5, tr.max_size)
@@ -371,17 +371,17 @@
                 '\nprotocol:.*\ntransport:.*'),
             exc_info=(OSError, MOCK_ANY, MOCK_ANY))
 
-    @unittest.mock.patch('os.read')
+    @mock.patch('os.read')
     def test_pause_reading(self, m_read):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
 
-        m = unittest.mock.Mock()
+        m = mock.Mock()
         self.loop.add_reader(5, m)
         tr.pause_reading()
         self.assertFalse(self.loop.readers)
 
-    @unittest.mock.patch('os.read')
+    @mock.patch('os.read')
     def test_resume_reading(self, m_read):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -389,26 +389,26 @@
         tr.resume_reading()
         self.loop.assert_reader(5, tr._read_ready)
 
-    @unittest.mock.patch('os.read')
+    @mock.patch('os.read')
     def test_close(self, m_read):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
 
-        tr._close = unittest.mock.Mock()
+        tr._close = mock.Mock()
         tr.close()
         tr._close.assert_called_with(None)
 
-    @unittest.mock.patch('os.read')
+    @mock.patch('os.read')
     def test_close_already_closing(self, m_read):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
 
         tr._closing = True
-        tr._close = unittest.mock.Mock()
+        tr._close = mock.Mock()
         tr.close()
         self.assertFalse(tr._close.called)
 
-    @unittest.mock.patch('os.read')
+    @mock.patch('os.read')
     def test__close(self, m_read):
         tr = unix_events._UnixReadPipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -459,16 +459,16 @@
     def setUp(self):
         self.loop = test_utils.TestLoop()
         self.protocol = test_utils.make_test_protocol(asyncio.BaseProtocol)
-        self.pipe = unittest.mock.Mock(spec_set=io.RawIOBase)
+        self.pipe = mock.Mock(spec_set=io.RawIOBase)
         self.pipe.fileno.return_value = 5
 
-        fcntl_patcher = unittest.mock.patch('fcntl.fcntl')
+        fcntl_patcher = mock.patch('fcntl.fcntl')
         fcntl_patcher.start()
         self.addCleanup(fcntl_patcher.stop)
 
-        fstat_patcher = unittest.mock.patch('os.fstat')
+        fstat_patcher = mock.patch('os.fstat')
         m_fstat = fstat_patcher.start()
-        st = unittest.mock.Mock()
+        st = mock.Mock()
         st.st_mode = stat.S_IFSOCK
         m_fstat.return_value = st
         self.addCleanup(fstat_patcher.stop)
@@ -493,7 +493,7 @@
             self.loop, self.pipe, self.protocol)
         self.assertTrue(tr.can_write_eof())
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test_write(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -504,7 +504,7 @@
         self.assertFalse(self.loop.writers)
         self.assertEqual([], tr._buffer)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test_write_no_data(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -514,7 +514,7 @@
         self.assertFalse(self.loop.writers)
         self.assertEqual([], tr._buffer)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test_write_partial(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -525,7 +525,7 @@
         self.loop.assert_writer(5, tr._write_ready)
         self.assertEqual([b'ta'], tr._buffer)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test_write_buffer(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -537,7 +537,7 @@
         self.loop.assert_writer(5, tr._write_ready)
         self.assertEqual([b'previous', b'data'], tr._buffer)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test_write_again(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -548,15 +548,15 @@
         self.loop.assert_writer(5, tr._write_ready)
         self.assertEqual([b'data'], tr._buffer)
 
-    @unittest.mock.patch('asyncio.unix_events.logger')
-    @unittest.mock.patch('os.write')
+    @mock.patch('asyncio.unix_events.logger')
+    @mock.patch('os.write')
     def test_write_err(self, m_write, m_log):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
 
         err = OSError()
         m_write.side_effect = err
-        tr._fatal_error = unittest.mock.Mock()
+        tr._fatal_error = mock.Mock()
         tr.write(b'data')
         m_write.assert_called_with(5, b'data')
         self.assertFalse(self.loop.writers)
@@ -576,7 +576,7 @@
         m_log.warning.assert_called_with(
             'pipe closed by peer or os.write(pipe, data) raised exception.')
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test_write_close(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -597,7 +597,7 @@
         test_utils.run_briefly(self.loop)
         self.protocol.connection_lost.assert_called_with(None)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test__write_ready(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -609,7 +609,7 @@
         self.assertFalse(self.loop.writers)
         self.assertEqual([], tr._buffer)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test__write_ready_partial(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -622,7 +622,7 @@
         self.loop.assert_writer(5, tr._write_ready)
         self.assertEqual([b'a'], tr._buffer)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test__write_ready_again(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -635,7 +635,7 @@
         self.loop.assert_writer(5, tr._write_ready)
         self.assertEqual([b'data'], tr._buffer)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test__write_ready_empty(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -648,8 +648,8 @@
         self.loop.assert_writer(5, tr._write_ready)
         self.assertEqual([b'data'], tr._buffer)
 
-    @unittest.mock.patch('asyncio.log.logger.error')
-    @unittest.mock.patch('os.write')
+    @mock.patch('asyncio.log.logger.error')
+    @mock.patch('os.write')
     def test__write_ready_err(self, m_write, m_logexc):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -672,7 +672,7 @@
         test_utils.run_briefly(self.loop)
         self.protocol.connection_lost.assert_called_with(err)
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test__write_ready_closing(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -689,7 +689,7 @@
         self.protocol.connection_lost.assert_called_with(None)
         self.pipe.close.assert_called_with()
 
-    @unittest.mock.patch('os.write')
+    @mock.patch('os.write')
     def test_abort(self, m_write):
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
@@ -742,7 +742,7 @@
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
 
-        tr.write_eof = unittest.mock.Mock()
+        tr.write_eof = mock.Mock()
         tr.close()
         tr.write_eof.assert_called_with()
 
@@ -750,7 +750,7 @@
         tr = unix_events._UnixWritePipeTransport(
             self.loop, self.pipe, self.protocol)
 
-        tr.write_eof = unittest.mock.Mock()
+        tr.write_eof = mock.Mock()
         tr._closing = True
         tr.close()
         self.assertFalse(tr.write_eof.called)
@@ -777,7 +777,7 @@
 class AbstractChildWatcherTests(unittest.TestCase):
 
     def test_not_implemented(self):
-        f = unittest.mock.Mock()
+        f = mock.Mock()
         watcher = asyncio.AbstractChildWatcher()
         self.assertRaises(
             NotImplementedError, watcher.add_child_handler, f, f)
@@ -796,7 +796,7 @@
 class BaseChildWatcherTests(unittest.TestCase):
 
     def test_not_implemented(self):
-        f = unittest.mock.Mock()
+        f = mock.Mock()
         watcher = unix_events.BaseChildWatcher()
         self.assertRaises(
             NotImplementedError, watcher._do_waitpid, f)
@@ -813,14 +813,14 @@
 
 class ChildWatcherTestsMixin:
 
-    ignore_warnings = unittest.mock.patch.object(log.logger, "warning")
+    ignore_warnings = mock.patch.object(log.logger, "warning")
 
     def setUp(self):
         self.loop = test_utils.TestLoop()
         self.running = False
         self.zombies = {}
 
-        with unittest.mock.patch.object(
+        with mock.patch.object(
                 self.loop, "add_signal_handler") as self.m_add_signal_handler:
             self.watcher = self.create_watcher()
             self.watcher.attach_loop(self.loop)
@@ -864,8 +864,8 @@
     def waitpid_mocks(func):
         def wrapped_func(self):
             def patch(target, wrapper):
-                return unittest.mock.patch(target, wraps=wrapper,
-                                           new_callable=unittest.mock.Mock)
+                return mock.patch(target, wraps=wrapper,
+                                  new_callable=mock.Mock)
 
             with patch('os.WTERMSIG', self.WTERMSIG) as m_WTERMSIG, \
                  patch('os.WEXITSTATUS', self.WEXITSTATUS) as m_WEXITSTATUS, \
@@ -881,7 +881,7 @@
     @waitpid_mocks
     def test_sigchld(self, m):
         # register a child
-        callback = unittest.mock.Mock()
+        callback = mock.Mock()
 
         with self.watcher:
             self.running = True
@@ -941,8 +941,8 @@
 
     @waitpid_mocks
     def test_sigchld_two_children(self, m):
-        callback1 = unittest.mock.Mock()
-        callback2 = unittest.mock.Mock()
+        callback1 = mock.Mock()
+        callback2 = mock.Mock()
 
         # register child 1
         with self.watcher:
@@ -1045,8 +1045,8 @@
 
     @waitpid_mocks
     def test_sigchld_two_children_terminating_together(self, m):
-        callback1 = unittest.mock.Mock()
-        callback2 = unittest.mock.Mock()
+        callback1 = mock.Mock()
+        callback2 = mock.Mock()
 
         # register child 1
         with self.watcher:
@@ -1115,7 +1115,7 @@
     @waitpid_mocks
     def test_sigchld_race_condition(self, m):
         # register a child
-        callback = unittest.mock.Mock()
+        callback = mock.Mock()
 
         with self.watcher:
             # child terminates before being registered
@@ -1136,8 +1136,8 @@
 
     @waitpid_mocks
     def test_sigchld_replace_handler(self, m):
-        callback1 = unittest.mock.Mock()
-        callback2 = unittest.mock.Mock()
+        callback1 = mock.Mock()
+        callback2 = mock.Mock()
 
         # register a child
         with self.watcher:
@@ -1189,7 +1189,7 @@
 
     @waitpid_mocks
     def test_sigchld_remove_handler(self, m):
-        callback = unittest.mock.Mock()
+        callback = mock.Mock()
 
         # register a child
         with self.watcher:
@@ -1221,7 +1221,7 @@
 
     @waitpid_mocks
     def test_sigchld_unknown_status(self, m):
-        callback = unittest.mock.Mock()
+        callback = mock.Mock()
 
         # register a child
         with self.watcher:
@@ -1258,9 +1258,9 @@
 
     @waitpid_mocks
     def test_remove_child_handler(self, m):
-        callback1 = unittest.mock.Mock()
-        callback2 = unittest.mock.Mock()
-        callback3 = unittest.mock.Mock()
+        callback1 = mock.Mock()
+        callback2 = mock.Mock()
+        callback3 = mock.Mock()
 
         # register children
         with self.watcher:
@@ -1291,7 +1291,7 @@
 
     @waitpid_mocks
     def test_sigchld_unhandled_exception(self, m):
-        callback = unittest.mock.Mock()
+        callback = mock.Mock()
 
         # register a child
         with self.watcher:
@@ -1301,8 +1301,8 @@
         # raise an exception
         m.waitpid.side_effect = ValueError
 
-        with unittest.mock.patch.object(log.logger,
-                                        'error') as m_error:
+        with mock.patch.object(log.logger,
+                               'error') as m_error:
 
             self.assertEqual(self.watcher._sig_chld(), None)
             self.assertTrue(m_error.called)
@@ -1310,7 +1310,7 @@
     @waitpid_mocks
     def test_sigchld_child_reaped_elsewhere(self, m):
         # register a child
-        callback = unittest.mock.Mock()
+        callback = mock.Mock()
 
         with self.watcher:
             self.running = True
@@ -1346,8 +1346,8 @@
     @waitpid_mocks
     def test_sigchld_unknown_pid_during_registration(self, m):
         # register two children
-        callback1 = unittest.mock.Mock()
-        callback2 = unittest.mock.Mock()
+        callback1 = mock.Mock()
+        callback2 = mock.Mock()
 
         with self.ignore_warnings, self.watcher:
             self.running = True
@@ -1367,7 +1367,7 @@
     @waitpid_mocks
     def test_set_loop(self, m):
         # register a child
-        callback = unittest.mock.Mock()
+        callback = mock.Mock()
 
         with self.watcher:
             self.running = True
@@ -1376,19 +1376,16 @@
         # attach a new loop
         old_loop = self.loop
         self.loop = test_utils.TestLoop()
+        patch = mock.patch.object
 
-        with unittest.mock.patch.object(
-                old_loop,
-                "remove_signal_handler") as m_old_remove_signal_handler, \
-             unittest.mock.patch.object(
-                self.loop,
-                "add_signal_handler") as m_new_add_signal_handler:
+        with patch(old_loop, "remove_signal_handler") as m_old_remove, \
+             patch(self.loop, "add_signal_handler") as m_new_add:
 
             self.watcher.attach_loop(self.loop)
 
-            m_old_remove_signal_handler.assert_called_once_with(
+            m_old_remove.assert_called_once_with(
                 signal.SIGCHLD)
-            m_new_add_signal_handler.assert_called_once_with(
+            m_new_add.assert_called_once_with(
                 signal.SIGCHLD, self.watcher._sig_chld)
 
         # child terminates
@@ -1401,9 +1398,9 @@
     @waitpid_mocks
     def test_set_loop_race_condition(self, m):
         # register 3 children
-        callback1 = unittest.mock.Mock()
-        callback2 = unittest.mock.Mock()
-        callback3 = unittest.mock.Mock()
+        callback1 = mock.Mock()
+        callback2 = mock.Mock()
+        callback3 = mock.Mock()
 
         with self.watcher:
             self.running = True
@@ -1415,7 +1412,7 @@
         old_loop = self.loop
         self.loop = None
 
-        with unittest.mock.patch.object(
+        with mock.patch.object(
                 old_loop, "remove_signal_handler") as m_remove_signal_handler:
 
             self.watcher.attach_loop(None)
@@ -1435,7 +1432,7 @@
         # attach a new loop
         self.loop = test_utils.TestLoop()
 
-        with unittest.mock.patch.object(
+        with mock.patch.object(
                 self.loop, "add_signal_handler") as m_add_signal_handler:
 
             self.watcher.attach_loop(self.loop)
@@ -1461,8 +1458,7 @@
     @waitpid_mocks
     def test_close(self, m):
         # register two children
-        callback1 = unittest.mock.Mock()
-        callback2 = unittest.mock.Mock()
+        callback1 = mock.Mock()
 
         with self.watcher:
             self.running = True
@@ -1479,7 +1475,7 @@
             if isinstance(self.watcher, asyncio.FastChildWatcher):
                 self.assertEqual(len(self.watcher._zombies), 1)
 
-            with unittest.mock.patch.object(
+            with mock.patch.object(
                     self.loop,
                     "remove_signal_handler") as m_remove_signal_handler:
 
diff --git a/Lib/test/test_asyncio/test_windows_events.py b/Lib/test/test_asyncio/test_windows_events.py
index 846049a..f652258 100644
--- a/Lib/test/test_asyncio/test_windows_events.py
+++ b/Lib/test/test_asyncio/test_windows_events.py
@@ -8,7 +8,6 @@
 import _winapi
 
 import asyncio
-from asyncio import test_utils
 from asyncio import _overlapped
 from asyncio import windows_events
 
@@ -50,7 +49,7 @@
         ADDRESS = r'\\.\pipe\test_double_bind-%s' % os.getpid()
         server1 = windows_events.PipeServer(ADDRESS)
         with self.assertRaises(PermissionError):
-            server2 = windows_events.PipeServer(ADDRESS)
+            windows_events.PipeServer(ADDRESS)
         server1.close()
 
     def test_pipe(self):
diff --git a/Lib/test/test_asyncio/test_windows_utils.py b/Lib/test/test_asyncio/test_windows_utils.py
index fa9d66c..9daf434 100644
--- a/Lib/test/test_asyncio/test_windows_utils.py
+++ b/Lib/test/test_asyncio/test_windows_utils.py
@@ -1,9 +1,11 @@
 """Tests for window_utils"""
 
+import socket
 import sys
 import test.support
 import unittest
-import unittest.mock
+from test.support import IPV6_ENABLED
+from unittest import mock
 
 if sys.platform != 'win32':
     raise unittest.SkipTest('Windows only')
@@ -16,23 +18,40 @@
 
 class WinsocketpairTests(unittest.TestCase):
 
-    def test_winsocketpair(self):
-        ssock, csock = windows_utils.socketpair()
-
+    def check_winsocketpair(self, ssock, csock):
         csock.send(b'xxx')
         self.assertEqual(b'xxx', ssock.recv(1024))
-
         csock.close()
         ssock.close()
 
-    @unittest.mock.patch('asyncio.windows_utils.socket')
+    def test_winsocketpair(self):
+        ssock, csock = windows_utils.socketpair()
+        self.check_winsocketpair(ssock, csock)
+
+    @unittest.skipUnless(IPV6_ENABLED, 'IPv6 not supported or enabled')
+    def test_winsocketpair_ipv6(self):
+        ssock, csock = windows_utils.socketpair(family=socket.AF_INET6)
+        self.check_winsocketpair(ssock, csock)
+
+    @mock.patch('asyncio.windows_utils.socket')
     def test_winsocketpair_exc(self, m_socket):
+        m_socket.AF_INET = socket.AF_INET
+        m_socket.SOCK_STREAM = socket.SOCK_STREAM
         m_socket.socket.return_value.getsockname.return_value = ('', 12345)
         m_socket.socket.return_value.accept.return_value = object(), object()
         m_socket.socket.return_value.connect.side_effect = OSError()
 
         self.assertRaises(OSError, windows_utils.socketpair)
 
+    def test_winsocketpair_invalid_args(self):
+        self.assertRaises(ValueError,
+                          windows_utils.socketpair, family=socket.AF_UNSPEC)
+        self.assertRaises(ValueError,
+                          windows_utils.socketpair, type=socket.SOCK_DGRAM)
+        self.assertRaises(ValueError,
+                          windows_utils.socketpair, proto=1)
+
+
 
 class PipeTests(unittest.TestCase):
 
diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py
index cde0bae..eb8d18c 100644
--- a/Lib/test/test_copy.py
+++ b/Lib/test/test_copy.py
@@ -98,6 +98,7 @@
             pass
         tests = [None, 42, 2**100, 3.14, True, False, 1j,
                  "hello", "hello\u1234", f.__code__,
+                 b"world", bytes(range(256)),
                  NewStyle, range(10), Classic, max, WithMetaclass]
         for x in tests:
             self.assertIs(copy.copy(x), x)
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index 26ed96c..2f89a10 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -124,6 +124,14 @@
         msg.set_payload([])
         self.assertEqual(msg.get_payload(), [])
 
+    def test_attach_when_payload_is_string(self):
+        msg = Message()
+        msg['Content-Type'] = 'multipart/mixed'
+        msg.set_payload('string payload')
+        sub_msg = MIMEMessage(Message())
+        self.assertRaisesRegex(TypeError, "[Aa]ttach.*non-multipart",
+                               msg.attach, sub_msg)
+
     def test_get_charsets(self):
         eq = self.assertEqual
 
diff --git a/Lib/test/test_email/test_policy.py b/Lib/test/test_email/test_policy.py
index 06ad5f2..e797f36 100644
--- a/Lib/test/test_email/test_policy.py
+++ b/Lib/test/test_email/test_policy.py
@@ -319,5 +319,14 @@
         self.assertEqual(msg.as_string(), "Subject: testXTo: fooXX")
 
 
+class TestConcretePolicies(unittest.TestCase):
+
+    def test_header_store_parse_rejects_newlines(self):
+        instance = email.policy.EmailPolicy()
+        self.assertRaises(ValueError,
+                          instance.header_store_parse,
+                          'From', 'spam\negg@foo.py')
+
+
 if __name__ == '__main__':
     unittest.main()
diff --git a/Lib/test/test_fileinput.py b/Lib/test/test_fileinput.py
index db6082c..eba55c9 100644
--- a/Lib/test/test_fileinput.py
+++ b/Lib/test/test_fileinput.py
@@ -260,6 +260,27 @@
             fi.readline()
         self.assertTrue(custom_open_hook.invoked, "openhook not invoked")
 
+    def test_readline(self):
+        with open(TESTFN, 'wb') as f:
+            f.write(b'A\nB\r\nC\r')
+            # Fill TextIOWrapper buffer.
+            f.write(b'123456789\n' * 1000)
+            # Issue #20501: readline() shouldn't read whole file.
+            f.write(b'\x80')
+        self.addCleanup(safe_unlink, TESTFN)
+
+        with FileInput(files=TESTFN,
+                       openhook=hook_encoded('ascii'), bufsize=8) as fi:
+            try:
+                self.assertEqual(fi.readline(), 'A\n')
+                self.assertEqual(fi.readline(), 'B\n')
+                self.assertEqual(fi.readline(), 'C\n')
+            except UnicodeDecodeError:
+                self.fail('Read to end of file')
+            with self.assertRaises(UnicodeDecodeError):
+                # Read to the end of file.
+                list(fi)
+
     def test_context_manager(self):
         try:
             t1 = writeTmp(1, ["A\nB\nC"])
@@ -837,6 +858,26 @@
         self.assertIs(kwargs.pop('encoding'), encoding)
         self.assertFalse(kwargs)
 
+    def test_modes(self):
+        with open(TESTFN, 'wb') as f:
+            # UTF-7 is a convenient, seldom used encoding
+            f.write(b'A\nB\r\nC\rD+IKw-')
+        self.addCleanup(safe_unlink, TESTFN)
+
+        def check(mode, expected_lines):
+            with FileInput(files=TESTFN, mode=mode,
+                           openhook=hook_encoded('utf-7')) as fi:
+                lines = list(fi)
+            self.assertEqual(lines, expected_lines)
+
+        check('r', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
+        with self.assertWarns(DeprecationWarning):
+            check('rU', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
+        with self.assertWarns(DeprecationWarning):
+            check('U', ['A\n', 'B\n', 'C\n', 'D\u20ac'])
+        with self.assertRaises(ValueError):
+            check('rb', ['A\n', 'B\r\n', 'C\r', 'D\u20ac'])
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py
index e967897..e59ed4d 100644
--- a/Lib/test/test_genericpath.py
+++ b/Lib/test/test_genericpath.py
@@ -329,7 +329,6 @@
             self.assertEqual(expandvars("$[foo]bar"), "$[foo]bar")
             self.assertEqual(expandvars("$bar bar"), "$bar bar")
             self.assertEqual(expandvars("$?bar"), "$?bar")
-            self.assertEqual(expandvars("${foo}bar"), "barbar")
             self.assertEqual(expandvars("$foo}bar"), "bar}bar")
             self.assertEqual(expandvars("${foo"), "${foo")
             self.assertEqual(expandvars("${{foo}}"), "baz1}")
@@ -342,13 +341,40 @@
             self.assertEqual(expandvars(b"$[foo]bar"), b"$[foo]bar")
             self.assertEqual(expandvars(b"$bar bar"), b"$bar bar")
             self.assertEqual(expandvars(b"$?bar"), b"$?bar")
-            self.assertEqual(expandvars(b"${foo}bar"), b"barbar")
             self.assertEqual(expandvars(b"$foo}bar"), b"bar}bar")
             self.assertEqual(expandvars(b"${foo"), b"${foo")
             self.assertEqual(expandvars(b"${{foo}}"), b"baz1}")
             self.assertEqual(expandvars(b"$foo$foo"), b"barbar")
             self.assertEqual(expandvars(b"$bar$bar"), b"$bar$bar")
 
+    @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
+    def test_expandvars_nonascii(self):
+        if self.pathmodule.__name__ == 'macpath':
+            self.skipTest('macpath.expandvars is a stub')
+        expandvars = self.pathmodule.expandvars
+        def check(value, expected):
+            self.assertEqual(expandvars(value), expected)
+        with support.EnvironmentVarGuard() as env:
+            env.clear()
+            nonascii = support.FS_NONASCII
+            env['spam'] = nonascii
+            env[nonascii] = 'ham' + nonascii
+            check(nonascii, nonascii)
+            check('$spam bar', '%s bar' % nonascii)
+            check('${spam}bar', '%sbar' % nonascii)
+            check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
+            check('$bar%s bar' % nonascii, '$bar%s bar' % nonascii)
+            check('$spam}bar', '%s}bar' % nonascii)
+
+            check(os.fsencode(nonascii), os.fsencode(nonascii))
+            check(b'$spam bar', os.fsencode('%s bar' % nonascii))
+            check(b'${spam}bar', os.fsencode('%sbar' % nonascii))
+            check(os.fsencode('${%s}bar' % nonascii),
+                  os.fsencode('ham%sbar' % nonascii))
+            check(os.fsencode('$bar%s bar' % nonascii),
+                  os.fsencode('$bar%s bar' % nonascii))
+            check(b'$spam}bar', os.fsencode('%s}bar' % nonascii))
+
     def test_abspath(self):
         self.assertIn("foo", self.pathmodule.abspath("foo"))
         with warnings.catch_warnings():
diff --git a/Lib/test/test_grammar.py b/Lib/test/test_grammar.py
index 9da6338..bba8820 100644
--- a/Lib/test/test_grammar.py
+++ b/Lib/test/test_grammar.py
@@ -319,8 +319,8 @@
             def f(self, *, __kw:1):
                 pass
         class Ham(Spam): pass
-        self.assertEquals(Spam.f.__annotations__, {'_Spam__kw': 1})
-        self.assertEquals(Ham.f.__annotations__, {'_Spam__kw': 1})
+        self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1})
+        self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1})
         # Check for SF Bug #1697248 - mixing decorators and a return annotation
         def null(x): return x
         @null
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index 5289407..b7a7e03 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -421,6 +421,13 @@
         with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
             self.assertEqual(f.read(), b'Test')
 
+    def test_prepend_error(self):
+        # See issue #20875
+        with gzip.open(self.filename, "wb") as f:
+            f.write(data1)
+        with gzip.open(self.filename, "rb") as f:
+            f.fileobj.prepend()
+
 class TestOpen(BaseTest):
     def test_binary_modes(self):
         uncompressed = data1 * 50
diff --git a/Lib/test/test_idle.py b/Lib/test/test_idle.py
index 819151e..7770ee5 100644
--- a/Lib/test/test_idle.py
+++ b/Lib/test/test_idle.py
@@ -14,6 +14,7 @@
     try:
         root = tk.Tk()
         root.destroy()
+        del root
     except tk.TclError:
         while 'gui' in use_resources:
             use_resources.remove('gui')
diff --git a/Lib/test/test_importlib/source/test_file_loader.py b/Lib/test/test_importlib/source/test_file_loader.py
index 25a3dae..2d415f9 100644
--- a/Lib/test/test_importlib/source/test_file_loader.py
+++ b/Lib/test/test_importlib/source/test_file_loader.py
@@ -190,6 +190,7 @@
             if os.path.exists(pycache):
                 shutil.rmtree(pycache)
 
+    @source_util.writes_bytecode_files
     def test_timestamp_overflow(self):
         # When a modification timestamp is larger than 2**32, it should be
         # truncated rather than raise an OverflowError.
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 4182da3..267537f 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -855,6 +855,16 @@
         bufio.__init__(rawio)
         self.assertEqual(b"abc", bufio.read())
 
+    def test_uninitialized(self):
+        bufio = self.tp.__new__(self.tp)
+        del bufio
+        bufio = self.tp.__new__(self.tp)
+        self.assertRaisesRegex((ValueError, AttributeError),
+                               'uninitialized|has no attribute',
+                               bufio.read, 0)
+        bufio.__init__(self.MockRawIO())
+        self.assertEqual(bufio.read(0), b'')
+
     def test_read(self):
         for arg in (None, 7):
             rawio = self.MockRawIO((b"abc", b"d", b"efg"))
@@ -1106,6 +1116,16 @@
         bufio.flush()
         self.assertEqual(b"".join(rawio._write_stack), b"abcghi")
 
+    def test_uninitialized(self):
+        bufio = self.tp.__new__(self.tp)
+        del bufio
+        bufio = self.tp.__new__(self.tp)
+        self.assertRaisesRegex((ValueError, AttributeError),
+                               'uninitialized|has no attribute',
+                               bufio.write, b'')
+        bufio.__init__(self.MockRawIO())
+        self.assertEqual(bufio.write(b''), 0)
+
     def test_detach_flush(self):
         raw = self.MockRawIO()
         buf = self.tp(raw)
@@ -1390,6 +1410,20 @@
         pair = self.tp(self.MockRawIO(), self.MockRawIO())
         self.assertFalse(pair.closed)
 
+    def test_uninitialized(self):
+        pair = self.tp.__new__(self.tp)
+        del pair
+        pair = self.tp.__new__(self.tp)
+        self.assertRaisesRegex((ValueError, AttributeError),
+                               'uninitialized|has no attribute',
+                               pair.read, 0)
+        self.assertRaisesRegex((ValueError, AttributeError),
+                               'uninitialized|has no attribute',
+                               pair.write, b'')
+        pair.__init__(self.MockRawIO(), self.MockRawIO())
+        self.assertEqual(pair.read(0), b'')
+        self.assertEqual(pair.write(b''), 0)
+
     def test_detach(self):
         pair = self.tp(self.MockRawIO(), self.MockRawIO())
         self.assertRaises(self.UnsupportedOperation, pair.detach)
@@ -1516,6 +1550,10 @@
         BufferedReaderTest.test_constructor(self)
         BufferedWriterTest.test_constructor(self)
 
+    def test_uninitialized(self):
+        BufferedReaderTest.test_uninitialized(self)
+        BufferedWriterTest.test_uninitialized(self)
+
     def test_read_and_write(self):
         raw = self.MockRawIO((b"asdf", b"ghjk"))
         rw = self.tp(raw, 8)
diff --git a/Lib/test/test_modulefinder.py b/Lib/test/test_modulefinder.py
index 53ea232..ed30d6f 100644
--- a/Lib/test/test_modulefinder.py
+++ b/Lib/test/test_modulefinder.py
@@ -1,5 +1,7 @@
 import os
 import errno
+import importlib.machinery
+import py_compile
 import shutil
 import unittest
 import tempfile
@@ -208,6 +210,14 @@
                                 from . import *
 """]
 
+bytecode_test = [
+    "a",
+    ["a"],
+    [],
+    [],
+    ""
+]
+
 
 def open_file(path):
     dirname = os.path.dirname(path)
@@ -288,6 +298,16 @@
     def test_relative_imports_4(self):
         self._do_test(relative_import_test_4)
 
+    def test_bytecode(self):
+        base_path = os.path.join(TEST_DIR, 'a')
+        source_path = base_path + importlib.machinery.SOURCE_SUFFIXES[0]
+        bytecode_path = base_path + importlib.machinery.BYTECODE_SUFFIXES[0]
+        with open_file(source_path) as file:
+            file.write('testing_modulefinder = True\n')
+        py_compile.compile(source_path, cfile=bytecode_path)
+        os.remove(source_path)
+        self._do_test(bytecode_test)
+
 
 def test_main():
     support.run_unittest(ModuleFinderTest)
diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py
index e789e22..000fc75 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -22,13 +22,15 @@
     fn = fn.replace('["', '[b"')
     fn = fn.replace(", '", ", b'")
     fn = fn.replace(', "', ', b"')
+    fn = os.fsencode(fn).decode('latin1')
+    fn = fn.encode('ascii', 'backslashreplace').decode('ascii')
     with warnings.catch_warnings():
         warnings.simplefilter("ignore", DeprecationWarning)
         gotResult = eval(fn)
     if isinstance(wantResult, str):
-        wantResult = wantResult.encode('ascii')
+        wantResult = os.fsencode(wantResult)
     elif isinstance(wantResult, tuple):
-        wantResult = tuple(r.encode('ascii') for r in wantResult)
+        wantResult = tuple(os.fsencode(r) for r in wantResult)
 
     gotResult = eval(fn)
     if wantResult != gotResult:
@@ -223,7 +225,6 @@
             tester('ntpath.expandvars("$[foo]bar")', "$[foo]bar")
             tester('ntpath.expandvars("$bar bar")', "$bar bar")
             tester('ntpath.expandvars("$?bar")', "$?bar")
-            tester('ntpath.expandvars("${foo}bar")', "barbar")
             tester('ntpath.expandvars("$foo}bar")', "bar}bar")
             tester('ntpath.expandvars("${foo")', "${foo")
             tester('ntpath.expandvars("${{foo}}")', "baz1}")
@@ -237,6 +238,26 @@
             tester('ntpath.expandvars("%foo%%bar")', "bar%bar")
             tester('ntpath.expandvars("\'%foo%\'%bar")', "\'%foo%\'%bar")
 
+    @unittest.skipUnless(support.FS_NONASCII, 'need support.FS_NONASCII')
+    def test_expandvars_nonascii(self):
+        def check(value, expected):
+            tester('ntpath.expandvars(%r)' % value, expected)
+        with support.EnvironmentVarGuard() as env:
+            env.clear()
+            nonascii = support.FS_NONASCII
+            env['spam'] = nonascii
+            env[nonascii] = 'ham' + nonascii
+            check('$spam bar', '%s bar' % nonascii)
+            check('$%s bar' % nonascii, '$%s bar' % nonascii)
+            check('${spam}bar', '%sbar' % nonascii)
+            check('${%s}bar' % nonascii, 'ham%sbar' % nonascii)
+            check('$spam}bar', '%s}bar' % nonascii)
+            check('$%s}bar' % nonascii, '$%s}bar' % nonascii)
+            check('%spam% bar', '%s bar' % nonascii)
+            check('%{}% bar'.format(nonascii), 'ham%s bar' % nonascii)
+            check('%spam%bar', '%sbar' % nonascii)
+            check('%{}%bar'.format(nonascii), 'ham%sbar' % nonascii)
+
     def test_abspath(self):
         # ntpath.abspath() can only be used on a system with the "nt" module
         # (reasonably), so we protect this test with "import nt".  This allows
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py
index aafdf3c..c4410a9 100644
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -368,6 +368,11 @@
 def test_main():
     run_unittest(PkgutilTests, PkgutilPEP302Tests, ExtendPathTests,
                  NestedNamespacePackageTest, ImportlibMigrationTests)
+    # this is necessary if test is run repeated (like when finding leaks)
+    import zipimport
+    import importlib
+    zipimport._zip_directory_cache.clear()
+    importlib.invalidate_caches()
 
 
 if __name__ == '__main__':
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 009f29d..9a5ee91 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -1161,7 +1161,7 @@
     def test_initgroups(self):
         # find missing group
 
-        g = max(self.saved_groups) + 1
+        g = max(self.saved_groups or [0]) + 1
         name = pwd.getpwuid(posix.getuid()).pw_name
         posix.initgroups(name, g)
         self.assertIn(g, posix.getgroups())
diff --git a/Lib/test/test_pydoc.py b/Lib/test/test_pydoc.py
index 81b58a2..f26fb15 100644
--- a/Lib/test/test_pydoc.py
+++ b/Lib/test/test_pydoc.py
@@ -387,6 +387,16 @@
             print_diffs(expected_text, result)
             self.fail("outputs are not equal, see diff above")
 
+    def test_text_enum_member_with_value_zero(self):
+        # Test issue #20654 to ensure enum member with value 0 can be
+        # displayed. It used to throw KeyError: 'zero'.
+        import enum
+        class BinaryInteger(enum.IntEnum):
+            zero = 0
+            one = 1
+        doc = pydoc.render_doc(BinaryInteger)
+        self.assertIn('<BinaryInteger.zero: 0>', doc)
+
     def test_issue8225(self):
         # Test issue8225 to ensure no doc link appears for xml.etree
         result, doc_loc = get_pydoc_text(xml.etree)
diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py
index d9d4cf8..fecda1b 100644
--- a/Lib/test/test_range.py
+++ b/Lib/test/test_range.py
@@ -380,6 +380,30 @@
                 it = pickle.loads(d)
                 self.assertEqual(list(it), data[1:])
 
+    def test_exhausted_iterator_pickling(self):
+        r = range(2**65, 2**65+2)
+        i = iter(r)
+        while True:
+            r = next(i)
+            if r == 2**65+1:
+                break
+        d = pickle.dumps(i)
+        i2 = pickle.loads(d)
+        self.assertEqual(list(i), [])
+        self.assertEqual(list(i2), [])
+
+    def test_large_exhausted_iterator_pickling(self):
+        r = range(20)
+        i = iter(r)
+        while True:
+            r = next(i)
+            if r == 19:
+                break
+        d = pickle.dumps(i)
+        i2 = pickle.loads(d)
+        self.assertEqual(list(i), [])
+        self.assertEqual(list(i2), [])
+
     def test_odd_bug(self):
         # This used to raise a "SystemError: NULL result without error"
         # because the range validation step was eating the exception
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index a229e23..33ccd15 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -1205,6 +1205,24 @@
         self.assertEqual(out.getvalue().splitlines(),
                          ['literal 102 ', 'literal 111 ', 'literal 111 '])
 
+    def test_keyword_parameters(self):
+        # Issue #20283: Accepting the string keyword parameter.
+        pat = re.compile(r'(ab)')
+        self.assertEqual(
+            pat.match(string='abracadabra', pos=7, endpos=10).span(), (7, 9))
+        self.assertEqual(
+            pat.fullmatch(string='abracadabra', pos=7, endpos=9).span(), (7, 9))
+        self.assertEqual(
+            pat.search(string='abracadabra', pos=3, endpos=10).span(), (7, 9))
+        self.assertEqual(
+            pat.findall(string='abracadabra', pos=3, endpos=10), ['ab'])
+        self.assertEqual(
+            pat.split(string='abracadabra', maxsplit=1),
+            ['', 'ab', 'racadabra'])
+        self.assertEqual(
+            pat.scanner(string='abracadabra', pos=3, endpos=10).search().span(),
+            (7, 9))
+
 
 class PatternReprTests(unittest.TestCase):
     def check(self, pattern, expected):
diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py
index d1dfd9e..ebc819c 100644
--- a/Lib/test/test_robotparser.py
+++ b/Lib/test/test_robotparser.py
@@ -275,6 +275,7 @@
                 self.skipTest('%s is unavailable' % url)
             self.assertEqual(parser.can_fetch("*", robots_url), False)
 
+    @unittest.skip('does not handle the gzip encoding delivered by pydotorg')
     def testPythonOrg(self):
         support.requires('network')
         with support.transient_internet('www.python.org'):
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index 1f47b03..a483fe1 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -1492,6 +1492,15 @@
         # Move a dir inside an existing dir on another filesystem.
         self.test_move_dir_to_dir()
 
+    def test_move_dir_sep_to_dir(self):
+        self._check_move_dir(self.src_dir + os.path.sep, self.dst_dir,
+            os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
+
+    @unittest.skipUnless(os.path.altsep, 'requires os.path.altsep')
+    def test_move_dir_altsep_to_dir(self):
+        self._check_move_dir(self.src_dir + os.path.altsep, self.dst_dir,
+            os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
+
     def test_existing_file_inside_dest_dir(self):
         # A file with the same name inside the destination dir already exists.
         with open(self.dst_file, "wb"):
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 2f5ed25..e94f539 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1234,9 +1234,15 @@
         # Issue #6697.
         self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800')
 
-        # Issue 17269
+        # Issue 17269: test workaround for OS X platform bug segfault
         if hasattr(socket, 'AI_NUMERICSERV'):
-            socket.getaddrinfo("localhost", None, 0, 0, 0, socket.AI_NUMERICSERV)
+            try:
+                # The arguments here are undefined and the call may succeed
+                # or fail.  All we care here is that it doesn't segfault.
+                socket.getaddrinfo("localhost", None, 0, 0, 0,
+                                   socket.AI_NUMERICSERV)
+            except socket.gaierror:
+                pass
 
     def test_getnameinfo(self):
         # only IP addresses are allowed
diff --git a/Lib/test/test_source_encoding.py b/Lib/test/test_source_encoding.py
index cd9d2b3..0c41e50 100644
--- a/Lib/test/test_source_encoding.py
+++ b/Lib/test/test_source_encoding.py
@@ -5,6 +5,7 @@
 import importlib
 import os
 import sys
+import subprocess
 
 class SourceEncodingTest(unittest.TestCase):
 
@@ -58,6 +59,15 @@
         # two bytes in common with the UTF-8 BOM
         self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20')
 
+    def test_20731(self):
+        sub = subprocess.Popen([sys.executable,
+                        os.path.join(os.path.dirname(__file__),
+                                     'coding20731.py')],
+                        stderr=subprocess.PIPE)
+        err = sub.communicate()[1]
+        self.assertEqual(sub.returncode, 0)
+        self.assertNotIn(b'SyntaxError', err)
+
     def test_error_message(self):
         compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
         compile(b'\xef\xbb\xbf\n', 'dummy', 'exec')
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 70e6396..9bd0a01 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -102,7 +102,7 @@
                 self.assertTrue(os.path.isdir(path))
             self.assertFalse(os.path.isdir(path))
         finally:
-            shutil.rmtree(parent_dir)
+            support.rmtree(parent_dir)
 
     def test_temp_dir__path_none(self):
         """Test passing no path."""
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index e25f296..5a9699f 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -1,4 +1,5 @@
 import unittest, test.support
+from test.script_helper import assert_python_ok, assert_python_failure
 import sys, io, os
 import struct
 import subprocess
@@ -89,74 +90,54 @@
     # Python/pythonrun.c::PyErr_PrintEx() is tricky.
 
     def test_exit(self):
-
+        # call with two arguments
         self.assertRaises(TypeError, sys.exit, 42, 42)
 
         # call without argument
-        try:
-            sys.exit(0)
-        except SystemExit as exc:
-            self.assertEqual(exc.code, 0)
-        except:
-            self.fail("wrong exception")
-        else:
-            self.fail("no exception")
+        with self.assertRaises(SystemExit) as cm:
+            sys.exit()
+        self.assertIsNone(cm.exception.code)
+
+        rc, out, err = assert_python_ok('-c', 'import sys; sys.exit()')
+        self.assertEqual(rc, 0)
+        self.assertEqual(out, b'')
+        self.assertEqual(err, b'')
+
+        # call with integer argument
+        with self.assertRaises(SystemExit) as cm:
+            sys.exit(42)
+        self.assertEqual(cm.exception.code, 42)
 
         # call with tuple argument with one entry
         # entry will be unpacked
-        try:
-            sys.exit(42)
-        except SystemExit as exc:
-            self.assertEqual(exc.code, 42)
-        except:
-            self.fail("wrong exception")
-        else:
-            self.fail("no exception")
-
-        # call with integer argument
-        try:
+        with self.assertRaises(SystemExit) as cm:
             sys.exit((42,))
-        except SystemExit as exc:
-            self.assertEqual(exc.code, 42)
-        except:
-            self.fail("wrong exception")
-        else:
-            self.fail("no exception")
+        self.assertEqual(cm.exception.code, 42)
 
         # call with string argument
-        try:
+        with self.assertRaises(SystemExit) as cm:
             sys.exit("exit")
-        except SystemExit as exc:
-            self.assertEqual(exc.code, "exit")
-        except:
-            self.fail("wrong exception")
-        else:
-            self.fail("no exception")
+        self.assertEqual(cm.exception.code, "exit")
 
         # call with tuple argument with two entries
-        try:
+        with self.assertRaises(SystemExit) as cm:
             sys.exit((17, 23))
-        except SystemExit as exc:
-            self.assertEqual(exc.code, (17, 23))
-        except:
-            self.fail("wrong exception")
-        else:
-            self.fail("no exception")
+        self.assertEqual(cm.exception.code, (17, 23))
 
         # test that the exit machinery handles SystemExits properly
-        rc = subprocess.call([sys.executable, "-c",
-                              "raise SystemExit(47)"])
+        rc, out, err = assert_python_failure('-c', 'raise SystemExit(47)')
         self.assertEqual(rc, 47)
+        self.assertEqual(out, b'')
+        self.assertEqual(err, b'')
 
-        def check_exit_message(code, expected, env=None):
-            process = subprocess.Popen([sys.executable, "-c", code],
-                                       stderr=subprocess.PIPE, env=env)
-            stdout, stderr = process.communicate()
-            self.assertEqual(process.returncode, 1)
-            self.assertTrue(stderr.startswith(expected),
-                "%s doesn't start with %s" % (ascii(stderr), ascii(expected)))
+        def check_exit_message(code, expected, **env_vars):
+            rc, out, err = assert_python_failure('-c', code, **env_vars)
+            self.assertEqual(rc, 1)
+            self.assertEqual(out, b'')
+            self.assertTrue(err.startswith(expected),
+                "%s doesn't start with %s" % (ascii(err), ascii(expected)))
 
-        # test that stderr buffer if flushed before the exit message is written
+        # test that stderr buffer is flushed before the exit message is written
         # into stderr
         check_exit_message(
             r'import sys; sys.stderr.write("unflushed,"); sys.exit("message")',
@@ -170,11 +151,9 @@
 
         # test that the unicode message is encoded to the stderr encoding
         # instead of the default encoding (utf8)
-        env = os.environ.copy()
-        env['PYTHONIOENCODING'] = 'latin-1'
         check_exit_message(
             r'import sys; sys.exit("h\xe9")',
-            b"h\xe9", env=env)
+            b"h\xe9", PYTHONIOENCODING='latin-1')
 
     def test_getdefaultencoding(self):
         self.assertRaises(TypeError, sys.getdefaultencoding, 42)
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index ab88be4..92f8bfe 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -240,14 +240,16 @@
         self.assertIn(b'ustar/dirtype/', out)
         self.assertIn(b'ustar/dirtype-with-size/', out)
         # Make sure it is able to print unencodable characters
-        self.assertIn(br'ustar/umlauts-'
-                      br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf', out)
-        self.assertIn(br'misc/regtype-hpux-signed-chksum-'
-                      br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf', out)
-        self.assertIn(br'misc/regtype-old-v7-signed-chksum-'
-                      br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf', out)
-        self.assertIn(br'pax/bad-pax-\udce4\udcf6\udcfc', out)
-        self.assertIn(br'pax/hdrcharset-\udce4\udcf6\udcfc', out)
+        def conv(b):
+            s = b.decode(self.tar.encoding, 'surrogateescape')
+            return s.encode('ascii', 'backslashreplace')
+        self.assertIn(conv(b'ustar/umlauts-\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out)
+        self.assertIn(conv(b'misc/regtype-hpux-signed-chksum-'
+                           b'\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out)
+        self.assertIn(conv(b'misc/regtype-old-v7-signed-chksum-'
+                           b'\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out)
+        self.assertIn(conv(b'pax/bad-pax-\xe4\xf6\xfc'), out)
+        self.assertIn(conv(b'pax/hdrcharset-\xe4\xf6\xfc'), out)
         # Make sure it prints files separated by one newline without any
         # 'ls -l'-like accessories if verbose flag is not being used
         # ...
diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py
index c0c6341..d12fb22 100644
--- a/Lib/test/test_tcl.py
+++ b/Lib/test/test_tcl.py
@@ -376,6 +376,7 @@
             result = arg
             return arg
         self.interp.createcommand('testfunc', testfunc)
+        self.addCleanup(self.interp.tk.deletecommand, 'testfunc')
         def check(value, expected, eq=self.assertEqual):
             r = self.interp.call('testfunc', value)
             self.assertIsInstance(result, str)
diff --git a/Lib/test/test_threadsignals.py b/Lib/test/test_threadsignals.py
index b1004e6..9d92742 100644
--- a/Lib/test/test_threadsignals.py
+++ b/Lib/test/test_threadsignals.py
@@ -74,6 +74,9 @@
 
     @unittest.skipIf(USING_PTHREAD_COND,
                      'POSIX condition variables cannot be interrupted')
+    # Issue #20564: sem_timedwait() cannot be interrupted on OpenBSD
+    @unittest.skipIf(sys.platform.startswith('openbsd'),
+                     'lock cannot be interrupted on OpenBSD')
     def test_lock_acquire_interruption(self):
         # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck
         # in a deadlock.
@@ -97,6 +100,9 @@
 
     @unittest.skipIf(USING_PTHREAD_COND,
                      'POSIX condition variables cannot be interrupted')
+    # Issue #20564: sem_timedwait() cannot be interrupted on OpenBSD
+    @unittest.skipIf(sys.platform.startswith('openbsd'),
+                     'lock cannot be interrupted on OpenBSD')
     def test_rlock_acquire_interruption(self):
         # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck
         # in a deadlock.
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index b8721a2..be7ddcc 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -14,6 +14,8 @@
 SIZEOF_INT = sysconfig.get_config_var('SIZEOF_INT') or 4
 TIME_MAXYEAR = (1 << 8 * SIZEOF_INT - 1) - 1
 TIME_MINYEAR = -TIME_MAXYEAR - 1
+_PyTime_ROUND_DOWN = 0
+_PyTime_ROUND_UP = 1
 
 
 class TimeTestCase(unittest.TestCase):
@@ -226,7 +228,7 @@
         self.assertEqual(time.ctime(t), 'Sun Sep 16 01:03:52 1973')
         t = time.mktime((2000, 1, 1, 0, 0, 0, 0, 0, -1))
         self.assertEqual(time.ctime(t), 'Sat Jan  1 00:00:00 2000')
-        for year in [-100, 100, 1000, 2000, 10000]:
+        for year in [-100, 100, 1000, 2000, 2050, 10000]:
             try:
                 testval = time.mktime((year, 1, 10) + (0,)*6)
             except (ValueError, OverflowError):
@@ -344,6 +346,13 @@
     def test_mktime(self):
         # Issue #1726687
         for t in (-2, -1, 0, 1):
+            if sys.platform.startswith('aix') and t == -1:
+                # Issue #11188, #19748: mktime() returns -1 on error. On Linux,
+                # the tm_wday field is used as a sentinel () to detect if -1 is
+                # really an error or a valid timestamp. On AIX, tm_wday is
+                # unchanged even on success and so cannot be used as a
+                # sentinel.
+                continue
             try:
                 tt = time.localtime(t)
             except (OverflowError, OSError):
@@ -585,58 +594,116 @@
     @support.cpython_only
     def test_time_t(self):
         from _testcapi import pytime_object_to_time_t
-        for obj, time_t in (
-            (0, 0),
-            (-1, -1),
-            (-1.0, -1),
-            (-1.9, -1),
-            (1.0, 1),
-            (1.9, 1),
+        for obj, time_t, rnd in (
+            # Round towards zero
+            (0, 0, _PyTime_ROUND_DOWN),
+            (-1, -1, _PyTime_ROUND_DOWN),
+            (-1.0, -1, _PyTime_ROUND_DOWN),
+            (-1.9, -1, _PyTime_ROUND_DOWN),
+            (1.0, 1, _PyTime_ROUND_DOWN),
+            (1.9, 1, _PyTime_ROUND_DOWN),
+            # Round away from zero
+            (0, 0, _PyTime_ROUND_UP),
+            (-1, -1, _PyTime_ROUND_UP),
+            (-1.0, -1, _PyTime_ROUND_UP),
+            (-1.9, -2, _PyTime_ROUND_UP),
+            (1.0, 1, _PyTime_ROUND_UP),
+            (1.9, 2, _PyTime_ROUND_UP),
         ):
-            self.assertEqual(pytime_object_to_time_t(obj), time_t)
+            self.assertEqual(pytime_object_to_time_t(obj, rnd), time_t)
 
+        rnd = _PyTime_ROUND_DOWN
         for invalid in self.invalid_values:
-            self.assertRaises(OverflowError, pytime_object_to_time_t, invalid)
+            self.assertRaises(OverflowError,
+                              pytime_object_to_time_t, invalid, rnd)
 
     @support.cpython_only
     def test_timeval(self):
         from _testcapi import pytime_object_to_timeval
-        for obj, timeval in (
-            (0, (0, 0)),
-            (-1, (-1, 0)),
-            (-1.0, (-1, 0)),
-            (1e-6, (0, 1)),
-            (-1e-6, (-1, 999999)),
-            (-1.2, (-2, 800000)),
-            (1.1234560, (1, 123456)),
-            (1.1234569, (1, 123456)),
-            (-1.1234560, (-2, 876544)),
-            (-1.1234561, (-2, 876543)),
+        for obj, timeval, rnd in (
+            # Round towards zero
+            (0, (0, 0), _PyTime_ROUND_DOWN),
+            (-1, (-1, 0), _PyTime_ROUND_DOWN),
+            (-1.0, (-1, 0), _PyTime_ROUND_DOWN),
+            (1e-6, (0, 1), _PyTime_ROUND_DOWN),
+            (1e-7, (0, 0), _PyTime_ROUND_DOWN),
+            (-1e-6, (-1, 999999), _PyTime_ROUND_DOWN),
+            (-1e-7, (-1, 999999), _PyTime_ROUND_DOWN),
+            (-1.2, (-2, 800000), _PyTime_ROUND_DOWN),
+            (0.9999999, (0, 999999), _PyTime_ROUND_DOWN),
+            (0.0000041, (0, 4), _PyTime_ROUND_DOWN),
+            (1.1234560, (1, 123456), _PyTime_ROUND_DOWN),
+            (1.1234569, (1, 123456), _PyTime_ROUND_DOWN),
+            (-0.0000040, (-1, 999996), _PyTime_ROUND_DOWN),
+            (-0.0000041, (-1, 999995), _PyTime_ROUND_DOWN),
+            (-1.1234560, (-2, 876544), _PyTime_ROUND_DOWN),
+            (-1.1234561, (-2, 876543), _PyTime_ROUND_DOWN),
+            # Round away from zero
+            (0, (0, 0), _PyTime_ROUND_UP),
+            (-1, (-1, 0), _PyTime_ROUND_UP),
+            (-1.0, (-1, 0), _PyTime_ROUND_UP),
+            (1e-6, (0, 1), _PyTime_ROUND_UP),
+            (1e-7, (0, 1), _PyTime_ROUND_UP),
+            (-1e-6, (-1, 999999), _PyTime_ROUND_UP),
+            (-1e-7, (-1, 999999), _PyTime_ROUND_UP),
+            (-1.2, (-2, 800000), _PyTime_ROUND_UP),
+            (0.9999999, (1, 0), _PyTime_ROUND_UP),
+            (0.0000041, (0, 5), _PyTime_ROUND_UP),
+            (1.1234560, (1, 123457), _PyTime_ROUND_UP),
+            (1.1234569, (1, 123457), _PyTime_ROUND_UP),
+            (-0.0000040, (-1, 999996), _PyTime_ROUND_UP),
+            (-0.0000041, (-1, 999995), _PyTime_ROUND_UP),
+            (-1.1234560, (-2, 876544), _PyTime_ROUND_UP),
+            (-1.1234561, (-2, 876543), _PyTime_ROUND_UP),
         ):
-            self.assertEqual(pytime_object_to_timeval(obj), timeval)
+            with self.subTest(obj=obj, round=rnd, timeval=timeval):
+                self.assertEqual(pytime_object_to_timeval(obj, rnd), timeval)
 
+        rnd = _PyTime_ROUND_DOWN
         for invalid in self.invalid_values:
-            self.assertRaises(OverflowError, pytime_object_to_timeval, invalid)
+            self.assertRaises(OverflowError,
+                              pytime_object_to_timeval, invalid, rnd)
 
     @support.cpython_only
     def test_timespec(self):
         from _testcapi import pytime_object_to_timespec
-        for obj, timespec in (
-            (0, (0, 0)),
-            (-1, (-1, 0)),
-            (-1.0, (-1, 0)),
-            (1e-9, (0, 1)),
-            (-1e-9, (-1, 999999999)),
-            (-1.2, (-2, 800000000)),
-            (1.1234567890, (1, 123456789)),
-            (1.1234567899, (1, 123456789)),
-            (-1.1234567890, (-2, 876543211)),
-            (-1.1234567891, (-2, 876543210)),
+        for obj, timespec, rnd in (
+            # Round towards zero
+            (0, (0, 0), _PyTime_ROUND_DOWN),
+            (-1, (-1, 0), _PyTime_ROUND_DOWN),
+            (-1.0, (-1, 0), _PyTime_ROUND_DOWN),
+            (1e-9, (0, 1), _PyTime_ROUND_DOWN),
+            (1e-10, (0, 0), _PyTime_ROUND_DOWN),
+            (-1e-9, (-1, 999999999), _PyTime_ROUND_DOWN),
+            (-1e-10, (-1, 999999999), _PyTime_ROUND_DOWN),
+            (-1.2, (-2, 800000000), _PyTime_ROUND_DOWN),
+            (0.9999999999, (0, 999999999), _PyTime_ROUND_DOWN),
+            (1.1234567890, (1, 123456789), _PyTime_ROUND_DOWN),
+            (1.1234567899, (1, 123456789), _PyTime_ROUND_DOWN),
+            (-1.1234567890, (-2, 876543211), _PyTime_ROUND_DOWN),
+            (-1.1234567891, (-2, 876543210), _PyTime_ROUND_DOWN),
+            # Round away from zero
+            (0, (0, 0), _PyTime_ROUND_UP),
+            (-1, (-1, 0), _PyTime_ROUND_UP),
+            (-1.0, (-1, 0), _PyTime_ROUND_UP),
+            (1e-9, (0, 1), _PyTime_ROUND_UP),
+            (1e-10, (0, 1), _PyTime_ROUND_UP),
+            (-1e-9, (-1, 999999999), _PyTime_ROUND_UP),
+            (-1e-10, (-1, 999999999), _PyTime_ROUND_UP),
+            (-1.2, (-2, 800000000), _PyTime_ROUND_UP),
+            (0.9999999999, (1, 0), _PyTime_ROUND_UP),
+            (1.1234567890, (1, 123456790), _PyTime_ROUND_UP),
+            (1.1234567899, (1, 123456790), _PyTime_ROUND_UP),
+            (-1.1234567890, (-2, 876543211), _PyTime_ROUND_UP),
+            (-1.1234567891, (-2, 876543210), _PyTime_ROUND_UP),
         ):
-            self.assertEqual(pytime_object_to_timespec(obj), timespec)
+            with self.subTest(obj=obj, round=rnd, timespec=timespec):
+                self.assertEqual(pytime_object_to_timespec(obj, rnd), timespec)
 
+        rnd = _PyTime_ROUND_DOWN
         for invalid in self.invalid_values:
-            self.assertRaises(OverflowError, pytime_object_to_timespec, invalid)
+            self.assertRaises(OverflowError,
+                              pytime_object_to_timespec, invalid, rnd)
 
     @unittest.skipUnless(time._STRUCT_TM_ITEMS == 11, "needs tm_zone support")
     def test_localtime_timezone(self):
diff --git a/Lib/test/test_tokenize.py b/Lib/test/test_tokenize.py
index 6ed8597..38611a7 100644
--- a/Lib/test/test_tokenize.py
+++ b/Lib/test/test_tokenize.py
@@ -2,7 +2,7 @@
 Tests for the tokenize module.
 
 The tests can be really simple. Given a small fragment of source
-code, print out a table with tokens. The ENDMARK is omitted for
+code, print out a table with tokens. The ENDMARKER is omitted for
 brevity.
 
     >>> dump_tokens("1 + 1")
@@ -578,9 +578,15 @@
     >>> tempdir = os.path.dirname(f) or os.curdir
     >>> testfiles = glob.glob(os.path.join(tempdir, "test*.py"))
 
-tokenize is broken on test_pep3131.py because regular expressions are broken on
-the obscure unicode identifiers in it. *sigh*
+Tokenize is broken on test_pep3131.py because regular expressions are
+broken on the obscure unicode identifiers in it. *sigh*
+With roundtrip extended to test the 5-tuple mode of  untokenize,
+7 more testfiles fail.  Remove them also until the failure is diagnosed.
+
     >>> testfiles.remove(os.path.join(tempdir, "test_pep3131.py"))
+    >>> for f in ('buffer', 'builtin', 'fileio', 'inspect', 'os', 'platform', 'sys'):
+    ...     testfiles.remove(os.path.join(tempdir, "test_%s.py") % f)
+    ...
     >>> if not support.is_resource_enabled("cpu"):
     ...     testfiles = random.sample(testfiles, 10)
     ...
@@ -638,7 +644,7 @@
 from test import support
 from tokenize import (tokenize, _tokenize, untokenize, NUMBER, NAME, OP,
                      STRING, ENDMARKER, ENCODING, tok_name, detect_encoding,
-                     open as tokenize_open)
+                     open as tokenize_open, Untokenizer)
 from io import BytesIO
 from unittest import TestCase
 import os, sys, glob
@@ -659,21 +665,39 @@
 def roundtrip(f):
     """
     Test roundtrip for `untokenize`. `f` is an open file or a string.
-    The source code in f is tokenized, converted back to source code via
-    tokenize.untokenize(), and tokenized again from the latter. The test
-    fails if the second tokenization doesn't match the first.
+    The source code in f is tokenized to both 5- and 2-tuples.
+    Both sequences are converted back to source code via
+    tokenize.untokenize(), and the latter tokenized again to 2-tuples.
+    The test fails if the 3 pair tokenizations do not match.
+
+    When untokenize bugs are fixed, untokenize with 5-tuples should
+    reproduce code that does not contain a backslash continuation
+    following spaces.  A proper test should test this.
+
+    This function would be more useful for correcting bugs if it reported
+    the first point of failure, like assertEqual, rather than just
+    returning False -- or if it were only used in unittests and not
+    doctest and actually used assertEqual.
     """
+    # Get source code and original tokenizations
     if isinstance(f, str):
-        f = BytesIO(f.encode('utf-8'))
-    try:
-        token_list = list(tokenize(f.readline))
-    finally:
+        code = f.encode('utf-8')
+    else:
+        code = f.read()
         f.close()
-    tokens1 = [tok[:2] for tok in token_list]
-    new_bytes = untokenize(tokens1)
-    readline = (line for line in new_bytes.splitlines(keepends=True)).__next__
-    tokens2 = [tok[:2] for tok in tokenize(readline)]
-    return tokens1 == tokens2
+    readline = iter(code.splitlines(keepends=True)).__next__
+    tokens5 = list(tokenize(readline))
+    tokens2 = [tok[:2] for tok in tokens5]
+    # Reproduce tokens2 from pairs
+    bytes_from2 = untokenize(tokens2)
+    readline2 = iter(bytes_from2.splitlines(keepends=True)).__next__
+    tokens2_from2 = [tok[:2] for tok in tokenize(readline2)]
+    # Reproduce tokens2 from 5-tuples
+    bytes_from5 = untokenize(tokens5)
+    readline5 = iter(bytes_from5.splitlines(keepends=True)).__next__
+    tokens2_from5 = [tok[:2] for tok in tokenize(readline5)]
+    # Compare 3 versions
+    return tokens2 == tokens2_from2 == tokens2_from5
 
 # This is an example from the docs, set up as a doctest.
 def decistmt(s):
@@ -1153,6 +1177,47 @@
         # See http://bugs.python.org/issue16152
         self.assertExactTypeEqual('@          ', token.AT)
 
+class UntokenizeTest(TestCase):
+
+    def test_bad_input_order(self):
+        # raise if previous row
+        u = Untokenizer()
+        u.prev_row = 2
+        u.prev_col = 2
+        with self.assertRaises(ValueError) as cm:
+            u.add_whitespace((1,3))
+        self.assertEqual(cm.exception.args[0],
+                'start (1,3) precedes previous end (2,2)')
+        # raise if previous column in row
+        self.assertRaises(ValueError, u.add_whitespace, (2,1))
+
+    def test_backslash_continuation(self):
+        # The problem is that <whitespace>\<newline> leaves no token
+        u = Untokenizer()
+        u.prev_row = 1
+        u.prev_col =  1
+        u.tokens = []
+        u.add_whitespace((2, 0))
+        self.assertEqual(u.tokens, ['\\\n'])
+        u.prev_row = 2
+        u.add_whitespace((4, 4))
+        self.assertEqual(u.tokens, ['\\\n', '\\\n\\\n', '    '])
+        self.assertTrue(roundtrip('a\n  b\n    c\n  \\\n  c\n'))
+
+    def test_iter_compat(self):
+        u = Untokenizer()
+        token = (NAME, 'Hello')
+        tokens = [(ENCODING, 'utf-8'), token]
+        u.compat(token, iter([]))
+        self.assertEqual(u.tokens, ["Hello "])
+        u = Untokenizer()
+        self.assertEqual(u.untokenize(iter([token])), 'Hello ')
+        u = Untokenizer()
+        self.assertEqual(u.untokenize(iter(tokens)), 'Hello ')
+        self.assertEqual(u.encoding, 'utf-8')
+        self.assertEqual(untokenize(iter(tokens)), b'Hello ')
+
+
 __test__ = {"doctests" : doctests, 'decistmt': decistmt}
 
 def test_main():
@@ -1162,6 +1227,7 @@
     support.run_unittest(Test_Tokenize)
     support.run_unittest(TestDetectEncoding)
     support.run_unittest(TestTokenize)
+    support.run_unittest(UntokenizeTest)
 
 if __name__ == "__main__":
     test_main()
diff --git a/Lib/test/test_tracemalloc.py b/Lib/test/test_tracemalloc.py
index d1e5aef..c953885 100644
--- a/Lib/test/test_tracemalloc.py
+++ b/Lib/test/test_tracemalloc.py
@@ -346,6 +346,8 @@
         self.assertIsNot(snapshot5.traces, snapshot.traces)
         self.assertEqual(snapshot5.traces, snapshot.traces)
 
+        self.assertRaises(TypeError, snapshot.filter_traces, filter1)
+
     def test_snapshot_group_by_line(self):
         snapshot, snapshot2 = create_snapshots()
         tb_0 = traceback_lineno('<unknown>', 0)
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index ec10752..18e6b0a 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -1,6 +1,6 @@
 # Python test set -- part 6, built-in types
 
-from test.support import run_unittest, run_with_locale
+from test.support import run_unittest, run_with_locale, cpython_only
 import collections
 import pickle
 import locale
@@ -1170,9 +1170,31 @@
             self.assertEqual(ns, ns_roundtrip, pname)
 
 
+class SharedKeyTests(unittest.TestCase):
+
+    @cpython_only
+    def test_subclasses(self):
+        # Verify that subclasses can share keys (per PEP 412)
+        class A:
+            pass
+        class B(A):
+            pass
+
+        a, b = A(), B()
+        self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
+        self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({}))
+        a.x, a.y, a.z, a.w = range(4)
+        self.assertNotEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(b)))
+        a2 = A()
+        self.assertEqual(sys.getsizeof(vars(a)), sys.getsizeof(vars(a2)))
+        self.assertLess(sys.getsizeof(vars(a)), sys.getsizeof({}))
+        b.u, b.v, b.w, b.t = range(4)
+        self.assertLess(sys.getsizeof(vars(b)), sys.getsizeof({}))
+
+
 def test_main():
     run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,
-                 SimpleNamespaceTests)
+                 SimpleNamespaceTests, SharedKeyTests)
 
 if __name__ == '__main__':
     test_main()
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index b32b409..4b92d63 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -1227,7 +1227,8 @@
             self.assertTrue(_proxy_bypass_macosx_sysconf(host, bypass),
                             'expected bypass of %s to be True' % host)
         # Check hosts that should not trigger the proxy bypass
-        for host in ('abc.foo.bar', 'bar.com', '127.0.0.2', '10.11.0.1', 'test'):
+        for host in ('abc.foo.bar', 'bar.com', '127.0.0.2', '10.11.0.1',
+                'notinbypass'):
             self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass),
                              'expected bypass of %s to be False' % host)
 
diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py
index d7352ea..5d6f31b 100644
--- a/Lib/tkinter/__init__.py
+++ b/Lib/tkinter/__init__.py
@@ -1339,6 +1339,21 @@
             args = args + (col2, row2)
         return self._getints(self.tk.call(*args)) or None
     bbox = grid_bbox
+
+    def _gridconvvalue(self, value):
+        if isinstance(value, (str, _tkinter.Tcl_Obj)):
+            try:
+                svalue = str(value)
+                if not svalue:
+                    return None
+                elif '.' in svalue:
+                    return getdouble(svalue)
+                else:
+                    return getint(svalue)
+            except ValueError:
+                pass
+        return value
+
     def _grid_configure(self, command, index, cnf, kw):
         """Internal function."""
         if isinstance(cnf, str) and not kw:
@@ -1357,22 +1372,14 @@
             for i in range(0, len(words), 2):
                 key = words[i][1:]
                 value = words[i+1]
-                if not value:
-                    value = None
-                elif '.' in str(value):
-                    value = getdouble(value)
-                else:
-                    value = getint(value)
-                dict[key] = value
+                dict[key] = self._gridconvvalue(value)
             return dict
         res = self.tk.call(
                   ('grid', command, self._w, index)
                   + options)
         if len(options) == 1:
-            if not res: return None
-            # In Tk 7.5, -width can be a float
-            if '.' in res: return getdouble(res)
-            return getint(res)
+            return self._gridconvvalue(res)
+
     def grid_columnconfigure(self, index, cnf={}, **kw):
         """Configure column INDEX of a grid.
 
diff --git a/Lib/tokenize.py b/Lib/tokenize.py
index 7785c98..98e9122 100644
--- a/Lib/tokenize.py
+++ b/Lib/tokenize.py
@@ -25,12 +25,14 @@
                'Skip Montanaro, Raymond Hettinger, Trent Nelson, '
                'Michael Foord')
 import builtins
-import re
-import sys
-from token import *
 from codecs import lookup, BOM_UTF8
 import collections
 from io import TextIOWrapper
+from itertools import chain
+import re
+import sys
+from token import *
+
 cookie_re = re.compile(r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)', re.ASCII)
 blank_re = re.compile(br'^[ \t\f]*(?:[#\r\n]|$)', re.ASCII)
 
@@ -229,20 +231,29 @@
 
     def add_whitespace(self, start):
         row, col = start
-        assert row <= self.prev_row
+        if row < self.prev_row or row == self.prev_row and col < self.prev_col:
+            raise ValueError("start ({},{}) precedes previous end ({},{})"
+                             .format(row, col, self.prev_row, self.prev_col))
+        row_offset = row - self.prev_row
+        if row_offset:
+            self.tokens.append("\\\n" * row_offset)
+            self.prev_col = 0
         col_offset = col - self.prev_col
         if col_offset:
             self.tokens.append(" " * col_offset)
 
     def untokenize(self, iterable):
-        for t in iterable:
+        it = iter(iterable)
+        for t in it:
             if len(t) == 2:
-                self.compat(t, iterable)
+                self.compat(t, it)
                 break
             tok_type, token, start, end, line = t
             if tok_type == ENCODING:
                 self.encoding = token
                 continue
+            if tok_type == ENDMARKER:
+                break
             self.add_whitespace(start)
             self.tokens.append(token)
             self.prev_row, self.prev_col = end
@@ -252,17 +263,12 @@
         return "".join(self.tokens)
 
     def compat(self, token, iterable):
-        startline = False
         indents = []
         toks_append = self.tokens.append
-        toknum, tokval = token
-
-        if toknum in (NAME, NUMBER):
-            tokval += ' '
-        if toknum in (NEWLINE, NL):
-            startline = True
+        startline = token[0] in (NEWLINE, NL)
         prevstring = False
-        for tok in iterable:
+
+        for tok in chain([token], iterable):
             toknum, tokval = tok[:2]
             if toknum == ENCODING:
                 self.encoding = tokval
diff --git a/Lib/tracemalloc.py b/Lib/tracemalloc.py
index 6f0a234..adedfc5 100644
--- a/Lib/tracemalloc.py
+++ b/Lib/tracemalloc.py
@@ -1,4 +1,4 @@
-from collections import Sequence
+from collections import Sequence, Iterable
 from functools import total_ordering
 import fnmatch
 import linecache
@@ -119,12 +119,12 @@
         previous = old_group.pop(traceback, None)
         if previous is not None:
             stat = StatisticDiff(traceback,
-                                  stat.size, stat.size - previous.size,
-                                  stat.count, stat.count - previous.count)
+                                 stat.size, stat.size - previous.size,
+                                 stat.count, stat.count - previous.count)
         else:
             stat = StatisticDiff(traceback,
-                                  stat.size, stat.size,
-                                  stat.count, stat.count)
+                                 stat.size, stat.size,
+                                 stat.count, stat.count)
         statistics.append(stat)
 
     for traceback, stat in old_group.items():
@@ -141,6 +141,7 @@
     __slots__ = ("_frame",)
 
     def __init__(self, frame):
+        # frame is a tuple: (filename: str, lineno: int)
         self._frame = frame
 
     @property
@@ -177,6 +178,8 @@
 
     def __init__(self, frames):
         Sequence.__init__(self)
+        # frames is a tuple of frame tuples: see Frame constructor for the
+        # format of a frame tuple
         self._frames = frames
 
     def __len__(self):
@@ -241,6 +244,8 @@
     __slots__ = ("_trace",)
 
     def __init__(self, trace):
+        # trace is a tuple: (size, traceback), see Traceback constructor
+        # for the format of the traceback tuple
         self._trace = trace
 
     @property
@@ -268,6 +273,7 @@
 class _Traces(Sequence):
     def __init__(self, traces):
         Sequence.__init__(self)
+        # traces is a tuple of trace tuples: see Trace constructor
         self._traces = traces
 
     def __len__(self):
@@ -338,6 +344,8 @@
     """
 
     def __init__(self, traces, traceback_limit):
+        # traces is a tuple of trace tuples: see _Traces constructor for
+        # the exact format
         self.traces = _Traces(traces)
         self.traceback_limit = traceback_limit
 
@@ -374,6 +382,9 @@
         is a list of Filter instances.  If filters is an empty list, return a
         new Snapshot instance with a copy of the traces.
         """
+        if not isinstance(filters, Iterable):
+            raise TypeError("filters must be a list of filters, not %s"
+                            % type(filters).__name__)
         if filters:
             include_filters = []
             exclude_filters = []
diff --git a/Mac/README b/Mac/README
index 61634e9..2774d10 100644
--- a/Mac/README
+++ b/Mac/README
@@ -86,6 +86,13 @@
 and the 10.4u SDK.  Starting with Xcode 3 and OS X 10.5, more configurations are
 available.
 
+In general, universal builds depend on specific features provided by the
+Apple-supplied compilers and other build tools included in Apple's Xcode
+development tools.  You should install Xcode and the command line tools
+component appropriate for the OS X release you are running on.  See the
+Python Developer's Guide (http://docs.python.org/devguide/setup.html)
+for more information.
+
 2.1 Flavors of universal binaries
 .................................
 
diff --git a/Misc/ACKS b/Misc/ACKS
index 12b5ab5..b8aaf75 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -40,6 +40,7 @@
 Oliver Andrich
 Ross Andrus
 Juancarlo Añez
+Chris Angelico
 Jérémy Anger
 Ankur Ankan
 Jon Anglin
@@ -585,6 +586,7 @@
 Lars Immisch
 Bobby Impollonia
 Meador Inge
+Peter Ingebretson
 Tony Ingraldi
 John Interrante
 Bob Ippolito
@@ -949,6 +951,7 @@
 James Oakley
 Elena Oat
 Jon Oberheide
+Milan Oberkirch
 Pascal Oberndoerfer
 Jeffrey Ollie
 Adam Olsen
@@ -1095,6 +1098,7 @@
 Jim Robinson
 Mark Roddy
 Kevin Rodgers
+Sean Rodman
 Giampaolo Rodola
 Elson Rodriguez
 Adi Roiban
@@ -1186,6 +1190,7 @@
 Ha Shao
 Mark Shannon
 Richard Shapiro
+Varun Sharma
 Vlad Shcherbina
 Justin Sheehy
 Charlie Shepherd
diff --git a/Misc/NEWS b/Misc/NEWS
index ff476fc..3de803b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,9 +10,69 @@
 Core and Builtins
 -----------------
 
+- Issue #20929: Add a type cast to avoid shifting a negative number.
+
+- Issue #20731: Properly position in source code files even if they
+  are opened in text mode. Patch by Serhiy Storchaka.
+
+- Issue #20637: Key-sharing now also works for instance dictionaries of
+  subclasses.  Patch by Peter Ingebretson.
+
 Library
 -------
 
+- Issue #19157: Include the broadcast address in the usuable hosts for IPv6
+  in ipaddress.
+
+- Issue #11599: When an external command (e.g. compiler) fails, distutils now
+  prints out the whole command line (instead of just the command name) if the
+  environment variable DISTUTILS_DEBUG is set.
+
+- Issue #4931: distutils should not produce unhelpful "error: None" messages
+  anymore.  distutils.util.grok_environment_error is kept but doc-deprecated.
+
+- Issue #20875: Prevent possible gzip "'read' is not defined" NameError.
+  Patch by Claudiu Popa.
+
+- Issue #11558: ``email.message.Message.attach`` now returns a more
+  useful error message if ``attach`` is called on a message for which
+  ``is_multipart`` is False.
+
+- Issue #20283: RE pattern methods now accept the string keyword parameters
+  as documented.  The pattern and source keyword parameters are left as
+  deprecated aliases.
+
+- Issue #20778: Fix modulefinder to work with bytecode-only modules.
+
+- Issue #20791: copy.copy() now doesn't make a copy when the input is
+  a bytes object.  Initial patch by Peter Otten.
+
+- Issue #19748: On AIX, time.mktime() now raises an OverflowError for year
+  outsize range [1902; 2037].
+
+Documentation
+-------------
+
+- Issue #20765: Add missing documentation for PurePath.with_name() and
+  PurePath.with_suffix().
+
+- Issue #19407: New package installation and distribution guides based on
+  the Python Packaging Authority tools. Existing guides have been retained
+  as legacy links from the distutils docs, as they still contain some
+  required reference material for tool developers that isn't recorded
+  anywhere else.
+
+Tests
+-----
+
+- Issue #20743: Fix a reference leak in test_tcl.
+
+Tools/Demos
+-----------
+
+- Issue #20535: PYTHONWARNING no longer affects the run_tests.py script.
+  Patch by Arfrever Frehtes Taifersar Arahesis.
+
 
 What's New in Python 3.4.0?
 ===========================
@@ -158,6 +218,8 @@
 - Issue #19255: The builtins module is restored to initial value before
   cleaning other modules.  The sys and builtins modules are cleaned last.
 
+- Issue #20588: Make Python-ast.c C89 compliant.
+
 - Issue #20437: Fixed 22 potential bugs when deleting objects references.
 
 - Issue #20500: Displaying an exception at interpreter shutdown no longer
@@ -1217,8 +1279,9 @@
 
 - Issue #19523: Closed FileHandler leak which occurred when delay was set.
 
-- Issue #19544 and #6516: Restore support for --user and --group parameters to
-  sdist command accidentally rolled back as part of the distutils2 rollback.
+- Issue #19544 and Issue #6516: Restore support for --user and --group
+  parameters to sdist command accidentally rolled back as part of the
+  distutils2 rollback.
 
 - Issue #13674: Prevented time.strftime from crashing on Windows when given
   a year before 1900 and a format of %y.
@@ -1374,8 +1437,6 @@
 
 - Issue #19324: Expose Linux-specific constants in resource module.
 
-- Issue #17400: ipaddress should make it easy to identify rfc6598 addresses.
-
 - Load SSL's error strings in hashlib.
 
 - Issue #18527: Upgrade internal copy of zlib to 1.2.8.
@@ -1422,7 +1483,7 @@
 
 - Issue #19085: Added basic tests for all tkinter widget options.
 
-- Issue 19384: Fix test_py_compile for root user, patch by Claudiu Popa.
+- Issue #19384: Fix test_py_compile for root user, patch by Claudiu Popa.
 
 Documentation
 -------------
@@ -1472,9 +1533,8 @@
 
 - Issue #19551: PEP 453 - the OS X installer now installs pip by default.
 
-- Update third-party libraries for OS X installers:
-    xz 5.0.3 -> 5.0.5
-    SQLite 3.7.13 -> 3.8.1
+- Update third-party libraries for OS X installers: xz 5.0.3 -> 5.0.5,
+  SQLite 3.7.13 -> 3.8.1
 
 - Issue #15663: Revert OS X installer built-in Tcl/Tk support for 3.4.0b1.
   Some third-party projects, such as Matplotlib and PIL/Pillow,
@@ -1567,8 +1627,9 @@
 
 - Issue #19254: Provide an optimized Python implementation of pbkdf2_hmac.
 
-- Issues #19201, #19222, #19223: Add "x" mode (exclusive creation) in opening
-  file to bz2, gzip and lzma modules. Patches by Tim Heaney and Vajrasky Kok.
+- Issues #19201, Issue #19222, Issue #19223: Add "x" mode (exclusive creation)
+  in opening file to bz2, gzip and lzma modules. Patches by Tim Heaney and
+  Vajrasky Kok.
 
 - Fix a reference count leak in _sre.
 
@@ -1589,6 +1650,9 @@
 
 - Issue #18281: Unused stat constants removed from `tarfile`.
 
+- Issue #18999: Multiprocessing now supports 'contexts' with the same API
+  as the module, but bound to specified start methods.
+
 - Issue #18468: The re.split, re.findall, and re.sub functions and the group()
   and groups() methods of match object now always return a string or a bytes
   object.
@@ -1793,9 +1857,9 @@
 - Issue #19018: The heapq.merge() function no longer suppresses IndexError
   in the underlying iterables.
 
-- Issue #18784: The uuid module no more attempts to load libc via ctypes.CDLL,
-  if all necessary functions are already found in libuuid.
-  Patch by Evgeny Sologubov.
+- Issue #18784: The uuid module no longer attempts to load libc via ctypes.CDLL
+  if all the necessary functions have already been found in libuuid.  Patch by
+  Evgeny Sologubov.
 
 - The :envvar:`PYTHONFAULTHANDLER` environment variable now only enables the
   faulthandler module if the variable is non-empty. Same behaviour than other
@@ -1850,9 +1914,6 @@
 
 - Issue #18942: sys._debugmallocstats() output was damaged on Windows.
 
-- Issue #18780: %-formatting now prints value instead of str for
-  int subclasses when using %d, %i, and %u codes.
-
 - Issue #18571: Implementation of the PEP 446: file descriptors and file
   handles are now created non-inheritable; add functions
   os.get/set_inheritable(), os.get/set_handle_inheritable() and
@@ -1864,7 +1925,7 @@
 - Issue #18808: Non-daemon threads are now automatically joined when
   a sub-interpreter is shutdown (it would previously dump a fatal error).
 
-- Remove supporting for compiling on systems without getcwd().
+- Remove support for compiling on systems without getcwd().
 
 - Issue #18774: Remove last bits of GNU PTH thread code and thread_pth.h.
 
@@ -1910,7 +1971,7 @@
 - Issue #18672: Fixed format specifiers for Py_ssize_t in debugging output in
   the _sre module.
 
-- Issue #18830: inspect.getclasstree() no more produces duplicated entries even
+- Issue #18830: inspect.getclasstree() no longer produces duplicate entries even
   when input list contains duplicates.
 
 - Issue #18878: sunau.open now supports the context manager protocol.  Based on
@@ -1957,7 +2018,7 @@
 
 - Issue #18394: Close cgi.FieldStorage's optional file.
 
-- Issue #17702: On error, os.environb now removes suppress the except context
+- Issue #17702: On error, os.environb now suppresses the exception context
   when raising a new KeyError with the original key.
 
 - Issue #16809: Fixed some tkinter incompabilities with Tcl/Tk 8.6.
@@ -2007,6 +2068,10 @@
 - Issue #18532: Change the builtin hash algorithms' names to lower case names
   as promised by hashlib's documentation.
 
+- Issue #8713: add new spwan and forkserver start methods, and new functions
+  get_all_start_methods, get_start_method, and set_start_method, to
+  multiprocessing.
+
 - Issue #18405: Improve the entropy of crypt.mksalt().
 
 - Issue #12015: The tempfile module now uses a suffix of 8 random characters
@@ -2332,6 +2397,9 @@
 - Issue #14850: Now a charmap decoder treats U+FFFE as "undefined mapping"
   in any mapping, not only in a string.
 
+- Issue #16613: Add *m* argument to ``collections.Chainmap.new_child`` to
+  allow the new child map to be specified explicitly.
+
 - Issue #16730: importlib.machinery.FileFinder now no longers raises an
   exception when trying to populate its cache and it finds out the directory is
   unreadable or has turned into a file. Reported and diagnosed by
@@ -2390,10 +2458,10 @@
   builtins.
 
 - Issue #16455: On FreeBSD and Solaris, if the locale is C, the
-  ASCII/surrogateescape codec is now used, instead of the locale encoding, to
+  ASCII/surrogateescape codec is now used (instead of the locale encoding) to
   decode the command line arguments. This change fixes inconsistencies with
-  os.fsencode() and os.fsdecode() because these operating systems announces an
-  ASCII locale encoding, whereas the ISO-8859-1 encoding is used in practice.
+  os.fsencode() and os.fsdecode(), because these operating systems announce an
+  ASCII locale encoding, but actually use the ISO-8859-1 encoding in practice.
 
 - Issue #16562: Optimize dict equality testing.  Patch by Serhiy Storchaka.
 
@@ -2484,6 +2552,9 @@
 
 - Issue #16160: Subclass support now works for types.SimpleNamespace.
 
+- Issue #16148: Implement PEP 424, adding operator.length_hint and
+  PyObject_LengthHint.
+
 - Upgrade Unicode data (UCD) to version 6.2.
 
 - Issue #15379: Fix passing of non-BMP characters as integers for the charmap
@@ -2996,7 +3067,7 @@
   to procedurally generate, in an easy way, small test instances.
 
 - Issue #17485: Also delete the Request Content-Length header if the data
-  attribute is deleted.  (Follow on to issue 16464).
+  attribute is deleted.  (Follow on to issue Issue #16464).
 
 - Issue #15927: CVS now correctly parses escaped newlines and carriage
   when parsing with quoting turned off.
@@ -3423,6 +3494,9 @@
   list() calls aren't added to filter(), map(), and zip() which are directly
   passed enumerate().
 
+- Issue #16464: Reset the Content-Length header when a urllib Request is reused
+  with new data.
+
 - Issue #12848: The pure Python pickle implementation now treats object
   lengths as unsigned 32-bit integers, like the C implementation does.
   Patch by Serhiy Storchaka.
@@ -3573,6 +3647,9 @@
 
 - Issue #16169: Fix ctypes.WinError()'s confusion between errno and winerror.
 
+- Issue #16110: logging.fileConfig now accepts a pre-initialised ConfigParser
+  instance.
+
 - Issue #1492704: shutil.copyfile() raises a distinct SameFileError now if
   source and destination are the same file. Patch by Atsuo Ishimoto.
 
@@ -3591,6 +3668,10 @@
 - Issue #9650: List commonly used format codes in time.strftime and
   time.strptime docsttings.  Original patch by Mike Hoy.
 
+- Issue #15452: logging configuration socket listener now has a verify option
+  that allows an application to apply a verification function to the
+  received configuration data before it is acted upon.
+
 - Issue #16034: Fix performance regressions in the new `bz2.BZ2File`
   implementation.  Initial patch by Serhiy Storchaka.
 
@@ -6290,7 +6371,7 @@
   deallocator calls one of the methods on the type (e.g. when subclassing
   IOBase).  Diagnosis and patch by Davide Rizzo.
 
-- Issue #9611, #9015: FileIO.read() clamps the length to INT_MAX on Windows.
+- Issue #9611, Issue #9015: FileIO.read() clamps the length to INT_MAX on Windows.
 
 - Issue #9642: Uniformize the tests on the availability of the mbcs codec, add
   a new HAVE_MBCS define.
@@ -6758,7 +6839,7 @@
   'importlib.abc.PyPycLoader', 'nntplib.NNTP.xgtitle', 'nntplib.NNTP.xpath',
   and private attributes of 'smtpd.SMTPChannel'.
 
-- Issue #5905, #13560: time.strftime() is now using the current locale
+- Issue #5905, Issue #13560: time.strftime() is now using the current locale
   encoding, instead of UTF-8, if the wcsftime() function is not available.
 
 - Issue #13464: Add a readinto() method to http.client.HTTPResponse.  Patch
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 65772cf..79d60f3 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -1640,9 +1640,9 @@
 /*
 #define CHAR_ALIGN (sizeof(s_char) - sizeof(char))
 #define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
-#define INT_ALIGN (sizeof(s_int) - sizeof(int))
 #define LONG_ALIGN (sizeof(s_long) - sizeof(long))
 */
+#define INT_ALIGN (sizeof(s_int) - sizeof(int))
 #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
 #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
 #define LONGDOUBLE_ALIGN (sizeof(s_long_double) - sizeof(long double))
@@ -1684,8 +1684,8 @@
 ffi_type ffi_type_uint16 = { 2, 2, FFI_TYPE_UINT16 };
 ffi_type ffi_type_sint16 = { 2, 2, FFI_TYPE_SINT16 };
 
-ffi_type ffi_type_uint32 = { 4, 4, FFI_TYPE_UINT32 };
-ffi_type ffi_type_sint32 = { 4, 4, FFI_TYPE_SINT32 };
+ffi_type ffi_type_uint32 = { 4, INT_ALIGN, FFI_TYPE_UINT32 };
+ffi_type ffi_type_sint32 = { 4, INT_ALIGN, FFI_TYPE_SINT32 };
 
 ffi_type ffi_type_uint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_UINT64 };
 ffi_type ffi_type_sint64 = { 8, LONG_LONG_ALIGN, FFI_TYPE_SINT64 };
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index fce6bbf..496ff34 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -2459,7 +2459,7 @@
     struct tm *tm;
     time_t t;
 
-    if (_PyTime_ObjectToTime_t(obj, &t) == -1)
+    if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_DOWN) == -1)
         return NULL;
 
     tm = localtime(&t);
@@ -4127,7 +4127,7 @@
     time_t timet;
     long us;
 
-    if (_PyTime_ObjectToTimeval(timestamp, &timet, &us) == -1)
+    if (_PyTime_ObjectToTimeval(timestamp, &timet, &us, _PyTime_ROUND_DOWN) == -1)
         return NULL;
     return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo);
 }
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index ac20308..ea5253e 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -3928,9 +3928,6 @@
     return ret;
 }
 
-static mpd_uint_t data_zero[1] = {0};
-static const mpd_t zero = {MPD_STATIC|MPD_CONST_DATA, 0, 1, 1, 1, data_zero};
-
 static PyObject *
 nm_mpd_qpow(PyObject *base, PyObject *exp, PyObject *mod)
 {
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 34c2bb9..7494646 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -2305,9 +2305,14 @@
 static PyObject *
 _forward_call(buffered *self, _Py_Identifier *name, PyObject *args)
 {
-    PyObject *func = _PyObject_GetAttrId((PyObject *)self, name);
-    PyObject *ret;
+    PyObject *func, *ret;
+    if (self == NULL) {
+        PyErr_SetString(PyExc_ValueError,
+                        "I/O operation on uninitialized object");
+        return NULL;
+    }
 
+    func = _PyObject_GetAttrId((PyObject *)self, name);
     if (func == NULL) {
         PyErr_SetString(PyExc_AttributeError, name->string);
         return NULL;
diff --git a/Modules/_math.c b/Modules/_math.c
index fe75a36..a14ff06 100644
--- a/Modules/_math.c
+++ b/Modules/_math.c
@@ -22,7 +22,9 @@
 static const double ln2 = 6.93147180559945286227E-01;
 static const double two_pow_m28 = 3.7252902984619141E-09; /* 2**-28 */
 static const double two_pow_p28 = 268435456.0; /* 2**28 */
+#ifndef Py_NAN
 static const double zero = 0.0;
+#endif
 
 /* acosh(x)
  * Method :
diff --git a/Modules/_sre.c b/Modules/_sre.c
index d4d1d9d..eb1106a 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -526,59 +526,49 @@
     return sre_ucs4_search(state, pattern);
 }
 
-/*[clinic input]
-module _sre
-class _sre.SRE_Pattern "PatternObject *" "&Pattern_Type"
-
-_sre.SRE_Pattern.match as pattern_match
-
-    pattern: object
-    pos: Py_ssize_t = 0
-    endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize
-
-Matches zero or more characters at the beginning of the string.
-[clinic start generated code]*/
-
-PyDoc_STRVAR(pattern_match__doc__,
-"match($self, /, pattern, pos=0, endpos=sys.maxsize)\n"
-"--\n"
-"\n"
-"Matches zero or more characters at the beginning of the string.");
-
-#define PATTERN_MATCH_METHODDEF    \
-    {"match", (PyCFunction)pattern_match, METH_VARARGS|METH_KEYWORDS, pattern_match__doc__},
-
 static PyObject *
-pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos);
+fix_string_param(PyObject *string, PyObject *string2, const char *oldname)
+{
+    if (string2 != NULL) {
+        if (string != NULL) {
+            PyErr_Format(PyExc_TypeError,
+                         "Argument given by name ('%s') and position (1)",
+                         oldname);
+            return NULL;
+        }
+        if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
+                             "The '%s' keyword parameter name is deprecated.  "
+                             "Use 'string' instead.", oldname) < 0)
+            return NULL;
+        return string2;
+    }
+    if (string == NULL) {
+        PyErr_SetString(PyExc_TypeError,
+                        "Required argument 'string' (pos 1) not found");
+        return NULL;
+    }
+    return string;
+}
 
 static PyObject *
 pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs)
 {
-    PyObject *return_value = NULL;
-    static char *_keywords[] = {"pattern", "pos", "endpos", NULL};
-    PyObject *pattern;
+    static char *_keywords[] = {"string", "pos", "endpos", "pattern", NULL};
+    PyObject *string = NULL;
     Py_ssize_t pos = 0;
     Py_ssize_t endpos = PY_SSIZE_T_MAX;
-
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
-        "O|nn:match", _keywords,
-        &pattern, &pos, &endpos))
-        goto exit;
-    return_value = pattern_match_impl(self, pattern, pos, endpos);
-
-exit:
-    return return_value;
-}
-
-static PyObject *
-pattern_match_impl(PatternObject *self, PyObject *pattern, Py_ssize_t pos, Py_ssize_t endpos)
-/*[clinic end generated code: output=1528eafdb8b025ad input=26f9fd31befe46b9]*/
-{
+    PyObject *pattern = NULL;
     SRE_STATE state;
     Py_ssize_t status;
-    PyObject *string;
 
-    string = state_init(&state, (PatternObject *)self, pattern, pos, endpos);
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
+        "|Onn$O:match", _keywords,
+        &string, &pos, &endpos, &pattern))
+        return NULL;
+    string = fix_string_param(string, pattern, "pattern");
+    if (!string)
+        return NULL;
+    string = state_init(&state, (PatternObject *)self, string, pos, endpos);
     if (!string)
         return NULL;
 
@@ -603,12 +593,16 @@
     SRE_STATE state;
     Py_ssize_t status;
 
-    PyObject* string;
+    PyObject *string = NULL, *string2 = NULL;
     Py_ssize_t start = 0;
     Py_ssize_t end = PY_SSIZE_T_MAX;
-    static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:fullmatch", kwlist,
-                                     &string, &start, &end))
+    static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL };
+    if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:fullmatch", kwlist,
+                                     &string, &start, &end, &string2))
+        return NULL;
+
+    string = fix_string_param(string, string2, "pattern");
+    if (!string)
         return NULL;
 
     string = state_init(&state, self, string, start, end);
@@ -637,12 +631,16 @@
     SRE_STATE state;
     Py_ssize_t status;
 
-    PyObject* string;
+    PyObject *string = NULL, *string2 = NULL;
     Py_ssize_t start = 0;
     Py_ssize_t end = PY_SSIZE_T_MAX;
-    static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:search", kwlist,
-                                     &string, &start, &end))
+    static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL };
+    if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:search", kwlist,
+                                     &string, &start, &end, &string2))
+        return NULL;
+
+    string = fix_string_param(string, string2, "pattern");
+    if (!string)
         return NULL;
 
     string = state_init(&state, self, string, start, end);
@@ -718,12 +716,16 @@
     Py_ssize_t status;
     Py_ssize_t i, b, e;
 
-    PyObject* string;
+    PyObject *string = NULL, *string2 = NULL;
     Py_ssize_t start = 0;
     Py_ssize_t end = PY_SSIZE_T_MAX;
-    static char* kwlist[] = { "source", "pos", "endpos", NULL };
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:findall", kwlist,
-                                     &string, &start, &end))
+    static char* kwlist[] = { "string", "pos", "endpos", "source", NULL };
+    if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:findall", kwlist,
+                                     &string, &start, &end, &string2))
+        return NULL;
+
+    string = fix_string_param(string, string2, "source");
+    if (!string)
         return NULL;
 
     string = state_init(&state, self, string, start, end);
@@ -840,11 +842,15 @@
     Py_ssize_t i;
     void* last;
 
-    PyObject* string;
+    PyObject *string = NULL, *string2 = NULL;
     Py_ssize_t maxsplit = 0;
-    static char* kwlist[] = { "source", "maxsplit", NULL };
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "O|n:split", kwlist,
-                                     &string, &maxsplit))
+    static char* kwlist[] = { "string", "maxsplit", "source", NULL };
+    if (!PyArg_ParseTupleAndKeywords(args, kw, "|On$O:split", kwlist,
+                                     &string, &maxsplit, &string2))
+        return NULL;
+
+    string = fix_string_param(string, string2, "source");
+    if (!string)
         return NULL;
 
     string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX);
@@ -1292,6 +1298,10 @@
     return result;
 }
 
+PyDoc_STRVAR(pattern_match_doc,
+"match(string[, pos[, endpos]]) -> match object or None.\n\
+    Matches zero or more characters at the beginning of the string");
+
 PyDoc_STRVAR(pattern_fullmatch_doc,
 "fullmatch(string[, pos[, endpos]]) -> match object or None.\n\
     Matches against all of the string");
@@ -1329,7 +1339,8 @@
 PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects");
 
 static PyMethodDef pattern_methods[] = {
-    PATTERN_MATCH_METHODDEF
+    {"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS,
+        pattern_match_doc},
     {"fullmatch", (PyCFunction) pattern_fullmatch, METH_VARARGS|METH_KEYWORDS,
         pattern_fullmatch_doc},
     {"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS,
@@ -2654,12 +2665,16 @@
 
     ScannerObject* self;
 
-    PyObject* string;
+    PyObject *string = NULL, *string2 = NULL;
     Py_ssize_t start = 0;
     Py_ssize_t end = PY_SSIZE_T_MAX;
-    static char* kwlist[] = { "source", "pos", "endpos", NULL };
-    if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:scanner", kwlist,
-                                     &string, &start, &end))
+    static char* kwlist[] = { "string", "pos", "endpos", "source", NULL };
+    if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:scanner", kwlist,
+                                     &string, &start, &end, &string2))
+        return NULL;
+
+    string = fix_string_param(string, string2, "source");
+    if (!string)
         return NULL;
 
     /* create scanner object */
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 9e81787..db2376d 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -2516,14 +2516,27 @@
     return PyLong_FromLong(r);
 }
 
+static int
+check_time_rounding(int round)
+{
+    if (round != _PyTime_ROUND_DOWN && round != _PyTime_ROUND_UP) {
+        PyErr_SetString(PyExc_ValueError, "invalid rounding");
+        return -1;
+    }
+    return 0;
+}
+
 static PyObject *
 test_pytime_object_to_time_t(PyObject *self, PyObject *args)
 {
     PyObject *obj;
     time_t sec;
-    if (!PyArg_ParseTuple(args, "O:pytime_object_to_time_t", &obj))
+    int round;
+    if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_time_t", &obj, &round))
         return NULL;
-    if (_PyTime_ObjectToTime_t(obj, &sec) == -1)
+    if (check_time_rounding(round) < 0)
+        return NULL;
+    if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1)
         return NULL;
     return _PyLong_FromTime_t(sec);
 }
@@ -2534,9 +2547,12 @@
     PyObject *obj;
     time_t sec;
     long usec;
-    if (!PyArg_ParseTuple(args, "O:pytime_object_to_timeval", &obj))
+    int round;
+    if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timeval", &obj, &round))
         return NULL;
-    if (_PyTime_ObjectToTimeval(obj, &sec, &usec) == -1)
+    if (check_time_rounding(round) < 0)
+        return NULL;
+    if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1)
         return NULL;
     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
 }
@@ -2547,9 +2563,12 @@
     PyObject *obj;
     time_t sec;
     long nsec;
-    if (!PyArg_ParseTuple(args, "O:pytime_object_to_timespec", &obj))
+    int round;
+    if (!PyArg_ParseTuple(args, "Oi:pytime_object_to_timespec", &obj, &round))
         return NULL;
-    if (_PyTime_ObjectToTimespec(obj, &sec, &nsec) == -1)
+    if (check_time_rounding(round) < 0)
+        return NULL;
+    if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1)
         return NULL;
     return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
 }
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 3b013ac..4a1c158 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2838,6 +2838,8 @@
         return NULL;
     if (index < 0)
         index = 0;
+    else if (index > Py_SIZE(it->ao))
+        index = Py_SIZE(it->ao); /* iterator exhausted */
     it->index = index;
     Py_RETURN_NONE;
 }
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 4b3e642..7f094ff 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -906,7 +906,9 @@
              "This is the smallest integral value >= x.");
 
 FUNC2(copysign, copysign,
-      "copysign(x, y)\n\nReturn x with the sign of y.")
+      "copysign(x, y)\n\nReturn a float with the magnitude (absolute value) "
+      "of x but the sign \nof y. On platforms that support signed zeros, "
+      "copysign(1.0, -0.0) \nreturns -1.0.\n")
 FUNC1(cos, cos, 0,
       "cos(x)\n\nReturn the cosine of x (measured in radians).")
 FUNC1(cosh, cosh, 1,
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 6cbc78f..dc9bd55 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -4901,9 +4901,9 @@
         }
         utime.now = 0;
         if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
-                                     &a_sec, &a_nsec) == -1 ||
+                                     &a_sec, &a_nsec, _PyTime_ROUND_DOWN) == -1 ||
             _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
-                                     &m_sec, &m_nsec) == -1) {
+                                     &m_sec, &m_nsec, _PyTime_ROUND_DOWN) == -1) {
             goto exit;
         }
         utime.atime_s = a_sec;
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 0b11a01..ffaf865 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -212,10 +212,18 @@
         return NULL;
     }
     else {
-#ifdef MS_WINDOWS
+        /* On OpenBSD 5.4, timeval.tv_sec is a long.
+         * Example: long is 64-bit, whereas time_t is 32-bit. */
         time_t sec;
-        if (_PyTime_ObjectToTimeval(tout, &sec, &tv.tv_usec) == -1)
+        /* On OS X 64-bit, timeval.tv_usec is an int (and thus still 4
+           bytes as required), but no longer defined by a long. */
+        long usec;
+        if (_PyTime_ObjectToTimeval(tout, &sec, &usec,
+                                    _PyTime_ROUND_UP) == -1)
             return NULL;
+#ifdef MS_WINDOWS
+        /* On Windows, timeval.tv_sec is a long (32 bit),
+         * whereas time_t can be 64-bit. */
         assert(sizeof(tv.tv_sec) == sizeof(long));
 #if SIZEOF_TIME_T > SIZEOF_LONG
         if (sec > LONG_MAX) {
@@ -226,13 +234,10 @@
 #endif
         tv.tv_sec = (long)sec;
 #else
-        /* 64-bit OS X has struct timeval.tv_usec as an int (and thus still 4
-           bytes as required), but no longer defined by a long. */
-        long tv_usec;
-        if (_PyTime_ObjectToTimeval(tout, &tv.tv_sec, &tv_usec) == -1)
-            return NULL;
-        tv.tv_usec = tv_usec;
+        assert(sizeof(tv.tv_sec) >= sizeof(sec));
+        tv.tv_sec = sec;
 #endif
+        tv.tv_usec = usec;
         if (tv.tv_sec < 0) {
             PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
             return NULL;
@@ -2037,8 +2042,8 @@
         ptimeoutspec = NULL;
     }
     else if (PyNumber_Check(otimeout)) {
-        if (_PyTime_ObjectToTimespec(otimeout,
-                                     &timeout.tv_sec, &timeout.tv_nsec) == -1)
+        if (_PyTime_ObjectToTimespec(otimeout, &timeout.tv_sec,
+                                     &timeout.tv_nsec, _PyTime_ROUND_UP) == -1)
             return NULL;
 
         if (timeout.tv_sec < 0) {
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 43e3ca1..fedaddf 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -799,7 +799,8 @@
                           &signals, &timeout))
         return NULL;
 
-    if (_PyTime_ObjectToTimespec(timeout, &tv_sec, &tv_nsec) == -1)
+    if (_PyTime_ObjectToTimespec(timeout, &tv_sec, &tv_nsec,
+                                 _PyTime_ROUND_DOWN) == -1)
         return NULL;
     buf.tv_sec = tv_sec;
     buf.tv_nsec = tv_nsec;
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index d3878b9..d0917a4 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -193,7 +193,7 @@
     if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj))
         return NULL;
 
-    if (_PyTime_ObjectToTimespec(obj, &tv_sec, &tv_nsec) == -1)
+    if (_PyTime_ObjectToTimespec(obj, &tv_sec, &tv_nsec, _PyTime_ROUND_DOWN) == -1)
         return NULL;
     tp.tv_sec = tv_sec;
     tp.tv_nsec = tv_nsec;
@@ -341,7 +341,7 @@
         whent = time(NULL);
     }
     else {
-        if (_PyTime_ObjectToTime_t(ot, &whent) == -1)
+        if (_PyTime_ObjectToTime_t(ot, &whent, _PyTime_ROUND_DOWN) == -1)
             return 0;
     }
     *pwhen = whent;
@@ -823,7 +823,18 @@
     time_t tt;
     if (!gettmarg(tup, &buf))
         return NULL;
+#ifdef _AIX
+    /* year < 1902 or year > 2037 */
+    if (buf.tm_year < 2 || buf.tm_year > 137) {
+        /* Issue #19748: On AIX, mktime() doesn't report overflow error for
+         * timestamp < -2^31 or timestamp > 2**31-1. */
+        PyErr_SetString(PyExc_OverflowError,
+                        "mktime argument out of range");
+        return NULL;
+    }
+#else
     buf.tm_wday = -1;  /* sentinel; original value ignored */
+#endif
     tt = mktime(&buf);
     /* Return value of -1 does not necessarily mean an error, but tm_wday
      * cannot remain set to -1 if mktime succeeded. */
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index 31cc4cc..5b75705 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -3025,9 +3025,13 @@
     Py_ssize_t index = PyLong_AsSsize_t(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0)
-        index = 0;
-    it->it_index = index;
+    if (it->it_seq != NULL) {
+        if (index < 0)
+            index = 0;
+        else if (index > PyByteArray_GET_SIZE(it->it_seq))
+            index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
+        it->it_index = index;
+    }
     Py_RETURN_NONE;
 }
 
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 614978b..b93b9ef 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -2937,9 +2937,13 @@
     Py_ssize_t index = PyLong_AsSsize_t(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0)
-        index = 0;
-    it->it_index = index;
+    if (it->it_seq != NULL) {
+        if (index < 0)
+            index = 0;
+        else if (index > PyBytes_GET_SIZE(it->it_seq))
+            index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */
+        it->it_index = index;
+    }
     Py_RETURN_NONE;
 }
 
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 5b75968..fd5a72a 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -1970,7 +1970,7 @@
             if (keys[i] == NULL) {
                 for (i=i-1 ; i>=0 ; i--)
                     Py_DECREF(keys[i]);
-                if (keys != &ms.temparray[saved_ob_size+1])
+                if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
                     PyMem_FREE(keys);
                 goto keyfunc_fail;
             }
@@ -2043,7 +2043,7 @@
     if (keys != NULL) {
         for (i = 0; i < saved_ob_size; i++)
             Py_DECREF(keys[i]);
-        if (keys != &ms.temparray[saved_ob_size+1])
+        if (saved_ob_size >= MERGESTATE_TEMP_SIZE/2)
             PyMem_FREE(keys);
     }
 
@@ -2811,6 +2811,8 @@
     if (it->it_seq != NULL) {
         if (index < 0)
             index = 0;
+        else if (index > PyList_GET_SIZE(it->it_seq))
+            index = PyList_GET_SIZE(it->it_seq); /* iterator exhausted */
         it->it_index = index;
     }
     Py_RETURN_NONE;
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 9411216..7036c0e 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -37,7 +37,9 @@
 static PyObject *
 get_small_int(sdigit ival)
 {
-    PyObject *v = (PyObject*)(small_ints + ival + NSMALLNEGINTS);
+    PyObject *v;
+    assert(-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS);
+    v = (PyObject *)&small_ints[ival + NSMALLNEGINTS];
     Py_INCREF(v);
 #ifdef COUNT_ALLOCS
     if (ival >= 0)
diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c
index a4d9fb3..d6b4279 100644
--- a/Objects/rangeobject.c
+++ b/Objects/rangeobject.c
@@ -807,10 +807,11 @@
     long index = PyLong_AsLong(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0 || index >= r->len) {
-        PyErr_SetString(PyExc_ValueError, "index out of range");
-        return NULL;
-    }
+    /* silently clip the index value */
+    if (index < 0)
+        index = 0;
+    else if (index > r->len)
+        index = r->len; /* exhausted iterator */
     r->index = index;
     Py_RETURN_NONE;
 }
@@ -985,6 +986,28 @@
 static PyObject *
 longrangeiter_setstate(longrangeiterobject *r, PyObject *state)
 {
+    int cmp;
+   
+    /* clip the value */
+    PyObject *zero = PyLong_FromLong(0);
+    if (zero == NULL)
+        return NULL;
+    cmp = PyObject_RichCompareBool(state, zero, Py_LT);
+    if (cmp > 0) {
+        Py_CLEAR(r->index);
+        r->index = zero;
+        Py_RETURN_NONE;
+    }
+    Py_DECREF(zero);
+    if (cmp < 0)
+        return NULL;
+
+    cmp = PyObject_RichCompareBool(r->len, state, Py_LT);
+    if (cmp < 0)
+        return NULL;
+    if (cmp > 0)
+        state = r->len;
+    
     Py_CLEAR(r->index);
     r->index = state;
     Py_INCREF(r->index);
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index c63afcc..6fd4db3 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -1011,8 +1011,8 @@
     if (it->it_seq != NULL) {
         if (index < 0)
             index = 0;
-        else if (it->it_seq != NULL && index > PyTuple_GET_SIZE(it->it_seq))
-            index = PyTuple_GET_SIZE(it->it_seq);
+        else if (index > PyTuple_GET_SIZE(it->it_seq))
+            index = PyTuple_GET_SIZE(it->it_seq); /* exhausted iterator */
         it->it_index = index;
     }
     Py_RETURN_NONE;
diff --git a/Objects/typeobject.c b/Objects/typeobject.c
index 49385e2..df8f351 100644
--- a/Objects/typeobject.c
+++ b/Objects/typeobject.c
@@ -2472,6 +2472,9 @@
             type->tp_dictoffset = slotoffset;
         slotoffset += sizeof(PyObject *);
     }
+    else if (!type->tp_dictoffset) {
+        type->tp_dictoffset = base->tp_dictoffset;
+    }
     if (type->tp_dictoffset) {
         et->ht_cached_keys = _PyDict_NewKeysForClass();
     }
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 994c4f5..ec22239 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -9666,7 +9666,7 @@
     PyObject *last_obj;
     unsigned int kind = 0;
 
-    fseq = PySequence_Fast(seq, "");
+    fseq = PySequence_Fast(seq, "can only join an iterable");
     if (fseq == NULL) {
         return NULL;
     }
@@ -15196,9 +15196,13 @@
     Py_ssize_t index = PyLong_AsSsize_t(state);
     if (index == -1 && PyErr_Occurred())
         return NULL;
-    if (index < 0)
-        index = 0;
-    it->it_index = index;
+    if (it->it_seq != NULL) {
+        if (index < 0)
+            index = 0;
+        else if (index > PyUnicode_GET_LENGTH(it->it_seq))
+            index = PyUnicode_GET_LENGTH(it->it_seq); /* iterator truncated */
+        it->it_index = index;
+    }
     Py_RETURN_NONE;
 }
 
diff --git a/PC/winreg.c b/PC/winreg.c
index 563a3eb..d23810b 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -943,8 +943,10 @@
 
                 fixupMultiSZ(str, data, len);
                 obData = PyList_New(s);
-                if (obData == NULL)
+                if (obData == NULL) {
+                    PyMem_Free(str);
                     return NULL;
+                }
                 for (index = 0; index < s; index++)
                 {
                     size_t len = wcslen(str[index]);
@@ -952,6 +954,7 @@
                         PyErr_SetString(PyExc_OverflowError,
                             "registry string is too long for a Python string");
                         Py_DECREF(obData);
+                        PyMem_Free(str);
                         return NULL;
                     }
                     PyList_SetItem(obData,
diff --git a/PCbuild/pywlauncher.vcxproj b/PCbuild/pywlauncher.vcxproj
index 08e35a7..96ea421 100644
--- a/PCbuild/pywlauncher.vcxproj
+++ b/PCbuild/pywlauncher.vcxproj
@@ -240,6 +240,11 @@
   <ItemGroup>
     <ResourceCompile Include="..\PC\pylauncher.rc" />
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="make_versioninfo.vcxproj">
+      <Project>{f0e0541e-f17d-430b-97c4-93adf0dd284e}</Project>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index 80e432a..d6086e6 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -1203,10 +1203,14 @@
 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
 {
     mod_ty res;
-    PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
-                            (PyObject*)Interactive_type};
+    PyObject *req_type[3];
     char *req_name[] = {"Module", "Expression", "Interactive"};
     int isinstance;
+
+    req_type[0] = (PyObject*)Module_type;
+    req_type[1] = (PyObject*)Expression_type;
+    req_type[2] = (PyObject*)Interactive_type;
+
     assert(0 <= mode && mode <= 2);
 
     if (!init_types())
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index c32a3bf..7283058 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -498,9 +498,13 @@
 
     fd = fileno(tok->fp);
     /* Due to buffering the file offset for fd can be different from the file
-     * position of tok->fp. */
+     * position of tok->fp.  If tok->fp was opened in text mode on Windows,
+     * its file position counts CRLF as one char and can't be directly mapped
+     * to the file offset for fd.  Instead we step back one byte and read to
+     * the end of line.*/
     pos = ftell(tok->fp);
-    if (pos == -1 || lseek(fd, (off_t)pos, SEEK_SET) == (off_t)-1) {
+    if (pos == -1 ||
+        lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
         PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
         goto cleanup;
     }
@@ -513,6 +517,12 @@
     Py_XDECREF(tok->decoding_readline);
     readline = _PyObject_GetAttrId(stream, &PyId_readline);
     tok->decoding_readline = readline;
+    if (pos > 0) {
+        if (PyObject_CallObject(readline, NULL) == NULL) {
+            readline = NULL;
+            goto cleanup;
+        }
+    }
 
   cleanup:
     Py_XDECREF(stream);
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index e07a93f..44fdafc 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -7023,10 +7023,14 @@
 mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
 {
     mod_ty res;
-    PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
-                            (PyObject*)Interactive_type};
+    PyObject *req_type[3];
     char *req_name[] = {"Module", "Expression", "Interactive"};
     int isinstance;
+
+    req_type[0] = (PyObject*)Module_type;
+    req_type[1] = (PyObject*)Expression_type;
+    req_type[2] = (PyObject*)Interactive_type;
+
     assert(0 <= mode && mode <= 2);
 
     if (!init_types())
diff --git a/Python/getargs.c b/Python/getargs.c
index bfea111..946faf2 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -200,8 +200,6 @@
 {
     char msgbuf[256];
     int levels[32];
-    freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
-    freelist_t freelist = {static_entries, 0, 0};
     const char *fname = NULL;
     const char *message = NULL;
     int min = -1;
@@ -212,6 +210,12 @@
     Py_ssize_t i, len;
     char *msg;
     int compat = flags & FLAG_COMPAT;
+    freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
+    freelist_t freelist;
+
+    freelist.entries = static_entries;
+    freelist.first_available = 0;
+    freelist.entries_malloced = 0;
 
     assert(compat || (args != (PyObject*)NULL));
     flags = flags & ~FLAG_COMPAT;
@@ -1439,7 +1443,11 @@
     Py_ssize_t nargs, nkeywords;
     PyObject *current_arg;
     freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
-    freelist_t freelist = {static_entries, 0, 0};
+    freelist_t freelist;
+
+    freelist.entries = static_entries;
+    freelist.first_available = 0;
+    freelist.entries_malloced = 0;
 
     assert(args != NULL && PyTuple_Check(args));
     assert(keywords == NULL || PyDict_Check(keywords));
diff --git a/Python/pytime.c b/Python/pytime.c
index beeab87..de6a41f 100644
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -152,7 +152,7 @@
 
 static int
 _PyTime_ObjectToDenominator(PyObject *obj, time_t *sec, long *numerator,
-                            double denominator)
+                            double denominator, _PyTime_round_t round)
 {
     assert(denominator <= LONG_MAX);
     if (PyFloat_Check(obj)) {
@@ -167,6 +167,20 @@
             intpart -= 1.0;
         }
 
+        floatpart *= denominator;
+        if (round == _PyTime_ROUND_UP) {
+            if (intpart >= 0) {
+                floatpart = ceil(floatpart);
+                if (floatpart >= denominator) {
+                    floatpart = 0.0;
+                    intpart += 1.0;
+                }
+            }
+            else {
+                floatpart = floor(floatpart);
+            }
+        }
+
         *sec = (time_t)intpart;
         err = intpart - (double)*sec;
         if (err <= -1.0 || err >= 1.0) {
@@ -174,7 +188,6 @@
             return -1;
         }
 
-        floatpart *= denominator;
         *numerator = (long)floatpart;
         return 0;
     }
@@ -188,12 +201,18 @@
 }
 
 int
-_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec)
+_PyTime_ObjectToTime_t(PyObject *obj, time_t *sec, _PyTime_round_t round)
 {
     if (PyFloat_Check(obj)) {
         double d, intpart, err;
 
         d = PyFloat_AsDouble(obj);
+        if (round == _PyTime_ROUND_UP) {
+            if (d >= 0)
+                d = ceil(d);
+            else
+                d = floor(d);
+        }
         (void)modf(d, &intpart);
 
         *sec = (time_t)intpart;
@@ -213,15 +232,17 @@
 }
 
 int
-_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec)
+_PyTime_ObjectToTimespec(PyObject *obj, time_t *sec, long *nsec,
+                         _PyTime_round_t round)
 {
-    return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9);
+    return _PyTime_ObjectToDenominator(obj, sec, nsec, 1e9, round);
 }
 
 int
-_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec)
+_PyTime_ObjectToTimeval(PyObject *obj, time_t *sec, long *usec,
+                        _PyTime_round_t round)
 {
-    return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6);
+    return _PyTime_ObjectToDenominator(obj, sec, usec, 1e6, round);
 }
 
 void
diff --git a/Tools/buildbot/test-amd64.bat b/Tools/buildbot/test-amd64.bat
index 1bf124c..de64f25 100644
--- a/Tools/buildbot/test-amd64.bat
+++ b/Tools/buildbot/test-amd64.bat
@@ -1,3 +1,3 @@
 @rem Used by the buildbot "test" step.
 cd PCbuild
-call rt.bat -d -q -x64 -uall -rwW -n %1 %2 %3 %4 %5 %6 %7 %8 %9
+call rt.bat -d -q -x64 -uall -rwW -n --timeout=3600 %1 %2 %3 %4 %5 %6 %7 %8 %9
diff --git a/Tools/hg/hgtouch.py b/Tools/hg/hgtouch.py
index cd990b3..119d812 100644
--- a/Tools/hg/hgtouch.py
+++ b/Tools/hg/hgtouch.py
@@ -125,6 +125,6 @@
 
 cmdtable = {
     "touch": (touch,
-              [('b', 'basedir', '', 'base dir of the tree to apply touching', 'BASEDIR')],
+              [('b', 'basedir', '', 'base dir of the tree to apply touching')],
               "hg touch [-b BASEDIR]")
 }
diff --git a/Tools/scripts/run_tests.py b/Tools/scripts/run_tests.py
index e2a2050..a6c5da3 100644
--- a/Tools/scripts/run_tests.py
+++ b/Tools/scripts/run_tests.py
@@ -32,6 +32,12 @@
             ]
     # Allow user-specified interpreter options to override our defaults.
     args.extend(test.support.args_from_interpreter_flags())
+
+    # Workaround for issue #20355
+    os.environ.pop("PYTHONWARNINGS", None)
+    # Workaround for issue #20361
+    args.extend(['-W', 'error::BytesWarning'])
+
     args.extend(['-m', 'test',    # Run the test suite
                  '-r',            # Randomize test order
                  '-w',            # Re-run failed tests in verbose mode
diff --git a/setup.py b/setup.py
index 8269e1c..26db763 100644
--- a/setup.py
+++ b/setup.py
@@ -1138,7 +1138,6 @@
                                   define_macros=sqlite_defines,
                                   include_dirs=include_dirs,
                                   library_dirs=sqlite_libdir,
-                                  runtime_library_dirs=sqlite_libdir,
                                   extra_link_args=sqlite_extra_link_args,
                                   libraries=["sqlite3",]))
         else: