Merge 3.6.0 release engineering head
diff --git a/.hgtags b/.hgtags
index 7b8ad93..8df6239 100644
--- a/.hgtags
+++ b/.hgtags
@@ -175,4 +175,5 @@
 8345e066c0ed713c3e510cbc8fafc1c38d6d306b v3.6.0b3
 18496abdb3d5c2730a659b747a89261b2219fecf v3.6.0b4
 29a273eee9a523ee178f6a66c4ac9d317c8fc84f v3.6.0rc1
+800a67f7806de45a7abd5273359e704bf147c079 v3.6.0rc2
 41df79263a11f2429d1dd0cfe12553de3dcb5508 v3.6.0
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index 323f017..ac6fd0b 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -199,8 +199,9 @@
 
    This field is deprecated.  When it is defined, it should point to a function
    that acts the same as the :c:member:`~PyTypeObject.tp_getattro` function, but taking a C string
-   instead of a Python string object to give the attribute name.  The signature is
-   the same as for :c:func:`PyObject_GetAttrString`.
+   instead of a Python string object to give the attribute name.  The signature is ::
+
+      PyObject * tp_getattr(PyObject *o, char *attr_name);
 
    This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_getattro`: a subtype
    inherits both :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` from its base type when
@@ -213,10 +214,11 @@
 
    This field is deprecated.  When it is defined, it should point to a function
    that acts the same as the :c:member:`~PyTypeObject.tp_setattro` function, but taking a C string
-   instead of a Python string object to give the attribute name.  The signature is
-   the same as for :c:func:`PyObject_SetAttrString`, but setting
-   *v* to *NULL* to delete an attribute must be supported.
+   instead of a Python string object to give the attribute name.  The signature is ::
 
+      PyObject * tp_setattr(PyObject *o, char *attr_name, PyObject *v);
+
+   The *v* argument is set to *NULL* to delete the attribute.
    This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_setattro`: a subtype
    inherits both :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` from its base type when
    the subtype's :c:member:`~PyTypeObject.tp_setattr` and :c:member:`~PyTypeObject.tp_setattro` are both *NULL*.
diff --git a/Doc/c-api/unicode.rst b/Doc/c-api/unicode.rst
index c11e049..b31d689 100644
--- a/Doc/c-api/unicode.rst
+++ b/Doc/c-api/unicode.rst
@@ -1668,10 +1668,6 @@
    * :const:`Py_True` or :const:`Py_False` for successful comparisons
    * :const:`Py_NotImplemented` in case the type combination is unknown
 
-   Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a
-   :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails
-   with a :exc:`UnicodeDecodeError`.
-
    Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`,
    :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`.
 
diff --git a/Doc/howto/clinic.rst b/Doc/howto/clinic.rst
index 4742d84..eaab20a 100644
--- a/Doc/howto/clinic.rst
+++ b/Doc/howto/clinic.rst
@@ -1,3 +1,5 @@
+.. highlightlang:: c
+
 **********************
 Argument Clinic How-To
 **********************
@@ -78,17 +80,23 @@
 ========================
 
 Argument Clinic ships with CPython; you'll find it in ``Tools/clinic/clinic.py``.
-If you run that script, specifying a C file as an argument::
+If you run that script, specifying a C file as an argument:
 
-    % python3 Tools/clinic/clinic.py foo.c
+.. code-block:: shell-session
+
+    $ python3 Tools/clinic/clinic.py foo.c
 
 Argument Clinic will scan over the file looking for lines that
-look exactly like this::
+look exactly like this:
+
+.. code-block:: none
 
     /*[clinic input]
 
 When it finds one, it reads everything up to a line that looks
-exactly like this::
+exactly like this:
+
+.. code-block:: none
 
     [clinic start generated code]*/
 
@@ -99,7 +107,9 @@
 When Argument Clinic parses one of these blocks, it
 generates output.  This output is rewritten into the C file
 immediately after the block, followed by a comment containing a checksum.
-The Argument Clinic block now looks like this::
+The Argument Clinic block now looks like this:
+
+.. code-block:: none
 
     /*[clinic input]
     ... clinic input goes here ...
@@ -375,15 +385,10 @@
         Write a pickled representation of obj to the open file.
         [clinic start generated code]*/
 
-12. Save and close the file, then run ``Tools/clinic/clinic.py`` on it.
-    With luck everything worked and your block now has output!  Reopen
-    the file in your text editor to see::
-
-       /*[clinic input]
-       module _pickle
-       class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
-       [clinic start generated code]*/
-       /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
+12. Save and close the file, then run ``Tools/clinic/clinic.py`` on
+    it.  With luck everything worked---your block now has output, and
+    a ``.c.h`` file has been generated! Reopen the file in your
+    text editor to see::
 
        /*[clinic input]
        _pickle.Pickler.dump
@@ -395,18 +400,20 @@
        Write a pickled representation of obj to the open file.
        [clinic start generated code]*/
 
-       PyDoc_STRVAR(_pickle_Pickler_dump__doc__,
-       "Write a pickled representation of obj to the open file.\n"
-       "\n"
-       ...
        static PyObject *
-       _pickle_Pickler_dump_impl(PicklerObject *self, PyObject *obj)
-       /*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/
+       _pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
+       /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
 
     Obviously, if Argument Clinic didn't produce any output, it's because
     it found an error in your input.  Keep fixing your errors and retrying
     until Argument Clinic processes your file without complaint.
 
+    For readability, most of the glue code has been generated to a ``.c.h``
+    file.  You'll need to include that in your original ``.c`` file,
+    typically right after the clinic module block::
+
+       #include "clinic/_pickle.c.h"
+
 13. Double-check that the argument-parsing code Argument Clinic generated
     looks basically the same as the existing code.
 
@@ -1027,7 +1034,9 @@
 value), then the generated code will propagate the error.  Otherwise it will
 encode the value you return like normal.
 
-Currently Argument Clinic supports only a few return converters::
+Currently Argument Clinic supports only a few return converters:
+
+.. code-block:: none
 
     bool
     int
@@ -1606,7 +1615,9 @@
     #endif /* HAVE_FUNCTIONNAME */
 
 And then in the ``PyMethodDef`` structure at the bottom the existing code
-will have::
+will have:
+
+.. code-block:: none
 
     #ifdef HAVE_FUNCTIONNAME
     {'functionname', ... },
@@ -1656,7 +1667,9 @@
 because that could be deactivated by the ``#ifdef``.  (That's the whole point!)
 
 In this situation, Argument Clinic writes the extra code to the "buffer" destination.
-This may mean that you get a complaint from Argument Clinic::
+This may mean that you get a complaint from Argument Clinic:
+
+.. code-block:: none
 
     Warning in file "Modules/posixmodule.c" on line 12357:
     Destination buffer 'buffer' not empty at end of file, emptying.
@@ -1676,7 +1689,9 @@
 to run Python blocks lets you use Python as a Python preprocessor!
 
 Since Python comments are different from C comments, Argument Clinic
-blocks embedded in Python files look slightly different.  They look like this::
+blocks embedded in Python files look slightly different.  They look like this:
+
+.. code-block:: python3
 
     #/*[python input]
     #print("def foo(): pass")
diff --git a/Doc/howto/pyporting.rst b/Doc/howto/pyporting.rst
index 696f8e7..8562d23 100644
--- a/Doc/howto/pyporting.rst
+++ b/Doc/howto/pyporting.rst
@@ -17,7 +17,8 @@
    please see :ref:`cporting-howto`.
 
    If you would like to read one core Python developer's take on why Python 3
-   came into existence, you can read Nick Coghlan's `Python 3 Q & A`_.
+   came into existence, you can read Nick Coghlan's `Python 3 Q & A`_ or
+   Brett Cannon's `Why Python 3 exists`_.
 
    For help with porting, you can email the python-porting_ mailing list with
    questions.
@@ -32,8 +33,7 @@
 #. Make sure you have good test coverage (coverage.py_ can help;
    ``pip install coverage``)
 #. Learn the differences between Python 2 & 3
-#. Use Modernize_ or Futurize_ to update your code (``pip install modernize`` or
-   ``pip install future``, respectively)
+#. Use Futurize_ (or Modernize_) to update your code (e.g. ``pip install future``)
 #. Use Pylint_ to help make sure you don't regress on your Python 3 support
    (``pip install pylint``)
 #. Use caniusepython3_ to find out which of your dependencies are blocking your
@@ -41,10 +41,9 @@
 #. Once your dependencies are no longer blocking you, use continuous integration
    to make sure you stay compatible with Python 2 & 3 (tox_ can help test
    against multiple versions of Python; ``pip install tox``)
-
-If you are dropping support for Python 2 entirely, then after you learn the
-differences between Python 2 & 3 you can run 2to3_ over your code and skip the
-rest of the steps outlined above.
+#. Consider using optional static type checking to make sure your type usage
+   works in both Python 2 & 3 (e.g. use mypy_ to check your typing under both
+   Python 2 & Python 3).
 
 
 Details
@@ -54,7 +53,7 @@
 **today**! Even if your dependencies are not supporting Python 3 yet that does
 not mean you can't modernize your code **now** to support Python 3. Most changes
 required to support Python 3 lead to cleaner code using newer practices even in
-Python 2.
+Python 2 code.
 
 Another key point is that modernizing your Python 2 code to also support
 Python 3 is largely automated for you. While you might have to make some API
@@ -82,12 +81,13 @@
 overall transformation should not feel foreign to you.
 
 But you should aim for only supporting Python 2.7. Python 2.6 is no longer
-supported and thus is not receiving bugfixes. This means **you** will have to
-work around any issues you come across with Python 2.6. There are also some
+freely supported and thus is not receiving bugfixes. This means **you** will have
+to work around any issues you come across with Python 2.6. There are also some
 tools mentioned in this HOWTO which do not support Python 2.6 (e.g., Pylint_),
 and this will become more commonplace as time goes on. It will simply be easier
 for you if you only support the versions of Python that you have to support.
 
+
 Make sure you specify the proper version support in your ``setup.py`` file
 --------------------------------------------------------------------------
 
@@ -98,6 +98,7 @@
 also specify each major/minor version of Python that you do support, e.g.
 ``Programming Language :: Python :: 2.7``.
 
+
 Have good test coverage
 -----------------------
 
@@ -106,9 +107,10 @@
 thumb is that if you want to be confident enough in your test suite that any
 failures that appear after having tools rewrite your code are actual bugs in the
 tools and not in your code. If you want a number to aim for, try to get over 80%
-coverage (and don't feel bad if you can't easily get past 90%). If you
-don't already have a tool to measure test coverage then coverage.py_ is
-recommended.
+coverage (and don't feel bad if you find it hard to get better than 90%
+coverage). If you don't already have a tool to measure test coverage then
+coverage.py_ is recommended.
+
 
 Learn the differences between Python 2 & 3
 -------------------------------------------
@@ -127,13 +129,15 @@
 
 Once you feel like you know what is different in Python 3 compared to Python 2,
 it's time to update your code! You have a choice between two tools in porting
-your code automatically: Modernize_ and Futurize_. Which tool you choose will
+your code automatically: Futurize_ and Modernize_. Which tool you choose will
 depend on how much like Python 3 you want your code to be. Futurize_ does its
 best to make Python 3 idioms and practices exist in Python 2, e.g. backporting
 the ``bytes`` type from Python 3 so that you have semantic parity between the
 major versions of Python. Modernize_,
 on the other hand, is more conservative and targets a Python 2/3 subset of
-Python, relying on six_ to help provide compatibility.
+Python, directly relying on six_ to help provide compatibility. As Python 3 is
+the future, it might be best to consider Futurize to begin adjusting to any new
+practices that Python 3 introduces which you are not accustomed to yet.
 
 Regardless of which tool you choose, they will update your code to run under
 Python 3 while staying compatible with the version of Python 2 you started with.
@@ -153,6 +157,7 @@
 though, there are only a couple of things to watch out for which can be
 considered large issues that may be hard to debug if not watched for.
 
+
 Division
 ++++++++
 
@@ -173,6 +178,7 @@
 code would begin to fail (e.g. a user-defined class that uses ``/`` to
 signify some operation but not ``//`` for the same thing or at all).
 
+
 Text versus binary data
 +++++++++++++++++++++++
 
@@ -189,7 +195,7 @@
 pronounced, Python 3 did what most languages created in the age of the internet
 have done and made text and binary data distinct types that cannot blindly be
 mixed together (Python predates widespread access to the internet). For any code
-that only deals with text or only binary data, this separation doesn't pose an
+that deals only with text or only binary data, this separation doesn't pose an
 issue. But for code that has to deal with both, it does mean you might have to
 now care about when you are using text compared to binary data, which is why
 this cannot be entirely automated.
@@ -198,15 +204,15 @@
 (it is **highly** recommended you don't design APIs that can take both due to
 the difficulty of keeping the code working; as stated earlier it is difficult to
 do well). In Python 2 this means making sure the APIs that take text can work
-with ``unicode`` in Python 2 and those that work with binary data work with the
-``bytes`` type from Python 3 and thus a subset of ``str`` in Python 2 (which the
-``bytes`` type in Python 2 is an alias for). Usually the biggest issue is
-realizing which methods exist for which types in Python 2 & 3 simultaneously
+with ``unicode`` and those that work with binary data work with the
+``bytes`` type from Python 3 (which is a subset of ``str`` in Python 2 and acts
+as an alias for ``bytes`` type in Python 2). Usually the biggest issue is
+realizing which methods exist on which types in Python 2 & 3 simultaneously
 (for text that's ``unicode`` in Python 2 and ``str`` in Python 3, for binary
 that's ``str``/``bytes`` in Python 2 and ``bytes`` in Python 3). The following
 table lists the **unique** methods of each data type across Python 2 & 3
 (e.g., the ``decode()`` method is usable on the equivalent binary data type in
-either Python 2 or 3, but it can't be used by the text data type consistently
+either Python 2 or 3, but it can't be used by the textual data type consistently
 between Python 2 and 3 because ``str`` in Python 3 doesn't have the method). Do
 note that as of Python 3.5 the ``__mod__`` method was added to the bytes type.
 
@@ -232,10 +238,11 @@
 having to keep track of what type of data you are working with.
 
 The next issue is making sure you know whether the string literals in your code
-represent text or binary data. At minimum you should add a ``b`` prefix to any
-literal that presents binary data. For text you should either use the
-``from __future__ import unicode_literals`` statement or add a ``u`` prefix to
-the text literal.
+represent text or binary data. You should add a ``b`` prefix to any
+literal that presents binary data. For text you should add a ``u`` prefix to
+the text literal. (there is a :mod:`__future__` import to force all unspecified
+literals to be Unicode, but usage has shown it isn't as effective as adding a
+``b`` or ``u`` prefix to all literals explicitly)
 
 As part of this dichotomy you also need to be careful about opening files.
 Unless you have been working on Windows, there is a chance you have not always
@@ -243,11 +250,13 @@
 binary reading).  Under Python 3, binary files and text files are clearly
 distinct and mutually incompatible; see the :mod:`io` module for details.
 Therefore, you **must** make a decision of whether a file will be used for
-binary access (allowing binary data to be read and/or written) or text access
+binary access (allowing binary data to be read and/or written) or textual access
 (allowing text data to be read and/or written). You should also use :func:`io.open`
 for opening files instead of the built-in :func:`open` function as the :mod:`io`
 module is consistent from Python 2 to 3 while the built-in :func:`open` function
-is not (in Python 3 it's actually :func:`io.open`).
+is not (in Python 3 it's actually :func:`io.open`). Do not bother with the
+outdated practice of using :func:`codecs.open` as that's only necessary for
+keeping compatibility with Python 2.5.
 
 The constructors of both ``str`` and ``bytes`` have different semantics for the
 same arguments between Python 2 & 3. Passing an integer to ``bytes`` in Python 2
@@ -274,21 +283,22 @@
 #. Make sure that your code that works with text also works with ``unicode`` and
    code for binary data works with ``bytes`` in Python 2 (see the table above
    for what methods you cannot use for each type)
-#. Mark all binary literals with a ``b`` prefix, use a ``u`` prefix or
-   :mod:`__future__` import statement for text literals
+#. Mark all binary literals with a ``b`` prefix, textual literals with a ``u``
+   prefix
 #. Decode binary data to text as soon as possible, encode text as binary data as
    late as possible
 #. Open files using :func:`io.open` and make sure to specify the ``b`` mode when
    appropriate
-#. Be careful when indexing binary data
+#. Be careful when indexing into binary data
 
 
 Use feature detection instead of version detection
 ++++++++++++++++++++++++++++++++++++++++++++++++++
+
 Inevitably you will have code that has to choose what to do based on what
 version of Python is running. The best way to do this is with feature detection
 of whether the version of Python you're running under supports what you need.
-If for some reason that doesn't work then you should make the version check is
+If for some reason that doesn't work then you should make the version check be
 against Python 2 and not Python 3. To help explain this, let's look at an
 example.
 
@@ -340,14 +350,12 @@
     from __future__ import absolute_import
     from __future__ import division
     from __future__ import print_function
-    from __future__ import unicode_literals
 
 You can also run Python 2 with the ``-3`` flag to be warned about various
 compatibility issues your code triggers during execution. If you turn warnings
 into errors with ``-Werror`` then you can make sure that you don't accidentally
 miss a warning.
 
-
 You can also use the Pylint_ project and its ``--py3k`` flag to lint your code
 to receive warnings when your code begins to deviate from Python 3
 compatibility. This also prevents you from having to run Modernize_ or Futurize_
@@ -364,22 +372,23 @@
 project was created to help you determine which projects
 -- directly or indirectly -- are blocking you from supporting Python 3. There
 is both a command-line tool as well as a web interface at
-https://caniusepython3.com .
+https://caniusepython3.com.
 
 The project also provides code which you can integrate into your test suite so
 that you will have a failing test when you no longer have dependencies blocking
 you from using Python 3. This allows you to avoid having to manually check your
 dependencies and to be notified quickly when you can start running on Python 3.
 
+
 Update your ``setup.py`` file to denote Python 3 compatibility
 --------------------------------------------------------------
 
 Once your code works under Python 3, you should update the classifiers in
 your ``setup.py`` to contain ``Programming Language :: Python :: 3`` and to not
-specify sole Python 2 support. This will tell
-anyone using your code that you support Python 2 **and** 3. Ideally you will
-also want to add classifiers for each major/minor version of Python you now
-support.
+specify sole Python 2 support. This will tell anyone using your code that you
+support Python 2 **and** 3. Ideally you will also want to add classifiers for
+each major/minor version of Python you now support.
+
 
 Use continuous integration to stay compatible
 ---------------------------------------------
@@ -404,20 +413,17 @@
 you typically run your tests under while developing.
 
 
-Dropping Python 2 support completely
-====================================
+Consider using optional static type checking
+--------------------------------------------
 
-If you are able to fully drop support for Python 2, then the steps required
-to transition to Python 3 simplify greatly.
-
-#. Update your code to only support Python 2.7
-#. Make sure you have good test coverage (coverage.py_ can help)
-#. Learn the differences between Python 2 & 3
-#. Use 2to3_ to rewrite your code to run only under Python 3
-
-After this your code will be fully Python 3 compliant but in a way that is not
-supported by Python 2. You should also update the classifiers in your
-``setup.py`` to contain ``Programming Language :: Python :: 3 :: Only``.
+Another way to help port your code is to use a static type checker like
+mypy_ or pytype_ on your code. These tools can be used to analyze your code as
+if it's being run under Python 2, then you can run the tool a second time as if
+your code is running under Python 3. By running a static type checker twice like
+this you can discover if you're e.g. misusing binary data type in one version
+of Python compared to another. If you add optional type hints to your code you
+can also explicitly state whether your APIs use textual or binary data, helping
+to make sure everything functions as expected in both versions of Python.
 
 
 .. _2to3: https://docs.python.org/3/library/2to3.html
@@ -428,13 +434,19 @@
 .. _importlib: https://docs.python.org/3/library/importlib.html#module-importlib
 .. _importlib2: https://pypi.python.org/pypi/importlib2
 .. _Modernize: https://python-modernize.readthedocs.org/en/latest/
+.. _mypy: http://mypy-lang.org/
 .. _Porting to Python 3: http://python3porting.com/
 .. _Pylint: https://pypi.python.org/pypi/pylint
+
 .. _Python 3 Q & A: https://ncoghlan-devs-python-notes.readthedocs.org/en/latest/python3/questions_and_answers.html
 
+.. _pytype: https://github.com/google/pytype
 .. _python-future: http://python-future.org/
 .. _python-porting: https://mail.python.org/mailman/listinfo/python-porting
 .. _six: https://pypi.python.org/pypi/six
 .. _tox: https://pypi.python.org/pypi/tox
 .. _trove classifier: https://pypi.python.org/pypi?%3Aaction=list_classifiers
+
 .. _"What's New": https://docs.python.org/3/whatsnew/index.html
+
+.. _Why Python 3 exists: http://www.snarky.ca/why-python-3-exists
diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst
index 712b4b7..18b5c65 100644
--- a/Doc/howto/urllib2.rst
+++ b/Doc/howto/urllib2.rst
@@ -578,7 +578,7 @@
 This document was reviewed and revised by John Lee.
 
 .. [#] Google for example.
-.. [#] Browser sniffing is a very bad practise for website design - building
+.. [#] Browser sniffing is a very bad practice for website design - building
        sites using web standards is much more sensible. Unfortunately a lot of
        sites still send different versions to different browsers.
 .. [#] The user agent for MSIE 6 is
diff --git a/Doc/library/pkgutil.rst b/Doc/library/pkgutil.rst
index 90d69e4..fba0ea6 100644
--- a/Doc/library/pkgutil.rst
+++ b/Doc/library/pkgutil.rst
@@ -224,4 +224,6 @@
 
    If the package cannot be located or loaded, or it uses a :term:`loader`
    which does not support :meth:`get_data <importlib.abc.ResourceLoader.get_data>`,
-   then ``None`` is returned.
+   then ``None`` is returned.  In particular, the :term:`loader` for
+   :term:`namespace packages <namespace package>` does not support
+   :meth:`get_data <importlib.abc.ResourceLoader.get_data>`.
diff --git a/Doc/library/re.rst b/Doc/library/re.rst
index a298d97..7ef4cbe 100644
--- a/Doc/library/re.rst
+++ b/Doc/library/re.rst
@@ -762,7 +762,7 @@
       now are errors.
 
    .. deprecated-removed:: 3.5 3.7
-      Unknown escapes in *repl* consist of ``'\'`` and ASCII letter now raise
+      Unknown escapes in *repl* consisting of ``'\'`` and an ASCII letter now raise
       a deprecation warning and will be forbidden in Python 3.7.
 
 
diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
index f559ca0..3451597 100644
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -458,6 +458,10 @@
 
 .. versionadded:: 3.2
 
+.. versionchanged:: 3.5
+    Added support for the *xztar* format.
+
+
 High-level utilities to create and read compressed and archived files are also
 provided.  They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
 
@@ -467,8 +471,9 @@
 
    *base_name* is the name of the file to create, including the path, minus
    any format-specific extension. *format* is the archive format: one of
-   "zip", "tar", "bztar" (if the :mod:`bz2` module is available), "xztar"
-   (if the :mod:`lzma` module is available) or "gztar".
+   "zip" (if the :mod:`zlib` module is available), "tar", "gztar" (if the
+   :mod:`zlib` module is available), "bztar" (if the :mod:`bz2` module is
+   available), or "xztar" (if the :mod:`lzma` module is available).
 
    *root_dir* is a directory that will be the root directory of the
    archive; for example, we typically chdir into *root_dir* before creating the
@@ -491,9 +496,6 @@
 
    The *verbose* argument is unused and deprecated.
 
-   .. versionchanged:: 3.5
-      Added support for the *xztar* format.
-
 
 .. function:: get_archive_formats()
 
@@ -502,11 +504,11 @@
 
    By default :mod:`shutil` provides these formats:
 
-   - *gztar*: gzip'ed tar-file
-   - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
-   - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available.)
-   - *tar*: uncompressed tar file
-   - *zip*: ZIP file
+   - *zip*: ZIP file (if the :mod:`zlib` module is available).
+   - *tar*: uncompressed tar file.
+   - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
+   - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
+   - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
 
    You can register new formats or provide your own archiver for any existing
    formats, by using :func:`register_archive_format`.
@@ -541,11 +543,12 @@
    *extract_dir* is the name of the target directory where the archive is
    unpacked. If not provided, the current working directory is used.
 
-   *format* is the archive format: one of "zip", "tar", or "gztar". Or any
-   other format registered with :func:`register_unpack_format`. If not
-   provided, :func:`unpack_archive` will use the archive file name extension
-   and see if an unpacker was registered for that extension. In case none is
-   found, a :exc:`ValueError` is raised.
+   *format* is the archive format: one of "zip", "tar", "gztar", "bztar", or
+   "xztar".  Or any other format registered with
+   :func:`register_unpack_format`.  If not provided, :func:`unpack_archive`
+   will use the archive file name extension and see if an unpacker was
+   registered for that extension.  In case none is found,
+   a :exc:`ValueError` is raised.
 
 
 .. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])
@@ -578,11 +581,12 @@
 
    By default :mod:`shutil` provides these formats:
 
-   - *gztar*: gzip'ed tar-file
-   - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available.)
-   - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available.)
-   - *tar*: uncompressed tar file
-   - *zip*: ZIP file
+   - *zip*: ZIP file (unpacking compressed files works only if the corresponding
+     module is available).
+   - *tar*: uncompressed tar file.
+   - *gztar*: gzip'ed tar-file (if the :mod:`zlib` module is available).
+   - *bztar*: bzip2'ed tar-file (if the :mod:`bz2` module is available).
+   - *xztar*: xz'ed tar-file (if the :mod:`lzma` module is available).
 
    You can register new formats or provide your own unpacker for any existing
    formats, by using :func:`register_unpack_format`.
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 122ed00..1e665af 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -1644,18 +1644,20 @@
 
    Return true if all characters in the string are decimal
    characters and there is at least one character, false
-   otherwise. Decimal characters are those from general category "Nd". This category
-   includes digit characters, and all characters
-   that can be used to form decimal-radix numbers, e.g. U+0660,
-   ARABIC-INDIC DIGIT ZERO.
+   otherwise. Decimal characters are those that can be used to form
+   numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT
+   ZERO.  Formally a decimal character is a character in the Unicode
+   General Category "Nd".
 
 
 .. method:: str.isdigit()
 
    Return true if all characters in the string are digits and there is at least one
    character, false otherwise.  Digits include decimal characters and digits that need
-   special handling, such as the compatibility superscript digits.  Formally, a digit
-   is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.
+   special handling, such as the compatibility superscript digits.
+   This covers digits which cannot be used to form numbers in base 10,
+   like the Kharosthi numbers.  Formally, a digit is a character that has the
+   property value Numeric_Type=Digit or Numeric_Type=Decimal.
 
 
 .. method:: str.isidentifier()
@@ -2158,7 +2160,7 @@
 +------------+-----------------------------------------------------+-------+
 | ``'o'``    | Signed octal value.                                 | \(1)  |
 +------------+-----------------------------------------------------+-------+
-| ``'u'``    | Obsolete type -- it is identical to ``'d'``.        | \(7)  |
+| ``'u'``    | Obsolete type -- it is identical to ``'d'``.        | \(6)  |
 +------------+-----------------------------------------------------+-------+
 | ``'x'``    | Signed hexadecimal (lowercase).                     | \(2)  |
 +------------+-----------------------------------------------------+-------+
@@ -2199,15 +2201,12 @@
 Notes:
 
 (1)
-   The alternate form causes a leading zero (``'0'``) to be inserted between
-   left-hand padding and the formatting of the number if the leading character
-   of the result is not already a zero.
+   The alternate form causes a leading octal specifier (``'0o'``) to be
+   inserted before the first digit.
 
 (2)
    The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
-   the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding
-   and the formatting of the number if the leading character of the result is not
-   already a zero.
+   the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit.
 
 (3)
    The alternate form causes the result to always contain a decimal point, even if
@@ -2226,8 +2225,7 @@
 (5)
    If precision is ``N``, the output is truncated to ``N`` characters.
 
-
-(7)
+(6)
    See :pep:`237`.
 
 Since Python strings have an explicit length, ``%s`` conversions do not assume
@@ -3303,15 +3301,12 @@
 Notes:
 
 (1)
-   The alternate form causes a leading zero (``'0'``) to be inserted between
-   left-hand padding and the formatting of the number if the leading character
-   of the result is not already a zero.
+   The alternate form causes a leading octal specifier (``'0o'``) to be
+   inserted before the first digit.
 
 (2)
    The alternate form causes a leading ``'0x'`` or ``'0X'`` (depending on whether
-   the ``'x'`` or ``'X'`` format was used) to be inserted between left-hand padding
-   and the formatting of the number if the leading character of the result is not
-   already a zero.
+   the ``'x'`` or ``'X'`` format was used) to be inserted before the first digit.
 
 (3)
    The alternate form causes the result to always contain a decimal point, even if
diff --git a/Doc/reference/datamodel.rst b/Doc/reference/datamodel.rst
index 82e35e5..f2a2b12 100644
--- a/Doc/reference/datamodel.rst
+++ b/Doc/reference/datamodel.rst
@@ -1338,11 +1338,14 @@
 
    Called by built-in function :func:`hash` and for operations on members of
    hashed collections including :class:`set`, :class:`frozenset`, and
-   :class:`dict`.  :meth:`__hash__` should return an integer.  The only
-   required property is that objects which compare equal have the same hash
-   value; it is advised to somehow mix together (e.g. using exclusive or) the
-   hash values for the components of the object that also play a part in
-   comparison of objects.
+   :class:`dict`.  :meth:`__hash__` should return an integer. The only required
+   property is that objects which compare equal have the same hash value; it is
+   advised to mix together the hash values of the components of the object that
+   also play a part in comparison of objects by packing them into a tuple and
+   hashing the tuple. Example::
+
+       def __hash__(self):
+           return hash((self.name, self.nick, self.color))
 
    .. note::
 
diff --git a/Doc/tutorial/classes.rst b/Doc/tutorial/classes.rst
index 75c79d2..e134d5d 100644
--- a/Doc/tutorial/classes.rst
+++ b/Doc/tutorial/classes.rst
@@ -374,11 +374,11 @@
 called without any --- even if the argument isn't actually used...
 
 Actually, you may have guessed the answer: the special thing about methods is
-that the object is passed as the first argument of the function.  In our
+that the instance object is passed as the first argument of the function.  In our
 example, the call ``x.f()`` is exactly equivalent to ``MyClass.f(x)``.  In
 general, calling a method with a list of *n* arguments is equivalent to calling
 the corresponding function with an argument list that is created by inserting
-the method's object before the first argument.
+the method's instance object before the first argument.
 
 If you still don't understand how methods work, a look at the implementation can
 perhaps clarify matters.  When an instance attribute is referenced that isn't a
@@ -906,4 +906,3 @@
    namespace; the name :attr:`~object.__dict__` is an attribute but not a global name.
    Obviously, using this violates the abstraction of namespace implementation, and
    should be restricted to things like post-mortem debuggers.
-
diff --git a/Doc/tutorial/modules.rst b/Doc/tutorial/modules.rst
index 35db305..1e3d5c0 100644
--- a/Doc/tutorial/modules.rst
+++ b/Doc/tutorial/modules.rst
@@ -501,7 +501,7 @@
 ``__all__`` is defined.)
 
 Although certain modules are designed to export only names that follow certain
-patterns when you use ``import *``, it is still considered bad practise in
+patterns when you use ``import *``, it is still considered bad practice in
 production code.
 
 Remember, there is nothing wrong with using ``from Package import
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
index c0d8ac4..4cabb60 100644
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -363,7 +363,7 @@
 PEP 487: Descriptor Protocol Enhancements
 -----------------------------------------
 
-:pep:`487` extends the descriptor protocol has to include the new optional
+:pep:`487` extends the descriptor protocol to include the new optional
 :meth:`~object.__set_name__` method.  Whenever a new class is defined, the new
 method will be called on all descriptors included in the definition, providing
 them with a reference to the class being defined and the name given to the
diff --git a/Include/patchlevel.h b/Include/patchlevel.h
index 92a63bd..49930e8 100644
--- a/Include/patchlevel.h
+++ b/Include/patchlevel.h
@@ -23,7 +23,7 @@
 #define PY_RELEASE_SERIAL	0
 
 /* Version as a string */
-#define PY_VERSION      	"3.6.0"
+#define PY_VERSION      	"3.6.0+"
 /*--end constants--*/
 
 /* Version as a single 4-byte hex number, e.g. 0x010502B2 == 1.5.2b2.
diff --git a/Include/unicodeobject.h b/Include/unicodeobject.h
index 8103a63..792d0e8 100644
--- a/Include/unicodeobject.h
+++ b/Include/unicodeobject.h
@@ -2075,10 +2075,6 @@
    - Py_True or Py_False for successful comparisons
    - Py_NotImplemented in case the type combination is unknown
 
-   Note that Py_EQ and Py_NE comparisons can cause a UnicodeWarning in
-   case the conversion of the arguments to Unicode fails with a
-   UnicodeDecodeError.
-
    Possible values for op:
 
      Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index d0947f0..2ebfb05 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -277,7 +277,7 @@
 try:
     UnsupportedOperation = io.UnsupportedOperation
 except AttributeError:
-    class UnsupportedOperation(ValueError, OSError):
+    class UnsupportedOperation(OSError, ValueError):
         pass
 
 
diff --git a/Lib/dbm/dumb.py b/Lib/dbm/dumb.py
index e7c6440..2296dbf 100644
--- a/Lib/dbm/dumb.py
+++ b/Lib/dbm/dumb.py
@@ -97,8 +97,9 @@
         try:
             f = _io.open(self._dirfile, 'r', encoding="Latin-1")
         except OSError:
-            pass
+            self._modified = not self._readonly
         else:
+            self._modified = False
             with f:
                 for line in f:
                     line = line.rstrip()
@@ -113,7 +114,7 @@
         # CAUTION:  It's vital that _commit() succeed, and _commit() can
         # be called from __del__().  Therefore we must never reference a
         # global in this routine.
-        if self._index is None:
+        if self._index is None or not self._modified:
             return  # nothing to do
 
         try:
@@ -197,6 +198,7 @@
         elif not isinstance(val, (bytes, bytearray)):
             raise TypeError("values must be bytes or strings")
         self._verify_open()
+        self._modified = True
         if key not in self._index:
             self._addkey(key, self._addval(val))
         else:
@@ -229,6 +231,7 @@
         if isinstance(key, str):
             key = key.encode('utf-8')
         self._verify_open()
+        self._modified = True
         # The blocks used by the associated value are lost.
         del self._index[key]
         # XXX It's unclear why we do a _commit() here (the code always
diff --git a/Lib/distutils/command/wininst-14.0-amd64.exe b/Lib/distutils/command/wininst-14.0-amd64.exe
index 2229954..253c2e2 100644
--- a/Lib/distutils/command/wininst-14.0-amd64.exe
+++ b/Lib/distutils/command/wininst-14.0-amd64.exe
Binary files differ
diff --git a/Lib/distutils/command/wininst-14.0.exe b/Lib/distutils/command/wininst-14.0.exe
index 0dac110..46f5f35 100644
--- a/Lib/distutils/command/wininst-14.0.exe
+++ b/Lib/distutils/command/wininst-14.0.exe
Binary files differ
diff --git a/Lib/distutils/tests/test_bdist_rpm.py b/Lib/distutils/tests/test_bdist_rpm.py
index d7c9feb..6453a02 100644
--- a/Lib/distutils/tests/test_bdist_rpm.py
+++ b/Lib/distutils/tests/test_bdist_rpm.py
@@ -94,7 +94,7 @@
     @unittest.skipIf(find_executable('rpmbuild') is None,
                      'the rpmbuild command is not found')
     def test_no_optimize_flag(self):
-        # let's create a package that brakes bdist_rpm
+        # let's create a package that breaks bdist_rpm
         tmp_dir = self.mkdtemp()
         os.environ['HOME'] = tmp_dir   # to confine dir '.rpmdb' creation
         pkg_dir = os.path.join(tmp_dir, 'foo')
diff --git a/Lib/functools.py b/Lib/functools.py
index 9845df2..45e5f87 100644
--- a/Lib/functools.py
+++ b/Lib/functools.py
@@ -574,14 +574,16 @@
                     last = root[PREV]
                     link = [last, root, key, result]
                     last[NEXT] = root[PREV] = cache[key] = link
-                    full = (len(cache) >= maxsize)
+                    # Use the __len__() method instead of the len() function
+                    # which could potentially be wrapped in an lru_cache itself.
+                    full = (cache.__len__() >= maxsize)
                 misses += 1
             return result
 
     def cache_info():
         """Report cache statistics"""
         with lock:
-            return _CacheInfo(hits, misses, maxsize, len(cache))
+            return _CacheInfo(hits, misses, maxsize, cache.__len__())
 
     def cache_clear():
         """Clear the cache and cache statistics"""
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py
index ab43446..9feec50 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -1440,6 +1440,4 @@
     _setup(_bootstrap_module)
     supported_loaders = _get_supported_file_loaders()
     sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
-    if _os.__name__ == 'nt':
-        sys.meta_path.append(WindowsRegistryFinder)
     sys.meta_path.append(PathFinder)
diff --git a/Lib/mailbox.py b/Lib/mailbox.py
index 0e23987..39f24f9 100644
--- a/Lib/mailbox.py
+++ b/Lib/mailbox.py
@@ -313,11 +313,12 @@
         # final position in order to prevent race conditions with changes
         # from other programs
         try:
-            if hasattr(os, 'link'):
+            try:
                 os.link(tmp_file.name, dest)
-                os.remove(tmp_file.name)
-            else:
+            except (AttributeError, PermissionError):
                 os.rename(tmp_file.name, dest)
+            else:
+                os.remove(tmp_file.name)
         except OSError as e:
             os.remove(tmp_file.name)
             if e.errno == errno.EEXIST:
@@ -1200,13 +1201,14 @@
         for key in self.iterkeys():
             if key - 1 != prev:
                 changes.append((key, prev + 1))
-                if hasattr(os, 'link'):
+                try:
                     os.link(os.path.join(self._path, str(key)),
                             os.path.join(self._path, str(prev + 1)))
-                    os.unlink(os.path.join(self._path, str(key)))
-                else:
+                except (AttributeError, PermissionError):
                     os.rename(os.path.join(self._path, str(key)),
                               os.path.join(self._path, str(prev + 1)))
+                else:
+                    os.unlink(os.path.join(self._path, str(key)))
             prev += 1
         self._next_key = prev + 1
         if len(changes) == 0:
@@ -2076,13 +2078,14 @@
                 else:
                     raise
             try:
-                if hasattr(os, 'link'):
+                try:
                     os.link(pre_lock.name, f.name + '.lock')
                     dotlock_done = True
-                    os.unlink(pre_lock.name)
-                else:
+                except (AttributeError, PermissionError):
                     os.rename(pre_lock.name, f.name + '.lock')
                     dotlock_done = True
+                else:
+                    os.unlink(pre_lock.name)
             except FileExistsError:
                 os.remove(pre_lock.name)
                 raise ExternalClashError('dot lock unavailable: %s' %
diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py
index 09455e2..623f6fb 100644
--- a/Lib/multiprocessing/context.py
+++ b/Lib/multiprocessing/context.py
@@ -196,7 +196,7 @@
     def get_start_method(self, allow_none=False):
         return self._name
 
-    def set_start_method(self, method=None):
+    def set_start_method(self, method, force=False):
         raise ValueError('cannot set start method of concrete context')
 
     @property
diff --git a/Lib/multiprocessing/spawn.py b/Lib/multiprocessing/spawn.py
index dfb9f65..4aba372 100644
--- a/Lib/multiprocessing/spawn.py
+++ b/Lib/multiprocessing/spawn.py
@@ -217,7 +217,7 @@
         process.ORIGINAL_DIR = data['orig_dir']
 
     if 'start_method' in data:
-        set_start_method(data['start_method'])
+        set_start_method(data['start_method'], force=True)
 
     if 'init_main_from_name' in data:
         _fixup_main_from_name(data['init_main_from_name'])
diff --git a/Lib/pydoc_data/topics.py b/Lib/pydoc_data/topics.py
index ad3fa25..c7fac33 100644
--- a/Lib/pydoc_data/topics.py
+++ b/Lib/pydoc_data/topics.py
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# Autogenerated by Sphinx on Fri Dec 16 16:33:16 2016
+# Autogenerated by Sphinx on Tue Dec  6 18:51:51 2016
 topics = {'assert': '\n'
            'The "assert" statement\n'
            '**********************\n'
@@ -2613,8 +2613,7 @@
              'functions, even if they do not contain "await" or "async" '
              'keywords.\n'
              '\n'
-             'It is a "SyntaxError" to use "yield from" expressions in "async '
-             'def"\n'
+             'It is a "SyntaxError" to use "yield" expressions in "async def"\n'
              'coroutines.\n'
              '\n'
              'An example of a coroutine function:\n'
@@ -7088,14 +7087,7 @@
            'generator is done and will cause "StopIteration" to be raised. '
            'The\n'
            'returned value (if any) is used as an argument to construct\n'
-           '"StopIteration" and becomes the "StopIteration.value" attribute.\n'
-           '\n'
-           'In an asynchronous generator function, an empty "return" '
-           'statement\n'
-           'indicates that the asynchronous generator is done and will cause\n'
-           '"StopAsyncIteration" to be raised.  A non-empty "return" statement '
-           'is\n'
-           'a syntax error in an asynchronous generator function.\n',
+           '"StopIteration" and becomes the "StopIteration.value" attribute.\n',
  'sequence-types': '\n'
                    'Emulating container types\n'
                    '*************************\n'
@@ -11105,27 +11097,6 @@
           'statements.\n'
           '      See also the Coroutine Objects section.\n'
           '\n'
-          '   Asynchronous generator functions\n'
-          '      A function or method which is defined using "async def" and\n'
-          '      which uses the "yield" statement is called a *asynchronous\n'
-          '      generator function*.  Such a function, when called, returns '
-          'an\n'
-          '      asynchronous iterator object which can be used in an "async '
-          'for"\n'
-          '      statement to execute the body of the function.\n'
-          '\n'
-          '      Calling the asynchronous iterator\'s "aiterator.__anext__()"\n'
-          '      method will return an *awaitable* which when awaited will\n'
-          '      execute until it provides a value using the "yield" '
-          'expression.\n'
-          '      When the function executes an empty "return" statement or '
-          'falls\n'
-          '      off the end, a "StopAsyncIteration" exception is raised and '
-          'the\n'
-          '      asynchronous iterator will have reached the end of the set '
-          'of\n'
-          '      values to be yielded.\n'
-          '\n'
           '   Built-in functions\n'
           '      A built-in function object is a wrapper around a C function.\n'
           '      Examples of built-in functions are "len()" and "math.sin()"\n'
diff --git a/Lib/shutil.py b/Lib/shutil.py
index 90b7198..bd4760f 100644
--- a/Lib/shutil.py
+++ b/Lib/shutil.py
@@ -10,7 +10,13 @@
 import fnmatch
 import collections
 import errno
-import tarfile
+
+try:
+    import zlib
+    del zlib
+    _ZLIB_SUPPORTED = True
+except ImportError:
+    _ZLIB_SUPPORTED = False
 
 try:
     import bz2
@@ -602,23 +608,22 @@
 
     Returns the output filename.
     """
-    tar_compression = {'gzip': 'gz', None: ''}
-    compress_ext = {'gzip': '.gz'}
-
-    if _BZ2_SUPPORTED:
-        tar_compression['bzip2'] = 'bz2'
-        compress_ext['bzip2'] = '.bz2'
-
-    if _LZMA_SUPPORTED:
-        tar_compression['xz'] = 'xz'
-        compress_ext['xz'] = '.xz'
-
-    # flags for compression program, each element of list will be an argument
-    if compress is not None and compress not in compress_ext:
+    if compress is None:
+        tar_compression = ''
+    elif _ZLIB_SUPPORTED and compress == 'gzip':
+        tar_compression = 'gz'
+    elif _BZ2_SUPPORTED and compress == 'bzip2':
+        tar_compression = 'bz2'
+    elif _LZMA_SUPPORTED and compress == 'xz':
+        tar_compression = 'xz'
+    else:
         raise ValueError("bad value for 'compress', or compression format not "
                          "supported : {0}".format(compress))
 
-    archive_name = base_name + '.tar' + compress_ext.get(compress, '')
+    import tarfile  # late import for breaking circular dependency
+
+    compress_ext = '.' + tar_compression if compress else ''
+    archive_name = base_name + '.tar' + compress_ext
     archive_dir = os.path.dirname(archive_name)
 
     if archive_dir and not os.path.exists(archive_dir):
@@ -644,7 +649,7 @@
         return tarinfo
 
     if not dry_run:
-        tar = tarfile.open(archive_name, 'w|%s' % tar_compression[compress])
+        tar = tarfile.open(archive_name, 'w|%s' % tar_compression)
         try:
             tar.add(base_dir, filter=_set_uid_gid)
         finally:
@@ -655,13 +660,10 @@
 def _make_zipfile(base_name, base_dir, verbose=0, dry_run=0, logger=None):
     """Create a zip file from all the files under 'base_dir'.
 
-    The output zip file will be named 'base_name' + ".zip".  Uses either the
-    "zipfile" Python module (if available) or the InfoZIP "zip" utility
-    (if installed and found on the default search path).  If neither tool is
-    available, raises ExecError.  Returns the name of the output zip
-    file.
+    The output zip file will be named 'base_name' + ".zip".  Returns the
+    name of the output zip file.
     """
-    import zipfile
+    import zipfile  # late import for breaking circular dependency
 
     zip_filename = base_name + ".zip"
     archive_dir = os.path.dirname(base_name)
@@ -700,10 +702,13 @@
     return zip_filename
 
 _ARCHIVE_FORMATS = {
-    'gztar': (_make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
     'tar':   (_make_tarball, [('compress', None)], "uncompressed tar file"),
-    'zip':   (_make_zipfile, [], "ZIP file")
-    }
+}
+
+if _ZLIB_SUPPORTED:
+    _ARCHIVE_FORMATS['gztar'] = (_make_tarball, [('compress', 'gzip')],
+                                "gzip'ed tar-file")
+    _ARCHIVE_FORMATS['zip'] = (_make_zipfile, [], "ZIP file")
 
 if _BZ2_SUPPORTED:
     _ARCHIVE_FORMATS['bztar'] = (_make_tarball, [('compress', 'bzip2')],
@@ -752,8 +757,8 @@
     """Create an archive file (eg. zip or tar).
 
     'base_name' is the name of the file to create, minus any format-specific
-    extension; 'format' is the archive format: one of "zip", "tar", "bztar"
-    or "gztar".
+    extension; 'format' is the archive format: one of "zip", "tar", "gztar",
+    "bztar", or "xztar".  Or any other registered format.
 
     'root_dir' is a directory that will be the root directory of the
     archive; ie. we typically chdir into 'root_dir' before creating the
@@ -866,10 +871,7 @@
 def _unpack_zipfile(filename, extract_dir):
     """Unpack zip `filename` to `extract_dir`
     """
-    try:
-        import zipfile
-    except ImportError:
-        raise ReadError('zlib not supported, cannot unpack this archive.')
+    import zipfile  # late import for breaking circular dependency
 
     if not zipfile.is_zipfile(filename):
         raise ReadError("%s is not a zip file" % filename)
@@ -903,6 +905,7 @@
 def _unpack_tarfile(filename, extract_dir):
     """Unpack tar/tar.gz/tar.bz2/tar.xz `filename` to `extract_dir`
     """
+    import tarfile  # late import for breaking circular dependency
     try:
         tarobj = tarfile.open(filename)
     except tarfile.TarError:
@@ -914,10 +917,13 @@
         tarobj.close()
 
 _UNPACK_FORMATS = {
-    'gztar': (['.tar.gz', '.tgz'], _unpack_tarfile, [], "gzip'ed tar-file"),
     'tar':   (['.tar'], _unpack_tarfile, [], "uncompressed tar file"),
-    'zip':   (['.zip'], _unpack_zipfile, [], "ZIP file")
-    }
+    'zip':   (['.zip'], _unpack_zipfile, [], "ZIP file"),
+}
+
+if _ZLIB_SUPPORTED:
+    _UNPACK_FORMATS['gztar'] = (['.tar.gz', '.tgz'], _unpack_tarfile, [],
+                                "gzip'ed tar-file")
 
 if _BZ2_SUPPORTED:
     _UNPACK_FORMATS['bztar'] = (['.tar.bz2', '.tbz2'], _unpack_tarfile, [],
@@ -942,10 +948,10 @@
     `extract_dir` is the name of the target directory, where the archive
     is unpacked. If not provided, the current working directory is used.
 
-    `format` is the archive format: one of "zip", "tar", or "gztar". Or any
-    other registered format. If not provided, unpack_archive will use the
-    filename extension and see if an unpacker was registered for that
-    extension.
+    `format` is the archive format: one of "zip", "tar", "gztar", "bztar",
+    or "xztar".  Or any other registered format.  If not provided,
+    unpack_archive will use the filename extension and see if an unpacker
+    was registered for that extension.
 
     In case none is found, a ValueError is raised.
     """
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index b78b1b1..5d4c86c 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -50,9 +50,13 @@
 import re
 
 try:
-    import grp, pwd
+    import pwd
 except ImportError:
-    grp = pwd = None
+    pwd = None
+try:
+    import grp
+except ImportError:
+    grp = None
 
 # os.symlink on Windows prior to 6.0 raises NotImplementedError
 symlink_exception = (AttributeError, NotImplementedError)
@@ -2219,22 +2223,25 @@
 
     def chown(self, tarinfo, targetpath, numeric_owner):
         """Set owner of targetpath according to tarinfo. If numeric_owner
-           is True, use .gid/.uid instead of .gname/.uname.
+           is True, use .gid/.uid instead of .gname/.uname. If numeric_owner
+           is False, fall back to .gid/.uid when the search based on name
+           fails.
         """
-        if pwd and hasattr(os, "geteuid") and os.geteuid() == 0:
+        if hasattr(os, "geteuid") and os.geteuid() == 0:
             # We have to be root to do so.
-            if numeric_owner:
-                g = tarinfo.gid
-                u = tarinfo.uid
-            else:
+            g = tarinfo.gid
+            u = tarinfo.uid
+            if not numeric_owner:
                 try:
-                    g = grp.getgrnam(tarinfo.gname)[2]
+                    if grp:
+                        g = grp.getgrnam(tarinfo.gname)[2]
                 except KeyError:
-                    g = tarinfo.gid
+                    pass
                 try:
-                    u = pwd.getpwnam(tarinfo.uname)[2]
+                    if pwd:
+                        u = pwd.getpwnam(tarinfo.uname)[2]
                 except KeyError:
-                    u = tarinfo.uid
+                    pass
             try:
                 if tarinfo.issym() and hasattr(os, "lchown"):
                     os.lchown(targetpath, u, g)
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index c00846c..b5f4782 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -3818,6 +3818,19 @@
             self.assertTrue(methods == ['fork', 'spawn'] or
                             methods == ['fork', 'spawn', 'forkserver'])
 
+    def test_preload_resources(self):
+        if multiprocessing.get_start_method() != 'forkserver':
+            self.skipTest("test only relevant for 'forkserver' method")
+        name = os.path.join(os.path.dirname(__file__), 'mp_preload.py')
+        rc, out, err = test.support.script_helper.assert_python_ok(name)
+        out = out.decode()
+        err = err.decode()
+        if out.rstrip() != 'ok' or err != '':
+            print(out)
+            print(err)
+            self.fail("failed spawning forkserver or grandchild")
+
+
 #
 # Check that killing process does not leak named semaphores
 #
diff --git a/Lib/test/eintrdata/eintr_tester.py b/Lib/test/eintrdata/eintr_tester.py
index 9fbe04d..d194e77 100644
--- a/Lib/test/eintrdata/eintr_tester.py
+++ b/Lib/test/eintrdata/eintr_tester.py
@@ -20,6 +20,7 @@
 import unittest
 
 from test import support
+android_not_root = support.android_not_root
 
 @contextlib.contextmanager
 def kill_on_error(proc):
@@ -311,6 +312,7 @@
     # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162
     @support.requires_freebsd_version(10, 3)
     @unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()')
+    @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
     def _test_open(self, do_open_close_reader, do_open_close_writer):
         filename = support.TESTFN
 
diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py
index 891b00c..d621f5f 100644
--- a/Lib/test/libregrtest/cmdline.py
+++ b/Lib/test/libregrtest/cmdline.py
@@ -301,9 +301,9 @@
 
     if ns.single and ns.fromfile:
         parser.error("-s and -f don't go together!")
-    if ns.use_mp and ns.trace:
+    if ns.use_mp is not None and ns.trace:
         parser.error("-T and -j don't go together!")
-    if ns.use_mp and ns.findleaks:
+    if ns.use_mp is not None and ns.findleaks:
         parser.error("-l and -j don't go together!")
     if ns.failfast and not (ns.verbose or ns.verbose3):
         parser.error("-G/--failfast needs either -v or -W")
diff --git a/Lib/test/mp_preload.py b/Lib/test/mp_preload.py
new file mode 100644
index 0000000..5314e8f
--- /dev/null
+++ b/Lib/test/mp_preload.py
@@ -0,0 +1,18 @@
+import multiprocessing
+
+multiprocessing.Lock()
+
+
+def f():
+    print("ok")
+
+
+if __name__ == "__main__":
+    ctx = multiprocessing.get_context("forkserver")
+    modname = "test.mp_preload"
+    # Make sure it's importable
+    __import__(modname)
+    ctx.set_forkserver_preload([modname])
+    proc = ctx.Process(target=f)
+    proc.start()
+    proc.join()
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
old mode 100644
new mode 100755
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 52c908e..15d8fc8 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -93,8 +93,10 @@
     "check__all__", "requires_android_level", "requires_multiprocessing_queue",
     # sys
     "is_jython", "is_android", "check_impl_detail", "unix_shell",
+    "setswitchinterval", "android_not_root",
     # network
     "HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource",
+    "bind_unix_socket",
     # processes
     'temp_umask', "reap_children",
     # logging
@@ -360,9 +362,9 @@
                     mode = 0
                 if stat.S_ISDIR(mode):
                     _waitfor(_rmtree_inner, fullname, waitall=True)
-                    _force_run(path, os.rmdir, fullname)
+                    _force_run(fullname, os.rmdir, fullname)
                 else:
-                    _force_run(path, os.unlink, fullname)
+                    _force_run(fullname, os.unlink, fullname)
         _waitfor(_rmtree_inner, path, waitall=True)
         _waitfor(lambda p: _force_run(p, os.rmdir, p), path)
 else:
@@ -707,6 +709,15 @@
     port = sock.getsockname()[1]
     return port
 
+def bind_unix_socket(sock, addr):
+    """Bind a unix socket, raising SkipTest if PermissionError is raised."""
+    assert sock.family == socket.AF_UNIX
+    try:
+        sock.bind(addr)
+    except PermissionError:
+        sock.close()
+        raise unittest.SkipTest('cannot bind AF_UNIX sockets')
+
 def _is_ipv6_enabled():
     """Check whether IPv6 is enabled on this host."""
     if socket.has_ipv6:
@@ -768,6 +779,7 @@
 
 _ANDROID_API_LEVEL = sysconfig.get_config_var('ANDROID_API_LEVEL')
 is_android = (_ANDROID_API_LEVEL is not None and _ANDROID_API_LEVEL > 0)
+android_not_root = (is_android and os.geteuid() != 0)
 
 if sys.platform != 'win32':
     unix_shell = '/system/bin/sh' if is_android else '/bin/sh'
@@ -1054,9 +1066,16 @@
         file.close()
         unlink(TESTFN)
 
-def check_syntax_error(testcase, statement):
-    testcase.assertRaises(SyntaxError, compile, statement,
-                          '<test string>', 'exec')
+def check_syntax_error(testcase, statement, *, lineno=None, offset=None):
+    with testcase.assertRaises(SyntaxError) as cm:
+        compile(statement, '<test string>', 'exec')
+    err = cm.exception
+    testcase.assertIsNotNone(err.lineno)
+    if lineno is not None:
+        testcase.assertEqual(err.lineno, lineno)
+    testcase.assertIsNotNone(err.offset)
+    if offset is not None:
+        testcase.assertEqual(err.offset, offset)
 
 def open_urlresource(url, *args, **kw):
     import urllib.request, urllib.parse
@@ -2547,3 +2566,18 @@
             continue
         if spawn.find_executable(cmd[0]) is None:
             return cmd[0]
+
+
+_is_android_emulator = None
+def setswitchinterval(interval):
+    # Setting a very low gil interval on the Android emulator causes python
+    # to hang (issue #26939).
+    minimum_interval = 1e-5
+    if is_android and interval < minimum_interval:
+        global _is_android_emulator
+        if _is_android_emulator is None:
+            _is_android_emulator = (subprocess.check_output(
+                               ['getprop', 'ro.kernel.qemu']).strip() == b'1')
+        if _is_android_emulator:
+            interval = minimum_interval
+    return sys.setswitchinterval(interval)
diff --git a/Lib/test/test_asyncore.py b/Lib/test/test_asyncore.py
index dbee593..51c6573 100644
--- a/Lib/test/test_asyncore.py
+++ b/Lib/test/test_asyncore.py
@@ -95,7 +95,9 @@
     if HAS_UNIX_SOCKETS and sock.family == socket.AF_UNIX:
         # Make sure the path doesn't exist.
         support.unlink(addr)
-    sock.bind(addr)
+        support.bind_unix_socket(sock, addr)
+    else:
+        sock.bind(addr)
 
 
 class HelperFunctionTests(unittest.TestCase):
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index b71bb9f..ae2bcd4 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -8,7 +8,7 @@
 import sys
 import subprocess
 import tempfile
-from test.support import script_helper
+from test.support import script_helper, is_android
 from test.support.script_helper import (spawn_python, kill_python, assert_python_ok,
     assert_python_failure)
 
@@ -178,15 +178,16 @@
         if not stdout.startswith(pattern):
             raise AssertionError("%a doesn't start with %a" % (stdout, pattern))
 
-    @unittest.skipUnless(sys.platform == 'darwin', 'test specific to Mac OS X')
-    def test_osx_utf8(self):
+    @unittest.skipUnless((sys.platform == 'darwin' or
+                is_android), 'test specific to Mac OS X and Android')
+    def test_osx_android_utf8(self):
         def check_output(text):
             decoded = text.decode('utf-8', 'surrogateescape')
             expected = ascii(decoded).encode('ascii') + b'\n'
 
             env = os.environ.copy()
             # C locale gives ASCII locale encoding, but Python uses UTF-8
-            # to parse the command line arguments on Mac OS X
+            # to parse the command line arguments on Mac OS X and Android.
             env['LC_ALL'] = 'C'
 
             p = subprocess.Popen(
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index 38cb2e2..e058ecd 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -10,6 +10,7 @@
 import os.path
 import py_compile
 import subprocess
+import io
 
 import textwrap
 from test import support
@@ -539,6 +540,38 @@
             text = stderr.decode('ascii')
             self.assertEqual(text, "some text")
 
+    def test_syntaxerror_unindented_caret_position(self):
+        script = "1 + 1 = 2\n"
+        with support.temp_dir() as script_dir:
+            script_name = _make_test_script(script_dir, 'script', script)
+            exitcode, stdout, stderr = assert_python_failure(script_name)
+            text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
+            # Confirm that the caret is located under the first 1 character
+            self.assertIn("\n    1 + 1 = 2\n    ^", text)
+
+    def test_syntaxerror_indented_caret_position(self):
+        script = textwrap.dedent("""\
+            if True:
+                1 + 1 = 2
+            """)
+        with support.temp_dir() as script_dir:
+            script_name = _make_test_script(script_dir, 'script', script)
+            exitcode, stdout, stderr = assert_python_failure(script_name)
+            text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
+            # Confirm that the caret is located under the first 1 character
+            self.assertIn("\n    1 + 1 = 2\n    ^", text)
+
+            # Try the same with a form feed at the start of the indented line
+            script = (
+                "if True:\n"
+                "\f    1 + 1 = 2\n"
+            )
+            script_name = _make_test_script(script_dir, "script", script)
+            exitcode, stdout, stderr = assert_python_failure(script_name)
+            text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read()
+            self.assertNotIn("\f", text)
+            self.assertIn("\n    1 + 1 = 2\n    ^", text)
+
 
 def test_main():
     support.run_unittest(CmdLineTest)
diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py
index 2d77f07..df531d6 100644
--- a/Lib/test/test_dbm_dumb.py
+++ b/Lib/test/test_dbm_dumb.py
@@ -5,6 +5,7 @@
 import io
 import operator
 import os
+import stat
 import unittest
 import warnings
 import dbm.dumb as dumbdbm
@@ -259,6 +260,21 @@
                 f = dumbdbm.open(_fname, flag)
             f.close()
 
+    @unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()')
+    def test_readonly_files(self):
+        with support.temp_dir() as dir:
+            fname = os.path.join(dir, 'db')
+            with dumbdbm.open(fname, 'n') as f:
+                self.assertEqual(list(f.keys()), [])
+                for key in self._dict:
+                    f[key] = self._dict[key]
+            os.chmod(fname + ".dir", stat.S_IRUSR)
+            os.chmod(fname + ".dat", stat.S_IRUSR)
+            os.chmod(dir, stat.S_IRUSR|stat.S_IXUSR)
+            with dumbdbm.open(fname, 'r') as f:
+                self.assertEqual(sorted(f.keys()), sorted(self._dict))
+                f.close()  # don't write
+
     def tearDown(self):
         _delete_files()
 
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
index 1e08ed9..c5bff77 100644
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -7,6 +7,7 @@
 import sys
 import types
 import unittest
+import warnings
 import weakref
 
 from copy import deepcopy
@@ -1661,6 +1662,77 @@
         self.assertEqual(b.foo, 3)
         self.assertEqual(b.__class__, D)
 
+    @unittest.expectedFailure
+    def test_bad_new(self):
+        self.assertRaises(TypeError, object.__new__)
+        self.assertRaises(TypeError, object.__new__, '')
+        self.assertRaises(TypeError, list.__new__, object)
+        self.assertRaises(TypeError, object.__new__, list)
+        class C(object):
+            __new__ = list.__new__
+        self.assertRaises(TypeError, C)
+        class C(list):
+            __new__ = object.__new__
+        self.assertRaises(TypeError, C)
+
+    def test_object_new(self):
+        class A(object):
+            pass
+        object.__new__(A)
+        self.assertRaises(TypeError, object.__new__, A, 5)
+        object.__init__(A())
+        self.assertRaises(TypeError, object.__init__, A(), 5)
+
+        class A(object):
+            def __init__(self, foo):
+                self.foo = foo
+        object.__new__(A)
+        object.__new__(A, 5)
+        object.__init__(A(3))
+        self.assertRaises(TypeError, object.__init__, A(3), 5)
+
+        class A(object):
+            def __new__(cls, foo):
+                return object.__new__(cls)
+        object.__new__(A)
+        self.assertRaises(TypeError, object.__new__, A, 5)
+        object.__init__(A(3))
+        object.__init__(A(3), 5)
+
+        class A(object):
+            def __new__(cls, foo):
+                return object.__new__(cls)
+            def __init__(self, foo):
+                self.foo = foo
+        object.__new__(A)
+        self.assertRaises(TypeError, object.__new__, A, 5)
+        object.__init__(A(3))
+        self.assertRaises(TypeError, object.__init__, A(3), 5)
+
+    @unittest.expectedFailure
+    def test_restored_object_new(self):
+        class A(object):
+            def __new__(cls, *args, **kwargs):
+                raise AssertionError
+        self.assertRaises(AssertionError, A)
+        class B(A):
+            __new__ = object.__new__
+            def __init__(self, foo):
+                self.foo = foo
+        with warnings.catch_warnings():
+            warnings.simplefilter('error', DeprecationWarning)
+            b = B(3)
+        self.assertEqual(b.foo, 3)
+        self.assertEqual(b.__class__, B)
+        del B.__new__
+        self.assertRaises(AssertionError, B)
+        del A.__new__
+        with warnings.catch_warnings():
+            warnings.simplefilter('error', DeprecationWarning)
+            b = B(3)
+        self.assertEqual(b.foo, 3)
+        self.assertEqual(b.__class__, B)
+
     def test_altmro(self):
         # Testing mro() and overriding it...
         class A(object):
@@ -3522,6 +3594,24 @@
         self.assertIsInstance(d, D)
         self.assertEqual(d.foo, 1)
 
+        class C(object):
+            @staticmethod
+            def __new__(*args):
+                return args
+        self.assertEqual(C(1, 2), (C, 1, 2))
+        class D(C):
+            pass
+        self.assertEqual(D(1, 2), (D, 1, 2))
+
+        class C(object):
+            @classmethod
+            def __new__(*args):
+                return args
+        self.assertEqual(C(1, 2), (C, C, 1, 2))
+        class D(C):
+            pass
+        self.assertEqual(D(1, 2), (D, D, 1, 2))
+
     def test_imul_bug(self):
         # Testing for __imul__ problems...
         # SF bug 544647
diff --git a/Lib/test/test_email/test_email.py b/Lib/test/test_email/test_email.py
index daa1285..f97ccc6 100644
--- a/Lib/test/test_email/test_email.py
+++ b/Lib/test/test_email/test_email.py
@@ -11,6 +11,7 @@
 from io import StringIO, BytesIO
 from itertools import chain
 from random import choice
+from socket import getfqdn
 try:
     from threading import Thread
 except ImportError:
@@ -3314,6 +3315,17 @@
             email.utils.make_msgid(domain='testdomain-string')[-19:],
             '@testdomain-string>')
 
+    def test_make_msgid_idstring(self):
+        self.assertEqual(
+            email.utils.make_msgid(idstring='test-idstring',
+                domain='testdomain-string')[-33:],
+            '.test-idstring@testdomain-string>')
+
+    def test_make_msgid_default_domain(self):
+        self.assertTrue(
+            email.utils.make_msgid().endswith(
+                '@' + getfqdn() + '>'))
+
     def test_Generator_linend(self):
         # Issue 14645.
         with openfile('msg_26.txt', newline='\n') as f:
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index 8afd5b8..da635a9 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -114,6 +114,7 @@
         testcommon("%o", 100000000000, "1351035564000")
         testcommon("%d", 10, "10")
         testcommon("%d", 100000000000, "100000000000")
+
         big = 123456789012345678901234567890
         testcommon("%d", big, "123456789012345678901234567890")
         testcommon("%d", -big, "-123456789012345678901234567890")
@@ -133,6 +134,7 @@
         testcommon("%.31d", big, "0123456789012345678901234567890")
         testcommon("%32.31d", big, " 0123456789012345678901234567890")
         testcommon("%d", float(big), "123456________________________", 6)
+
         big = 0x1234567890abcdef12345  # 21 hex digits
         testcommon("%x", big, "1234567890abcdef12345")
         testcommon("%x", -big, "-1234567890abcdef12345")
@@ -156,19 +158,26 @@
         testcommon("%#X", big, "0X1234567890ABCDEF12345")
         testcommon("%#x", big, "0x1234567890abcdef12345")
         testcommon("%#x", -big, "-0x1234567890abcdef12345")
+        testcommon("%#27x", big, "    0x1234567890abcdef12345")
+        testcommon("%#-27x", big, "0x1234567890abcdef12345    ")
+        testcommon("%#027x", big, "0x00001234567890abcdef12345")
+        testcommon("%#.23x", big, "0x001234567890abcdef12345")
         testcommon("%#.23x", -big, "-0x001234567890abcdef12345")
+        testcommon("%#27.23x", big, "  0x001234567890abcdef12345")
+        testcommon("%#-27.23x", big, "0x001234567890abcdef12345  ")
+        testcommon("%#027.23x", big, "0x00001234567890abcdef12345")
         testcommon("%#+.23x", big, "+0x001234567890abcdef12345")
         testcommon("%# .23x", big, " 0x001234567890abcdef12345")
         testcommon("%#+.23X", big, "+0X001234567890ABCDEF12345")
-        testcommon("%#-+.23X", big, "+0X001234567890ABCDEF12345")
-        testcommon("%#-+26.23X", big, "+0X001234567890ABCDEF12345")
-        testcommon("%#-+27.23X", big, "+0X001234567890ABCDEF12345 ")
-        testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345")
         # next one gets two leading zeroes from precision, and another from the
         # 0 flag and the width
         testcommon("%#+027.23X", big, "+0X0001234567890ABCDEF12345")
+        testcommon("%# 027.23X", big, " 0X0001234567890ABCDEF12345")
         # same, except no 0 flag
         testcommon("%#+27.23X", big, " +0X001234567890ABCDEF12345")
+        testcommon("%#-+27.23x", big, "+0x001234567890abcdef12345 ")
+        testcommon("%#- 27.23x", big, " 0x001234567890abcdef12345 ")
+
         big = 0o12345670123456701234567012345670  # 32 octal digits
         testcommon("%o", big, "12345670123456701234567012345670")
         testcommon("%o", -big, "-12345670123456701234567012345670")
@@ -191,51 +200,46 @@
         testcommon("%o", big, "12345670123456701234567012345670")
         testcommon("%#o", big, "0o12345670123456701234567012345670")
         testcommon("%#o", -big, "-0o12345670123456701234567012345670")
+        testcommon("%#38o", big, "    0o12345670123456701234567012345670")
+        testcommon("%#-38o", big, "0o12345670123456701234567012345670    ")
+        testcommon("%#038o", big, "0o000012345670123456701234567012345670")
+        testcommon("%#.34o", big, "0o0012345670123456701234567012345670")
         testcommon("%#.34o", -big, "-0o0012345670123456701234567012345670")
+        testcommon("%#38.34o", big, "  0o0012345670123456701234567012345670")
+        testcommon("%#-38.34o", big, "0o0012345670123456701234567012345670  ")
+        testcommon("%#038.34o", big, "0o000012345670123456701234567012345670")
         testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670")
         testcommon("%# .34o", big, " 0o0012345670123456701234567012345670")
-        testcommon("%#+.34o", big, "+0o0012345670123456701234567012345670")
-        testcommon("%#-+.34o", big, "+0o0012345670123456701234567012345670")
-        testcommon("%#-+37.34o", big, "+0o0012345670123456701234567012345670")
-        testcommon("%#+37.34o", big, "+0o0012345670123456701234567012345670")
+        testcommon("%#+38.34o", big, " +0o0012345670123456701234567012345670")
+        testcommon("%#-+38.34o", big, "+0o0012345670123456701234567012345670 ")
+        testcommon("%#- 38.34o", big, " 0o0012345670123456701234567012345670 ")
+        testcommon("%#+038.34o", big, "+0o00012345670123456701234567012345670")
+        testcommon("%# 038.34o", big, " 0o00012345670123456701234567012345670")
         # next one gets one leading zero from precision
         testcommon("%.33o", big, "012345670123456701234567012345670")
-        # base marker shouldn't change that, since "0" is redundant
+        # base marker added in spite of leading zero (different to Python 2)
         testcommon("%#.33o", big, "0o012345670123456701234567012345670")
-        # but reduce precision, and base marker should add a zero
+        # reduce precision, and base marker is always added
         testcommon("%#.32o", big, "0o12345670123456701234567012345670")
-        # one leading zero from precision, and another from "0" flag & width
-        testcommon("%034.33o", big, "0012345670123456701234567012345670")
-        # base marker shouldn't change that
-        testcommon("%0#34.33o", big, "0o012345670123456701234567012345670")
+        # one leading zero from precision, plus two from "0" flag & width
+        testcommon("%035.33o", big, "00012345670123456701234567012345670")
+        # base marker shouldn't change the size
+        testcommon("%0#35.33o", big, "0o012345670123456701234567012345670")
+
         # Some small ints, in both Python int and flavors).
         testcommon("%d", 42, "42")
         testcommon("%d", -42, "-42")
-        testcommon("%d", 42, "42")
-        testcommon("%d", -42, "-42")
         testcommon("%d", 42.0, "42")
         testcommon("%#x", 1, "0x1")
-        testcommon("%#x", 1, "0x1")
-        testcommon("%#X", 1, "0X1")
         testcommon("%#X", 1, "0X1")
         testcommon("%#o", 1, "0o1")
-        testcommon("%#o", 1, "0o1")
-        testcommon("%#o", 0, "0o0")
         testcommon("%#o", 0, "0o0")
         testcommon("%o", 0, "0")
-        testcommon("%o", 0, "0")
-        testcommon("%d", 0, "0")
         testcommon("%d", 0, "0")
         testcommon("%#x", 0, "0x0")
-        testcommon("%#x", 0, "0x0")
-        testcommon("%#X", 0, "0X0")
         testcommon("%#X", 0, "0X0")
         testcommon("%x", 0x42, "42")
         testcommon("%x", -0x42, "-42")
-        testcommon("%x", 0x42, "42")
-        testcommon("%x", -0x42, "-42")
-        testcommon("%o", 0o42, "42")
-        testcommon("%o", -0o42, "-42")
         testcommon("%o", 0o42, "42")
         testcommon("%o", -0o42, "-42")
         # alternate float formatting
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index 8205083..708ed25 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -70,18 +70,18 @@
         # Make sure x was called.
         self.assertTrue(x.called)
 
+    def test_docstring(self):
+        def f():
+            f'''Not a docstring'''
+        self.assertIsNone(f.__doc__)
+        def g():
+            '''Not a docstring''' \
+            f''
+        self.assertIsNone(g.__doc__)
+
     def test_literal_eval(self):
-        # With no expressions, an f-string is okay.
-        self.assertEqual(ast.literal_eval("f'x'"), 'x')
-        self.assertEqual(ast.literal_eval("f'x' 'y'"), 'xy')
-
-        # But this should raise an error.
         with self.assertRaisesRegex(ValueError, 'malformed node or string'):
-            ast.literal_eval("f'x{3}'")
-
-        # As should this, which uses a different ast node
-        with self.assertRaisesRegex(ValueError, 'malformed node or string'):
-            ast.literal_eval("f'{3}'")
+            ast.literal_eval("f'x'")
 
     def test_ast_compile_time_concat(self):
         x = ['']
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
index 75427df..3a40861 100644
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -1,4 +1,5 @@
 import abc
+import builtins
 import collections
 import copy
 from itertools import permutations
@@ -1189,6 +1190,18 @@
         self.assertEqual(misses, 4)
         self.assertEqual(currsize, 2)
 
+    def test_lru_reentrancy_with_len(self):
+        # Test to make sure the LRU cache code isn't thrown-off by
+        # caching the built-in len() function.  Since len() can be
+        # cached, we shouldn't use it inside the lru code itself.
+        old_len = builtins.len
+        try:
+            builtins.len = self.module.lru_cache(4)(len)
+            for i in [0, 0, 1, 2, 3, 3, 4, 5, 6, 1, 7, 2, 1]:
+                self.assertEqual(len('abcdefghijklmn'[:i]), i)
+        finally:
+            builtins.len = old_len
+
     def test_lru_type_error(self):
         # Regression test for issue #28653.
         # lru_cache was leaking when one of the arguments
@@ -1322,7 +1335,7 @@
                 f.cache_clear()
 
         orig_si = sys.getswitchinterval()
-        sys.setswitchinterval(1e-6)
+        support.setswitchinterval(1e-6)
         try:
             # create n threads in order to fill cache
             threads = [threading.Thread(target=full, args=[k])
diff --git a/Lib/test/test_future.py b/Lib/test/test_future.py
index 213b2ba..2f1c410 100644
--- a/Lib/test/test_future.py
+++ b/Lib/test/test_future.py
@@ -2,6 +2,7 @@
 
 import unittest
 from test import support
+import os
 import re
 
 rx = re.compile(r'\((\S+).py, line (\d+)')
@@ -12,6 +13,12 @@
 
 class FutureTest(unittest.TestCase):
 
+    def check_syntax_error(self, err, basename, lineno, offset=0):
+        self.assertIn('%s.py, line %d' % (basename, lineno), str(err))
+        self.assertEqual(os.path.basename(err.filename), basename + '.py')
+        self.assertEqual(err.lineno, lineno)
+        self.assertEqual(err.offset, offset)
+
     def test_future1(self):
         with support.CleanImport('future_test1'):
             from test import future_test1
@@ -27,68 +34,44 @@
             from test import test_future3
 
     def test_badfuture3(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future3
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future3", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future3", 3)
 
     def test_badfuture4(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future4
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future4", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future4", 3)
 
     def test_badfuture5(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future5
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future5", '4'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future5", 4)
 
     def test_badfuture6(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future6
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future6", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future6", 3)
 
     def test_badfuture7(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future7
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future7", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 53)
 
     def test_badfuture8(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future8
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future8", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future8", 3)
 
     def test_badfuture9(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future9
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future9", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future9", 3, 0)
 
     def test_badfuture10(self):
-        try:
+        with self.assertRaises(SyntaxError) as cm:
             from test import badsyntax_future10
-        except SyntaxError as msg:
-            self.assertEqual(get_error_location(msg), ("badsyntax_future10", '3'))
-        else:
-            self.fail("expected exception didn't occur")
+        self.check_syntax_error(cm.exception, "badsyntax_future10", 3, 0)
 
     def test_parserhack(self):
         # test that the parser.c::future_hack function works as expected
diff --git a/Lib/test/test_genericpath.py b/Lib/test/test_genericpath.py
index ae5dd6a..f698e13 100644
--- a/Lib/test/test_genericpath.py
+++ b/Lib/test/test_genericpath.py
@@ -8,6 +8,7 @@
 import unittest
 import warnings
 from test import support
+android_not_root = support.android_not_root
 
 
 def create_file(filename, data=b'foo'):
@@ -212,6 +213,7 @@
     def test_samefile_on_symlink(self):
         self._test_samefile_on_link_func(os.symlink)
 
+    @unittest.skipIf(android_not_root, "hard links not allowed, non root user")
     def test_samefile_on_link(self):
         self._test_samefile_on_link_func(os.link)
 
@@ -251,6 +253,7 @@
     def test_samestat_on_symlink(self):
         self._test_samestat_on_link_func(os.symlink)
 
+    @unittest.skipIf(android_not_root, "hard links not allowed, non root user")
     def test_samestat_on_link(self):
         self._test_samestat_on_link_func(os.link)
 
diff --git a/Lib/test/test_global.py b/Lib/test/test_global.py
index 37ec672..9eeccf1 100644
--- a/Lib/test/test_global.py
+++ b/Lib/test/test_global.py
@@ -24,7 +24,7 @@
     global a
     global b
 """
-        check_syntax_error(self, prog_text_1)
+        check_syntax_error(self, prog_text_1, lineno=4, offset=4)
 
     def test2(self):
         prog_text_2 = """\
@@ -32,7 +32,7 @@
     print(x)
     global x
 """
-        check_syntax_error(self, prog_text_2)
+        check_syntax_error(self, prog_text_2, lineno=3, offset=4)
 
     def test3(self):
         prog_text_3 = """\
@@ -41,7 +41,7 @@
     x = 2
     global x
 """
-        check_syntax_error(self, prog_text_3)
+        check_syntax_error(self, prog_text_3, lineno=4, offset=4)
 
     def test4(self):
         prog_text_4 = """\
diff --git a/Lib/test/test_importlib/test_locks.py b/Lib/test/test_importlib/test_locks.py
index b2aadff..dbce9c2 100644
--- a/Lib/test/test_importlib/test_locks.py
+++ b/Lib/test/test_importlib/test_locks.py
@@ -57,7 +57,7 @@
         def setUp(self):
             try:
                 self.old_switchinterval = sys.getswitchinterval()
-                sys.setswitchinterval(0.000001)
+                support.setswitchinterval(0.000001)
             except AttributeError:
                 self.old_switchinterval = None
 
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
index fae2c3d..99fab58 100644
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -1,4 +1,4 @@
-from test.support import verbose
+from test.support import verbose, is_android
 import unittest
 import locale
 import sys
@@ -353,7 +353,7 @@
         enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name
         if enc not in ('utf-8', 'iso8859-1', 'cp1252'):
             raise unittest.SkipTest('encoding not suitable')
-        if enc != 'iso8859-1' and (sys.platform == 'darwin' or
+        if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or
                                    sys.platform.startswith('freebsd')):
             raise unittest.SkipTest('wcscoll/wcsxfrm have known bugs')
         BaseLocalizedTest.setUp(self)
diff --git a/Lib/test/test_mailbox.py b/Lib/test/test_mailbox.py
index aeabdbb..2ba9443 100644
--- a/Lib/test/test_mailbox.py
+++ b/Lib/test/test_mailbox.py
@@ -2137,9 +2137,9 @@
             if mbox:
                 fp.write(FROM_)
             fp.write(DUMMY_MESSAGE)
-        if hasattr(os, "link"):
+        try:
             os.link(tmpname, newname)
-        else:
+        except (AttributeError, PermissionError):
             with open(newname, "w") as fp:
                 fp.write(DUMMY_MESSAGE)
         self._msgfiles.append(newname)
diff --git a/Lib/test/test_nntplib.py b/Lib/test/test_nntplib.py
index feaba3c..c46ded1 100644
--- a/Lib/test/test_nntplib.py
+++ b/Lib/test/test_nntplib.py
@@ -132,6 +132,8 @@
         self.assertLessEqual(art_num, last)
         self._check_art_dict(art_dict)
 
+    @unittest.skipIf(True, 'temporarily skipped until a permanent solution'
+                           ' is found for issue #28971')
     def test_over(self):
         resp, count, first, last, name = self.server.group(self.GROUP_NAME)
         start = last - 10
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index f98c1fe..ce1ca15 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -10,6 +10,7 @@
 import unittest
 
 from test import support
+android_not_root = support.android_not_root
 TESTFN = support.TESTFN
 
 try:
@@ -1864,6 +1865,7 @@
         self.assertFalse((P / 'fileA' / 'bah').is_fifo())
 
     @unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required")
+    @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
     def test_is_fifo_true(self):
         P = self.cls(BASE, 'myfifo')
         os.mkfifo(str(P))
@@ -1886,7 +1888,8 @@
         try:
             sock.bind(str(P))
         except OSError as e:
-            if "AF_UNIX path too long" in str(e):
+            if (isinstance(e, PermissionError) or
+                    "AF_UNIX path too long" in str(e)):
                 self.skipTest("cannot bind Unix socket: " + str(e))
         self.assertTrue(P.is_socket())
         self.assertFalse(P.is_fifo())
@@ -2080,6 +2083,8 @@
         self.assertEqual(given, expect)
         self.assertEqual(set(p.rglob("FILEd*")), set())
 
+    @unittest.skipUnless(hasattr(pwd, 'getpwall'),
+                         'pwd module does not expose getpwall()')
     def test_expanduser(self):
         P = self.cls
         support.import_module('pwd')
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 63c74cd..029d081 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -1,6 +1,7 @@
 "Test posix functions"
 
 from test import support
+android_not_root = support.android_not_root
 
 # Skip these tests if there is no posix module.
 posix = support.import_module('posix')
@@ -422,6 +423,7 @@
                 posix.stat, list(os.fsencode(support.TESTFN)))
 
     @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
+    @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
     def test_mkfifo(self):
         support.unlink(support.TESTFN)
         posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR)
@@ -429,6 +431,7 @@
 
     @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'),
                          "don't have mknod()/S_IFIFO")
+    @unittest.skipIf(android_not_root, "mknod not allowed, non root user")
     def test_mknod(self):
         # Test using mknod() to create a FIFO (the only use specified
         # by POSIX).
@@ -907,6 +910,7 @@
             posix.close(f)
 
     @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()")
+    @unittest.skipIf(android_not_root, "hard link not allowed, non root user")
     def test_link_dir_fd(self):
         f = posix.open(posix.getcwd(), posix.O_RDONLY)
         try:
@@ -930,6 +934,7 @@
 
     @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'),
                          "test requires both stat.S_IFIFO and dir_fd support for os.mknod()")
+    @unittest.skipIf(android_not_root, "mknod not allowed, non root user")
     def test_mknod_dir_fd(self):
         # Test using mknodat() to create a FIFO (the only use specified
         # by POSIX).
@@ -1013,6 +1018,7 @@
             posix.close(f)
 
     @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()")
+    @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
     def test_mkfifo_dir_fd(self):
         support.unlink(support.TESTFN)
         f = posix.open(posix.getcwd(), posix.O_RDONLY)
diff --git a/Lib/test/test_pwd.py b/Lib/test/test_pwd.py
index b7b1a4a..ac9cff7 100644
--- a/Lib/test/test_pwd.py
+++ b/Lib/test/test_pwd.py
@@ -4,10 +4,19 @@
 
 pwd = support.import_module('pwd')
 
+def _getpwall():
+    # Android does not have getpwall.
+    if hasattr(pwd, 'getpwall'):
+        return pwd.getpwall()
+    elif hasattr(pwd, 'getpwuid'):
+        return [pwd.getpwuid(0)]
+    else:
+        return []
+
 class PwdTest(unittest.TestCase):
 
     def test_values(self):
-        entries = pwd.getpwall()
+        entries = _getpwall()
 
         for e in entries:
             self.assertEqual(len(e), 7)
@@ -33,7 +42,7 @@
             # and check afterwards (done in test_values_extended)
 
     def test_values_extended(self):
-        entries = pwd.getpwall()
+        entries = _getpwall()
         entriesbyname = {}
         entriesbyuid = {}
 
@@ -57,12 +66,13 @@
         self.assertRaises(TypeError, pwd.getpwuid, 3.14)
         self.assertRaises(TypeError, pwd.getpwnam)
         self.assertRaises(TypeError, pwd.getpwnam, 42)
-        self.assertRaises(TypeError, pwd.getpwall, 42)
+        if hasattr(pwd, 'getpwall'):
+            self.assertRaises(TypeError, pwd.getpwall, 42)
 
         # try to get some errors
         bynames = {}
         byuids = {}
-        for (n, p, u, g, gecos, d, s) in pwd.getpwall():
+        for (n, p, u, g, gecos, d, s) in _getpwall():
             bynames[n] = u
             byuids[u] = n
 
@@ -96,13 +106,17 @@
         # loop, say), pwd.getpwuid() might still be able to find data for that
         # uid. Using sys.maxint may provoke the same problems, but hopefully
         # it will be a more repeatable failure.
+        # Android accepts a very large span of uids including sys.maxsize and
+        # -1; it raises KeyError with 1 or 2 for example.
         fakeuid = sys.maxsize
         self.assertNotIn(fakeuid, byuids)
-        self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
+        if not support.is_android:
+            self.assertRaises(KeyError, pwd.getpwuid, fakeuid)
 
         # -1 shouldn't be a valid uid because it has a special meaning in many
         # uid-related functions
-        self.assertRaises(KeyError, pwd.getpwuid, -1)
+        if not support.is_android:
+            self.assertRaises(KeyError, pwd.getpwuid, -1)
         # should be out of uid_t range
         self.assertRaises(KeyError, pwd.getpwuid, 2**128)
         self.assertRaises(KeyError, pwd.getpwuid, -2**128)
diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
index 52909d8..751df15 100644
--- a/Lib/test/test_regrtest.py
+++ b/Lib/test/test_regrtest.py
@@ -226,6 +226,8 @@
                 self.checkError([opt, 'foo'], 'invalid int value')
                 self.checkError([opt, '2', '-T'], "don't go together")
                 self.checkError([opt, '2', '-l'], "don't go together")
+                self.checkError([opt, '0', '-T'], "don't go together")
+                self.checkError([opt, '0', '-l'], "don't go together")
 
     def test_coverage(self):
         for opt in '-T', '--coverage':
diff --git a/Lib/test/test_resource.py b/Lib/test/test_resource.py
index 2ecae0f..cc9c570 100644
--- a/Lib/test/test_resource.py
+++ b/Lib/test/test_resource.py
@@ -158,6 +158,20 @@
         self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, limit),
                          limit)
 
+    # Issue 20191: Reference counting bug
+    @unittest.skipUnless(hasattr(resource, 'prlimit'), 'no prlimit')
+    @support.requires_linux_version(2, 6, 36)
+    def test_prlimit_refcount(self):
+        class BadSeq:
+            def __len__(self):
+                return 2
+            def __getitem__(self, key):
+                return limits[key] - 1  # new reference
+
+        limits = resource.getrlimit(resource.RLIMIT_AS)
+        self.assertEqual(resource.prlimit(0, resource.RLIMIT_AS, BadSeq()),
+                         limits)
+
 
 def test_main(verbose=None):
     support.run_unittest(ResourceTest)
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index af5f00f..2ad3a21 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -19,22 +19,12 @@
                     unregister_unpack_format, get_unpack_formats,
                     SameFileError)
 import tarfile
+import zipfile
 import warnings
 
 from test import support
-from test.support import TESTFN, check_warnings, captured_stdout, requires_zlib
-
-try:
-    import bz2
-    BZ2_SUPPORTED = True
-except ImportError:
-    BZ2_SUPPORTED = False
-
-try:
-    import lzma
-    LZMA_SUPPORTED = True
-except ImportError:
-    LZMA_SUPPORTED = False
+from test.support import (TESTFN, check_warnings, captured_stdout,
+                          android_not_root)
 
 TESTFN2 = TESTFN + "2"
 
@@ -45,12 +35,6 @@
 except ImportError:
     UID_GID_SUPPORT = False
 
-try:
-    import zipfile
-    ZIP_SUPPORT = True
-except ImportError:
-    ZIP_SUPPORT = shutil.which('zip')
-
 def _fake_rename(*args, **kwargs):
     # Pretend the destination path is on a different filesystem.
     raise OSError(getattr(errno, 'EXDEV', 18), "Invalid cross-device link")
@@ -787,6 +771,7 @@
 
     @unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows')
     @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link')
+    @unittest.skipIf(android_not_root, "hard links not allowed, non root user")
     def test_dont_copy_file_onto_link_to_itself(self):
         # bug 851123.
         os.mkdir(TESTFN)
@@ -839,6 +824,7 @@
 
     # Issue #3002: copyfile and copytree block indefinitely on named pipes
     @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
+    @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
     def test_copyfile_named_pipe(self):
         os.mkfifo(TESTFN)
         try:
@@ -849,6 +835,7 @@
         finally:
             os.remove(TESTFN)
 
+    @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
     @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
     @support.skip_unless_symlink
     def test_copytree_named_pipe(self):
@@ -962,7 +949,7 @@
             self.assertEqual(getattr(file1_stat, 'st_flags'),
                              getattr(file2_stat, 'st_flags'))
 
-    @requires_zlib
+    @support.requires_zlib
     def test_make_tarball(self):
         # creating something to tar
         root_dir, base_dir = self._create_files('')
@@ -1018,7 +1005,7 @@
             write_file((root_dir, 'outer'), 'xxx')
         return root_dir, base_dir
 
-    @requires_zlib
+    @support.requires_zlib
     @unittest.skipUnless(shutil.which('tar'),
                          'Need the tar command to run')
     def test_tarfile_vs_tar(self):
@@ -1051,8 +1038,7 @@
         self.assertEqual(tarball, base_name + '.tar')
         self.assertTrue(os.path.isfile(tarball))
 
-    @requires_zlib
-    @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
+    @support.requires_zlib
     def test_make_zipfile(self):
         # creating something to zip
         root_dir, base_dir = self._create_files()
@@ -1089,8 +1075,7 @@
                     ['dist/', 'dist/sub/', 'dist/sub2/',
                      'dist/file1', 'dist/file2', 'dist/sub/file3'])
 
-    @requires_zlib
-    @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
+    @support.requires_zlib
     @unittest.skipUnless(shutil.which('zip'),
                          'Need the zip command to run')
     def test_zipfile_vs_zip(self):
@@ -1116,8 +1101,7 @@
             names2 = zf.namelist()
         self.assertEqual(sorted(names), sorted(names2))
 
-    @requires_zlib
-    @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
+    @support.requires_zlib
     @unittest.skipUnless(shutil.which('unzip'),
                          'Need the unzip command to run')
     def test_unzip_zipfile(self):
@@ -1144,7 +1128,7 @@
         base_name = os.path.join(tmpdir, 'archive')
         self.assertRaises(ValueError, make_archive, base_name, 'xxx')
 
-    @requires_zlib
+    @support.requires_zlib
     def test_make_archive_owner_group(self):
         # testing make_archive with owner and group, with various combinations
         # this works even if there's not gid/uid support
@@ -1172,7 +1156,7 @@
         self.assertTrue(os.path.isfile(res))
 
 
-    @requires_zlib
+    @support.requires_zlib
     @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
     def test_tarfile_root_owner(self):
         root_dir, base_dir = self._create_files()
@@ -1217,7 +1201,7 @@
             self.assertEqual(make_archive('test', 'tar'), 'test.tar')
             self.assertTrue(os.path.isfile('test.tar'))
 
-    @requires_zlib
+    @support.requires_zlib
     def test_make_zipfile_in_curdir(self):
         # Issue #21280
         root_dir = self.mkdtemp()
@@ -1241,33 +1225,46 @@
         formats = [name for name, params in get_archive_formats()]
         self.assertNotIn('xxx', formats)
 
-    @requires_zlib
-    def test_unpack_archive(self):
-        formats = ['tar', 'gztar', 'zip']
-        if BZ2_SUPPORTED:
-            formats.append('bztar')
-        if LZMA_SUPPORTED:
-            formats.append('xztar')
-
+    def check_unpack_archive(self, format):
         root_dir, base_dir = self._create_files()
         expected = rlistdir(root_dir)
         expected.remove('outer')
-        for format in formats:
-            base_name = os.path.join(self.mkdtemp(), 'archive')
-            filename = make_archive(base_name, format, root_dir, base_dir)
 
-            # let's try to unpack it now
-            tmpdir2 = self.mkdtemp()
-            unpack_archive(filename, tmpdir2)
-            self.assertEqual(rlistdir(tmpdir2), expected)
+        base_name = os.path.join(self.mkdtemp(), 'archive')
+        filename = make_archive(base_name, format, root_dir, base_dir)
 
-            # and again, this time with the format specified
-            tmpdir3 = self.mkdtemp()
-            unpack_archive(filename, tmpdir3, format=format)
-            self.assertEqual(rlistdir(tmpdir3), expected)
+        # let's try to unpack it now
+        tmpdir2 = self.mkdtemp()
+        unpack_archive(filename, tmpdir2)
+        self.assertEqual(rlistdir(tmpdir2), expected)
+
+        # and again, this time with the format specified
+        tmpdir3 = self.mkdtemp()
+        unpack_archive(filename, tmpdir3, format=format)
+        self.assertEqual(rlistdir(tmpdir3), expected)
+
         self.assertRaises(shutil.ReadError, unpack_archive, TESTFN)
         self.assertRaises(ValueError, unpack_archive, TESTFN, format='xxx')
 
+    def test_unpack_archive_tar(self):
+        self.check_unpack_archive('tar')
+
+    @support.requires_zlib
+    def test_unpack_archive_gztar(self):
+        self.check_unpack_archive('gztar')
+
+    @support.requires_bz2
+    def test_unpack_archive_bztar(self):
+        self.check_unpack_archive('bztar')
+
+    @support.requires_lzma
+    def test_unpack_archive_xztar(self):
+        self.check_unpack_archive('xztar')
+
+    @support.requires_zlib
+    def test_unpack_archive_zip(self):
+        self.check_unpack_archive('zip')
+
     def test_unpack_registry(self):
 
         formats = get_unpack_formats()
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 59564c9..6b29e18 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -278,8 +278,14 @@
 
     def clientRun(self, test_func):
         self.server_ready.wait()
-        self.clientSetUp()
-        self.client_ready.set()
+        try:
+            self.clientSetUp()
+        except BaseException as e:
+            self.queue.put(e)
+            self.clientTearDown()
+            return
+        finally:
+            self.client_ready.set()
         if self.server_crashed:
             self.clientTearDown()
             return
@@ -520,8 +526,11 @@
         self.serv_conn = self.cli
 
     def clientTearDown(self):
-        self.serv_conn.close()
-        self.serv_conn = None
+        try:
+            self.serv_conn.close()
+            self.serv_conn = None
+        except AttributeError:
+            pass
         super().clientTearDown()
 
 
@@ -540,7 +549,7 @@
 
     def bindSock(self, sock):
         path = tempfile.mktemp(dir=self.dir_path)
-        sock.bind(path)
+        support.bind_unix_socket(sock, path)
         self.addCleanup(support.unlink, path)
 
 class UnixStreamBase(UnixSocketTestBase):
@@ -4631,7 +4640,7 @@
     def bind(self, sock, path):
         # Bind the socket
         try:
-            sock.bind(path)
+            support.bind_unix_socket(sock, path)
         except OSError as e:
             if str(e) == "AF_UNIX path too long":
                 self.skipTest(
diff --git a/Lib/test/test_stat.py b/Lib/test/test_stat.py
index f1a5938..cd02a6e 100644
--- a/Lib/test/test_stat.py
+++ b/Lib/test/test_stat.py
@@ -1,7 +1,7 @@
 import unittest
 import os
 import sys
-from test.support import TESTFN, import_fresh_module
+from test.support import TESTFN, import_fresh_module, android_not_root
 
 c_stat = import_fresh_module('stat', fresh=['_stat'])
 py_stat = import_fresh_module('stat', blocked=['_stat'])
@@ -168,6 +168,7 @@
             self.assertS_IS("LNK", st_mode)
 
     @unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
+    @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user")
     def test_fifo(self):
         os.mkfifo(TESTFN, 0o700)
         st_mode, modestr = self.get_mode()
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index 553822b..e83a4d6 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -240,7 +240,7 @@
         self.assertEqual(cm.exception.errno, errno.EBADF)
 
     def test_check_syntax_error(self):
-        support.check_syntax_error(self, "def class")
+        support.check_syntax_error(self, "def class", lineno=1, offset=9)
         with self.assertRaises(AssertionError):
             support.check_syntax_error(self, "x=1")
 
diff --git a/Lib/test/test_symtable.py b/Lib/test/test_symtable.py
index 3047165..dfaee17 100644
--- a/Lib/test/test_symtable.py
+++ b/Lib/test/test_symtable.py
@@ -159,15 +159,17 @@
     def test_filename_correct(self):
         ### Bug tickler: SyntaxError file name correct whether error raised
         ### while parsing or building symbol table.
-        def checkfilename(brokencode):
+        def checkfilename(brokencode, offset):
             try:
                 symtable.symtable(brokencode, "spam", "exec")
             except SyntaxError as e:
                 self.assertEqual(e.filename, "spam")
+                self.assertEqual(e.lineno, 1)
+                self.assertEqual(e.offset, offset)
             else:
                 self.fail("no SyntaxError for %r" % (brokencode,))
-        checkfilename("def f(x): foo)(")  # parse-time
-        checkfilename("def f(x): global x")  # symtable-build-time
+        checkfilename("def f(x): foo)(", 14)  # parse-time
+        checkfilename("def f(x): global x", 10)  # symtable-build-time
         symtable.symtable("pass", b"spam", "exec")
         with self.assertWarns(DeprecationWarning), \
              self.assertRaises(TypeError):
diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py
index 80e36fd..7f7e6da 100644
--- a/Lib/test/test_syntax.py
+++ b/Lib/test/test_syntax.py
@@ -544,7 +544,7 @@
 class SyntaxTestCase(unittest.TestCase):
 
     def _check_error(self, code, errtext,
-                     filename="<testcase>", mode="exec", subclass=None):
+                     filename="<testcase>", mode="exec", subclass=None, lineno=None, offset=None):
         """Check that compiling code raises SyntaxError with errtext.
 
         errtest is a regular expression that must be present in the
@@ -559,6 +559,11 @@
             mo = re.search(errtext, str(err))
             if mo is None:
                 self.fail("SyntaxError did not contain '%r'" % (errtext,))
+            self.assertEqual(err.filename, filename)
+            if lineno is not None:
+                self.assertEqual(err.lineno, lineno)
+            if offset is not None:
+                self.assertEqual(err.offset, offset)
         else:
             self.fail("compile() did not raise SyntaxError")
 
@@ -569,7 +574,7 @@
         self._check_error("del f()", "delete")
 
     def test_global_err_then_warn(self):
-        # Bug tickler:  The SyntaxError raised for one global statement
+        # Bug #763201:  The SyntaxError raised for one global statement
         # shouldn't be clobbered by a SyntaxWarning issued for a later one.
         source = """if 1:
             def error(a):
diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py
index 091e905..355bc61 100644
--- a/Lib/test/test_sysconfig.py
+++ b/Lib/test/test_sysconfig.py
@@ -385,7 +385,9 @@
         self.assertIsNotNone(vars['SO'])
         self.assertEqual(vars['SO'], vars['EXT_SUFFIX'])
 
-    @unittest.skipUnless(sys.platform == 'linux', 'Linux-specific test')
+    @unittest.skipUnless(sys.platform == 'linux' and
+                         hasattr(sys.implementation, '_multiarch'),
+                         'multiarch-specific test')
     def test_triplet_in_ext_suffix(self):
         import ctypes, platform, re
         machine = platform.machine()
@@ -395,7 +397,6 @@
         if re.match('(i[3-6]86|x86_64)$', machine):
             if ctypes.sizeof(ctypes.c_char_p()) == 4:
                 self.assertTrue(suffix.endswith('i386-linux-gnu.so') or
-                                suffix.endswith('i686-linux-android.so') or
                                 suffix.endswith('x86_64-linux-gnux32.so'),
                                 suffix)
             else: # 8 byte pointer size
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index 845e7d4..2c2914f 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -462,7 +462,7 @@
         self.addCleanup(sys.setswitchinterval, old_interval)
 
         # Make the bug more likely to manifest.
-        sys.setswitchinterval(1e-6)
+        test.support.setswitchinterval(1e-6)
 
         for i in range(20):
             t = threading.Thread(target=lambda: None)
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index 0737140..883c362 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -2639,7 +2639,7 @@
                      b'repr=%V', None, b'abc\xff')
 
         # not supported: copy the raw format string. these tests are just here
-        # to check for crashs and should not be considered as specifications
+        # to check for crashes and should not be considered as specifications
         check_format('%s',
                      b'%1%s', b'abc')
         check_format('%1abc',
diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py
index a474a07..9341c6d 100644
--- a/Lib/test/test_weakref.py
+++ b/Lib/test/test_weakref.py
@@ -6,6 +6,7 @@
 import operator
 import contextlib
 import copy
+import time
 
 from test import support
 from test.support import script_helper
@@ -72,6 +73,29 @@
         self.cbcalled += 1
 
 
+@contextlib.contextmanager
+def collect_in_thread(period=0.0001):
+    """
+    Ensure GC collections happen in a different thread, at a high frequency.
+    """
+    threading = support.import_module('threading')
+    please_stop = False
+
+    def collect():
+        while not please_stop:
+            time.sleep(period)
+            gc.collect()
+
+    with support.disable_gc():
+        t = threading.Thread(target=collect)
+        t.start()
+        try:
+            yield
+        finally:
+            please_stop = True
+            t.join()
+
+
 class ReferencesTestCase(TestBase):
 
     def test_basic_ref(self):
@@ -1636,6 +1660,23 @@
         dict = weakref.WeakKeyDictionary()
         self.assertRegex(repr(dict), '<WeakKeyDictionary at 0x.*>')
 
+    def test_threaded_weak_valued_setdefault(self):
+        d = weakref.WeakValueDictionary()
+        with collect_in_thread():
+            for i in range(100000):
+                x = d.setdefault(10, RefCycle())
+                self.assertIsNot(x, None)  # we never put None in there!
+                del x
+
+    def test_threaded_weak_valued_pop(self):
+        d = weakref.WeakValueDictionary()
+        with collect_in_thread():
+            for i in range(100000):
+                d[10] = RefCycle()
+                x = d.pop(10, 10)
+                self.assertIsNot(x, None)  # we never put None in there!
+
+
 from test import mapping_tests
 
 class WeakValueDictionaryTestCase(mapping_tests.BasicTestMappingProtocol):
diff --git a/Lib/test/test_winreg.py b/Lib/test/test_winreg.py
index d642b13..2be61ae 100644
--- a/Lib/test/test_winreg.py
+++ b/Lib/test/test_winreg.py
@@ -57,7 +57,7 @@
 
     def delete_tree(self, root, subkey):
         try:
-            hkey = OpenKey(root, subkey, KEY_ALL_ACCESS)
+            hkey = OpenKey(root, subkey, 0, KEY_ALL_ACCESS)
         except OSError:
             # subkey does not exist
             return
@@ -368,6 +368,18 @@
         finally:
             DeleteKey(HKEY_CURRENT_USER, test_key_name)
 
+    def test_read_string_containing_null(self):
+        # Test for issue 25778: REG_SZ should not contain null characters
+        try:
+            with CreateKey(HKEY_CURRENT_USER, test_key_name) as ck:
+                self.assertNotEqual(ck.handle, 0)
+                test_val = "A string\x00 with a null"
+                SetValueEx(ck, "test_name", 0, REG_SZ, test_val)
+                ret_val, ret_type = QueryValueEx(ck, "test_name")
+                self.assertEqual(ret_type, REG_SZ)
+                self.assertEqual(ret_val, "A string")
+        finally:
+            DeleteKey(HKEY_CURRENT_USER, test_key_name)
 
 
 @unittest.skipUnless(REMOTE_NAME, "Skipping remote registry tests")
diff --git a/Lib/test/test_xml_etree_c.py b/Lib/test/test_xml_etree_c.py
index 87f3f27..7c60699 100644
--- a/Lib/test/test_xml_etree_c.py
+++ b/Lib/test/test_xml_etree_c.py
@@ -11,6 +11,7 @@
                                 fresh=['_elementtree', 'xml.etree'])
 
 
+@unittest.skipUnless(cET, 'requires _elementtree')
 class MiscTests(unittest.TestCase):
     # Issue #8651.
     @support.bigmemtest(size=support._2G + 100, memuse=1, dry_run=False)
@@ -54,6 +55,15 @@
             del element.attrib
         self.assertEqual(element.attrib, {'A': 'B', 'C': 'D'})
 
+    def test_trashcan(self):
+        # If this test fails, it will most likely die via segfault.
+        e = root = cET.Element('root')
+        for i in range(200000):
+            e = cET.SubElement(e, 'x')
+        del e
+        del root
+        support.gc_collect()
+
 
 @unittest.skipUnless(cET, 'requires _elementtree')
 class TestAliasWorking(unittest.TestCase):
diff --git a/Lib/tkinter/tix.py b/Lib/tkinter/tix.py
index e7cb10a..d9c097a 100644
--- a/Lib/tkinter/tix.py
+++ b/Lib/tkinter/tix.py
@@ -1,7 +1,3 @@
-# -*-mode: python; fill-column: 75; tab-width: 8 -*-
-#
-# $Id$
-#
 # Tix.py -- Tix widget wrappers.
 #
 #       For Tix, see http://tix.sourceforge.net
diff --git a/Lib/weakref.py b/Lib/weakref.py
index 2968fb9..9f8ef3e 100644
--- a/Lib/weakref.py
+++ b/Lib/weakref.py
@@ -239,24 +239,27 @@
         try:
             o = self.data.pop(key)()
         except KeyError:
+            o = None
+        if o is None:
             if args:
                 return args[0]
-            raise
-        if o is None:
-            raise KeyError(key)
+            else:
+                raise KeyError(key)
         else:
             return o
 
     def setdefault(self, key, default=None):
         try:
-            wr = self.data[key]
+            o = self.data[key]()
         except KeyError:
+            o = None
+        if o is None:
             if self._pending_removals:
                 self._commit_removals()
             self.data[key] = KeyedRef(default, self._remove, key)
             return default
         else:
-            return wr()
+            return o
 
     def update(*args, **kwargs):
         if not args:
diff --git a/Makefile.pre.in b/Makefile.pre.in
index cd7d33d..48b5180 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -858,8 +858,8 @@
 		$(srcdir)/Objects/stringlib/unicode_format.h \
 		$(srcdir)/Objects/stringlib/unicodedefs.h
 
+Objects/bytes_methods.o: $(srcdir)/Objects/bytes_methods.c $(BYTESTR_DEPS)
 Objects/bytesobject.o: $(srcdir)/Objects/bytesobject.c $(BYTESTR_DEPS)
-
 Objects/bytearrayobject.o: $(srcdir)/Objects/bytearrayobject.c $(BYTESTR_DEPS)
 
 Objects/unicodeobject.o: $(srcdir)/Objects/unicodeobject.c $(UNICODE_DEPS)
diff --git a/Misc/ACKS b/Misc/ACKS
index 2a3a278..73370ba 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -189,6 +189,7 @@
 Anthony Briggs
 Keith Briggs
 Tobias Brink
+Dillon Brock
 Richard Brodie
 Michael Broghton
 Ammar Brohi
@@ -850,6 +851,7 @@
 Chris Lawrence
 Mark Lawrence
 Chris Laws
+Michael Layzell
 Michael Lazar
 Brian Leair
 Mathieu Leduc-Hamel
diff --git a/Misc/HISTORY b/Misc/HISTORY
index 6cadaec..73ec926 100644
--- a/Misc/HISTORY
+++ b/Misc/HISTORY
@@ -6495,7 +6495,7 @@
   NULL).
 
 - Issue #10829: Refactor PyUnicode_FromFormat(), use the same function to parse
-  the format string in the 3 steps, fix crashs on invalid format strings.
+  the format string in the 3 steps, fix crashes on invalid format strings.
 
 - Issue #13007: whichdb should recognize gdbm 1.9 magic numbers.
 
diff --git a/Misc/NEWS b/Misc/NEWS
index 848add0..6947359 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -2,6 +2,102 @@
 Python News
 +++++++++++
 
+What's New in Python 3.6.1 release candidate 1?
+===============================================
+
+*Release date: XXXX-XX-XX*
+
+Core and Builtins
+-----------------
+
+- Issue #28932: Do not include <sys/random.h> if it does not exist.
+
+- Issue #25677: Correct the positioning of the syntax error caret for
+  indented blocks.  Based on patch by Michael Layzell.
+
+- Issue #29000: Fixed bytes formatting of octals with zero padding in alternate
+  form.
+
+- Issue #26919: On Android, operating system data is now always encoded/decoded
+  to/from UTF-8, instead of the locale encoding to avoid inconsistencies with
+  os.fsencode() and os.fsdecode() which are already using UTF-8.
+
+- Issue #28991:  functools.lru_cache() was susceptible to an obscure reentrancy
+  bug triggerable by a monkey-patched len() function.
+
+- Issue #28739: f-string expressions are no longer accepted as docstrings and
+  by ast.literal_eval() even if they do not include expressions.
+
+- Issue #28512: Fixed setting the offset attribute of SyntaxError by
+  PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().
+
+- Issue #28918: Fix the cross compilation of xxlimited when Python has been
+  built with Py_DEBUG defined.
+
+- Issue #28731: Optimize _PyDict_NewPresized() to create correct size dict.
+  Improve speed of dict literal with constant keys up to 30%.
+
+Library
+-------
+
+- Issue 28923: Remove editor artifacts from Tix.py.
+
+- Issue #28871: Fixed a crash when deallocate deep ElementTree.
+
+- Issue #19542: Fix bugs in WeakValueDictionary.setdefault() and
+  WeakValueDictionary.pop() when a GC collection happens in another
+  thread.
+
+- Issue #20191: Fixed a crash in resource.prlimit() when pass a sequence that
+  doesn't own its elements as limits.
+
+- Issue #28779: multiprocessing.set_forkserver_preload() would crash the
+  forkserver process if a preloaded module instantiated some
+  multiprocessing objects such as locks.
+
+- Issue #28847: dbm.dumb now supports reading read-only files and no longer
+  writes the index file when it is not changed.
+
+- Issue #26937: The chown() method of the tarfile.TarFile class does not fail
+  now when the grp module cannot be imported, as for example on Android
+  platforms.
+
+Windows
+-------
+
+- Issue #25778: winreg does not truncate string correctly (Patch by Eryk Sun)
+
+- Issue #28896: Deprecate WindowsRegistryFinder and disable it by default.
+
+Tests
+-----
+
+- Issue #28950: Disallow -j0 to be combined with -T/-l in regrtest
+  command line arguments.
+
+- Issue #28683: Fix the tests that bind() a unix socket and raise
+  PermissionError on Android for a non-root user.
+
+- Issue #26939: Add the support.setswitchinterval() function to fix
+  test_functools hanging on the Android armv7 qemu emulator.
+
+Build
+-----
+
+- Issue #28762: lockf() is available on Android API level 24, but the F_LOCK
+  macro is not defined in android-ndk-r13.
+
+- Issue #28538: Fix the compilation error that occurs because if_nameindex() is
+  available on Android API level 24, but the if_nameindex structure is not
+  defined.
+
+- Issue #20211: Do not add the directory for installing C header files and the
+  directory for installing object code libraries to the cross compilation
+  search paths. Original patch by Thomas Petazzoni.
+
+- Issue #28849: Do not define sys.implementation._multiarch on Android.
+
+
 What's New in Python 3.6.0?
 ===========================
 
@@ -22,7 +118,7 @@
   must not convert combined table into split table. Patch written by INADA
   Naoki.
 
-- Issue #28990: Fix asynchio SSL hanging if connection is closed before
+- Issue #28990: Fix asyncio SSL hanging if connection is closed before
   handshake is completed. (Patch by HoHo-Ho)
 
 Tools/Demos
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index 4e8f74a..fff9046 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -1327,7 +1327,7 @@
         return -1;
     }
 
-    res = _PyObject_CallMethodId(all_tasks, &PyId_add, "O", self, NULL);
+    res = _PyObject_CallMethodIdObjArgs(all_tasks, &PyId_add, self, NULL);
     if (res == NULL) {
         return -1;
     }
@@ -1838,8 +1838,8 @@
     }
     else {
         /* `task` is a subclass of Task */
-        return _PyObject_CallMethodId(
-            (PyObject*)task, &PyId__wakeup, "O", fut, NULL);
+        return _PyObject_CallMethodIdObjArgs((PyObject*)task, &PyId__wakeup,
+                                             fut, NULL);
     }
 }
 
@@ -1854,8 +1854,8 @@
         if (arg == NULL) {
             arg = Py_None;
         }
-        return _PyObject_CallMethodId(
-            (PyObject*)task, &PyId__step, "O", arg, NULL);
+        return _PyObject_CallMethodIdObjArgs((PyObject*)task, &PyId__step,
+                                             arg, NULL);
     }
 }
 
@@ -1869,8 +1869,8 @@
         return -1;
     }
 
-    handle = _PyObject_CallMethodId(
-        task->task_loop, &PyId_call_soon, "O", cb, NULL);
+    handle = _PyObject_CallMethodIdObjArgs(task->task_loop, &PyId_call_soon,
+                                           cb, NULL);
     Py_DECREF(cb);
     if (handle == NULL) {
         return -1;
@@ -2135,8 +2135,9 @@
                 if (wrapper == NULL) {
                     goto fail;
                 }
-                res = _PyObject_CallMethodId(
-                    result, &PyId_add_done_callback, "O", wrapper, NULL);
+                res = _PyObject_CallMethodIdObjArgs(result,
+                                                    &PyId_add_done_callback,
+                                                    wrapper, NULL);
                 Py_DECREF(wrapper);
                 if (res == NULL) {
                     goto fail;
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 5937520..2cda98e 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -626,6 +626,7 @@
 element_dealloc(ElementObject* self)
 {
     PyObject_GC_UnTrack(self);
+    Py_TRASHCAN_SAFE_BEGIN(self)
 
     if (self->weakreflist != NULL)
         PyObject_ClearWeakRefs((PyObject *) self);
@@ -636,6 +637,7 @@
 
     RELEASE(sizeof(ElementObject), "destroy element");
     Py_TYPE(self)->tp_free((PyObject *)self);
+    Py_TRASHCAN_SAFE_END(self)
 }
 
 /* -------------------------------------------------------------------- */
diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index 60f8b54..f4d3cbd 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -683,7 +683,7 @@
     /* UnsupportedOperation inherits from ValueError and IOError */
     state->unsupported_operation = PyObject_CallFunction(
         (PyObject *)&PyType_Type, "s(OO){}",
-        "UnsupportedOperation", PyExc_ValueError, PyExc_IOError);
+        "UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
     if (state->unsupported_operation == NULL)
         goto fail;
     Py_INCREF(state->unsupported_operation);
diff --git a/Modules/resource.c b/Modules/resource.c
index 6702b7a..113ad5c 100644
--- a/Modules/resource.c
+++ b/Modules/resource.c
@@ -107,29 +107,46 @@
 }
 
 static int
-py2rlimit(PyObject *curobj, PyObject *maxobj, struct rlimit *rl_out)
+py2rlimit(PyObject *limits, struct rlimit *rl_out)
 {
+    PyObject *curobj, *maxobj;
+    limits = PySequence_Tuple(limits);
+    if (!limits)
+        /* Here limits is a borrowed reference */
+        return -1;
+
+    if (PyTuple_GET_SIZE(limits) != 2) {
+        PyErr_SetString(PyExc_ValueError,
+                        "expected a tuple of 2 integers");
+        goto error;
+    }
+    curobj = PyTuple_GET_ITEM(limits, 0);
+    maxobj = PyTuple_GET_ITEM(limits, 1);
 #if !defined(HAVE_LARGEFILE_SUPPORT)
     rl_out->rlim_cur = PyLong_AsLong(curobj);
     if (rl_out->rlim_cur == (rlim_t)-1 && PyErr_Occurred())
-        return -1;
+        goto error;
     rl_out->rlim_max = PyLong_AsLong(maxobj);
     if (rl_out->rlim_max == (rlim_t)-1 && PyErr_Occurred())
-        return -1;
+        goto error;
 #else
     /* The limits are probably bigger than a long */
     rl_out->rlim_cur = PyLong_AsLongLong(curobj);
     if (rl_out->rlim_cur == (rlim_t)-1 && PyErr_Occurred())
-        return -1;
+        goto error;
     rl_out->rlim_max = PyLong_AsLongLong(maxobj);
     if (rl_out->rlim_max == (rlim_t)-1 && PyErr_Occurred())
-        return -1;
+        goto error;
 #endif
 
+    Py_DECREF(limits);
     rl_out->rlim_cur = rl_out->rlim_cur & RLIM_INFINITY;
     rl_out->rlim_max = rl_out->rlim_max & RLIM_INFINITY;
     return 0;
 
+error:
+    Py_DECREF(limits);
+    return -1;
 }
 
 static PyObject*
@@ -170,7 +187,7 @@
 {
     struct rlimit rl;
     int resource;
-    PyObject *limits, *curobj, *maxobj;
+    PyObject *limits;
 
     if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits))
         return NULL;
@@ -181,21 +198,8 @@
         return NULL;
     }
 
-    limits = PySequence_Tuple(limits);
-    if (!limits)
-        /* Here limits is a borrowed reference */
+    if (py2rlimit(limits, &rl) < 0) {
         return NULL;
-
-    if (PyTuple_GET_SIZE(limits) != 2) {
-        PyErr_SetString(PyExc_ValueError,
-                        "expected a tuple of 2 integers");
-        goto error;
-    }
-    curobj = PyTuple_GET_ITEM(limits, 0);
-    maxobj = PyTuple_GET_ITEM(limits, 1);
-
-    if (py2rlimit(curobj, maxobj, &rl) < 0) {
-        goto error;
     }
 
     if (setrlimit(resource, &rl) == -1) {
@@ -207,15 +211,9 @@
                             "not allowed to raise maximum limit");
         else
             PyErr_SetFromErrno(PyExc_OSError);
-        goto error;
+        return NULL;
     }
-    Py_DECREF(limits);
-    Py_INCREF(Py_None);
-    return Py_None;
-
-  error:
-    Py_DECREF(limits);
-    return NULL;
+    Py_RETURN_NONE;
 }
 
 #ifdef HAVE_PRLIMIT
@@ -225,10 +223,10 @@
     struct rlimit old_limit, new_limit;
     int resource, retval;
     pid_t pid;
-    PyObject *curobj=NULL, *maxobj=NULL;
+    PyObject *limits = NULL;
 
-    if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i|(OO):prlimit",
-                          &pid, &resource, &curobj, &maxobj))
+    if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i|O:prlimit",
+                          &pid, &resource, &limits))
         return NULL;
 
     if (resource < 0 || resource >= RLIM_NLIMITS) {
@@ -237,8 +235,8 @@
         return NULL;
     }
 
-    if (curobj != NULL) {
-        if (py2rlimit(curobj, maxobj, &new_limit) < 0) {
+    if (limits != NULL) {
+        if (py2rlimit(limits, &new_limit) < 0) {
             return NULL;
         }
         retval = prlimit(pid, resource, &new_limit, &old_limit);
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index db0ab58..ebd44ad 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -398,7 +398,7 @@
         struct tm local = buf;
         char zone[100];
         int gmtoff;
-        strftime(zone, sizeof(buf), "%Z", &buf);
+        strftime(zone, sizeof(zone), "%Z", &buf);
         gmtoff = timegm(&buf) - when;
         return tmtotuple(&local, zone, gmtoff);
     }
diff --git a/Objects/abstract.c b/Objects/abstract.c
index f9afece..d838856 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -2325,7 +2325,7 @@
     return result;
 }
 
-/* Positional arguments are obj followed args. */
+/* Positional arguments are obj followed by args. */
 PyObject *
 _PyObject_Call_Prepend(PyObject *func,
                        PyObject *obj, PyObject *args, PyObject *kwargs)
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 779fe29..b22e57e 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -974,7 +974,7 @@
             /* Write the numeric prefix for "x", "X" and "o" formats
                if the alternate form is used.
                For example, write "0x" for the "%#x" format. */
-            if ((flags & F_ALT) && (c == 'x' || c == 'X')) {
+            if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
                 assert(pbuf[0] == '0');
                 assert(pbuf[1] == c);
                 if (fill != ' ') {
@@ -999,8 +999,7 @@
             if (fill == ' ') {
                 if (sign)
                     *res++ = sign;
-                if ((flags & F_ALT) &&
-                    (c == 'x' || c == 'X')) {
+                if ((flags & F_ALT) && (c == 'o' || c == 'x' || c == 'X')) {
                     assert(pbuf[0] == '0');
                     assert(pbuf[1] == c);
                     *res++ = *pbuf++;
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 20b6f2f..5fff34b 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -388,7 +388,7 @@
  * This can be used to reserve enough size to insert n entries without
  * resizing.
  */
-#define ESTIMATE_SIZE(n)  (((n)*3) >> 1)
+#define ESTIMATE_SIZE(n)  (((n)*3+1) >> 1)
 
 /* Alternative fraction that is otherwise close enough to 2n/3 to make
  * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10.
@@ -1359,12 +1359,26 @@
 PyObject *
 _PyDict_NewPresized(Py_ssize_t minused)
 {
+    const Py_ssize_t max_presize = 128 * 1024;
     Py_ssize_t newsize;
     PyDictKeysObject *new_keys;
-    for (newsize = PyDict_MINSIZE;
-         newsize <= minused && newsize > 0;
-         newsize <<= 1)
-        ;
+
+    /* There are no strict guarantee that returned dict can contain minused
+     * items without resize.  So we create medium size dict instead of very
+     * large dict or MemoryError.
+     */
+    if (minused > USABLE_FRACTION(max_presize)) {
+        newsize = max_presize;
+    }
+    else {
+        Py_ssize_t minsize = ESTIMATE_SIZE(minused);
+        newsize = PyDict_MINSIZE;
+        while (newsize < minsize) {
+            newsize <<= 1;
+        }
+    }
+    assert(IS_POWER_OF_2(newsize));
+
     new_keys = new_keys_object(newsize);
     if (new_keys == NULL)
         return NULL;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 9c998f7..b0c410c 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -5083,10 +5083,10 @@
     return NULL;
 }
 
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__ANDROID__)
 
 /* Simplified UTF-8 decoder using surrogateescape error handler,
-   used to decode the command line arguments on Mac OS X.
+   used to decode the command line arguments on Mac OS X and Android.
 
    Return a pointer to a newly allocated wide character string (use
    PyMem_RawFree() to free the memory), or NULL on memory allocation error. */
@@ -5137,7 +5137,7 @@
     return unicode;
 }
 
-#endif /* __APPLE__ */
+#endif /* __APPLE__ or __ANDROID__ */
 
 /* Primary internal function which creates utf8 encoded bytes objects.
 
@@ -14324,11 +14324,12 @@
             if (iobj == NULL) {
                 goto onError;
             }
-            v = iobj;
+            x = PyLong_AsLong(iobj);
             Py_DECREF(iobj);
         }
-        /* Integer input truncated to a character */
-        x = PyLong_AsLong(v);
+        else {
+            x = PyLong_AsLong(v);
+        }
         if (x == -1 && PyErr_Occurred())
             goto onError;
 
diff --git a/PC/bdist_wininst/install.c b/PC/bdist_wininst/install.c
index fd977f5..17cc30d 100644
--- a/PC/bdist_wininst/install.c
+++ b/PC/bdist_wininst/install.c
@@ -154,7 +154,7 @@
 char *bitmap_bytes;
 
 static const char *REGISTRY_SUFFIX_6432 =
-#ifdef MS_WIN64
+#ifdef _WIN64
                                           "";
 #else
                                           "-32";
diff --git a/PC/winreg.c b/PC/winreg.c
index 9524838..5efdc5e 100644
--- a/PC/winreg.c
+++ b/PC/winreg.c
@@ -719,14 +719,13 @@
         case REG_SZ:
         case REG_EXPAND_SZ:
             {
-                /* the buffer may or may not have a trailing NULL */
+                /* REG_SZ should be a NUL terminated string, but only by
+                 * convention. The buffer may have been saved without a NUL
+                 * or with embedded NULs. To be consistent with reg.exe and
+                 * regedit.exe, consume only up to the first NUL. */
                 wchar_t *data = (wchar_t *)retDataBuf;
-                int len = retDataSize / 2;
-                if (retDataSize && data[len-1] == '\0')
-                    retDataSize -= 2;
-                if (retDataSize <= 0)
-                    data = L"";
-                obData = PyUnicode_FromWideChar(data, retDataSize/2);
+                size_t len = wcsnlen(data, retDataSize / sizeof(wchar_t));
+                obData = PyUnicode_FromWideChar(data, len);
                 break;
             }
         case REG_MULTI_SZ:
diff --git a/PCbuild/_freeze_importlib.vcxproj b/PCbuild/_freeze_importlib.vcxproj
index f7714c0..c732663 100644
--- a/PCbuild/_freeze_importlib.vcxproj
+++ b/PCbuild/_freeze_importlib.vcxproj
@@ -76,31 +76,44 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
-    <None Include="..\Lib\importlib\_bootstrap.py" />
+    <None Include="..\Lib\importlib\_bootstrap.py">
+      <IntFile>$(IntDir)importlib.g.h</IntFile>
+      <OutFile>$(PySourcePath)Python\importlib.h</OutFile>
+    </None>
+    <None Include="..\Lib\importlib\_bootstrap_external.py">
+      <IntFile>$(IntDir)importlib_external.g.h</IntFile>
+      <OutFile>$(PySourcePath)Python\importlib_external.h</OutFile>
+    </None>
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-  <Target Name="RebuildImportLib" AfterTargets="AfterBuild" Condition="$(Configuration) == 'Debug' or $(Configuration) == 'Release'">
-    <Exec Command='"$(TargetPath)" "$(PySourcePath)Lib\importlib\_bootstrap.py" "$(IntDir)importlib.g.h"' />
+  <Target Name="_RebuildImportLib">
+    <Exec Command='"$(TargetPath)" "%(None.FullPath)" "%(None.IntFile)"' />
     
     <PropertyGroup>
-      <_OldContent Condition="Exists('$(PySourcePath)Python\importlib.h')">$([System.IO.File]::ReadAllText('$(PySourcePath)Python\importlib.h').Replace(`&#x0D;&#x0A;`, `&#x0A;`))</_OldContent>
-      <_NewContent Condition="Exists('$(IntDir)importlib.g.h')">$([System.IO.File]::ReadAllText('$(IntDir)importlib.g.h').Replace(`&#x0D;&#x0A;`, `&#x0A;`))</_NewContent>
+      <_OldContent Condition="Exists($(OutTargetPath))"></_OldContent>
+      <_NewContent Condition="Exists($(IntTargetPath))">$([System.IO.File]::ReadAllText($(IntTargetPath)).Replace(`&#x0D;&#x0A;`, `&#x0A;`))</_NewContent>
     </PropertyGroup>
     
-    <Copy SourceFiles="$(IntDir)importlib.g.h"
-          DestinationFiles="$(PySourcePath)Python\importlib.h"
-          Condition="Exists('$(IntDir)importlib.g.h') and '$(_OldContent)' != '$(_NewContent)'" />
+    <Copy SourceFiles="%(None.IntFile)"
+          DestinationFiles="%(None.OutFile)"
+          Condition="!Exists(%(None.OutFile)) or (Exists(%(None.IntFile)) and '$([System.IO.File]::ReadAllText(%(None.OutFile)).Replace(`&#x0D;&#x0A;`, `&#x0A;`))' != '$([System.IO.File]::ReadAllText(%(None.IntFile)).Replace(`&#x0D;&#x0A;`, `&#x0A;`))')">
+      <Output TaskParameter="CopiedFiles" ItemName="_Updated" />
+    </Copy>
     
-    <Warning Text="importlib.h has been updated. You will need to rebuild pythoncore to see the changes."
-             Condition="Exists('$(IntDir)importlib.g.h') and '$(_OldContent)' != '$(_NewContent)' and $(Configuration) == 'Debug'" />
-    <Error Text="importlib.h has been updated. You will need to rebuild pythoncore to see the changes."
-           Condition="Exists('$(IntDir)importlib.g.h') and '$(_OldContent)' != '$(_NewContent)' and $(Configuration) == 'Release'" />
+    <Warning Text="@(_Updated->'%(Filename)%(Extension)',', ') updated. You will need to rebuild pythoncore to see the changes."
+             Condition="'@(_Updated)' != '' and $(Configuration) == 'Debug'" />
+    <Error Text="@(_Updated->'%(Filename)%(Extension)',', ') updated. You will need to rebuild pythoncore to see the changes."
+           Condition="'@(_Updated)' != '' and $(Configuration) == 'Release'" />
+  </Target>
+  <Target Name="RebuildImportLib" AfterTargets="AfterBuild" Condition="$(Configuration) == 'Debug' or $(Configuration) == 'Release'"
+          DependsOnTargets="_RebuildImportLib">
   </Target>
   <Target Name="_CleanImportLib" BeforeTargets="CoreClean">
     <ItemGroup>
       <Clean Include="$(IntDir)importlib.g.h" />
+      <Clean Include="$(IntDir)importlib_external.g.h" />
     </ItemGroup>
   </Target>
 </Project>
diff --git a/PCbuild/pcbuild.proj b/PCbuild/pcbuild.proj
index e0e6e93..c6b8487 100644
--- a/PCbuild/pcbuild.proj
+++ b/PCbuild/pcbuild.proj
@@ -28,7 +28,7 @@
       <BuildTarget>Build</BuildTarget>
       <CleanTarget>Clean</CleanTarget>
       <CleanAllTarget>CleanAll</CleanAllTarget>
-      <BuildInParallel>true</BuildInParallel>
+      <BuildInParallel>false</BuildInParallel>
     </Projects2>
   </ItemDefinitionGroup>
   <ItemGroup>
@@ -48,8 +48,6 @@
     <Projects Include="pylauncher.vcxproj;pywlauncher.vcxproj" />
     <!-- pyshellext.dll -->
     <Projects Include="pyshellext.vcxproj" />
-    <!-- _freeze_importlib -->
-    <Projects Include="_freeze_importlib.vcxproj" />
     <!-- Extension modules -->
     <ExtensionModules Include="_asyncio;_ctypes;_decimal;_elementtree;_msi;_multiprocessing;_overlapped;pyexpat;select;unicodedata;winsound" />
     <!-- Extension modules that require external sources -->
@@ -68,10 +66,10 @@
       <BuildInParallel>false</BuildInParallel>
     </Projects>
     
+    <!-- _freeze_importlib -->
+    <Projects2 Include="_freeze_importlib.vcxproj" />
     <!-- python[w].exe -->
-    <Projects2 Include="python.vcxproj;pythonw.vcxproj">
-      <BuildInParallel>false</BuildInParallel>
-    </Projects2>
+    <Projects2 Include="python.vcxproj;pythonw.vcxproj" />
   </ItemGroup>
 
   <Target Name="Build">
diff --git a/Python/ast.c b/Python/ast.c
index 82f4529..217ea14 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -4789,6 +4789,7 @@
 typedef struct {
     PyObject *last_str;
     ExprList expr_list;
+    int fmode;
 } FstringParser;
 
 #ifdef NDEBUG
@@ -4807,6 +4808,7 @@
 FstringParser_Init(FstringParser *state)
 {
     state->last_str = NULL;
+    state->fmode = 0;
     ExprList_Init(&state->expr_list);
     FstringParser_check_invariants(state);
 }
@@ -4869,6 +4871,7 @@
                             struct compiling *c, const node *n)
 {
     FstringParser_check_invariants(state);
+    state->fmode = 1;
 
     /* Parse the f-string. */
     while (1) {
@@ -4960,7 +4963,8 @@
 
     /* If we're just a constant string with no expressions, return
        that. */
-    if(state->expr_list.size == 0) {
+    if (!state->fmode) {
+        assert(!state->expr_list.size);
         if (!state->last_str) {
             /* Create a zero length string. */
             state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
@@ -4984,11 +4988,6 @@
     if (!seq)
         goto error;
 
-    /* If there's only one expression, return it. Otherwise, we need
-       to join them together. */
-    if (seq->size == 1)
-        return seq->elements[0];
-
     return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
 
 error:
diff --git a/Python/compile.c b/Python/compile.c
index 35151cd..0e16075 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3415,7 +3415,8 @@
 compiler_joined_str(struct compiler *c, expr_ty e)
 {
     VISIT_SEQ(c, expr, e->v.JoinedStr.values);
-    ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
+    if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
+        ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
     return 1;
 }
 
diff --git a/Python/errors.c b/Python/errors.c
index 7b25a22..6095843 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -1059,16 +1059,15 @@
             PyErr_Clear();
         Py_DECREF(tmp);
     }
+    tmp = NULL;
     if (col_offset >= 0) {
         tmp = PyLong_FromLong(col_offset);
         if (tmp == NULL)
             PyErr_Clear();
-        else {
-            if (_PyObject_SetAttrId(v, &PyId_offset, tmp))
-                PyErr_Clear();
-            Py_DECREF(tmp);
-        }
     }
+    if (_PyObject_SetAttrId(v, &PyId_offset, tmp ? tmp : Py_None))
+        PyErr_Clear();
+    Py_XDECREF(tmp);
     if (filename != NULL) {
         if (_PyObject_SetAttrId(v, &PyId_filename, filename))
             PyErr_Clear();
@@ -1080,9 +1079,6 @@
             Py_DECREF(tmp);
         }
     }
-    if (_PyObject_SetAttrId(v, &PyId_offset, Py_None)) {
-        PyErr_Clear();
-    }
     if (exc != PyExc_SyntaxError) {
         if (!_PyObject_HasAttrId(v, &PyId_msg)) {
             tmp = PyObject_Str(v);
@@ -1148,11 +1144,8 @@
     }
     fclose(fp);
     if (i == lineno) {
-        char *p = linebuf;
         PyObject *res;
-        while (*p == ' ' || *p == '\t' || *p == '\014')
-            p++;
-        res = PyUnicode_FromString(p);
+        res = PyUnicode_FromString(linebuf);
         if (res == NULL)
             PyErr_Clear();
         return res;
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 6a32c42..e84d66e 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -20,7 +20,7 @@
 #include <fcntl.h>
 #endif /* HAVE_FCNTL_H */
 
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__ANDROID__)
 extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size);
 #endif
 
@@ -273,7 +273,7 @@
 wchar_t*
 Py_DecodeLocale(const char* arg, size_t *size)
 {
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__ANDROID__)
     wchar_t *wstr;
     wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
     if (size != NULL) {
@@ -406,7 +406,7 @@
     if (size != NULL)
         *size = (size_t)-1;
     return NULL;
-#endif   /* __APPLE__ */
+#endif   /* __APPLE__ or __ANDROID__ */
 }
 
 /* Encode a wide character string to the locale encoding with the
@@ -424,7 +424,7 @@
 char*
 Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
 {
-#ifdef __APPLE__
+#if defined(__APPLE__) || defined(__ANDROID__)
     Py_ssize_t len;
     PyObject *unicode, *bytes = NULL;
     char *cpath;
@@ -522,7 +522,7 @@
         bytes = result;
     }
     return result;
-#endif   /* __APPLE__ */
+#endif   /* __APPLE__ or __ANDROID__ */
 }
 
 
diff --git a/Python/importlib_external.h b/Python/importlib_external.h
index 1b767c5..2f40978 100644
--- a/Python/importlib_external.h
+++ b/Python/importlib_external.h
@@ -2334,7 +2334,7 @@
     111,111,116,115,116,114,97,112,32,109,111,100,117,108,101,46,
     10,10,32,32,32,32,114,52,0,0,0,114,63,0,0,0,
     218,8,98,117,105,108,116,105,110,115,114,140,0,0,0,90,
-    5,112,111,115,105,120,250,1,47,218,2,110,116,250,1,92,
+    5,112,111,115,105,120,250,1,47,90,2,110,116,250,1,92,
     99,1,0,0,0,0,0,0,0,2,0,0,0,3,0,0,
     0,115,0,0,0,115,26,0,0,0,124,0,93,18,125,1,
     116,0,124,1,131,1,100,0,107,2,86,0,1,0,113,2,
@@ -2376,61 +2376,58 @@
     1,10,1,4,2,2,1,10,1,6,1,14,1,12,2,8,
     1,12,1,12,1,18,3,2,1,14,1,16,2,10,1,12,
     3,10,1,12,3,10,1,10,1,12,3,14,1,14,1,10,
-    1,10,1,10,1,114,32,1,0,0,99,1,0,0,0,0,
+    1,10,1,10,1,114,31,1,0,0,99,1,0,0,0,0,
     0,0,0,2,0,0,0,3,0,0,0,67,0,0,0,115,
-    72,0,0,0,116,0,124,0,131,1,1,0,116,1,131,0,
+    50,0,0,0,116,0,124,0,131,1,1,0,116,1,131,0,
     125,1,116,2,106,3,106,4,116,5,106,6,124,1,142,0,
-    103,1,131,1,1,0,116,7,106,8,100,1,107,2,114,56,
-    116,2,106,9,106,10,116,11,131,1,1,0,116,2,106,9,
-    106,10,116,12,131,1,1,0,100,2,83,0,41,3,122,41,
-    73,110,115,116,97,108,108,32,116,104,101,32,112,97,116,104,
-    45,98,97,115,101,100,32,105,109,112,111,114,116,32,99,111,
-    109,112,111,110,101,110,116,115,46,114,27,1,0,0,78,41,
-    13,114,32,1,0,0,114,159,0,0,0,114,8,0,0,0,
-    114,253,0,0,0,114,147,0,0,0,114,5,1,0,0,114,
-    18,1,0,0,114,3,0,0,0,114,109,0,0,0,218,9,
-    109,101,116,97,95,112,97,116,104,114,161,0,0,0,114,166,
-    0,0,0,114,248,0,0,0,41,2,114,31,1,0,0,90,
-    17,115,117,112,112,111,114,116,101,100,95,108,111,97,100,101,
-    114,115,114,4,0,0,0,114,4,0,0,0,114,6,0,0,
-    0,218,8,95,105,110,115,116,97,108,108,158,5,0,0,115,
-    12,0,0,0,0,2,8,1,6,1,20,1,10,1,12,1,
-    114,34,1,0,0,41,1,114,0,0,0,0,41,2,114,1,
-    0,0,0,114,2,0,0,0,41,1,114,49,0,0,0,41,
-    1,78,41,3,78,78,78,41,3,78,78,78,41,2,114,62,
-    0,0,0,114,62,0,0,0,41,1,78,41,1,78,41,58,
-    114,111,0,0,0,114,12,0,0,0,90,37,95,67,65,83,
-    69,95,73,78,83,69,78,83,73,84,73,86,69,95,80,76,
-    65,84,70,79,82,77,83,95,66,89,84,69,83,95,75,69,
-    89,114,11,0,0,0,114,13,0,0,0,114,19,0,0,0,
-    114,21,0,0,0,114,30,0,0,0,114,40,0,0,0,114,
-    41,0,0,0,114,45,0,0,0,114,46,0,0,0,114,48,
-    0,0,0,114,58,0,0,0,218,4,116,121,112,101,218,8,
-    95,95,99,111,100,101,95,95,114,142,0,0,0,114,17,0,
-    0,0,114,132,0,0,0,114,16,0,0,0,114,20,0,0,
-    0,90,17,95,82,65,87,95,77,65,71,73,67,95,78,85,
-    77,66,69,82,114,77,0,0,0,114,76,0,0,0,114,88,
-    0,0,0,114,78,0,0,0,90,23,68,69,66,85,71,95,
-    66,89,84,69,67,79,68,69,95,83,85,70,70,73,88,69,
-    83,90,27,79,80,84,73,77,73,90,69,68,95,66,89,84,
-    69,67,79,68,69,95,83,85,70,70,73,88,69,83,114,83,
-    0,0,0,114,89,0,0,0,114,95,0,0,0,114,99,0,
-    0,0,114,101,0,0,0,114,120,0,0,0,114,127,0,0,
-    0,114,139,0,0,0,114,145,0,0,0,114,148,0,0,0,
-    114,153,0,0,0,218,6,111,98,106,101,99,116,114,160,0,
-    0,0,114,165,0,0,0,114,166,0,0,0,114,181,0,0,
-    0,114,191,0,0,0,114,207,0,0,0,114,215,0,0,0,
-    114,220,0,0,0,114,226,0,0,0,114,221,0,0,0,114,
-    227,0,0,0,114,246,0,0,0,114,248,0,0,0,114,5,
-    1,0,0,114,23,1,0,0,114,159,0,0,0,114,32,1,
-    0,0,114,34,1,0,0,114,4,0,0,0,114,4,0,0,
-    0,114,4,0,0,0,114,6,0,0,0,218,8,60,109,111,
-    100,117,108,101,62,8,0,0,0,115,108,0,0,0,4,16,
-    4,1,4,1,2,1,6,3,8,17,8,5,8,5,8,6,
-    8,12,8,10,8,9,8,5,8,7,10,22,10,123,16,1,
-    12,2,4,1,4,2,6,2,6,2,8,2,16,45,8,34,
-    8,19,8,12,8,12,8,28,8,17,10,55,10,12,10,10,
-    8,14,6,3,4,1,14,67,14,64,14,29,16,110,14,41,
-    18,45,18,16,4,3,18,53,14,60,14,42,14,127,0,5,
-    14,127,0,22,10,23,8,11,8,68,
+    103,1,131,1,1,0,116,2,106,7,106,8,116,9,131,1,
+    1,0,100,1,83,0,41,2,122,41,73,110,115,116,97,108,
+    108,32,116,104,101,32,112,97,116,104,45,98,97,115,101,100,
+    32,105,109,112,111,114,116,32,99,111,109,112,111,110,101,110,
+    116,115,46,78,41,10,114,31,1,0,0,114,159,0,0,0,
+    114,8,0,0,0,114,253,0,0,0,114,147,0,0,0,114,
+    5,1,0,0,114,18,1,0,0,218,9,109,101,116,97,95,
+    112,97,116,104,114,161,0,0,0,114,248,0,0,0,41,2,
+    114,30,1,0,0,90,17,115,117,112,112,111,114,116,101,100,
+    95,108,111,97,100,101,114,115,114,4,0,0,0,114,4,0,
+    0,0,114,6,0,0,0,218,8,95,105,110,115,116,97,108,
+    108,158,5,0,0,115,8,0,0,0,0,2,8,1,6,1,
+    20,1,114,33,1,0,0,41,1,114,0,0,0,0,41,2,
+    114,1,0,0,0,114,2,0,0,0,41,1,114,49,0,0,
+    0,41,1,78,41,3,78,78,78,41,3,78,78,78,41,2,
+    114,62,0,0,0,114,62,0,0,0,41,1,78,41,1,78,
+    41,58,114,111,0,0,0,114,12,0,0,0,90,37,95,67,
+    65,83,69,95,73,78,83,69,78,83,73,84,73,86,69,95,
+    80,76,65,84,70,79,82,77,83,95,66,89,84,69,83,95,
+    75,69,89,114,11,0,0,0,114,13,0,0,0,114,19,0,
+    0,0,114,21,0,0,0,114,30,0,0,0,114,40,0,0,
+    0,114,41,0,0,0,114,45,0,0,0,114,46,0,0,0,
+    114,48,0,0,0,114,58,0,0,0,218,4,116,121,112,101,
+    218,8,95,95,99,111,100,101,95,95,114,142,0,0,0,114,
+    17,0,0,0,114,132,0,0,0,114,16,0,0,0,114,20,
+    0,0,0,90,17,95,82,65,87,95,77,65,71,73,67,95,
+    78,85,77,66,69,82,114,77,0,0,0,114,76,0,0,0,
+    114,88,0,0,0,114,78,0,0,0,90,23,68,69,66,85,
+    71,95,66,89,84,69,67,79,68,69,95,83,85,70,70,73,
+    88,69,83,90,27,79,80,84,73,77,73,90,69,68,95,66,
+    89,84,69,67,79,68,69,95,83,85,70,70,73,88,69,83,
+    114,83,0,0,0,114,89,0,0,0,114,95,0,0,0,114,
+    99,0,0,0,114,101,0,0,0,114,120,0,0,0,114,127,
+    0,0,0,114,139,0,0,0,114,145,0,0,0,114,148,0,
+    0,0,114,153,0,0,0,218,6,111,98,106,101,99,116,114,
+    160,0,0,0,114,165,0,0,0,114,166,0,0,0,114,181,
+    0,0,0,114,191,0,0,0,114,207,0,0,0,114,215,0,
+    0,0,114,220,0,0,0,114,226,0,0,0,114,221,0,0,
+    0,114,227,0,0,0,114,246,0,0,0,114,248,0,0,0,
+    114,5,1,0,0,114,23,1,0,0,114,159,0,0,0,114,
+    31,1,0,0,114,33,1,0,0,114,4,0,0,0,114,4,
+    0,0,0,114,4,0,0,0,114,6,0,0,0,218,8,60,
+    109,111,100,117,108,101,62,8,0,0,0,115,108,0,0,0,
+    4,16,4,1,4,1,2,1,6,3,8,17,8,5,8,5,
+    8,6,8,12,8,10,8,9,8,5,8,7,10,22,10,123,
+    16,1,12,2,4,1,4,2,6,2,6,2,8,2,16,45,
+    8,34,8,19,8,12,8,12,8,28,8,17,10,55,10,12,
+    10,10,8,14,6,3,4,1,14,67,14,64,14,29,16,110,
+    14,41,18,45,18,16,4,3,18,53,14,60,14,42,14,127,
+    0,5,14,127,0,22,10,23,8,11,8,68,
 };
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index c881f90..8befa54 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -528,7 +528,7 @@
             offset -= (int)(nl+1-text);
             text = nl+1;
         }
-        while (*text == ' ' || *text == '\t') {
+        while (*text == ' ' || *text == '\t' || *text == '\f') {
             text++;
             offset--;
         }
diff --git a/Python/random.c b/Python/random.c
index 46e3bb5..f9d600f 100644
--- a/Python/random.c
+++ b/Python/random.c
@@ -12,7 +12,7 @@
 #  ifdef HAVE_LINUX_RANDOM_H
 #    include <linux/random.h>
 #  endif
-#  if defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY)
+#  ifdef HAVE_SYS_RANDOM_H
 #    include <sys/random.h>
 #  endif
 #  if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL)
diff --git a/README b/README
index d52d86c..922f283 100644
--- a/README
+++ b/README
@@ -1,5 +1,5 @@
-This is Python version 3.6.0
-============================
+This is Python version 3.6.1 release candidate 1
+================================================
 
 Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
 2012, 2013, 2014, 2015, 2016 Python Software Foundation.  All rights reserved.
diff --git a/Tools/gdb/libpython.py b/Tools/gdb/libpython.py
index 798b062..cc1afbe 100755
--- a/Tools/gdb/libpython.py
+++ b/Tools/gdb/libpython.py
@@ -1570,7 +1570,11 @@
     def get_selected_python_frame(cls):
         '''Try to obtain the Frame for the python-related code in the selected
         frame, or None'''
-        frame = cls.get_selected_frame()
+        try:
+            frame = cls.get_selected_frame()
+        except gdb.error:
+            # No frame: Python didn't start yet
+            return None
 
         while frame:
             if frame.is_python_frame():
@@ -1711,6 +1715,10 @@
 def move_in_stack(move_up):
     '''Move up or down the stack (for the py-up/py-down command)'''
     frame = Frame.get_selected_python_frame()
+    if not frame:
+        print('Unable to locate python frame')
+        return
+
     while frame:
         if move_up:
             iter_frame = frame.older()
@@ -1773,6 +1781,10 @@
 
     def invoke(self, args, from_tty):
         frame = Frame.get_selected_python_frame()
+        if not frame:
+            print('Unable to locate python frame')
+            return
+
         while frame:
             if frame.is_python_frame():
                 frame.print_summary()
@@ -1790,8 +1802,12 @@
 
 
     def invoke(self, args, from_tty):
-        sys.stdout.write('Traceback (most recent call first):\n')
         frame = Frame.get_selected_python_frame()
+        if not frame:
+            print('Unable to locate python frame')
+            return
+
+        sys.stdout.write('Traceback (most recent call first):\n')
         while frame:
             if frame.is_python_frame():
                 frame.print_traceback()
diff --git a/configure b/configure
index cf95b27..e10510d 100755
--- a/configure
+++ b/configure
@@ -5218,29 +5218,7 @@
 #undef sparc
 #undef unix
 #if defined(__ANDROID__)
-# if defined(__x86_64__) && defined(__LP64__)
-        x86_64-linux-android
-# elif defined(__i386__)
-        i686-linux-android
-# elif defined(__aarch64__) && defined(__AARCH64EL__)
-#  if defined(__ILP32__)
-        aarch64_ilp32-linux-android
-#  else
-        aarch64-linux-android
-#  endif
-# elif defined(__ARM_EABI__) && defined(__ARMEL__)
-        arm-linux-androideabi
-# elif defined(__mips_hard_float) && defined(_MIPSEL)
-#  if _MIPS_SIM == _ABIO32
-        mipsel-linux-android
-#  elif _MIPS_SIM == _ABI64
-        mips64el-linux-android
-#  else
-#   error unknown platform triplet
-#  endif
-# else
-#   error unknown platform triplet
-# endif
+    # Android is not a multiarch system.
 #elif defined(__linux__)
 # if defined(__x86_64__) && defined(__LP64__)
         x86_64-linux-gnu
@@ -7785,7 +7763,7 @@
 poll.h sys/devpoll.h sys/epoll.h sys/poll.h \
 sys/audioio.h sys/xattr.h sys/bsdtty.h sys/event.h sys/file.h sys/ioctl.h \
 sys/kern_control.h sys/loadavg.h sys/lock.h sys/mkdev.h sys/modem.h \
-sys/param.h sys/select.h sys/sendfile.h sys/socket.h sys/statvfs.h \
+sys/param.h sys/random.h sys/select.h sys/sendfile.h sys/socket.h sys/statvfs.h \
 sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \
 sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \
 libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
@@ -11220,8 +11198,7 @@
  futimens futimes gai_strerror getentropy \
  getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
- if_nameindex \
- initgroups kill killpg lchmod lchown lockf linkat lstat lutimes mmap \
+ initgroups kill killpg lchmod lchown linkat lstat lutimes mmap \
  memrchr mbrtowc mkdirat mkfifo \
  mkfifoat mknod mknodat mktime mremap nice openat pathconf pause pipe2 plock poll \
  posix_fallocate posix_fadvise pread \
@@ -12668,6 +12645,80 @@
 rm -f core conftest.err conftest.$ac_objext \
     conftest$ac_exeext conftest.$ac_ext
 
+# On Android API level 24 with android-ndk-r13, if_nameindex() is available,
+# but the if_nameindex structure is not defined.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for if_nameindex" >&5
+$as_echo_n "checking for if_nameindex... " >&6; }
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+#include <stdio.h>
+#ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <stddef.h>
+#else
+# ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+# endif
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#ifdef HAVE_NET_IF_H
+# include <net/if.h>
+#endif
+
+int
+main ()
+{
+struct if_nameindex *ni = if_nameindex(); int x = ni[0].if_index;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+
+$as_echo "#define HAVE_IF_NAMEINDEX 1" >>confdefs.h
+
+   { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+
+# Issue #28762: lockf() is available on Android API level 24, but the F_LOCK
+# macro is not defined in android-ndk-r13.
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for lockf" >&5
+$as_echo_n "checking for lockf... " >&6; }
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#include <unistd.h>
+int
+main ()
+{
+lockf(0, F_LOCK, 0);
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+
+$as_echo "#define HAVE_LOCKF 1" >>confdefs.h
+
+   { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+
 # On OSF/1 V5.1, getaddrinfo is available, but a define
 # for [no]getaddrinfo in netdb.h.
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for getaddrinfo" >&5
@@ -15712,7 +15763,9 @@
 
 # first curses header check
 ac_save_cppflags="$CPPFLAGS"
-CPPFLAGS="$CPPFLAGS -I/usr/include/ncursesw"
+if test "$cross_compiling" = no; then
+  CPPFLAGS="$CPPFLAGS -I/usr/include/ncursesw"
+fi
 
 for ac_header in curses.h ncurses.h
 do :
diff --git a/configure.ac b/configure.ac
index 1d63813..9ffdfdf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -770,29 +770,7 @@
 #undef sparc
 #undef unix
 #if defined(__ANDROID__)
-# if defined(__x86_64__) && defined(__LP64__)
-        x86_64-linux-android
-# elif defined(__i386__)
-        i686-linux-android
-# elif defined(__aarch64__) && defined(__AARCH64EL__)
-#  if defined(__ILP32__)
-        aarch64_ilp32-linux-android
-#  else
-        aarch64-linux-android
-#  endif
-# elif defined(__ARM_EABI__) && defined(__ARMEL__)
-        arm-linux-androideabi
-# elif defined(__mips_hard_float) && defined(_MIPSEL)
-#  if _MIPS_SIM == _ABIO32
-        mipsel-linux-android
-#  elif _MIPS_SIM == _ABI64
-        mips64el-linux-android
-#  else
-#   error unknown platform triplet
-#  endif
-# else
-#   error unknown platform triplet
-# endif
+    # Android is not a multiarch system.
 #elif defined(__linux__)
 # if defined(__x86_64__) && defined(__LP64__)
         x86_64-linux-gnu
@@ -2041,7 +2019,7 @@
 poll.h sys/devpoll.h sys/epoll.h sys/poll.h \
 sys/audioio.h sys/xattr.h sys/bsdtty.h sys/event.h sys/file.h sys/ioctl.h \
 sys/kern_control.h sys/loadavg.h sys/lock.h sys/mkdev.h sys/modem.h \
-sys/param.h sys/select.h sys/sendfile.h sys/socket.h sys/statvfs.h \
+sys/param.h sys/random.h sys/select.h sys/sendfile.h sys/socket.h sys/statvfs.h \
 sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \
 sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \
 libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
@@ -3406,8 +3384,7 @@
  futimens futimes gai_strerror getentropy \
  getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \
  getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
- if_nameindex \
- initgroups kill killpg lchmod lchown lockf linkat lstat lutimes mmap \
+ initgroups kill killpg lchmod lchown linkat lstat lutimes mmap \
  memrchr mbrtowc mkdirat mkfifo \
  mkfifoat mknod mknodat mktime mremap nice openat pathconf pause pipe2 plock poll \
  posix_fallocate posix_fadvise pread \
@@ -3759,6 +3736,40 @@
   AC_MSG_RESULT(no)
 ])
 
+# On Android API level 24 with android-ndk-r13, if_nameindex() is available,
+# but the if_nameindex structure is not defined.
+AC_MSG_CHECKING(for if_nameindex)
+AC_LINK_IFELSE([AC_LANG_PROGRAM([[
+#include <stdio.h>
+#ifdef STDC_HEADERS
+# include <stdlib.h>
+# include <stddef.h>
+#else
+# ifdef HAVE_STDLIB_H
+#  include <stdlib.h>
+# endif
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#ifdef HAVE_NET_IF_H
+# include <net/if.h>
+#endif
+]], [[struct if_nameindex *ni = if_nameindex(); int x = ni[0].if_index;]])],
+  [AC_DEFINE(HAVE_IF_NAMEINDEX, 1, Define to 1 if you have the 'if_nameindex' function.)
+   AC_MSG_RESULT(yes)],
+  [AC_MSG_RESULT(no)
+])
+
+# Issue #28762: lockf() is available on Android API level 24, but the F_LOCK
+# macro is not defined in android-ndk-r13.
+AC_MSG_CHECKING(for lockf)
+AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h> ]],[[lockf(0, F_LOCK, 0);]])],
+  [AC_DEFINE(HAVE_LOCKF, 1, Define to 1 if you have the 'lockf' function and the F_LOCK macro.)
+   AC_MSG_RESULT(yes)],
+  [AC_MSG_RESULT(no)
+])
+
 # On OSF/1 V5.1, getaddrinfo is available, but a define
 # for [no]getaddrinfo in netdb.h.
 AC_MSG_CHECKING(for getaddrinfo)
@@ -4907,7 +4918,9 @@
 
 # first curses header check
 ac_save_cppflags="$CPPFLAGS"
-CPPFLAGS="$CPPFLAGS -I/usr/include/ncursesw"
+if test "$cross_compiling" = no; then
+  CPPFLAGS="$CPPFLAGS -I/usr/include/ncursesw"
+fi
 
 AC_CHECK_HEADERS(curses.h ncurses.h)
 
diff --git a/pyconfig.h.in b/pyconfig.h.in
index e7a836c..b10c57f 100644
--- a/pyconfig.h.in
+++ b/pyconfig.h.in
@@ -472,7 +472,7 @@
 /* Define to 1 if you have the <ieeefp.h> header file. */
 #undef HAVE_IEEEFP_H
 
-/* Define to 1 if you have the `if_nameindex' function. */
+/* Define to 1 if you have the 'if_nameindex' function. */
 #undef HAVE_IF_NAMEINDEX
 
 /* Define if you have the 'inet_aton' function. */
@@ -574,7 +574,7 @@
 /* Define to 1 if you have the <linux/tipc.h> header file. */
 #undef HAVE_LINUX_TIPC_H
 
-/* Define to 1 if you have the `lockf' function. */
+/* Define to 1 if you have the 'lockf' function and the F_LOCK macro. */
 #undef HAVE_LOCKF
 
 /* Define to 1 if you have the `log1p' function. */
@@ -1033,6 +1033,9 @@
 /* Define to 1 if you have the <sys/poll.h> header file. */
 #undef HAVE_SYS_POLL_H
 
+/* Define to 1 if you have the <sys/random.h> header file. */
+#undef HAVE_SYS_RANDOM_H
+
 /* Define to 1 if you have the <sys/resource.h> header file. */
 #undef HAVE_SYS_RESOURCE_H
 
diff --git a/setup.py b/setup.py
index af9a414..d218722 100644
--- a/setup.py
+++ b/setup.py
@@ -532,8 +532,9 @@
                     for directory in reversed(options.dirs):
                         add_dir_to_list(dir_list, directory)
 
-        if os.path.normpath(sys.base_prefix) != '/usr' \
-                and not sysconfig.get_config_var('PYTHONFRAMEWORK'):
+        if (not cross_compiling and
+                os.path.normpath(sys.base_prefix) != '/usr' and
+                not sysconfig.get_config_var('PYTHONFRAMEWORK')):
             # OSX note: Don't add LIBDIR and INCLUDEDIR to building a framework
             # (PYTHONFRAMEWORK is set) to avoid # linking problems when
             # building a framework with different architectures than
@@ -1349,7 +1350,8 @@
         panel_library = 'panel'
         if curses_library == 'ncursesw':
             curses_defines.append(('HAVE_NCURSESW', '1'))
-            curses_includes.append('/usr/include/ncursesw')
+            if not cross_compiling:
+                curses_includes.append('/usr/include/ncursesw')
             # Bug 1464056: If _curses.so links with ncursesw,
             # _curses_panel.so must link with panelw.
             panel_library = 'panelw'
@@ -1630,7 +1632,7 @@
 ##         ext = Extension('xx', ['xxmodule.c'])
 ##         self.extensions.append(ext)
 
-        if 'd' not in sys.abiflags:
+        if 'd' not in sysconfig.get_config_var('ABIFLAGS'):
             ext = Extension('xxlimited', ['xxlimited.c'],
                             define_macros=[('Py_LIMITED_API', '0x03050000')])
             self.extensions.append(ext)