Issue #17701: Improving strftime documentation
diff --git a/Demo/newmetaclasses/Eiffel.py b/Demo/newmetaclasses/Eiffel.py
index 730a85d..f3f116f 100644
--- a/Demo/newmetaclasses/Eiffel.py
+++ b/Demo/newmetaclasses/Eiffel.py
@@ -29,7 +29,7 @@
             pre = dict.get("%s_pre" % m)
             post = dict.get("%s_post" % m)
             if pre or post:
-                dict[k] = cls.make_eiffel_method(dict[m], pre, post)
+                dict[m] = cls.make_eiffel_method(dict[m], pre, post)
 
 class EiffelMetaClass1(EiffelBaseMetaClass):
     # an implementation of the "eiffel" meta class that uses nested functions
diff --git a/Doc/c-api/allocation.rst b/Doc/c-api/allocation.rst
index cb43cbf..32a414b 100644
--- a/Doc/c-api/allocation.rst
+++ b/Doc/c-api/allocation.rst
@@ -43,7 +43,7 @@
    Allocate a new Python object using the C structure type *TYPE* and the
    Python type object *type*.  Fields not defined by the Python object header
    are not initialized; the object's reference count will be one.  The size of
-   the memory allocation is determined from the :attr:`tp_basicsize` field of
+   the memory allocation is determined from the :c:member:`~PyTypeObject.tp_basicsize` field of
    the type object.
 
 
@@ -52,7 +52,7 @@
    Allocate a new Python object using the C structure type *TYPE* and the
    Python type object *type*.  Fields not defined by the Python object header
    are not initialized.  The allocated memory allows for the *TYPE* structure
-   plus *size* fields of the size given by the :attr:`tp_itemsize` field of
+   plus *size* fields of the size given by the :c:member:`~PyTypeObject.tp_itemsize` field of
    *type*.  This is useful for implementing objects like tuples, which are
    able to determine their size at construction time.  Embedding the array of
    fields into the same allocation decreases the number of allocations,
@@ -67,7 +67,7 @@
 
    Releases memory allocated to an object using :c:func:`PyObject_New` or
    :c:func:`PyObject_NewVar`.  This is normally called from the
-   :attr:`tp_dealloc` handler specified in the object's type.  The fields of
+   :c:member:`~PyTypeObject.tp_dealloc` handler specified in the object's type.  The fields of
    the object should not be accessed after this call as the memory is no
    longer a valid Python object.
 
diff --git a/Doc/c-api/gcsupport.rst b/Doc/c-api/gcsupport.rst
index 2a4fda4..b0a2d5c 100644
--- a/Doc/c-api/gcsupport.rst
+++ b/Doc/c-api/gcsupport.rst
@@ -15,10 +15,10 @@
 .. An example showing the use of these interfaces can be found in "Supporting the
 .. Cycle Collector (XXX not found: ../ext/example-cycle-support.html)".
 
-To create a container type, the :attr:`tp_flags` field of the type object must
+To create a container type, the :c:member:`~PyTypeObject.tp_flags` field of the type object must
 include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
-:attr:`tp_traverse` handler.  If instances of the type are mutable, a
-:attr:`tp_clear` implementation must also be provided.
+:c:member:`~PyTypeObject.tp_traverse` handler.  If instances of the type are mutable, a
+:c:member:`~PyTypeObject.tp_clear` implementation must also be provided.
 
 
 .. data:: Py_TPFLAGS_HAVE_GC
@@ -68,7 +68,7 @@
    Adds the object *op* to the set of container objects tracked by the
    collector.  The collector can run at unexpected times so objects must be
    valid while being tracked.  This should be called once all the fields
-   followed by the :attr:`tp_traverse` handler become valid, usually near the
+   followed by the :c:member:`~PyTypeObject.tp_traverse` handler become valid, usually near the
    end of the constructor.
 
 
@@ -97,8 +97,8 @@
    Remove the object *op* from the set of container objects tracked by the
    collector.  Note that :c:func:`PyObject_GC_Track` can be called again on
    this object to add it back to the set of tracked objects.  The deallocator
-   (:attr:`tp_dealloc` handler) should call this for the object before any of
-   the fields used by the :attr:`tp_traverse` handler become invalid.
+   (:c:member:`~PyTypeObject.tp_dealloc` handler) should call this for the object before any of
+   the fields used by the :c:member:`~PyTypeObject.tp_traverse` handler become invalid.
 
 
 .. c:function:: void _PyObject_GC_UNTRACK(PyObject *op)
@@ -106,19 +106,19 @@
    A macro version of :c:func:`PyObject_GC_UnTrack`.  It should not be used for
    extension modules.
 
-The :attr:`tp_traverse` handler accepts a function parameter of this type:
+The :c:member:`~PyTypeObject.tp_traverse` handler accepts a function parameter of this type:
 
 
 .. c:type:: int (*visitproc)(PyObject *object, void *arg)
 
-   Type of the visitor function passed to the :attr:`tp_traverse` handler.
+   Type of the visitor function passed to the :c:member:`~PyTypeObject.tp_traverse` handler.
    The function should be called with an object to traverse as *object* and
-   the third parameter to the :attr:`tp_traverse` handler as *arg*.  The
+   the third parameter to the :c:member:`~PyTypeObject.tp_traverse` handler as *arg*.  The
    Python core uses several visitor functions to implement cyclic garbage
    detection; it's not expected that users will need to write their own
    visitor functions.
 
-The :attr:`tp_traverse` handler must have the following type:
+The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
 
 
 .. c:type:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
@@ -130,15 +130,15 @@
    object argument.  If *visit* returns a non-zero value that value should be
    returned immediately.
 
-To simplify writing :attr:`tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
-provided.  In order to use this macro, the :attr:`tp_traverse` implementation
+To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
+provided.  In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation
 must name its arguments exactly *visit* and *arg*:
 
 
 .. c:function:: void Py_VISIT(PyObject *o)
 
    Call the *visit* callback, with arguments *o* and *arg*. If *visit* returns
-   a non-zero value, then return it.  Using this macro, :attr:`tp_traverse`
+   a non-zero value, then return it.  Using this macro, :c:member:`~PyTypeObject.tp_traverse`
    handlers look like::
 
       static int
@@ -151,7 +151,7 @@
 
    .. versionadded:: 2.4
 
-The :attr:`tp_clear` handler must be of the :c:type:`inquiry` type, or *NULL*
+The :c:member:`~PyTypeObject.tp_clear` handler must be of the :c:type:`inquiry` type, or *NULL*
 if the object is immutable.
 
 
diff --git a/Doc/c-api/structures.rst b/Doc/c-api/structures.rst
index f5007ac..e31687f 100644
--- a/Doc/c-api/structures.rst
+++ b/Doc/c-api/structures.rst
@@ -293,6 +293,6 @@
 .. c:function:: PyObject* Py_FindMethod(PyMethodDef table[], PyObject *ob, char *name)
 
    Return a bound method object for an extension type implemented in C.  This
-   can be useful in the implementation of a :attr:`tp_getattro` or
-   :attr:`tp_getattr` handler that does not use the
+   can be useful in the implementation of a :c:member:`~PyTypeObject.tp_getattro` or
+   :c:member:`~PyTypeObject.tp_getattr` handler that does not use the
    :c:func:`PyObject_GenericGetAttr` function.
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index 7c37786..c35f727 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -35,7 +35,7 @@
 The type object structure extends the :c:type:`PyVarObject` structure. The
 :attr:`ob_size` field is used for dynamic types (created by  :func:`type_new`,
 usually called from a class statement). Note that :c:data:`PyType_Type` (the
-metatype) initializes :attr:`tp_itemsize`, which means that its instances (i.e.
+metatype) initializes :c:member:`~PyTypeObject.tp_itemsize`, which means that its instances (i.e.
 type objects) *must* have the :attr:`ob_size` field.
 
 
@@ -108,7 +108,7 @@
    should be just the type name.  If the module is a submodule of a package, the
    full package name is part of the full module name.  For example, a type named
    :class:`T` defined in module :mod:`M` in subpackage :mod:`Q` in package :mod:`P`
-   should have the :attr:`tp_name` initializer ``"P.Q.M.T"``.
+   should have the :c:member:`~PyTypeObject.tp_name` initializer ``"P.Q.M.T"``.
 
    For dynamically allocated type objects, this should just be the type name, and
    the module name explicitly stored in the type dict as the value for key
@@ -119,7 +119,7 @@
    attribute, and everything after the last dot is made accessible as the
    :attr:`__name__` attribute.
 
-   If no dot is present, the entire :attr:`tp_name` field is made accessible as the
+   If no dot is present, the entire :c:member:`~PyTypeObject.tp_name` field is made accessible as the
    :attr:`__name__` attribute, and the :attr:`__module__` attribute is undefined
    (unless explicitly set in the dictionary, as explained above).  This means your
    type will be impossible to pickle.
@@ -133,13 +133,13 @@
    These fields allow calculating the size in bytes of instances of the type.
 
    There are two kinds of types: types with fixed-length instances have a zero
-   :attr:`tp_itemsize` field, types with variable-length instances have a non-zero
-   :attr:`tp_itemsize` field.  For a type with fixed-length instances, all
-   instances have the same size, given in :attr:`tp_basicsize`.
+   :c:member:`~PyTypeObject.tp_itemsize` field, types with variable-length instances have a non-zero
+   :c:member:`~PyTypeObject.tp_itemsize` field.  For a type with fixed-length instances, all
+   instances have the same size, given in :c:member:`~PyTypeObject.tp_basicsize`.
 
    For a type with variable-length instances, the instances must have an
-   :attr:`ob_size` field, and the instance size is :attr:`tp_basicsize` plus N
-   times :attr:`tp_itemsize`, where N is the "length" of the object.  The value of
+   :attr:`ob_size` field, and the instance size is :c:member:`~PyTypeObject.tp_basicsize` plus N
+   times :c:member:`~PyTypeObject.tp_itemsize`, where N is the "length" of the object.  The value of
    N is typically stored in the instance's :attr:`ob_size` field.  There are
    exceptions:  for example, long ints use a negative :attr:`ob_size` to indicate a
    negative number, and N is ``abs(ob_size)`` there.  Also, the presence of an
@@ -152,21 +152,21 @@
    :c:macro:`PyObject_HEAD` or :c:macro:`PyObject_VAR_HEAD` (whichever is used to
    declare the instance struct) and this in turn includes the :attr:`_ob_prev` and
    :attr:`_ob_next` fields if they are present.  This means that the only correct
-   way to get an initializer for the :attr:`tp_basicsize` is to use the
+   way to get an initializer for the :c:member:`~PyTypeObject.tp_basicsize` is to use the
    ``sizeof`` operator on the struct used to declare the instance layout.
    The basic size does not include the GC header size (this is new in Python 2.2;
-   in 2.1 and 2.0, the GC header size was included in :attr:`tp_basicsize`).
+   in 2.1 and 2.0, the GC header size was included in :c:member:`~PyTypeObject.tp_basicsize`).
 
    These fields are inherited separately by subtypes.  If the base type has a
-   non-zero :attr:`tp_itemsize`, it is generally not safe to set
-   :attr:`tp_itemsize` to a different non-zero value in a subtype (though this
+   non-zero :c:member:`~PyTypeObject.tp_itemsize`, it is generally not safe to set
+   :c:member:`~PyTypeObject.tp_itemsize` to a different non-zero value in a subtype (though this
    depends on the implementation of the base type).
 
    A note about alignment: if the variable items require a particular alignment,
-   this should be taken care of by the value of :attr:`tp_basicsize`.  Example:
-   suppose a type implements an array of ``double``. :attr:`tp_itemsize` is
+   this should be taken care of by the value of :c:member:`~PyTypeObject.tp_basicsize`.  Example:
+   suppose a type implements an array of ``double``. :c:member:`~PyTypeObject.tp_itemsize` is
    ``sizeof(double)``. It is the programmer's responsibility that
-   :attr:`tp_basicsize` is a multiple of ``sizeof(double)`` (assuming this is the
+   :c:member:`~PyTypeObject.tp_basicsize` is a multiple of ``sizeof(double)`` (assuming this is the
    alignment requirement for ``double``).
 
 
@@ -182,10 +182,10 @@
    destructor function should free all references which the instance owns, free all
    memory buffers owned by the instance (using the freeing function corresponding
    to the allocation function used to allocate the buffer), and finally (as its
-   last action) call the type's :attr:`tp_free` function.  If the type is not
+   last action) call the type's :c:member:`~PyTypeObject.tp_free` function.  If the type is not
    subtypable (doesn't have the :const:`Py_TPFLAGS_BASETYPE` flag bit set), it is
    permissible to call the object deallocator directly instead of via
-   :attr:`tp_free`.  The object deallocator should be the one used to allocate the
+   :c:member:`~PyTypeObject.tp_free`.  The object deallocator should be the one used to allocate the
    instance; this is normally :c:func:`PyObject_Del` if the instance was allocated
    using :c:func:`PyObject_New` or :c:func:`PyObject_VarNew`, or
    :c:func:`PyObject_GC_Del` if the instance was allocated using
@@ -200,25 +200,25 @@
 
    The print function is only called when the instance is printed to a *real* file;
    when it is printed to a pseudo-file (like a :class:`StringIO` instance), the
-   instance's :attr:`tp_repr` or :attr:`tp_str` function is called to convert it to
-   a string.  These are also called when the type's :attr:`tp_print` field is
-   *NULL*.  A type should never implement :attr:`tp_print` in a way that produces
-   different output than :attr:`tp_repr` or :attr:`tp_str` would.
+   instance's :c:member:`~PyTypeObject.tp_repr` or :c:member:`~PyTypeObject.tp_str` function is called to convert it to
+   a string.  These are also called when the type's :c:member:`~PyTypeObject.tp_print` field is
+   *NULL*.  A type should never implement :c:member:`~PyTypeObject.tp_print` in a way that produces
+   different output than :c:member:`~PyTypeObject.tp_repr` or :c:member:`~PyTypeObject.tp_str` would.
 
    The print function is called with the same signature as :c:func:`PyObject_Print`:
    ``int tp_print(PyObject *self, FILE *file, int flags)``.  The *self* argument is
    the instance to be printed.  The *file* argument is the stdio file to which it
    is to be printed.  The *flags* argument is composed of flag bits. The only flag
    bit currently defined is :const:`Py_PRINT_RAW`. When the :const:`Py_PRINT_RAW`
-   flag bit is set, the instance should be printed the same way as :attr:`tp_str`
+   flag bit is set, the instance should be printed the same way as :c:member:`~PyTypeObject.tp_str`
    would format it; when the :const:`Py_PRINT_RAW` flag bit is clear, the instance
-   should be printed the same was as :attr:`tp_repr` would format it. It should
+   should be printed the same was as :c:member:`~PyTypeObject.tp_repr` would format it. It should
    return ``-1`` and set an exception condition when an error occurred during the
    comparison.
 
-   It is possible that the :attr:`tp_print` field will be deprecated. In any case,
-   it is recommended not to define :attr:`tp_print`, but instead to rely on
-   :attr:`tp_repr` and :attr:`tp_str` for printing.
+   It is possible that the :c:member:`~PyTypeObject.tp_print` field will be deprecated. In any case,
+   it is recommended not to define :c:member:`~PyTypeObject.tp_print`, but instead to rely on
+   :c:member:`~PyTypeObject.tp_repr` and :c:member:`~PyTypeObject.tp_str` for printing.
 
    This field is inherited by subtypes.
 
@@ -228,13 +228,13 @@
    An optional pointer to the get-attribute-string function.
 
    This field is deprecated.  When it is defined, it should point to a function
-   that acts the same as the :attr:`tp_getattro` function, but taking a C string
+   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`.
 
-   This field is inherited by subtypes together with :attr:`tp_getattro`: a subtype
-   inherits both :attr:`tp_getattr` and :attr:`tp_getattro` from its base type when
-   the subtype's :attr:`tp_getattr` and :attr:`tp_getattro` are both *NULL*.
+   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
+   the subtype's :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` are both *NULL*.
 
 
 .. c:member:: setattrfunc PyTypeObject.tp_setattr
@@ -242,13 +242,13 @@
    An optional pointer to the set-attribute-string function.
 
    This field is deprecated.  When it is defined, it should point to a function
-   that acts the same as the :attr:`tp_setattro` function, but taking a C string
+   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`.
 
-   This field is inherited by subtypes together with :attr:`tp_setattro`: a subtype
-   inherits both :attr:`tp_setattr` and :attr:`tp_setattro` from its base type when
-   the subtype's :attr:`tp_setattr` and :attr:`tp_setattro` are both *NULL*.
+   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*.
 
 
 .. c:member:: cmpfunc PyTypeObject.tp_compare
@@ -260,10 +260,10 @@
    *other*, and ``-1`` if *self* less than *other*.  It should return ``-1`` and
    set an exception condition when an error occurred during the comparison.
 
-   This field is inherited by subtypes together with :attr:`tp_richcompare` and
-   :attr:`tp_hash`: a subtypes inherits all three of :attr:`tp_compare`,
-   :attr:`tp_richcompare`, and :attr:`tp_hash` when the subtype's
-   :attr:`tp_compare`, :attr:`tp_richcompare`, and :attr:`tp_hash` are all *NULL*.
+   This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_richcompare` and
+   :c:member:`~PyTypeObject.tp_hash`: a subtypes inherits all three of :c:member:`~PyTypeObject.tp_compare`,
+   :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash` when the subtype's
+   :c:member:`~PyTypeObject.tp_compare`, :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash` are all *NULL*.
 
 
 .. c:member:: reprfunc PyTypeObject.tp_repr
@@ -292,7 +292,7 @@
    objects which implement the number protocol.  These fields are documented in
    :ref:`number-structs`.
 
-   The :attr:`tp_as_number` field is not inherited, but the contained fields are
+   The :c:member:`~PyTypeObject.tp_as_number` field is not inherited, but the contained fields are
    inherited individually.
 
 
@@ -302,7 +302,7 @@
    objects which implement the sequence protocol.  These fields are documented
    in :ref:`sequence-structs`.
 
-   The :attr:`tp_as_sequence` field is not inherited, but the contained fields
+   The :c:member:`~PyTypeObject.tp_as_sequence` field is not inherited, but the contained fields
    are inherited individually.
 
 
@@ -312,7 +312,7 @@
    objects which implement the mapping protocol.  These fields are documented in
    :ref:`mapping-structs`.
 
-   The :attr:`tp_as_mapping` field is not inherited, but the contained fields
+   The :c:member:`~PyTypeObject.tp_as_mapping` field is not inherited, but the contained fields
    are inherited individually.
 
 
@@ -336,14 +336,14 @@
    the Python level will result in the ``tp_hash`` slot being set to
    :c:func:`PyObject_HashNotImplemented`.
 
-   When this field is not set, two possibilities exist: if the :attr:`tp_compare`
-   and :attr:`tp_richcompare` fields are both *NULL*, a default hash value based on
+   When this field is not set, two possibilities exist: if the :c:member:`~PyTypeObject.tp_compare`
+   and :c:member:`~PyTypeObject.tp_richcompare` fields are both *NULL*, a default hash value based on
    the object's address is returned; otherwise, a :exc:`TypeError` is raised.
 
-   This field is inherited by subtypes together with :attr:`tp_richcompare` and
-   :attr:`tp_compare`: a subtypes inherits all three of :attr:`tp_compare`,
-   :attr:`tp_richcompare`, and :attr:`tp_hash`, when the subtype's
-   :attr:`tp_compare`, :attr:`tp_richcompare` and :attr:`tp_hash` are all *NULL*.
+   This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_richcompare` and
+   :c:member:`~PyTypeObject.tp_compare`: a subtypes inherits all three of :c:member:`~PyTypeObject.tp_compare`,
+   :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash`, when the subtype's
+   :c:member:`~PyTypeObject.tp_compare`, :c:member:`~PyTypeObject.tp_richcompare` and :c:member:`~PyTypeObject.tp_hash` are all *NULL*.
 
 
 .. c:member:: ternaryfunc PyTypeObject.tp_call
@@ -381,9 +381,9 @@
    convenient to set this field to :c:func:`PyObject_GenericGetAttr`, which
    implements the normal way of looking for object attributes.
 
-   This field is inherited by subtypes together with :attr:`tp_getattr`: a subtype
-   inherits both :attr:`tp_getattr` and :attr:`tp_getattro` from its base type when
-   the subtype's :attr:`tp_getattr` and :attr:`tp_getattro` are both *NULL*.
+   This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_getattr`: a subtype
+   inherits both :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` from its base type when
+   the subtype's :c:member:`~PyTypeObject.tp_getattr` and :c:member:`~PyTypeObject.tp_getattro` are both *NULL*.
 
 
 .. c:member:: setattrofunc PyTypeObject.tp_setattro
@@ -394,9 +394,9 @@
    convenient to set this field to :c:func:`PyObject_GenericSetAttr`, which
    implements the normal way of setting object attributes.
 
-   This field is inherited by subtypes together with :attr:`tp_setattr`: a subtype
-   inherits both :attr:`tp_setattr` and :attr:`tp_setattro` from its base type when
-   the subtype's :attr:`tp_setattr` and :attr:`tp_setattro` are both *NULL*.
+   This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_setattr`: 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*.
 
 
 .. c:member:: PyBufferProcs* PyTypeObject.tp_as_buffer
@@ -405,7 +405,7 @@
    which implement the buffer interface.  These fields are documented in
    :ref:`buffer-structs`.
 
-   The :attr:`tp_as_buffer` field is not inherited, but the contained fields are
+   The :c:member:`~PyTypeObject.tp_as_buffer` field is not inherited, but the contained fields are
    inherited individually.
 
 
@@ -414,8 +414,8 @@
    This field is a bit mask of various flags.  Some flags indicate variant
    semantics for certain situations; others are used to indicate that certain
    fields in the type object (or in the extension structures referenced via
-   :attr:`tp_as_number`, :attr:`tp_as_sequence`, :attr:`tp_as_mapping`, and
-   :attr:`tp_as_buffer`) that were historically not always present are valid; if
+   :c:member:`~PyTypeObject.tp_as_number`, :c:member:`~PyTypeObject.tp_as_sequence`, :c:member:`~PyTypeObject.tp_as_mapping`, and
+   :c:member:`~PyTypeObject.tp_as_buffer`) that were historically not always present are valid; if
    such a flag bit is clear, the type fields it guards must not be accessed and
    must be considered to have a zero or *NULL* value instead.
 
@@ -425,14 +425,14 @@
    inherited if the extension structure is inherited, i.e. the base type's value of
    the flag bit is copied into the subtype together with a pointer to the extension
    structure.  The :const:`Py_TPFLAGS_HAVE_GC` flag bit is inherited together with
-   the :attr:`tp_traverse` and :attr:`tp_clear` fields, i.e. if the
+   the :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` fields, i.e. if the
    :const:`Py_TPFLAGS_HAVE_GC` flag bit is clear in the subtype and the
-   :attr:`tp_traverse` and :attr:`tp_clear` fields in the subtype exist (as
+   :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` fields in the subtype exist (as
    indicated by the :const:`Py_TPFLAGS_HAVE_RICHCOMPARE` flag bit) and have *NULL*
    values.
 
    The following bit masks are currently defined; these can be ORed together using
-   the ``|`` operator to form the value of the :attr:`tp_flags` field.  The macro
+   the ``|`` operator to form the value of the :c:member:`~PyTypeObject.tp_flags` field.  The macro
    :c:func:`PyType_HasFeature` takes a type and a flags value, *tp* and *f*, and
    checks whether ``tp->tp_flags & f`` is non-zero.
 
@@ -440,13 +440,13 @@
    .. data:: Py_TPFLAGS_HAVE_GETCHARBUFFER
 
       If this bit is set, the :c:type:`PyBufferProcs` struct referenced by
-      :attr:`tp_as_buffer` has the :attr:`bf_getcharbuffer` field.
+      :c:member:`~PyTypeObject.tp_as_buffer` has the :attr:`bf_getcharbuffer` field.
 
 
    .. data:: Py_TPFLAGS_HAVE_SEQUENCE_IN
 
       If this bit is set, the :c:type:`PySequenceMethods` struct referenced by
-      :attr:`tp_as_sequence` has the :attr:`sq_contains` field.
+      :c:member:`~PyTypeObject.tp_as_sequence` has the :attr:`sq_contains` field.
 
 
    .. data:: Py_TPFLAGS_GC
@@ -458,8 +458,8 @@
    .. data:: Py_TPFLAGS_HAVE_INPLACEOPS
 
       If this bit is set, the :c:type:`PySequenceMethods` struct referenced by
-      :attr:`tp_as_sequence` and the :c:type:`PyNumberMethods` structure referenced by
-      :attr:`tp_as_number` contain the fields for in-place operators. In particular,
+      :c:member:`~PyTypeObject.tp_as_sequence` and the :c:type:`PyNumberMethods` structure referenced by
+      :c:member:`~PyTypeObject.tp_as_number` contain the fields for in-place operators. In particular,
       this means that the :c:type:`PyNumberMethods` structure has the fields
       :attr:`nb_inplace_add`, :attr:`nb_inplace_subtract`,
       :attr:`nb_inplace_multiply`, :attr:`nb_inplace_divide`,
@@ -473,7 +473,7 @@
    .. data:: Py_TPFLAGS_CHECKTYPES
 
       If this bit is set, the binary and ternary operations in the
-      :c:type:`PyNumberMethods` structure referenced by :attr:`tp_as_number` accept
+      :c:type:`PyNumberMethods` structure referenced by :c:member:`~PyTypeObject.tp_as_number` accept
       arguments of arbitrary object types, and do their own type conversions if
       needed.  If this bit is clear, those operations require that all arguments have
       the current type as their type, and the caller is supposed to perform a coercion
@@ -485,31 +485,31 @@
 
    .. data:: Py_TPFLAGS_HAVE_RICHCOMPARE
 
-      If this bit is set, the type object has the :attr:`tp_richcompare` field, as
-      well as the :attr:`tp_traverse` and the :attr:`tp_clear` fields.
+      If this bit is set, the type object has the :c:member:`~PyTypeObject.tp_richcompare` field, as
+      well as the :c:member:`~PyTypeObject.tp_traverse` and the :c:member:`~PyTypeObject.tp_clear` fields.
 
 
    .. data:: Py_TPFLAGS_HAVE_WEAKREFS
 
-      If this bit is set, the :attr:`tp_weaklistoffset` field is defined.  Instances
-      of a type are weakly referenceable if the type's :attr:`tp_weaklistoffset` field
+      If this bit is set, the :c:member:`~PyTypeObject.tp_weaklistoffset` field is defined.  Instances
+      of a type are weakly referenceable if the type's :c:member:`~PyTypeObject.tp_weaklistoffset` field
       has a value greater than zero.
 
 
    .. data:: Py_TPFLAGS_HAVE_ITER
 
-      If this bit is set, the type object has the :attr:`tp_iter` and
-      :attr:`tp_iternext` fields.
+      If this bit is set, the type object has the :c:member:`~PyTypeObject.tp_iter` and
+      :c:member:`~PyTypeObject.tp_iternext` fields.
 
 
    .. data:: Py_TPFLAGS_HAVE_CLASS
 
       If this bit is set, the type object has several new fields defined starting in
-      Python 2.2: :attr:`tp_methods`, :attr:`tp_members`, :attr:`tp_getset`,
-      :attr:`tp_base`, :attr:`tp_dict`, :attr:`tp_descr_get`, :attr:`tp_descr_set`,
-      :attr:`tp_dictoffset`, :attr:`tp_init`, :attr:`tp_alloc`, :attr:`tp_new`,
-      :attr:`tp_free`, :attr:`tp_is_gc`, :attr:`tp_bases`, :attr:`tp_mro`,
-      :attr:`tp_cache`, :attr:`tp_subclasses`, and :attr:`tp_weaklist`.
+      Python 2.2: :c:member:`~PyTypeObject.tp_methods`, :c:member:`~PyTypeObject.tp_members`, :c:member:`~PyTypeObject.tp_getset`,
+      :c:member:`~PyTypeObject.tp_base`, :c:member:`~PyTypeObject.tp_dict`, :c:member:`~PyTypeObject.tp_descr_get`, :c:member:`~PyTypeObject.tp_descr_set`,
+      :c:member:`~PyTypeObject.tp_dictoffset`, :c:member:`~PyTypeObject.tp_init`, :c:member:`~PyTypeObject.tp_alloc`, :c:member:`~PyTypeObject.tp_new`,
+      :c:member:`~PyTypeObject.tp_free`, :c:member:`~PyTypeObject.tp_is_gc`, :c:member:`~PyTypeObject.tp_bases`, :c:member:`~PyTypeObject.tp_mro`,
+      :c:member:`~PyTypeObject.tp_cache`, :c:member:`~PyTypeObject.tp_subclasses`, and :c:member:`~PyTypeObject.tp_weaklist`.
 
 
    .. data:: Py_TPFLAGS_HEAPTYPE
@@ -547,7 +547,7 @@
       is set, instances must be created using :c:func:`PyObject_GC_New` and
       destroyed using :c:func:`PyObject_GC_Del`.  More information in section
       :ref:`supporting-cycle-detection`.  This bit also implies that the
-      GC-related fields :attr:`tp_traverse` and :attr:`tp_clear` are present in
+      GC-related fields :c:member:`~PyTypeObject.tp_traverse` and :c:member:`~PyTypeObject.tp_clear` are present in
       the type object; but those fields also exist when
       :const:`Py_TPFLAGS_HAVE_GC` is clear but
       :const:`Py_TPFLAGS_HAVE_RICHCOMPARE` is set.
@@ -582,8 +582,8 @@
    about Python's garbage collection scheme can be found in section
    :ref:`supporting-cycle-detection`.
 
-   The :attr:`tp_traverse` pointer is used by the garbage collector to detect
-   reference cycles. A typical implementation of a :attr:`tp_traverse` function
+   The :c:member:`~PyTypeObject.tp_traverse` pointer is used by the garbage collector to detect
+   reference cycles. A typical implementation of a :c:member:`~PyTypeObject.tp_traverse` function
    simply calls :c:func:`Py_VISIT` on each of the instance's members that are Python
    objects.  For example, this is function :c:func:`local_traverse` from the
    :mod:`thread` extension module::
@@ -609,9 +609,9 @@
    :c:func:`local_traverse` to have these specific names; don't name them just
    anything.
 
-   This field is inherited by subtypes together with :attr:`tp_clear` and the
-   :const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :attr:`tp_traverse`, and
-   :attr:`tp_clear` are all inherited from the base type if they are all zero in
+   This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_clear` and the
+   :const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :c:member:`~PyTypeObject.tp_traverse`, and
+   :c:member:`~PyTypeObject.tp_clear` are all inherited from the base type if they are all zero in
    the subtype *and* the subtype has the :const:`Py_TPFLAGS_HAVE_RICHCOMPARE` flag
    bit set.
 
@@ -621,17 +621,17 @@
    An optional pointer to a clear function for the garbage collector. This is only
    used if the :const:`Py_TPFLAGS_HAVE_GC` flag bit is set.
 
-   The :attr:`tp_clear` member function is used to break reference cycles in cyclic
-   garbage detected by the garbage collector.  Taken together, all :attr:`tp_clear`
+   The :c:member:`~PyTypeObject.tp_clear` member function is used to break reference cycles in cyclic
+   garbage detected by the garbage collector.  Taken together, all :c:member:`~PyTypeObject.tp_clear`
    functions in the system must combine to break all reference cycles.  This is
-   subtle, and if in any doubt supply a :attr:`tp_clear` function.  For example,
-   the tuple type does not implement a :attr:`tp_clear` function, because it's
+   subtle, and if in any doubt supply a :c:member:`~PyTypeObject.tp_clear` function.  For example,
+   the tuple type does not implement a :c:member:`~PyTypeObject.tp_clear` function, because it's
    possible to prove that no reference cycle can be composed entirely of tuples.
-   Therefore the :attr:`tp_clear` functions of other types must be sufficient to
+   Therefore the :c:member:`~PyTypeObject.tp_clear` functions of other types must be sufficient to
    break any cycle containing a tuple.  This isn't immediately obvious, and there's
-   rarely a good reason to avoid implementing :attr:`tp_clear`.
+   rarely a good reason to avoid implementing :c:member:`~PyTypeObject.tp_clear`.
 
-   Implementations of :attr:`tp_clear` should drop the instance's references to
+   Implementations of :c:member:`~PyTypeObject.tp_clear` should drop the instance's references to
    those of its members that may be Python objects, and set its pointers to those
    members to *NULL*, as in the following example::
 
@@ -656,18 +656,18 @@
    so that *self* knows the contained object can no longer be used.  The
    :c:func:`Py_CLEAR` macro performs the operations in a safe order.
 
-   Because the goal of :attr:`tp_clear` functions is to break reference cycles,
+   Because the goal of :c:member:`~PyTypeObject.tp_clear` functions is to break reference cycles,
    it's not necessary to clear contained objects like Python strings or Python
    integers, which can't participate in reference cycles. On the other hand, it may
    be convenient to clear all contained Python objects, and write the type's
-   :attr:`tp_dealloc` function to invoke :attr:`tp_clear`.
+   :c:member:`~PyTypeObject.tp_dealloc` function to invoke :c:member:`~PyTypeObject.tp_clear`.
 
    More information about Python's garbage collection scheme can be found in
    section :ref:`supporting-cycle-detection`.
 
-   This field is inherited by subtypes together with :attr:`tp_traverse` and the
-   :const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :attr:`tp_traverse`, and
-   :attr:`tp_clear` are all inherited from the base type if they are all zero in
+   This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_traverse` and the
+   :const:`Py_TPFLAGS_HAVE_GC` flag bit: the flag bit, :c:member:`~PyTypeObject.tp_traverse`, and
+   :c:member:`~PyTypeObject.tp_clear` are all inherited from the base type if they are all zero in
    the subtype *and* the subtype has the :const:`Py_TPFLAGS_HAVE_RICHCOMPARE` flag
    bit set.
 
@@ -688,13 +688,13 @@
       comparisons makes sense (e.g. ``==`` and ``!=``, but not ``<`` and
       friends), directly raise :exc:`TypeError` in the rich comparison function.
 
-   This field is inherited by subtypes together with :attr:`tp_compare` and
-   :attr:`tp_hash`: a subtype inherits all three of :attr:`tp_compare`,
-   :attr:`tp_richcompare`, and :attr:`tp_hash`, when the subtype's
-   :attr:`tp_compare`, :attr:`tp_richcompare`, and :attr:`tp_hash` are all *NULL*.
+   This field is inherited by subtypes together with :c:member:`~PyTypeObject.tp_compare` and
+   :c:member:`~PyTypeObject.tp_hash`: a subtype inherits all three of :c:member:`~PyTypeObject.tp_compare`,
+   :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash`, when the subtype's
+   :c:member:`~PyTypeObject.tp_compare`, :c:member:`~PyTypeObject.tp_richcompare`, and :c:member:`~PyTypeObject.tp_hash` are all *NULL*.
 
    The following constants are defined to be used as the third argument for
-   :attr:`tp_richcompare` and for :c:func:`PyObject_RichCompare`:
+   :c:member:`~PyTypeObject.tp_richcompare` and for :c:func:`PyObject_RichCompare`:
 
    +----------------+------------+
    | Constant       | Comparison |
@@ -725,26 +725,26 @@
    instance structure needs to include a field of type :c:type:`PyObject\*` which is
    initialized to *NULL*.
 
-   Do not confuse this field with :attr:`tp_weaklist`; that is the list head for
+   Do not confuse this field with :c:member:`~PyTypeObject.tp_weaklist`; that is the list head for
    weak references to the type object itself.
 
    This field is inherited by subtypes, but see the rules listed below. A subtype
    may override this offset; this means that the subtype uses a different weak
    reference list head than the base type.  Since the list head is always found via
-   :attr:`tp_weaklistoffset`, this should not be a problem.
+   :c:member:`~PyTypeObject.tp_weaklistoffset`, this should not be a problem.
 
    When a type defined by a class statement has no :attr:`__slots__` declaration,
    and none of its base types are weakly referenceable, the type is made weakly
    referenceable by adding a weak reference list head slot to the instance layout
-   and setting the :attr:`tp_weaklistoffset` of that slot's offset.
+   and setting the :c:member:`~PyTypeObject.tp_weaklistoffset` of that slot's offset.
 
    When a type's :attr:`__slots__` declaration contains a slot named
    :attr:`__weakref__`, that slot becomes the weak reference list head for
    instances of the type, and the slot's offset is stored in the type's
-   :attr:`tp_weaklistoffset`.
+   :c:member:`~PyTypeObject.tp_weaklistoffset`.
 
    When a type's :attr:`__slots__` declaration does not contain a slot named
-   :attr:`__weakref__`, the type inherits its :attr:`tp_weaklistoffset` from its
+   :attr:`__weakref__`, the type inherits its :c:member:`~PyTypeObject.tp_weaklistoffset` from its
    base type.
 
 The next two fields only exist if the :const:`Py_TPFLAGS_HAVE_ITER` flag bit is
@@ -772,7 +772,7 @@
    are iterators (although classic instances always have this function, even if
    they don't define a :meth:`next` method).
 
-   Iterator types should also define the :attr:`tp_iter` function, and that
+   Iterator types should also define the :c:member:`~PyTypeObject.tp_iter` function, and that
    function should return the iterator instance itself (not a new iterator
    instance).
 
@@ -780,7 +780,7 @@
 
    This field is inherited by subtypes.
 
-The next fields, up to and including :attr:`tp_weaklist`, only exist if the
+The next fields, up to and including :c:member:`~PyTypeObject.tp_weaklist`, only exist if the
 :const:`Py_TPFLAGS_HAVE_CLASS` flag bit is set.
 
 
@@ -790,7 +790,7 @@
    structures, declaring regular methods of this type.
 
    For each entry in the array, an entry is added to the type's dictionary (see
-   :attr:`tp_dict` below) containing a method descriptor.
+   :c:member:`~PyTypeObject.tp_dict` below) containing a method descriptor.
 
    This field is not inherited by subtypes (methods are inherited through a
    different mechanism).
@@ -803,7 +803,7 @@
    this type.
 
    For each entry in the array, an entry is added to the type's dictionary (see
-   :attr:`tp_dict` below) containing a member descriptor.
+   :c:member:`~PyTypeObject.tp_dict` below) containing a member descriptor.
 
    This field is not inherited by subtypes (members are inherited through a
    different mechanism).
@@ -815,7 +815,7 @@
    structures, declaring computed attributes of instances of this type.
 
    For each entry in the array, an entry is added to the type's dictionary (see
-   :attr:`tp_dict` below) containing a getset descriptor.
+   :c:member:`~PyTypeObject.tp_dict` below) containing a getset descriptor.
 
    This field is not inherited by subtypes (computed attributes are inherited
    through a different mechanism).
@@ -894,7 +894,7 @@
    the instance variable dictionary; this offset is used by
    :c:func:`PyObject_GenericGetAttr`.
 
-   Do not confuse this field with :attr:`tp_dict`; that is the dictionary for
+   Do not confuse this field with :c:member:`~PyTypeObject.tp_dict`; that is the dictionary for
    attributes of the type object itself.
 
    If the value of this field is greater than zero, it specifies the offset from
@@ -903,20 +903,20 @@
    offset is more expensive to use, and should only be used when the instance
    structure contains a variable-length part.  This is used for example to add an
    instance variable dictionary to subtypes of :class:`str` or :class:`tuple`. Note
-   that the :attr:`tp_basicsize` field should account for the dictionary added to
+   that the :c:member:`~PyTypeObject.tp_basicsize` field should account for the dictionary added to
    the end in that case, even though the dictionary is not included in the basic
    object layout.  On a system with a pointer size of 4 bytes,
-   :attr:`tp_dictoffset` should be set to ``-4`` to indicate that the dictionary is
+   :c:member:`~PyTypeObject.tp_dictoffset` should be set to ``-4`` to indicate that the dictionary is
    at the very end of the structure.
 
    The real dictionary offset in an instance can be computed from a negative
-   :attr:`tp_dictoffset` as follows::
+   :c:member:`~PyTypeObject.tp_dictoffset` as follows::
 
       dictoffset = tp_basicsize + abs(ob_size)*tp_itemsize + tp_dictoffset
       if dictoffset is not aligned on sizeof(void*):
           round up to sizeof(void*)
 
-   where :attr:`tp_basicsize`, :attr:`tp_itemsize` and :attr:`tp_dictoffset` are
+   where :c:member:`~PyTypeObject.tp_basicsize`, :c:member:`~PyTypeObject.tp_itemsize` and :c:member:`~PyTypeObject.tp_dictoffset` are
    taken from the type object, and :attr:`ob_size` is taken from the instance.  The
    absolute value is taken because long ints use the sign of :attr:`ob_size` to
    store the sign of the number.  (There's never a need to do this calculation
@@ -925,15 +925,15 @@
    This field is inherited by subtypes, but see the rules listed below. A subtype
    may override this offset; this means that the subtype instances store the
    dictionary at a difference offset than the base type.  Since the dictionary is
-   always found via :attr:`tp_dictoffset`, this should not be a problem.
+   always found via :c:member:`~PyTypeObject.tp_dictoffset`, this should not be a problem.
 
    When a type defined by a class statement has no :attr:`__slots__` declaration,
    and none of its base types has an instance variable dictionary, a dictionary
-   slot is added to the instance layout and the :attr:`tp_dictoffset` is set to
+   slot is added to the instance layout and the :c:member:`~PyTypeObject.tp_dictoffset` is set to
    that slot's offset.
 
    When a type defined by a class statement has a :attr:`__slots__` declaration,
-   the type inherits its :attr:`tp_dictoffset` from its base type.
+   the type inherits its :c:member:`~PyTypeObject.tp_dictoffset` from its base type.
 
    (Adding a slot named :attr:`__dict__` to the :attr:`__slots__` declaration does
    not have the expected effect, it just causes confusion.  Maybe this should be
@@ -957,15 +957,15 @@
    arguments represent positional and keyword arguments of the call to
    :meth:`__init__`.
 
-   The :attr:`tp_init` function, if not *NULL*, is called when an instance is
-   created normally by calling its type, after the type's :attr:`tp_new` function
-   has returned an instance of the type.  If the :attr:`tp_new` function returns an
+   The :c:member:`~PyTypeObject.tp_init` function, if not *NULL*, is called when an instance is
+   created normally by calling its type, after the type's :c:member:`~PyTypeObject.tp_new` function
+   has returned an instance of the type.  If the :c:member:`~PyTypeObject.tp_new` function returns an
    instance of some other type that is not a subtype of the original type, no
-   :attr:`tp_init` function is called; if :attr:`tp_new` returns an instance of a
-   subtype of the original type, the subtype's :attr:`tp_init` is called.  (VERSION
+   :c:member:`~PyTypeObject.tp_init` function is called; if :c:member:`~PyTypeObject.tp_new` returns an instance of a
+   subtype of the original type, the subtype's :c:member:`~PyTypeObject.tp_init` is called.  (VERSION
    NOTE: described here is what is implemented in Python 2.2.1 and later.  In
-   Python 2.2, the :attr:`tp_init` of the type of the object returned by
-   :attr:`tp_new` was always called, if not *NULL*.)
+   Python 2.2, the :c:member:`~PyTypeObject.tp_init` of the type of the object returned by
+   :c:member:`~PyTypeObject.tp_new` was always called, if not *NULL*.)
 
    This field is inherited by subtypes.
 
@@ -982,14 +982,14 @@
    initialization.  It should return a pointer to a block of memory of adequate
    length for the instance, suitably aligned, and initialized to zeros, but with
    :attr:`ob_refcnt` set to ``1`` and :attr:`ob_type` set to the type argument.  If
-   the type's :attr:`tp_itemsize` is non-zero, the object's :attr:`ob_size` field
+   the type's :c:member:`~PyTypeObject.tp_itemsize` is non-zero, the object's :attr:`ob_size` field
    should be initialized to *nitems* and the length of the allocated memory block
    should be ``tp_basicsize + nitems*tp_itemsize``, rounded up to a multiple of
    ``sizeof(void*)``; otherwise, *nitems* is not used and the length of the block
-   should be :attr:`tp_basicsize`.
+   should be :c:member:`~PyTypeObject.tp_basicsize`.
 
    Do not use this function to do any other instance initialization, not even to
-   allocate additional memory; that should be done by :attr:`tp_new`.
+   allocate additional memory; that should be done by :c:member:`~PyTypeObject.tp_new`.
 
    This field is inherited by static subtypes, but not by dynamic subtypes
    (subtypes created by a class statement); in the latter, this field is always set
@@ -1011,20 +1011,20 @@
 
    The subtype argument is the type of the object being created; the *args* and
    *kwds* arguments represent positional and keyword arguments of the call to the
-   type.  Note that subtype doesn't have to equal the type whose :attr:`tp_new`
+   type.  Note that subtype doesn't have to equal the type whose :c:member:`~PyTypeObject.tp_new`
    function is called; it may be a subtype of that type (but not an unrelated
    type).
 
-   The :attr:`tp_new` function should call ``subtype->tp_alloc(subtype, nitems)``
+   The :c:member:`~PyTypeObject.tp_new` function should call ``subtype->tp_alloc(subtype, nitems)``
    to allocate space for the object, and then do only as much further
    initialization as is absolutely necessary.  Initialization that can safely be
-   ignored or repeated should be placed in the :attr:`tp_init` handler.  A good
+   ignored or repeated should be placed in the :c:member:`~PyTypeObject.tp_init` handler.  A good
    rule of thumb is that for immutable types, all initialization should take place
-   in :attr:`tp_new`, while for mutable types, most initialization should be
-   deferred to :attr:`tp_init`.
+   in :c:member:`~PyTypeObject.tp_new`, while for mutable types, most initialization should be
+   deferred to :c:member:`~PyTypeObject.tp_init`.
 
    This field is inherited by subtypes, except it is not inherited by static types
-   whose :attr:`tp_base` is *NULL* or ``&PyBaseObject_Type``.  The latter exception
+   whose :c:member:`~PyTypeObject.tp_base` is *NULL* or ``&PyBaseObject_Type``.  The latter exception
    is a precaution so that old extension types don't become callable simply by
    being linked with Python 2.2.
 
@@ -1057,7 +1057,7 @@
 
    The garbage collector needs to know whether a particular object is collectible
    or not.  Normally, it is sufficient to look at the object's type's
-   :attr:`tp_flags` field, and check the :const:`Py_TPFLAGS_HAVE_GC` flag bit.  But
+   :c:member:`~PyTypeObject.tp_flags` field, and check the :const:`Py_TPFLAGS_HAVE_GC` flag bit.  But
    some types have a mixture of statically and dynamically allocated instances, and
    the statically allocated instances are not collectible.  Such types should
    define this function; it should return ``1`` for a collectible instance, and
@@ -1129,7 +1129,7 @@
 
 .. c:member:: PyTypeObject* PyTypeObject.tp_next
 
-   Pointer to the next type object with a non-zero :attr:`tp_allocs` field.
+   Pointer to the next type object with a non-zero :c:member:`~PyTypeObject.tp_allocs` field.
 
 Also, note that, in a garbage collected Python, tp_dealloc may be called from
 any Python thread, not just the thread which created the object (if the object
@@ -1289,13 +1289,13 @@
 
    This function is used by :c:func:`PySequence_Concat` and has the same
    signature.  It is also used by the ``+`` operator, after trying the numeric
-   addition via the :attr:`tp_as_number.nb_add` slot.
+   addition via the :c:member:`~PyTypeObject.tp_as_number.nb_add` slot.
 
 .. c:member:: ssizeargfunc PySequenceMethods.sq_repeat
 
    This function is used by :c:func:`PySequence_Repeat` and has the same
    signature.  It is also used by the ``*`` operator, after trying numeric
-   multiplication via the :attr:`tp_as_number.nb_mul` slot.
+   multiplication via the :c:member:`~PyTypeObject.tp_as_number.nb_mul` slot.
 
 .. c:member:: ssizeargfunc PySequenceMethods.sq_item
 
@@ -1348,14 +1348,14 @@
 pointer/length pair.  These chunks are called :dfn:`segments` and are presumed
 to be non-contiguous in memory.
 
-If an object does not export the buffer interface, then its :attr:`tp_as_buffer`
+If an object does not export the buffer interface, then its :c:member:`~PyTypeObject.tp_as_buffer`
 member in the :c:type:`PyTypeObject` structure should be *NULL*.  Otherwise, the
-:attr:`tp_as_buffer` will point to a :c:type:`PyBufferProcs` structure.
+:c:member:`~PyTypeObject.tp_as_buffer` will point to a :c:type:`PyBufferProcs` structure.
 
 .. note::
 
    It is very important that your :c:type:`PyTypeObject` structure uses
-   :const:`Py_TPFLAGS_DEFAULT` for the value of the :attr:`tp_flags` member rather
+   :const:`Py_TPFLAGS_DEFAULT` for the value of the :c:member:`~PyTypeObject.tp_flags` member rather
    than ``0``.  This tells the Python runtime that your :c:type:`PyBufferProcs`
    structure contains the :attr:`bf_getcharbuffer` slot. Older versions of Python
    did not have this member, so a new Python interpreter using an old extension
@@ -1385,7 +1385,7 @@
 
    The last slot is :attr:`bf_getcharbuffer`, of type :c:type:`getcharbufferproc`.
    This slot will only be present if the :const:`Py_TPFLAGS_HAVE_GETCHARBUFFER`
-   flag is present in the :attr:`tp_flags` field of the object's
+   flag is present in the :c:member:`~PyTypeObject.tp_flags` field of the object's
    :c:type:`PyTypeObject`. Before using this slot, the caller should test whether it
    is present by using the :c:func:`PyType_HasFeature` function.  If the flag is
    present, :attr:`bf_getcharbuffer` may be *NULL*, indicating that the object's
diff --git a/Doc/data/refcounts.dat b/Doc/data/refcounts.dat
index 1fc896f..06c19d0 100644
--- a/Doc/data/refcounts.dat
+++ b/Doc/data/refcounts.dat
@@ -932,7 +932,7 @@
 
 PyObject_CallMethodObjArgs:PyObject*::+1:
 PyObject_CallMethodObjArgs:PyObject*:o:0:
-PyObject_CallMethodObjArgs:char*:name::
+PyObject_CallMethodObjArgs:PyObject*:name:0:
 PyObject_CallMethodObjArgs::...::
 
 PyObject_CallObject:PyObject*::+1:
diff --git a/Doc/extending/embedding.rst b/Doc/extending/embedding.rst
index ae5efc4..981e1d5 100644
--- a/Doc/extending/embedding.rst
+++ b/Doc/extending/embedding.rst
@@ -258,37 +258,55 @@
 
 .. _link-reqs:
 
-Linking Requirements
-====================
+Compiling and Linking under Unix-like systems
+=============================================
 
-While the :program:`configure` script shipped with the Python sources will
-correctly build Python to export the symbols needed by dynamically linked
-extensions, this is not automatically inherited by applications which embed the
-Python library statically, at least on Unix.  This is an issue when the
-application is linked to the static runtime library (:file:`libpython.a`) and
-needs to load dynamic extensions (implemented as :file:`.so` files).
+It is not necessarily trivial to find the right flags to pass to your
+compiler (and linker) in order to embed the Python interpreter into your
+application, particularly because Python needs to load library modules
+implemented as C dynamic extensions (:file:`.so` files) linked against
+it.
 
-The problem is that some entry points are defined by the Python runtime solely
-for extension modules to use.  If the embedding application does not use any of
-these entry points, some linkers will not include those entries in the symbol
-table of the finished executable.  Some additional options are needed to inform
-the linker not to remove these symbols.
+To find out the required compiler and linker flags, you can execute the
+:file:`python{X.Y}-config` script which is generated as part of the
+installation process (a :file:`python-config` script may also be
+available).  This script has several options, of which the following will
+be directly useful to you:
 
-Determining the right options to use for any given platform can be quite
-difficult, but fortunately the Python configuration already has those values.
-To retrieve them from an installed Python interpreter, start an interactive
-interpreter and have a short session like this
+* ``pythonX.Y-config --cflags`` will give you the recommended flags when
+  compiling::
+
+   $ /opt/bin/python2.7-config --cflags
+   -I/opt/include/python2.7 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes
+
+* ``pythonX.Y-config --ldflags`` will give you the recommended flags when
+  linking::
+
+   $ /opt/bin/python2.7-config --ldflags
+   -L/opt/lib/python2.7/config -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic
+
+.. note::
+   To avoid confusion between several Python installations (and especially
+   between the system Python and your own compiled Python), it is recommended
+   that you use the absolute path to :file:`python{X.Y}-config`, as in the above
+   example.
+
+If this procedure doesn't work for you (it is not guaranteed to work for
+all Unix-like platforms; however, we welcome :ref:`bug reports <reporting-bugs>`)
+you will have to read your system's documentation about dynamic linking and/or
+examine Python's :file:`Makefile` (use :func:`sysconfig.get_makefile_filename`
+to find its location) and compilation
+options.  In this case, the :mod:`sysconfig` module is a useful tool to
+programmatically extract the configuration values that you will want to
+combine together.  For example:
 
 .. code-block:: python
 
-   >>> import distutils.sysconfig
-   >>> distutils.sysconfig.get_config_var('LINKFORSHARED')
+   >>> import sysconfig
+   >>> sysconfig.get_config_var('LIBS')
+   '-lpthread -ldl  -lutil'
+   >>> sysconfig.get_config_var('LINKFORSHARED')
    '-Xlinker -export-dynamic'
 
-.. index:: module: distutils.sysconfig
 
-The contents of the string presented will be the options that should be used.
-If the string is empty, there's no need to add any additional options.  The
-:const:`LINKFORSHARED` definition corresponds to the variable of the same name
-in Python's top-level :file:`Makefile`.
-
+.. XXX similar documentation for Windows missing
diff --git a/Doc/extending/newtypes.rst b/Doc/extending/newtypes.rst
index 269c8fd..4278213 100644
--- a/Doc/extending/newtypes.rst
+++ b/Doc/extending/newtypes.rst
@@ -150,11 +150,11 @@
 .. note::
 
    If you want your type to be subclassable from Python, and your type has the same
-   :attr:`tp_basicsize` as its base type, you may have problems with multiple
+   :c:member:`~PyTypeObject.tp_basicsize` as its base type, you may have problems with multiple
    inheritance.  A Python subclass of your type will have to list your type first
    in its :attr:`__bases__`, or else it will not be able to call your type's
    :meth:`__new__` method without getting an error.  You can avoid this problem by
-   ensuring that your type has a larger value for :attr:`tp_basicsize` than its
+   ensuring that your type has a larger value for :c:member:`~PyTypeObject.tp_basicsize` than its
    base type does.  Most of the time, this will be true anyway, because either your
    base type will be :class:`object`, or else you will be adding data members to
    your base type, and therefore increasing its size.
@@ -174,7 +174,7 @@
 All types should include this constant in their flags.  It enables all of the
 members defined by the current version of Python.
 
-We provide a doc string for the type in :attr:`tp_doc`. ::
+We provide a doc string for the type in :c:member:`~PyTypeObject.tp_doc`. ::
 
    "Noddy objects",           /* tp_doc */
 
@@ -183,12 +183,12 @@
 the module.  We'll expand this example later to have more interesting behavior.
 
 For now, all we want to be able to do is to create new :class:`Noddy` objects.
-To enable object creation, we have to provide a :attr:`tp_new` implementation.
+To enable object creation, we have to provide a :c:member:`~PyTypeObject.tp_new` implementation.
 In this case, we can just use the default implementation provided by the API
 function :c:func:`PyType_GenericNew`.  We'd like to just assign this to the
-:attr:`tp_new` slot, but we can't, for portability sake, On some platforms or
+:c:member:`~PyTypeObject.tp_new` slot, but we can't, for portability sake, On some platforms or
 compilers, we can't statically initialize a structure member with a function
-defined in another C module, so, instead, we'll assign the :attr:`tp_new` slot
+defined in another C module, so, instead, we'll assign the :c:member:`~PyTypeObject.tp_new` slot
 in the module initialization function just before calling
 :c:func:`PyType_Ready`::
 
@@ -283,13 +283,13 @@
        self->ob_type->tp_free((PyObject*)self);
    }
 
-which is assigned to the :attr:`tp_dealloc` member::
+which is assigned to the :c:member:`~PyTypeObject.tp_dealloc` member::
 
    (destructor)Noddy_dealloc, /*tp_dealloc*/
 
 This method decrements the reference counts of the two Python attributes. We use
 :c:func:`Py_XDECREF` here because the :attr:`first` and :attr:`last` members
-could be *NULL*.  It then calls the :attr:`tp_free` member of the object's type
+could be *NULL*.  It then calls the :c:member:`~PyTypeObject.tp_free` member of the object's type
 to free the object's memory.  Note that the object's type might not be
 :class:`NoddyType`, because the object may be an instance of a subclass.
 
@@ -323,7 +323,7 @@
        return (PyObject *)self;
    }
 
-and install it in the :attr:`tp_new` member::
+and install it in the :c:member:`~PyTypeObject.tp_new` member::
 
    Noddy_new,                 /* tp_new */
 
@@ -344,16 +344,16 @@
 often ignore the arguments, leaving the argument handling to initializer
 methods. Note that if the type supports subclassing, the type passed may not be
 the type being defined.  The new method calls the tp_alloc slot to allocate
-memory. We don't fill the :attr:`tp_alloc` slot ourselves. Rather
+memory. We don't fill the :c:member:`~PyTypeObject.tp_alloc` slot ourselves. Rather
 :c:func:`PyType_Ready` fills it for us by inheriting it from our base class,
 which is :class:`object` by default.  Most types use the default allocation.
 
 .. note::
 
-   If you are creating a co-operative :attr:`tp_new` (one that calls a base type's
-   :attr:`tp_new` or :meth:`__new__`), you must *not* try to determine what method
+   If you are creating a co-operative :c:member:`~PyTypeObject.tp_new` (one that calls a base type's
+   :c:member:`~PyTypeObject.tp_new` or :meth:`__new__`), you must *not* try to determine what method
    to call using method resolution order at runtime.  Always statically determine
-   what type you are going to call, and call its :attr:`tp_new` directly, or via
+   what type you are going to call, and call its :c:member:`~PyTypeObject.tp_new` directly, or via
    ``type->tp_base->tp_new``.  If you do not do this, Python subclasses of your
    type that also inherit from other Python-defined classes may not work correctly.
    (Specifically, you may not be able to create instances of such subclasses
@@ -390,11 +390,11 @@
        return 0;
    }
 
-by filling the :attr:`tp_init` slot. ::
+by filling the :c:member:`~PyTypeObject.tp_init` slot. ::
 
    (initproc)Noddy_init,         /* tp_init */
 
-The :attr:`tp_init` slot is exposed in Python as the :meth:`__init__` method. It
+The :c:member:`~PyTypeObject.tp_init` slot is exposed in Python as the :meth:`__init__` method. It
 is used to initialize an object after it's created. Unlike the new method, we
 can't guarantee that the initializer is called.  The initializer isn't called
 when unpickling objects and it can be overridden.  Our initializer accepts
@@ -424,7 +424,7 @@
 * when we know that deallocation of the object [#]_ will not cause any calls
   back into our type's code
 
-* when decrementing a reference count in a :attr:`tp_dealloc` handler when
+* when decrementing a reference count in a :c:member:`~PyTypeObject.tp_dealloc` handler when
   garbage-collections is not supported [#]_
 
 We want to expose our instance variables as attributes. There are a
@@ -440,7 +440,7 @@
        {NULL}  /* Sentinel */
    };
 
-and put the definitions in the :attr:`tp_members` slot::
+and put the definitions in the :c:member:`~PyTypeObject.tp_members` slot::
 
    Noddy_members,             /* tp_members */
 
@@ -516,7 +516,7 @@
        {NULL}  /* Sentinel */
    };
 
-and assign them to the :attr:`tp_methods` slot::
+and assign them to the :c:member:`~PyTypeObject.tp_methods` slot::
 
    Noddy_methods,             /* tp_methods */
 
@@ -611,7 +611,7 @@
        {NULL}  /* Sentinel */
    };
 
-and register it in the :attr:`tp_getset` slot::
+and register it in the :c:member:`~PyTypeObject.tp_getset` slot::
 
    Noddy_getseters,           /* tp_getset */
 
@@ -628,7 +628,7 @@
        {NULL}  /* Sentinel */
    };
 
-We also need to update the :attr:`tp_init` handler to only allow strings [#]_ to
+We also need to update the :c:member:`~PyTypeObject.tp_init` handler to only allow strings [#]_ to
 be passed::
 
    static int
@@ -747,7 +747,7 @@
 
 .. note::
 
-   Note that the :attr:`tp_traverse` implementation must name its arguments exactly
+   Note that the :c:member:`~PyTypeObject.tp_traverse` implementation must name its arguments exactly
    *visit* and *arg* in order to use :c:func:`Py_VISIT`.  This is to encourage
    uniformity across these boring implementations.
 
@@ -784,7 +784,7 @@
 reference count drops to zero, we might cause code to run that calls back into
 the object.  In addition, because we now support garbage collection, we also
 have to worry about code being run that triggers garbage collection.  If garbage
-collection is run, our :attr:`tp_traverse` handler could get called. We can't
+collection is run, our :c:member:`~PyTypeObject.tp_traverse` handler could get called. We can't
 take a chance of having :c:func:`Noddy_traverse` called when a member's reference
 count has dropped to zero and its value hasn't been set to *NULL*.
 
@@ -804,8 +804,8 @@
 
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
 
-That's pretty much it.  If we had written custom :attr:`tp_alloc` or
-:attr:`tp_free` slots, we'd need to modify them for cyclic-garbage collection.
+That's pretty much it.  If we had written custom :c:member:`~PyTypeObject.tp_alloc` or
+:c:member:`~PyTypeObject.tp_free` slots, we'd need to modify them for cyclic-garbage collection.
 Most extensions will use the versions automatically provided.
 
 
@@ -864,8 +864,8 @@
 
 This pattern is important when writing a type with custom :attr:`new` and
 :attr:`dealloc` methods. The :attr:`new` method should not actually create the
-memory for the object with :attr:`tp_alloc`, that will be handled by the base
-class when calling its :attr:`tp_new`.
+memory for the object with :c:member:`~PyTypeObject.tp_alloc`, that will be handled by the base
+class when calling its :c:member:`~PyTypeObject.tp_new`.
 
 When filling out the :c:func:`PyTypeObject` for the :class:`Shoddy` type, you see
 a slot for :c:func:`tp_base`. Due to cross platform compiler issues, you can't
@@ -890,8 +890,8 @@
    }
 
 Before calling :c:func:`PyType_Ready`, the type structure must have the
-:attr:`tp_base` slot filled in. When we are deriving a new type, it is not
-necessary to fill out the :attr:`tp_alloc` slot with :c:func:`PyType_GenericNew`
+:c:member:`~PyTypeObject.tp_base` slot filled in. When we are deriving a new type, it is not
+necessary to fill out the :c:member:`~PyTypeObject.tp_alloc` slot with :c:func:`PyType_GenericNew`
 -- the allocate function from the base type will be inherited.
 
 After that, calling :c:func:`PyType_Ready` and adding the type object to the
@@ -934,7 +934,7 @@
 
 These fields tell the runtime how much memory to allocate when new objects of
 this type are created.  Python has some built-in support for variable length
-structures (think: strings, lists) which is where the :attr:`tp_itemsize` field
+structures (think: strings, lists) which is where the :c:member:`~PyTypeObject.tp_itemsize` field
 comes in.  This will be dealt with later. ::
 
    char *tp_doc;
@@ -1032,13 +1032,13 @@
 expensive.
 
 These handlers are all optional, and most types at most need to implement the
-:attr:`tp_str` and :attr:`tp_repr` handlers. ::
+:c:member:`~PyTypeObject.tp_str` and :c:member:`~PyTypeObject.tp_repr` handlers. ::
 
    reprfunc tp_repr;
    reprfunc tp_str;
    printfunc tp_print;
 
-The :attr:`tp_repr` handler should return a string object containing a
+The :c:member:`~PyTypeObject.tp_repr` handler should return a string object containing a
 representation of the instance for which it is called.  Here is a simple
 example::
 
@@ -1049,15 +1049,15 @@
                                   obj->obj_UnderlyingDatatypePtr->size);
    }
 
-If no :attr:`tp_repr` handler is specified, the interpreter will supply a
-representation that uses the type's :attr:`tp_name` and a uniquely-identifying
+If no :c:member:`~PyTypeObject.tp_repr` handler is specified, the interpreter will supply a
+representation that uses the type's :c:member:`~PyTypeObject.tp_name` and a uniquely-identifying
 value for the object.
 
-The :attr:`tp_str` handler is to :func:`str` what the :attr:`tp_repr` handler
+The :c:member:`~PyTypeObject.tp_str` handler is to :func:`str` what the :c:member:`~PyTypeObject.tp_repr` handler
 described above is to :func:`repr`; that is, it is called when Python code calls
 :func:`str` on an instance of your object.  Its implementation is very similar
-to the :attr:`tp_repr` function, but the resulting string is intended for human
-consumption.  If :attr:`tp_str` is not specified, the :attr:`tp_repr` handler is
+to the :c:member:`~PyTypeObject.tp_repr` function, but the resulting string is intended for human
+consumption.  If :c:member:`~PyTypeObject.tp_str` is not specified, the :c:member:`~PyTypeObject.tp_repr` handler is
 used instead.
 
 Here is a simple example::
@@ -1152,7 +1152,7 @@
 type object.  Each descriptor controls access to one attribute of the instance
 object.  Each of the tables is optional; if all three are *NULL*, instances of
 the type will only have attributes that are inherited from their base type, and
-should leave the :attr:`tp_getattro` and :attr:`tp_setattro` fields *NULL* as
+should leave the :c:member:`~PyTypeObject.tp_getattro` and :c:member:`~PyTypeObject.tp_setattro` fields *NULL* as
 well, allowing the base type to handle attributes.
 
 The tables are declared as three fields of the type object::
@@ -1161,7 +1161,7 @@
    struct PyMemberDef *tp_members;
    struct PyGetSetDef *tp_getset;
 
-If :attr:`tp_methods` is not *NULL*, it must refer to an array of
+If :c:member:`~PyTypeObject.tp_methods` is not *NULL*, it must refer to an array of
 :c:type:`PyMethodDef` structures.  Each entry in the table is an instance of this
 structure::
 
@@ -1225,13 +1225,13 @@
    single: WRITE_RESTRICTED
    single: RESTRICTED
 
-An interesting advantage of using the :attr:`tp_members` table to build
+An interesting advantage of using the :c:member:`~PyTypeObject.tp_members` table to build
 descriptors that are used at runtime is that any attribute defined this way can
 have an associated doc string simply by providing the text in the table.  An
 application can use the introspection API to retrieve the descriptor from the
 class object, and get the doc string using its :attr:`__doc__` attribute.
 
-As with the :attr:`tp_methods` table, a sentinel entry with a :attr:`name` value
+As with the :c:member:`~PyTypeObject.tp_methods` table, a sentinel entry with a :attr:`name` value
 of *NULL* is required.
 
 .. XXX Descriptors need to be explained in more detail somewhere, but not here.
@@ -1257,7 +1257,7 @@
 called, so that if you do need to extend their functionality, you'll understand
 what needs to be done.
 
-The :attr:`tp_getattr` handler is called when the object requires an attribute
+The :c:member:`~PyTypeObject.tp_getattr` handler is called when the object requires an attribute
 look-up.  It is called in the same situations where the :meth:`__getattr__`
 method of a class would be called.
 
@@ -1265,7 +1265,7 @@
 :c:func:`newdatatype_getSize` and :c:func:`newdatatype_setSize` in the example
 below), (2) provide a method table listing these functions, and (3) provide a
 getattr function that returns the result of a lookup in that table.  The method
-table uses the same structure as the :attr:`tp_methods` field of the type
+table uses the same structure as the :c:member:`~PyTypeObject.tp_methods` field of the type
 object.
 
 Here is an example::
@@ -1284,11 +1284,11 @@
        return Py_FindMethod(newdatatype_methods, (PyObject *)obj, name);
    }
 
-The :attr:`tp_setattr` handler is called when the :meth:`__setattr__` or
+The :c:member:`~PyTypeObject.tp_setattr` handler is called when the :meth:`__setattr__` or
 :meth:`__delattr__` method of a class instance would be called.  When an
 attribute should be deleted, the third parameter will be *NULL*.  Here is an
 example that simply raises an exception; if this were really all you wanted, the
-:attr:`tp_setattr` handler should be set to *NULL*. ::
+:c:member:`~PyTypeObject.tp_setattr` handler should be set to *NULL*. ::
 
    static int
    newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v)
@@ -1305,7 +1305,7 @@
 
    cmpfunc tp_compare;
 
-The :attr:`tp_compare` handler is called when comparisons are needed and the
+The :c:member:`~PyTypeObject.tp_compare` handler is called when comparisons are needed and the
 object does not implement the specific rich comparison method which matches the
 requested comparison.  (It is always used if defined and the
 :c:func:`PyObject_Compare` or :c:func:`PyObject_Cmp` functions are used, or if
@@ -1316,7 +1316,7 @@
 greater than, respectively; as of Python 2.2, this is no longer allowed.  In the
 future, other return values may be assigned a different meaning.)
 
-A :attr:`tp_compare` handler may raise an exception.  In this case it should
+A :c:member:`~PyTypeObject.tp_compare` handler may raise an exception.  In this case it should
 return a negative value.  The caller has to test for the exception using
 :c:func:`PyErr_Occurred`.
 
@@ -1391,7 +1391,7 @@
 
 This function is called when an instance of your data type is "called", for
 example, if ``obj1`` is an instance of your data type and the Python script
-contains ``obj1('hello')``, the :attr:`tp_call` handler is invoked.
+contains ``obj1('hello')``, the :c:member:`~PyTypeObject.tp_call` handler is invoked.
 
 This function takes three arguments:
 
@@ -1480,7 +1480,7 @@
 For an object to be weakly referencable, the extension must include a
 :c:type:`PyObject\*` field in the instance structure for the use of the weak
 reference mechanism; it must be initialized to *NULL* by the object's
-constructor.  It must also set the :attr:`tp_weaklistoffset` field of the
+constructor.  It must also set the :c:member:`~PyTypeObject.tp_weaklistoffset` field of the
 corresponding type object to the offset of the field. For example, the instance
 type is defined with the following structure::
 
@@ -1566,7 +1566,7 @@
 .. [#] This is true when we know that the object is a basic type, like a string or a
    float.
 
-.. [#] We relied on this in the :attr:`tp_dealloc` handler in this example, because our
+.. [#] We relied on this in the :c:member:`~PyTypeObject.tp_dealloc` handler in this example, because our
    type doesn't support garbage collection. Even if a type supports garbage
    collection, there are calls that can be made to "untrack" the object from
    garbage collection, however, these calls are advanced and not covered here.
diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst
index b9ed534..3878006 100644
--- a/Doc/faq/programming.rst
+++ b/Doc/faq/programming.rst
@@ -360,9 +360,9 @@
 Assume you use a for loop to define a few different lambdas (or even plain
 functions), e.g.::
 
-   squares = []
-   for x in range(5):
-      squares.append(lambda: x**2)
+   >>> squares = []
+   >>> for x in range(5):
+   ...    squares.append(lambda: x**2)
 
 This gives you a list that contains 5 lambdas that calculate ``x**2``.  You
 might expect that, when called, they would return, respectively, ``0``, ``1``,
@@ -387,9 +387,9 @@
 In order to avoid this, you need to save the values in variables local to the
 lambdas, so that they don't rely on the value of the global ``x``::
 
-   squares = []
-   for x in range(5):
-      squares.append(lambda n=x: n**2)
+   >>> squares = []
+   >>> for x in range(5):
+   ...    squares.append(lambda n=x: n**2)
 
 Here, ``n=x`` creates a new variable ``n`` local to the lambda and computed
 when the lambda is defined so that it has the same value that ``x`` had at
@@ -748,11 +748,11 @@
 Since the comma is not an operator, but a separator between expressions the
 above is evaluated as if you had entered::
 
-    >>> ("a" in "b"), "a"
+    ("a" in "b"), "a"
 
 not::
 
-    >>> "a" in ("b", "a")
+    "a" in ("b", "a")
 
 The same is true of the various assignment operators (``=``, ``+=`` etc).  They
 are not truly operators but syntactic delimiters in assignment statements.
@@ -897,6 +897,7 @@
 You can't, because strings are immutable.  If you need an object with this
 ability, try converting the string to a list or use the array module::
 
+   >>> import io
    >>> s = "Hello, world"
    >>> a = list(s)
    >>> print a
@@ -910,7 +911,7 @@
    >>> print a
    array('c', 'Hello, world')
    >>> a[0] = 'y' ; print a
-   array('c', 'yello world')
+   array('c', 'yello, world')
    >>> a.tostring()
    'yello, world'
 
@@ -1172,7 +1173,7 @@
 
 You probably tried to make a multidimensional array like this::
 
-   A = [[None] * 2] * 3
+   >>> A = [[None] * 2] * 3
 
 This looks correct if you print it::
 
@@ -1204,7 +1205,7 @@
    A = [[None] * w for i in range(h)]
 
 Or, you can use an extension that provides a matrix datatype; `Numeric Python
-<http://numpy.scipy.org/>`_ is the best known.
+<http://www.numpy.org/>`_ is the best known.
 
 
 How do I apply a method to a sequence of objects?
@@ -1740,13 +1741,13 @@
 (permissions, free space, etc...) to write the compiled module back to the
 directory.
 
-Running Python on a top level script is not considered an import and no ``.pyc``
-will be created.  For example, if you have a top-level module ``abc.py`` that
-imports another module ``xyz.py``, when you run abc, ``xyz.pyc`` will be created
-since xyz is imported, but no ``abc.pyc`` file will be created since ``abc.py``
-isn't being imported.
+Running Python on a top level script is not considered an import and no
+``.pyc`` will be created.  For example, if you have a top-level module
+``foo.py`` that imports another module ``xyz.py``, when you run ``foo``,
+``xyz.pyc`` will be created since ``xyz`` is imported, but no ``foo.pyc`` file
+will be created since ``foo.py`` isn't being imported.
 
-If you need to create abc.pyc -- that is, to create a .pyc file for a module
+If you need to create ``foo.pyc`` -- that is, to create a ``.pyc`` file for a module
 that is not imported -- you can, using the :mod:`py_compile` and
 :mod:`compileall` modules.
 
@@ -1754,9 +1755,9 @@
 the ``compile()`` function in that module interactively::
 
    >>> import py_compile
-   >>> py_compile.compile('abc.py')
+   >>> py_compile.compile('foo.py')                 # doctest: +SKIP
 
-This will write the ``.pyc`` to the same location as ``abc.py`` (or you can
+This will write the ``.pyc`` to the same location as ``foo.py`` (or you can
 override that with the optional parameter ``cfile``).
 
 You can also automatically compile all files in a directory or directories using
diff --git a/Doc/howto/argparse.rst b/Doc/howto/argparse.rst
index 4f64948..3c2b592 100644
--- a/Doc/howto/argparse.rst
+++ b/Doc/howto/argparse.rst
@@ -468,7 +468,7 @@
        print answer
 
 We have introduced another action, "count",
-to count the number of occurences of a specific optional arguments:
+to count the number of occurrences of a specific optional arguments:
 
 .. code-block:: sh
 
diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst
index 422bf62..00d0e80 100644
--- a/Doc/howto/logging-cookbook.rst
+++ b/Doc/howto/logging-cookbook.rst
@@ -460,69 +460,31 @@
 the keys of the dict-like object. If you need a different method, e.g. if you
 want to prepend or append the contextual information to the message string,
 you just need to subclass :class:`LoggerAdapter` and override :meth:`process`
-to do what you need. Here's an example script which uses this class, which
-also illustrates what dict-like behaviour is needed from an arbitrary
-'dict-like' object for use in the constructor::
+to do what you need. Here is a simple example::
 
-   import logging
+    class CustomAdapter(logging.LoggerAdapter):
+        """
+        This example adapter expects the passed in dict-like object to have a
+        'connid' key, whose value in brackets is prepended to the log message.
+        """
+        def process(self, msg, kwargs):
+            return '[%s] %s' % (self.extra['connid'], msg), kwargs
 
-   class ConnInfo:
-       """
-       An example class which shows how an arbitrary class can be used as
-       the 'extra' context information repository passed to a LoggerAdapter.
-       """
+which you can use like this::
 
-       def __getitem__(self, name):
-           """
-           To allow this instance to look like a dict.
-           """
-           from random import choice
-           if name == 'ip':
-               result = choice(['127.0.0.1', '192.168.0.1'])
-           elif name == 'user':
-               result = choice(['jim', 'fred', 'sheila'])
-           else:
-               result = self.__dict__.get(name, '?')
-           return result
+    logger = logging.getLogger(__name__)
+    adapter = CustomAdapter(logger, {'connid': some_conn_id})
 
-       def __iter__(self):
-           """
-           To allow iteration over keys, which will be merged into
-           the LogRecord dict before formatting and output.
-           """
-           keys = ['ip', 'user']
-           keys.extend(self.__dict__.keys())
-           return keys.__iter__()
+Then any events that you log to the adapter will have the value of
+``some_conn_id`` prepended to the log messages.
 
-   if __name__ == '__main__':
-       from random import choice
-       levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL)
-       a1 = logging.LoggerAdapter(logging.getLogger('a.b.c'),
-                                  { 'ip' : '123.231.231.123', 'user' : 'sheila' })
-       logging.basicConfig(level=logging.DEBUG,
-                           format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s')
-       a1.debug('A debug message')
-       a1.info('An info message with %s', 'some parameters')
-       a2 = logging.LoggerAdapter(logging.getLogger('d.e.f'), ConnInfo())
-       for x in range(10):
-           lvl = choice(levels)
-           lvlname = logging.getLevelName(lvl)
-           a2.log(lvl, 'A message at %s level with %d %s', lvlname, 2, 'parameters')
+Using objects other than dicts to pass contextual information
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-When this script is run, the output should look something like this::
-
-   2008-01-18 14:49:54,023 a.b.c DEBUG    IP: 123.231.231.123 User: sheila   A debug message
-   2008-01-18 14:49:54,023 a.b.c INFO     IP: 123.231.231.123 User: sheila   An info message with some parameters
-   2008-01-18 14:49:54,023 d.e.f CRITICAL IP: 192.168.0.1     User: jim      A message at CRITICAL level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f INFO     IP: 192.168.0.1     User: jim      A message at INFO level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f WARNING  IP: 192.168.0.1     User: sheila   A message at WARNING level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f ERROR    IP: 127.0.0.1       User: fred     A message at ERROR level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f ERROR    IP: 127.0.0.1       User: sheila   A message at ERROR level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f WARNING  IP: 192.168.0.1     User: sheila   A message at WARNING level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f WARNING  IP: 192.168.0.1     User: jim      A message at WARNING level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f INFO     IP: 192.168.0.1     User: fred     A message at INFO level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f WARNING  IP: 192.168.0.1     User: sheila   A message at WARNING level with 2 parameters
-   2008-01-18 14:49:54,033 d.e.f WARNING  IP: 127.0.0.1       User: jim      A message at WARNING level with 2 parameters
+You don't need to pass an actual dict to a :class:`LoggerAdapter` - you could
+pass an instance of a class which implements ``__getitem__`` and ``__iter__`` so
+that it looks like a dict to logging. This would be useful if you want to
+generate values dynamically (whereas the values in a dict would be constant).
 
 
 .. _filters-contextual:
diff --git a/Doc/howto/sockets.rst b/Doc/howto/sockets.rst
index 6338d3b..e0083ed 100644
--- a/Doc/howto/sockets.rst
+++ b/Doc/howto/sockets.rst
@@ -19,12 +19,6 @@
 Sockets
 =======
 
-Sockets are used nearly everywhere, but are one of the most severely
-misunderstood technologies around. This is a 10,000 foot overview of sockets.
-It's not really a tutorial - you'll still have work to do in getting things
-working. It doesn't cover the fine points (and there are a lot of them), but I
-hope it will give you enough background to begin using them decently.
-
 I'm only going to talk about INET sockets, but they account for at least 99% of
 the sockets in use. And I'll only talk about STREAM sockets - unless you really
 know what you're doing (in which case this HOWTO isn't for you!), you'll get
diff --git a/Doc/library/array.rst b/Doc/library/array.rst
index d34cf38..1766d47 100644
--- a/Doc/library/array.rst
+++ b/Doc/library/array.rst
@@ -268,9 +268,7 @@
       Packing and unpacking of External Data Representation (XDR) data as used in some
       remote procedure call systems.
 
-   `The Numerical Python Manual <http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm>`_
+   `The Numerical Python Documentation <http://docs.scipy.org/doc/>`_
       The Numeric Python extension (NumPy) defines another array type; see
-      http://numpy.sourceforge.net/ for further information about Numerical Python.
-      (A PDF version of the NumPy manual is available at
-      http://numpy.sourceforge.net/numdoc/numdoc.pdf).
+      http://www.numpy.org/ for further information about Numerical Python.
 
diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst
index 39a3c5d..61da3ed 100644
--- a/Doc/library/codecs.rst
+++ b/Doc/library/codecs.rst
@@ -653,7 +653,7 @@
       Read one line from the input stream and return the decoded data.
 
       *size*, if given, is passed as size argument to the stream's
-      :meth:`readline` method.
+      :meth:`read` method.
 
       If *keepends* is false line-endings will be stripped from the lines
       returned.
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index ab290e7..b7628bb 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -639,6 +639,12 @@
            'Return self as a plain tuple.   Used by copy and pickle.'
            return tuple(self)
    <BLANKLINE>
+       __dict__ = _property(_asdict)
+   <BLANKLINE>
+       def __getstate__(self):
+           'Exclude the OrderedDict from pickling'
+           pass
+   <BLANKLINE>
        x = _property(_itemgetter(0), doc='Alias for field number 0')
    <BLANKLINE>
        y = _property(_itemgetter(1), doc='Alias for field number 1')
diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
index e33f3f0..be41e12 100644
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -1739,7 +1739,7 @@
    ``%f`` is an extension to the set of format characters in the C standard
    (but implemented separately in datetime objects, and therefore always
    available).  When used with the :meth:`strptime` method, the ``%f``
-   directive accepts from one to six digits and zero pads on the right.  
+   directive accepts from one to six digits and zero pads on the right.
 
    .. versionadded:: 2.6
 
diff --git a/Doc/library/fileinput.rst b/Doc/library/fileinput.rst
index 172a643..ce892ce 100644
--- a/Doc/library/fileinput.rst
+++ b/Doc/library/fileinput.rst
@@ -50,7 +50,7 @@
 The following function is the primary interface of this module:
 
 
-.. function:: input([files[, inplace[, backup[, mode[, openhook]]]]])
+.. function:: input([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]])
 
    Create an instance of the :class:`FileInput` class.  The instance will be used
    as global state for the functions of this module, and is also returned to use
@@ -122,7 +122,7 @@
 available for subclassing as well:
 
 
-.. class:: FileInput([files[, inplace[, backup[, mode[, openhook]]]]])
+.. class:: FileInput([files[, inplace[, backup[,bufsize[, mode[, openhook]]]]]])
 
    Class :class:`FileInput` is the implementation; its methods :meth:`filename`,
    :meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`,
diff --git a/Doc/library/gc.rst b/Doc/library/gc.rst
index 80a2d92..f02f09d 100644
--- a/Doc/library/gc.rst
+++ b/Doc/library/gc.rst
@@ -132,8 +132,8 @@
 
    Return a list of objects directly referred to by any of the arguments. The
    referents returned are those objects visited by the arguments' C-level
-   :attr:`tp_traverse` methods (if any), and may not be all objects actually
-   directly reachable.  :attr:`tp_traverse` methods are supported only by objects
+   :c:member:`~PyTypeObject.tp_traverse` methods (if any), and may not be all objects actually
+   directly reachable.  :c:member:`~PyTypeObject.tp_traverse` methods are supported only by objects
    that support garbage collection, and are only required to visit objects that may
    be involved in a cycle.  So, for example, if an integer is directly reachable
    from an argument, that integer object may or may not appear in the result list.
diff --git a/Doc/library/heapq.rst b/Doc/library/heapq.rst
index f0723b7..e8acd6c 100644
--- a/Doc/library/heapq.rst
+++ b/Doc/library/heapq.rst
@@ -260,7 +260,7 @@
 the sort is going on, provided that the inserted items are not "better" than the
 last 0'th element you extracted.  This is especially useful in simulation
 contexts, where the tree holds all incoming events, and the "win" condition
-means the smallest scheduled time.  When an event schedule other events for
+means the smallest scheduled time.  When an event schedules other events for
 execution, they are scheduled into the future, so they can easily go into the
 heap.  So, a heap is a good structure for implementing schedulers (this is what
 I used for my MIDI sequencer :-).
diff --git a/Doc/library/idle.rst b/Doc/library/idle.rst
index b40e470..36d78b0 100644
--- a/Doc/library/idle.rst
+++ b/Doc/library/idle.rst
@@ -33,8 +33,8 @@
 File menu
 ^^^^^^^^^
 
-New window
-   create a new editing window
+New file
+   create a new file editing window
 
 Open...
    open an existing file
diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst
index 6f9298a..6e8c3a5 100644
--- a/Doc/library/multiprocessing.rst
+++ b/Doc/library/multiprocessing.rst
@@ -287,6 +287,9 @@
        print result.get(timeout=1)           # prints "100" unless your computer is *very* slow
        print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"
 
+Note that the methods of a pool should only ever be used by the
+process which created it.
+
 
 Reference
 ---------
@@ -423,9 +426,9 @@
          acquired a lock or semaphore etc. then terminating it is liable to
          cause other processes to deadlock.
 
-   Note that the :meth:`start`, :meth:`join`, :meth:`is_alive` and
-   :attr:`exit_code` methods should only be called by the process that created
-   the process object.
+   Note that the :meth:`start`, :meth:`join`, :meth:`is_alive`,
+   :meth:`terminate` and :attr:`exitcode` methods should only be called by
+   the process that created the process object.
 
    Example usage of some of the methods of :class:`Process`:
 
@@ -486,6 +489,24 @@
    the :mod:`multiprocessing` namespace so you need to import them from
    :mod:`Queue`.
 
+.. note::
+
+   When an object is put on a queue, the object is pickled and a
+   background thread later flushes the pickled data to an underlying
+   pipe.  This has some consequences which are a little surprising,
+   but should not cause any practical difficulties -- if they really
+   bother you then you can instead use a queue created with a
+   :ref:`manager <multiprocessing-managers>`.
+
+   (1) After putting an object on an empty queue there may be an
+       infinitesimal delay before the queue's :meth:`~Queue.empty`
+       method returns :const:`False` and :meth:`~Queue.get_nowait` can
+       return without raising :exc:`Queue.Empty`.
+
+   (2) If multiple processes are enqueuing objects, it is possible for
+       the objects to be received at the other end out-of-order.
+       However, objects enqueued by the same process will always be in
+       the expected order with respect to each other.
 
 .. warning::
 
@@ -609,6 +630,13 @@
       the background thread from being joined automatically when the process
       exits -- see :meth:`join_thread`.
 
+      A better name for this method might be
+      ``allow_exit_without_flush()``.  It is likely to cause enqueued
+      data to lost, and you almost certainly will not need to use it.
+      It is really only there if you need the current process to exit
+      immediately without waiting to flush enqueued data to the
+      underlying pipe, and you don't care about lost data.
+
 
 .. class:: multiprocessing.queues.SimpleQueue()
 
@@ -1581,6 +1609,9 @@
    *initializer* is not ``None`` then each worker process will call
    ``initializer(*initargs)`` when it starts.
 
+   Note that the methods of the pool object should only be called by
+   the process which created the pool.
+
    .. versionadded:: 2.7
       *maxtasksperchild* is the number of tasks a worker process can complete
       before it will exit and be replaced with a fresh worker process, to enable
diff --git a/Doc/library/os.path.rst b/Doc/library/os.path.rst
index 5e245d0..c84aaf2 100644
--- a/Doc/library/os.path.rst
+++ b/Doc/library/os.path.rst
@@ -236,8 +236,10 @@
 
 .. function:: relpath(path[, start])
 
-   Return a relative filepath to *path* either from the current directory or from
-   an optional *start* point.
+   Return a relative filepath to *path* either from the current directory or
+   from an optional *start* directory.  This is a path computation:  the
+   filesystem is not accessed to confirm the existence or nature of *path* or
+   *start*.
 
    *start* defaults to :attr:`os.curdir`.
 
diff --git a/Doc/library/smtplib.rst b/Doc/library/smtplib.rst
index 18560c1..4f3b572 100644
--- a/Doc/library/smtplib.rst
+++ b/Doc/library/smtplib.rst
@@ -24,12 +24,15 @@
 
    A :class:`SMTP` instance encapsulates an SMTP connection.  It has methods
    that support a full repertoire of SMTP and ESMTP operations. If the optional
-   host and port parameters are given, the SMTP :meth:`connect` method is called
-   with those parameters during initialization.  If the :meth:`connect` call
-   returns anything other than a success code, an :exc:`SMTPConnectError` is
-   raised. The optional *timeout* parameter specifies a timeout in seconds for
-   blocking operations like the connection attempt (if not specified, the
-   global default timeout setting will be used).
+   host and port parameters are given, the SMTP :meth:`connect` method is
+   called with those parameters during initialization.  If specified,
+   *local_hostname* is used as the FQDN of the local host in the HELO/EHLO
+   command.  Otherwise, the local hostname is found using
+   :func:`socket.getfqdn`.  If the :meth:`connect` call returns anything other
+   than a success code, an :exc:`SMTPConnectError` is raised. The optional
+   *timeout* parameter specifies a timeout in seconds for blocking operations
+   like the connection attempt (if not specified, the global default timeout
+   setting will be used).
 
    For normal use, you should only require the initialization/connect,
    :meth:`sendmail`, and :meth:`~smtplib.quit` methods.
@@ -45,12 +48,13 @@
    :class:`SMTP`. :class:`SMTP_SSL` should be used for situations where SSL is
    required from the beginning of the connection and using :meth:`starttls` is
    not appropriate. If *host* is not specified, the local host is used. If
-   *port* is omitted, the standard SMTP-over-SSL port (465) is used. *keyfile*
-   and *certfile* are also optional, and can contain a PEM formatted private key
-   and certificate chain file for the SSL connection. The optional *timeout*
-   parameter specifies a timeout in seconds for blocking operations like the
-   connection attempt (if not specified, the global default timeout setting
-   will be used).
+   *port* is omitted, the standard SMTP-over-SSL port (465) is used.
+   *local_hostname* has the same meaning as it does for the :class:`SMTP`
+   class.  *keyfile* and *certfile* are also optional, and can contain a PEM
+   formatted private key and certificate chain file for the SSL connection. The
+   optional *timeout* parameter specifies a timeout in seconds for blocking
+   operations like the connection attempt (if not specified, the global default
+   timeout setting will be used).
 
    .. versionadded:: 2.6
 
@@ -58,13 +62,15 @@
 .. class:: LMTP([host[, port[, local_hostname]]])
 
    The LMTP protocol, which is very similar to ESMTP, is heavily based on the
-   standard SMTP client. It's common to use Unix sockets for LMTP, so our :meth:`connect`
-   method must support that as well as a regular host:port server. To specify a
-   Unix socket, you must use an absolute path for *host*, starting with a '/'.
+   standard SMTP client. It's common to use Unix sockets for LMTP, so our
+   :meth:`connect` method must support that as well as a regular host:port
+   server.  *local_hostname* has the same meaning as it does for the
+   :class:`SMTP` class.  To specify a Unix socket, you must use an absolute
+   path for *host*, starting with a '/'.
 
-   Authentication is supported, using the regular SMTP mechanism. When using a Unix
-   socket, LMTP generally don't support or require any authentication, but your
-   mileage might vary.
+   Authentication is supported, using the regular SMTP mechanism. When using a
+   Unix socket, LMTP generally don't support or require any authentication, but
+   your mileage might vary.
 
    .. versionadded:: 2.6
 
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index 76345e5..756339d 100644
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -614,7 +614,7 @@
    iterators for those iteration types.  (An example of an object supporting
    multiple forms of iteration would be a tree structure which supports both
    breadth-first and depth-first traversal.)  This method corresponds to the
-   :attr:`tp_iter` slot of the type structure for Python objects in the Python/C
+   :c:member:`~PyTypeObject.tp_iter` slot of the type structure for Python objects in the Python/C
    API.
 
 The iterator objects themselves are required to support the following two
@@ -625,7 +625,7 @@
 
    Return the iterator object itself.  This is required to allow both containers
    and iterators to be used with the :keyword:`for` and :keyword:`in` statements.
-   This method corresponds to the :attr:`tp_iter` slot of the type structure for
+   This method corresponds to the :c:member:`~PyTypeObject.tp_iter` slot of the type structure for
    Python objects in the Python/C API.
 
 
@@ -633,7 +633,7 @@
 
    Return the next item from the container.  If there are no further items, raise
    the :exc:`StopIteration` exception.  This method corresponds to the
-   :attr:`tp_iternext` slot of the type structure for Python objects in the
+   :c:member:`~PyTypeObject.tp_iternext` slot of the type structure for Python objects in the
    Python/C API.
 
 Python defines several iterator objects to support iteration over general and
@@ -743,10 +743,10 @@
 +------------------+--------------------------------+----------+
 | ``max(s)``       | largest item of *s*            |          |
 +------------------+--------------------------------+----------+
-| ``s.index(i)``   | index of the first occurence   |          |
+| ``s.index(i)``   | index of the first occurrence  |          |
 |                  | of *i* in *s*                  |          |
 +------------------+--------------------------------+----------+
-| ``s.count(i)``   | total number of occurences of  |          |
+| ``s.count(i)``   | total number of occurrences of |          |
 |                  | *i* in *s*                     |          |
 +------------------+--------------------------------+----------+
 
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
index 46f429c..f36a44d 100644
--- a/Doc/library/unittest.rst
+++ b/Doc/library/unittest.rst
@@ -958,7 +958,7 @@
       a regular expression object or a string containing a regular expression
       suitable for use by :func:`re.search`.  Examples::
 
-         self.assertRaisesRegexp(ValueError, 'invalid literal for.*XYZ$',
+         self.assertRaisesRegexp(ValueError, "invalid literal for.*XYZ'$",
                                  int, 'XYZ')
 
       or::
@@ -1017,7 +1017,7 @@
       like the :func:`round` function) and not *significant digits*.
 
       If *delta* is supplied instead of *places* then the difference
-      between *first* and *second* must be less (or more) than *delta*.
+      between *first* and *second* must be less or equal to (or greater than) *delta*.
 
       Supplying both *delta* and *places* raises a ``TypeError``.
 
diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst
index ea92b8e..2f31c85 100644
--- a/Doc/reference/lexical_analysis.rst
+++ b/Doc/reference/lexical_analysis.rst
@@ -529,8 +529,7 @@
 (2)
    Any Unicode character can be encoded this way, but characters outside the Basic
    Multilingual Plane (BMP) will be encoded using a surrogate pair if Python is
-   compiled to use 16-bit code units (the default).  Individual code units which
-   form parts of a surrogate pair can be encoded using this escape sequence.
+   compiled to use 16-bit code units (the default).
 
 (3)
    As in Standard C, up to three octal digits are accepted.
diff --git a/Doc/whatsnew/2.2.rst b/Doc/whatsnew/2.2.rst
index 412c1d0..5f6fe11 100644
--- a/Doc/whatsnew/2.2.rst
+++ b/Doc/whatsnew/2.2.rst
@@ -450,9 +450,9 @@
 Python classes can define an :meth:`__iter__` method, which should create and
 return a new iterator for the object; if the object is its own iterator, this
 method can just return ``self``.  In particular, iterators will usually be their
-own iterators.  Extension types implemented in C can implement a :attr:`tp_iter`
+own iterators.  Extension types implemented in C can implement a :c:member:`~PyTypeObject.tp_iter`
 function in order to return an iterator, and extension types that want to behave
-as iterators can define a :attr:`tp_iternext` function.
+as iterators can define a :c:member:`~PyTypeObject.tp_iternext` function.
 
 So, after all this, what do iterators actually do?  They have one required
 method, :meth:`next`, which takes no arguments and returns the next value.  When
@@ -478,7 +478,7 @@
 In 2.2, Python's :keyword:`for` statement no longer expects a sequence; it
 expects something for which :func:`iter` will return an iterator. For backward
 compatibility and convenience, an iterator is automatically constructed for
-sequences that don't implement :meth:`__iter__` or a :attr:`tp_iter` slot, so
+sequences that don't implement :meth:`__iter__` or a :c:member:`~PyTypeObject.tp_iter` slot, so
 ``for i in [1,2,3]`` will still work.  Wherever the Python interpreter loops
 over a sequence, it's been changed to use the iterator protocol.  This means you
 can do things like this::
diff --git a/Lib/Queue.py b/Lib/Queue.py
index 2db8d76..00364b3 100644
--- a/Lib/Queue.py
+++ b/Lib/Queue.py
@@ -109,7 +109,7 @@
 
         If optional args 'block' is true and 'timeout' is None (the default),
         block if necessary until a free slot is available. If 'timeout' is
-        a positive number, it blocks at most 'timeout' seconds and raises
+        a non-negative number, it blocks at most 'timeout' seconds and raises
         the Full exception if no free slot was available within that time.
         Otherwise ('block' is false), put an item on the queue if a free slot
         is immediately available, else raise the Full exception ('timeout'
@@ -125,7 +125,7 @@
                     while self._qsize() == self.maxsize:
                         self.not_full.wait()
                 elif timeout < 0:
-                    raise ValueError("'timeout' must be a positive number")
+                    raise ValueError("'timeout' must be a non-negative number")
                 else:
                     endtime = _time() + timeout
                     while self._qsize() == self.maxsize:
@@ -152,7 +152,7 @@
 
         If optional args 'block' is true and 'timeout' is None (the default),
         block if necessary until an item is available. If 'timeout' is
-        a positive number, it blocks at most 'timeout' seconds and raises
+        a non-negative number, it blocks at most 'timeout' seconds and raises
         the Empty exception if no item was available within that time.
         Otherwise ('block' is false), return an item if one is immediately
         available, else raise the Empty exception ('timeout' is ignored
@@ -167,7 +167,7 @@
                 while not self._qsize():
                     self.not_empty.wait()
             elif timeout < 0:
-                raise ValueError("'timeout' must be a positive number")
+                raise ValueError("'timeout' must be a non-negative number")
             else:
                 endtime = _time() + timeout
                 while not self._qsize():
diff --git a/Lib/_osx_support.py b/Lib/_osx_support.py
index 79dbdc9..195561a 100644
--- a/Lib/_osx_support.py
+++ b/Lib/_osx_support.py
@@ -53,7 +53,7 @@
 
 
 def _read_output(commandstring):
-    """Output from succesful command execution or None"""
+    """Output from successful command execution or None"""
     # Similar to os.popen(commandstring, "r").read(),
     # but without actually using os.popen because that
     # function is not usable during python bootstrap.
@@ -68,7 +68,7 @@
 
     with contextlib.closing(fp) as fp:
         cmd = "%s 2>/dev/null >'%s'" % (commandstring, fp.name)
-        return fp.read().decode('utf-8').strip() if not os.system(cmd) else None
+        return fp.read().strip() if not os.system(cmd) else None
 
 
 def _find_build_tool(toolname):
diff --git a/Lib/cgi.py b/Lib/cgi.py
index 67079db..64ba6d1 100755
--- a/Lib/cgi.py
+++ b/Lib/cgi.py
@@ -697,6 +697,9 @@
             if not line:
                 self.done = -1
                 break
+            if delim == "\r":
+                line = delim + line
+                delim = ""
             if line[:2] == "--" and last_line_lfend:
                 strippedline = line.strip()
                 if strippedline == next:
@@ -713,6 +716,12 @@
                 delim = "\n"
                 line = line[:-1]
                 last_line_lfend = True
+            elif line[-1] == "\r":
+                # We may interrupt \r\n sequences if they span the 2**16
+                # byte boundary
+                delim = "\r"
+                line = line[:-1]
+                last_line_lfend = False
             else:
                 delim = ""
                 last_line_lfend = False
diff --git a/Lib/collections.py b/Lib/collections.py
index af32e44..b9df2c1 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -270,6 +270,12 @@
         'Return self as a plain tuple.  Used by copy and pickle.'
         return tuple(self)
 
+    __dict__ = _property(_asdict)
+
+    def __getstate__(self):
+        'Exclude the OrderedDict from pickling'
+        pass
+
 {field_defs}
 '''
 
diff --git a/Lib/csv.py b/Lib/csv.py
index 984ed7e..98480ba 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -261,8 +261,9 @@
 
         # if we see an extra quote between delimiters, we've got a
         # double quoted format
-        dq_regexp = re.compile(r"((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)" % \
-                               {'delim':delim, 'quote':quotechar}, re.MULTILINE)
+        dq_regexp = re.compile(
+                               r"((%(delim)s)|^)\W*%(quote)s[^%(delim)s\n]*%(quote)s[^%(delim)s\n]*%(quote)s\W*((%(delim)s)|$)" % \
+                               {'delim':re.escape(delim), 'quote':quotechar}, re.MULTILINE)
 
 
 
diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py
index 0c726d9..4aa9334 100644
--- a/Lib/distutils/sysconfig.py
+++ b/Lib/distutils/sysconfig.py
@@ -175,9 +175,15 @@
                             'CCSHARED', 'LDSHARED', 'SO', 'AR',
                             'ARFLAGS')
 
-        newcc = None
         if 'CC' in os.environ:
-            cc = os.environ['CC']
+            newcc = os.environ['CC']
+            if (sys.platform == 'darwin'
+                    and 'LDSHARED' not in os.environ
+                    and ldshared.startswith(cc)):
+                # On OS X, if CC is overridden, use that as the default
+                #       command for LDSHARED as well
+                ldshared = newcc + ldshared[len(cc):]
+            cc = newcc
         if 'CXX' in os.environ:
             cxx = os.environ['CXX']
         if 'LDSHARED' in os.environ:
diff --git a/Lib/distutils/tests/test_unixccompiler.py b/Lib/distutils/tests/test_unixccompiler.py
index 40c908a..64f9d26 100644
--- a/Lib/distutils/tests/test_unixccompiler.py
+++ b/Lib/distutils/tests/test_unixccompiler.py
@@ -1,7 +1,8 @@
 """Tests for distutils.unixccompiler."""
+import os
 import sys
 import unittest
-from test.test_support import run_unittest
+from test.test_support import EnvironmentVarGuard, run_unittest
 
 from distutils import sysconfig
 from distutils.unixccompiler import UnixCCompiler
@@ -122,6 +123,37 @@
         sysconfig.get_config_var = gcv
         self.assertEqual(self.cc.rpath_foo(), '-R/foo')
 
+    @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X')
+    def test_osx_cc_overrides_ldshared(self):
+        # Issue #18080:
+        # ensure that setting CC env variable also changes default linker
+        def gcv(v):
+            if v == 'LDSHARED':
+                return 'gcc-4.2 -bundle -undefined dynamic_lookup '
+            return 'gcc-4.2'
+        sysconfig.get_config_var = gcv
+        with EnvironmentVarGuard() as env:
+            env['CC'] = 'my_cc'
+            del env['LDSHARED']
+            sysconfig.customize_compiler(self.cc)
+        self.assertEqual(self.cc.linker_so[0], 'my_cc')
+
+    @unittest.skipUnless(sys.platform == 'darwin', 'test only relevant for OS X')
+    def test_osx_explict_ldshared(self):
+        # Issue #18080:
+        # ensure that setting CC env variable does not change
+        #   explicit LDSHARED setting for linker
+        def gcv(v):
+            if v == 'LDSHARED':
+                return 'gcc-4.2 -bundle -undefined dynamic_lookup '
+            return 'gcc-4.2'
+        sysconfig.get_config_var = gcv
+        with EnvironmentVarGuard() as env:
+            env['CC'] = 'my_cc'
+            env['LDSHARED'] = 'my_ld -bundle -dynamic'
+            sysconfig.customize_compiler(self.cc)
+        self.assertEqual(self.cc.linker_so[0], 'my_ld')
+
 
 def test_suite():
     return unittest.makeSuite(UnixCCompilerTestCase)
diff --git a/Lib/email/charset.py b/Lib/email/charset.py
index dddaa76..30a13ff 100644
--- a/Lib/email/charset.py
+++ b/Lib/email/charset.py
@@ -183,7 +183,7 @@
                    header encoding.  Charset.SHORTEST is not allowed for
                    body_encoding.
 
-    output_charset: Some character sets must be converted before the can be
+    output_charset: Some character sets must be converted before they can be
                     used in email headers or bodies.  If the input_charset is
                     one of them, this attribute will contain the name of the
                     charset output will be converted to.  Otherwise, it will
diff --git a/Lib/fileinput.py b/Lib/fileinput.py
index ba48575..04e97bd 100644
--- a/Lib/fileinput.py
+++ b/Lib/fileinput.py
@@ -90,12 +90,11 @@
 
 def input(files=None, inplace=0, backup="", bufsize=0,
           mode="r", openhook=None):
-    """input([files[, inplace[, backup[, mode[, openhook]]]]])
+    """Return an instance of the FileInput class, which can be iterated.
 
-    Create an instance of the FileInput class. The instance will be used
-    as global state for the functions of this module, and is also returned
-    to use during iteration. The parameters to this function will be passed
-    along to the constructor of the FileInput class.
+    The parameters are passed to the constructor of the FileInput class.
+    The returned instance, in addition to being an iterator,
+    keeps global state for the functions of this module,.
     """
     global _state
     if _state and _state._file:
@@ -182,7 +181,7 @@
     return _state.isstdin()
 
 class FileInput:
-    """class FileInput([files[, inplace[, backup[, mode[, openhook]]]]])
+    """FileInput([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]])
 
     Class FileInput is the implementation of the module; its methods
     filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
diff --git a/Lib/idlelib/Bindings.py b/Lib/idlelib/Bindings.py
index fe6e812..65c0317 100644
--- a/Lib/idlelib/Bindings.py
+++ b/Lib/idlelib/Bindings.py
@@ -15,7 +15,7 @@
 menudefs = [
  # underscore prefixes character to underscore
  ('file', [
-   ('_New Window', '<<open-new-window>>'),
+   ('_New File', '<<open-new-window>>'),
    ('_Open...', '<<open-window-from-file>>'),
    ('Open _Module...', '<<open-module>>'),
    ('Class _Browser', '<<open-class-browser>>'),
diff --git a/Lib/idlelib/CallTips.py b/Lib/idlelib/CallTips.py
index d533ce1..c29f89b 100644
--- a/Lib/idlelib/CallTips.py
+++ b/Lib/idlelib/CallTips.py
@@ -163,7 +163,7 @@
             if fob.func_code.co_flags & 0x8:
                 items.append("***")
             arg_text = ", ".join(items)
-            arg_text = "(%s)" % re.sub("\.\d+", "<tuple>", arg_text)
+            arg_text = "(%s)" % re.sub("(?<!\d)\.\d+", "<tuple>", arg_text)
         # See if we can use the docstring
         doc = getattr(ob, "__doc__", "")
         if doc:
@@ -223,4 +223,6 @@
     tests = (t1, t2, t3, t4, t5, t6, t7,
              TC, tc.t1, tc.t2, tc.t3, tc.t4, tc.t5, tc.t6, tc.t7)
 
-    test(tests)
+    # test(tests)
+    from unittest import main
+    main('idlelib.idle_test.test_calltips', verbosity=2, exit=False)
diff --git a/Lib/idlelib/Delegator.py b/Lib/idlelib/Delegator.py
index 6125591..c476516 100644
--- a/Lib/idlelib/Delegator.py
+++ b/Lib/idlelib/Delegator.py
@@ -4,30 +4,22 @@
 
     def __init__(self, delegate=None):
         self.delegate = delegate
-        self.__cache = {}
+        self.__cache = set()
 
     def __getattr__(self, name):
         attr = getattr(self.delegate, name) # May raise AttributeError
         setattr(self, name, attr)
-        self.__cache[name] = attr
+        self.__cache.add(name)
         return attr
 
     def resetcache(self):
-        for key in self.__cache.keys():
+        for key in self.__cache:
             try:
                 delattr(self, key)
             except AttributeError:
                 pass
         self.__cache.clear()
 
-    def cachereport(self):
-        keys = self.__cache.keys()
-        keys.sort()
-        print keys
-
     def setdelegate(self, delegate):
         self.resetcache()
         self.delegate = delegate
-
-    def getdelegate(self):
-        return self.delegate
diff --git a/Lib/idlelib/EditorWindow.py b/Lib/idlelib/EditorWindow.py
index 7b9b583..07ca556 100644
--- a/Lib/idlelib/EditorWindow.py
+++ b/Lib/idlelib/EditorWindow.py
@@ -689,7 +689,7 @@
         # XXX Ought to insert current file's directory in front of path
         try:
             (f, file, (suffix, mode, type)) = _find_module(name)
-        except (NameError, ImportError), msg:
+        except (NameError, ImportError) as msg:
             tkMessageBox.showerror("Import error", str(msg), parent=self.text)
             return
         if type != imp.PY_SOURCE:
@@ -833,7 +833,11 @@
                     menuEventDict[menu[0]][prepstr(item[0])[1]] = item[1]
         for menubarItem in self.menudict.keys():
             menu = self.menudict[menubarItem]
-            end = menu.index(END) + 1
+            end = menu.index(END)
+            if end is None:
+                # Skip empty menus
+                continue
+            end += 1
             for index in range(0, end):
                 if menu.type(index) == 'command':
                     accel = menu.entrycget(index, 'accelerator')
@@ -890,11 +894,8 @@
         "Load and update the recent files list and menus"
         rf_list = []
         if os.path.exists(self.recent_files_path):
-            rf_list_file = open(self.recent_files_path,'r')
-            try:
+            with  open(self.recent_files_path, 'r') as rf_list_file:
                 rf_list = rf_list_file.readlines()
-            finally:
-                rf_list_file.close()
         if new_file:
             new_file = os.path.abspath(new_file) + '\n'
             if new_file in rf_list:
diff --git a/Lib/idlelib/FormatParagraph.py b/Lib/idlelib/FormatParagraph.py
index 557d8a9..ae4e6e7 100644
--- a/Lib/idlelib/FormatParagraph.py
+++ b/Lib/idlelib/FormatParagraph.py
@@ -1,18 +1,19 @@
-# Extension to format a paragraph
+"""Extension to format a paragraph or selection to a max width.
 
-# Does basic, standard text formatting, and also understands Python
-# comment blocks.  Thus, for editing Python source code, this
-# extension is really only suitable for reformatting these comment
-# blocks or triple-quoted strings.
+Does basic, standard text formatting, and also understands Python
+comment blocks. Thus, for editing Python source code, this
+extension is really only suitable for reformatting these comment
+blocks or triple-quoted strings.
 
-# Known problems with comment reformatting:
-# * If there is a selection marked, and the first line of the
-#   selection is not complete, the block will probably not be detected
-#   as comments, and will have the normal "text formatting" rules
-#   applied.
-# * If a comment block has leading whitespace that mixes tabs and
-#   spaces, they will not be considered part of the same block.
-# * Fancy comments, like this bulleted list, arent handled :-)
+Known problems with comment reformatting:
+* If there is a selection marked, and the first line of the
+  selection is not complete, the block will probably not be detected
+  as comments, and will have the normal "text formatting" rules
+  applied.
+* If a comment block has leading whitespace that mixes tabs and
+  spaces, they will not be considered part of the same block.
+* Fancy comments, like this bulleted list, aren't handled :-)
+"""
 
 import re
 from idlelib.configHandler import idleConf
@@ -32,41 +33,31 @@
         self.editwin = None
 
     def format_paragraph_event(self, event):
-        maxformatwidth = int(idleConf.GetOption('main','FormatParagraph',
-                                                'paragraph', type='int'))
+        """Formats paragraph to a max width specified in idleConf.
+
+        If text is selected, format_paragraph_event will start breaking lines
+        at the max width, starting from the beginning selection.
+
+        If no text is selected, format_paragraph_event uses the current
+        cursor location to determine the paragraph (lines of text surrounded
+        by blank lines) and formats it.
+        """
+        maxformatwidth = idleConf.GetOption(
+                'main', 'FormatParagraph', 'paragraph', type='int')
         text = self.editwin.text
         first, last = self.editwin.get_selection_indices()
         if first and last:
             data = text.get(first, last)
-            comment_header = ''
+            comment_header = get_comment_header(data)
         else:
             first, last, comment_header, data = \
                     find_paragraph(text, text.index("insert"))
         if comment_header:
-            # Reformat the comment lines - convert to text sans header.
-            lines = data.split("\n")
-            lines = map(lambda st, l=len(comment_header): st[l:], lines)
-            data = "\n".join(lines)
-            # Reformat to maxformatwidth chars or a 20 char width, whichever is greater.
-            format_width = max(maxformatwidth - len(comment_header), 20)
-            newdata = reformat_paragraph(data, format_width)
-            # re-split and re-insert the comment header.
-            newdata = newdata.split("\n")
-            # If the block ends in a \n, we dont want the comment
-            # prefix inserted after it. (Im not sure it makes sense to
-            # reformat a comment block that isnt made of complete
-            # lines, but whatever!)  Can't think of a clean solution,
-            # so we hack away
-            block_suffix = ""
-            if not newdata[-1]:
-                block_suffix = "\n"
-                newdata = newdata[:-1]
-            builder = lambda item, prefix=comment_header: prefix+item
-            newdata = '\n'.join(map(builder, newdata)) + block_suffix
+            newdata = reformat_comment(data, maxformatwidth, comment_header)
         else:
-            # Just a normal text format
             newdata = reformat_paragraph(data, maxformatwidth)
         text.tag_remove("sel", "1.0", "end")
+
         if newdata != data:
             text.mark_set("insert", first)
             text.undo_block_start()
@@ -79,31 +70,44 @@
         return "break"
 
 def find_paragraph(text, mark):
+    """Returns the start/stop indices enclosing the paragraph that mark is in.
+
+    Also returns the comment format string, if any, and paragraph of text
+    between the start/stop indices.
+    """
     lineno, col = map(int, mark.split("."))
-    line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
+    line = text.get("%d.0" % lineno, "%d.end" % lineno)
+
+    # Look for start of next paragraph if the index passed in is a blank line
     while text.compare("%d.0" % lineno, "<", "end") and is_all_white(line):
         lineno = lineno + 1
-        line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
+        line = text.get("%d.0" % lineno, "%d.end" % lineno)
     first_lineno = lineno
     comment_header = get_comment_header(line)
     comment_header_len = len(comment_header)
+
+    # Once start line found, search for end of paragraph (a blank line)
     while get_comment_header(line)==comment_header and \
               not is_all_white(line[comment_header_len:]):
         lineno = lineno + 1
-        line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
+        line = text.get("%d.0" % lineno, "%d.end" % lineno)
     last = "%d.0" % lineno
-    # Search back to beginning of paragraph
+
+    # Search back to beginning of paragraph (first blank line before)
     lineno = first_lineno - 1
-    line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
+    line = text.get("%d.0" % lineno, "%d.end" % lineno)
     while lineno > 0 and \
               get_comment_header(line)==comment_header and \
               not is_all_white(line[comment_header_len:]):
         lineno = lineno - 1
-        line = text.get("%d.0" % lineno, "%d.0 lineend" % lineno)
+        line = text.get("%d.0" % lineno, "%d.end" % lineno)
     first = "%d.0" % (lineno+1)
+
     return first, last, comment_header, text.get(first, last)
 
+# This should perhaps be replaced with textwrap.wrap
 def reformat_paragraph(data, limit):
+    """Return data reformatted to specified width (limit)."""
     lines = data.split("\n")
     i = 0
     n = len(lines)
@@ -126,7 +130,7 @@
             if not word:
                 continue # Can happen when line ends in whitespace
             if len((partial + word).expandtabs()) > limit and \
-               partial != indent1:
+                   partial != indent1:
                 new.append(partial.rstrip())
                 partial = indent2
             partial = partial + word + " "
@@ -138,13 +142,50 @@
     new.extend(lines[i:])
     return "\n".join(new)
 
+def reformat_comment(data, limit, comment_header):
+    """Return data reformatted to specified width with comment header."""
+
+    # Remove header from the comment lines
+    lc = len(comment_header)
+    data = "\n".join(line[lc:] for line in data.split("\n"))
+    # Reformat to maxformatwidth chars or a 20 char width,
+    # whichever is greater.
+    format_width = max(limit - len(comment_header), 20)
+    newdata = reformat_paragraph(data, format_width)
+    # re-split and re-insert the comment header.
+    newdata = newdata.split("\n")
+    # If the block ends in a \n, we dont want the comment prefix
+    # inserted after it. (Im not sure it makes sense to reformat a
+    # comment block that is not made of complete lines, but whatever!)
+    # Can't think of a clean solution, so we hack away
+    block_suffix = ""
+    if not newdata[-1]:
+        block_suffix = "\n"
+        newdata = newdata[:-1]
+    return '\n'.join(comment_header+line for line in newdata) + block_suffix
+
 def is_all_white(line):
+    """Return True if line is empty or all whitespace."""
+
     return re.match(r"^\s*$", line) is not None
 
 def get_indent(line):
-    return re.match(r"^(\s*)", line).group()
+    """Return the initial space or tab indent of line."""
+    return re.match(r"^([ \t]*)", line).group()
 
 def get_comment_header(line):
-    m = re.match(r"^(\s*#*)", line)
+    """Return string with leading whitespace and '#' from line or ''.
+
+    A null return indicates that the line is not a comment line. A non-
+    null return, such as '    #', will be used to find the other lines of
+    a comment block with the same  indent.
+    """
+    m = re.match(r"^([ \t]*#*)", line)
     if m is None: return ""
     return m.group(1)
+
+if __name__ == "__main__":
+    from test import support; support.use_resources = ['gui']
+    import unittest
+    unittest.main('idlelib.idle_test.test_formatparagraph',
+            verbosity=2, exit=False)
diff --git a/Lib/idlelib/GrepDialog.py b/Lib/idlelib/GrepDialog.py
index e40e546..b5e35d7 100644
--- a/Lib/idlelib/GrepDialog.py
+++ b/Lib/idlelib/GrepDialog.py
@@ -81,36 +81,24 @@
         hits = 0
         for fn in list:
             try:
-                f = open(fn)
-            except IOError, msg:
+                with open(fn) as f:
+                    for lineno, line in enumerate(f, 1):
+                        if line[-1:] == '\n':
+                            line = line[:-1]
+                        if prog.search(line):
+                            sys.stdout.write("%s: %s: %s\n" %
+                                             (fn, lineno, line))
+                            hits += 1
+            except IOError as msg:
                 print msg
-                continue
-            lineno = 0
-            while 1:
-                block = f.readlines(100000)
-                if not block:
-                    break
-                for line in block:
-                    lineno = lineno + 1
-                    if line[-1:] == '\n':
-                        line = line[:-1]
-                    if prog.search(line):
-                        sys.stdout.write("%s: %s: %s\n" % (fn, lineno, line))
-                        hits = hits + 1
-        if hits:
-            if hits == 1:
-                s = ""
-            else:
-                s = "s"
-            print "Found", hits, "hit%s." % s
-            print "(Hint: right-click to open locations.)"
-        else:
-            print "No hits."
+        print(("Hits found: %s\n"
+              "(Hint: right-click to open locations.)"
+              % hits) if hits else "No hits.")
 
     def findfiles(self, dir, base, rec):
         try:
             names = os.listdir(dir or os.curdir)
-        except os.error, msg:
+        except os.error as msg:
             print msg
             return []
         list = []
@@ -131,3 +119,9 @@
         if self.top:
             self.top.grab_release()
             self.top.withdraw()
+
+if __name__ == "__main__":
+    # A human test is a bit tricky since EditorWindow() imports this module.
+    # Hence Idle must be restarted after editing this file for a live test.
+    import unittest
+    unittest.main('idlelib.idle_test.test_grep', verbosity=2, exit=False)
diff --git a/Lib/idlelib/IOBinding.py b/Lib/idlelib/IOBinding.py
index 8078c3f..ba45ee8 100644
--- a/Lib/idlelib/IOBinding.py
+++ b/Lib/idlelib/IOBinding.py
@@ -248,10 +248,9 @@
         try:
             # open the file in binary mode so that we can handle
             #   end-of-line convention ourselves.
-            f = open(filename,'rb')
-            chars = f.read()
-            f.close()
-        except IOError, msg:
+            with open(filename, 'rb') as f:
+                chars = f.read()
+        except IOError as msg:
             tkMessageBox.showerror("I/O Error", str(msg), master=self.text)
             return False
 
@@ -294,7 +293,7 @@
         # Next look for coding specification
         try:
             enc = coding_spec(chars)
-        except LookupError, name:
+        except LookupError as name:
             tkMessageBox.showerror(
                 title="Error loading the file",
                 message="The encoding '%s' is not known to this Python "\
@@ -383,12 +382,10 @@
         if self.eol_convention != "\n":
             chars = chars.replace("\n", self.eol_convention)
         try:
-            f = open(filename, "wb")
-            f.write(chars)
-            f.flush()
-            f.close()
+            with open(filename, "wb") as f:
+                f.write(chars)
             return True
-        except IOError, msg:
+        except IOError as msg:
             tkMessageBox.showerror("I/O Error", str(msg),
                                    master=self.text)
             return False
@@ -408,7 +405,7 @@
         try:
             enc = coding_spec(chars)
             failed = None
-        except LookupError, msg:
+        except LookupError as msg:
             failed = msg
             enc = None
         if enc:
diff --git a/Lib/idlelib/PathBrowser.py b/Lib/idlelib/PathBrowser.py
index d88a48e..0f4fb3e 100644
--- a/Lib/idlelib/PathBrowser.py
+++ b/Lib/idlelib/PathBrowser.py
@@ -92,4 +92,5 @@
         mainloop()
 
 if __name__ == "__main__":
-    main()
+    from unittest import main
+    main('idlelib.idle_test.test_pathbrowser', verbosity=2, exit=False)
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py
index 0a77bb5..0acfe4a 100644
--- a/Lib/idlelib/PyShell.py
+++ b/Lib/idlelib/PyShell.py
@@ -50,35 +50,55 @@
 # internal warnings to the console.  ScriptBinding.check_syntax() will
 # temporarily redirect the stream to the shell window to display warnings when
 # checking user's code.
-global warning_stream
-warning_stream = sys.__stderr__
-try:
-    import warnings
-except ImportError:
-    pass
-else:
-    def idle_showwarning(message, category, filename, lineno,
-                         file=None, line=None):
-        if file is None:
-            file = warning_stream
-        try:
-            file.write(warnings.formatwarning(message, category, filename,
-                                              lineno, line=line))
-        except IOError:
-            pass  ## file (probably __stderr__) is invalid, warning dropped.
-    warnings.showwarning = idle_showwarning
-    def idle_formatwarning(message, category, filename, lineno, line=None):
-        """Format warnings the IDLE way"""
-        s = "\nWarning (from warnings module):\n"
-        s += '  File \"%s\", line %s\n' % (filename, lineno)
-        if line is None:
-            line = linecache.getline(filename, lineno)
-        line = line.strip()
-        if line:
-            s += "    %s\n" % line
-        s += "%s: %s\n>>> " % (category.__name__, message)
-        return s
-    warnings.formatwarning = idle_formatwarning
+warning_stream = sys.__stderr__  # None, at least on Windows, if no console.
+import warnings
+
+def idle_formatwarning(message, category, filename, lineno, line=None):
+    """Format warnings the IDLE way."""
+
+    s = "\nWarning (from warnings module):\n"
+    s += '  File \"%s\", line %s\n' % (filename, lineno)
+    if line is None:
+        line = linecache.getline(filename, lineno)
+    line = line.strip()
+    if line:
+        s += "    %s\n" % line
+    s += "%s: %s\n" % (category.__name__, message)
+    return s
+
+def idle_showwarning(
+        message, category, filename, lineno, file=None, line=None):
+    """Show Idle-format warning (after replacing warnings.showwarning).
+
+    The differences are the formatter called, the file=None replacement,
+    which can be None, the capture of the consequence AttributeError,
+    and the output of a hard-coded prompt.
+    """
+    if file is None:
+        file = warning_stream
+    try:
+        file.write(idle_formatwarning(
+                message, category, filename, lineno, line=line))
+        file.write(">>> ")
+    except (AttributeError, IOError):
+        pass  # if file (probably __stderr__) is invalid, skip warning.
+
+_warnings_showwarning = None
+
+def capture_warnings(capture):
+    "Replace warning.showwarning with idle_showwarning, or reverse."
+
+    global _warnings_showwarning
+    if capture:
+        if _warnings_showwarning is None:
+            _warnings_showwarning = warnings.showwarning
+            warnings.showwarning = idle_showwarning
+    else:
+        if _warnings_showwarning is not None:
+            warnings.showwarning = _warnings_showwarning
+            _warnings_showwarning = None
+
+capture_warnings(True)
 
 def extended_linecache_checkcache(filename=None,
                                   orig_checkcache=linecache.checkcache):
@@ -370,6 +390,7 @@
         self.port = PORT
         self.original_compiler_flags = self.compile.compiler.flags
 
+    _afterid = None
     rpcclt = None
     rpcpid = None
 
@@ -409,7 +430,7 @@
             try:
                 self.rpcclt = MyRPCClient(addr)
                 break
-            except socket.error, err:
+            except socket.error as err:
                 pass
         else:
             self.display_port_binding_error()
@@ -430,7 +451,7 @@
         self.rpcclt.listening_sock.settimeout(10)
         try:
             self.rpcclt.accept()
-        except socket.timeout, err:
+        except socket.timeout as err:
             self.display_no_subprocess_error()
             return None
         self.rpcclt.register("console", self.tkconsole)
@@ -465,7 +486,7 @@
         self.spawn_subprocess()
         try:
             self.rpcclt.accept()
-        except socket.timeout, err:
+        except socket.timeout as err:
             self.display_no_subprocess_error()
             return None
         self.transfer_path(with_cwd=with_cwd)
@@ -497,6 +518,8 @@
         threading.Thread(target=self.__request_interrupt).start()
 
     def kill_subprocess(self):
+        if self._afterid is not None:
+            self.tkconsole.text.after_cancel(self._afterid)
         try:
             self.rpcclt.close()
         except AttributeError:  # no socket
@@ -569,8 +592,8 @@
                 pass
         # Reschedule myself
         if not self.tkconsole.closing:
-            self.tkconsole.text.after(self.tkconsole.pollinterval,
-                                      self.poll_subprocess)
+            self._afterid = self.tkconsole.text.after(
+                self.tkconsole.pollinterval, self.poll_subprocess)
 
     debugger = None
 
@@ -987,10 +1010,6 @@
         self.stop_readline()
         self.canceled = True
         self.closing = True
-        # Wait for poll_subprocess() rescheduling to stop
-        self.text.after(2 * self.pollinterval, self.close2)
-
-    def close2(self):
         return EditorWindow.close(self)
 
     def _close(self):
@@ -1429,6 +1448,7 @@
 def main():
     global flist, root, use_subprocess
 
+    capture_warnings(True)
     use_subprocess = True
     enable_shell = False
     enable_edit = False
@@ -1438,7 +1458,7 @@
     startup = False
     try:
         opts, args = getopt.getopt(sys.argv[1:], "c:deihnr:st:")
-    except getopt.error, msg:
+    except getopt.error as msg:
         sys.stderr.write("Error: %s\n" % str(msg))
         sys.stderr.write(usage_msg)
         sys.exit(2)
@@ -1561,7 +1581,10 @@
     while flist.inversedict:  # keep IDLE running while files are open.
         root.mainloop()
     root.destroy()
+    capture_warnings(False)
 
 if __name__ == "__main__":
     sys.modules['PyShell'] = sys.modules['__main__']
     main()
+
+capture_warnings(False)  # Make sure turned off; see issue 18081
diff --git a/Lib/idlelib/RstripExtension.py b/Lib/idlelib/RstripExtension.py
index 19e35d4..2ce3c7e 100644
--- a/Lib/idlelib/RstripExtension.py
+++ b/Lib/idlelib/RstripExtension.py
@@ -1,13 +1,9 @@
 'Provides "Strip trailing whitespace" under the "Format" menu.'
 
-__author__ = "Roger D. Serwy <roger.serwy at gmail.com>"
-
 class RstripExtension:
 
     menudefs = [
-        ('format', [None,
-               ('Strip trailing whitespace', '<<do-rstrip>>'),
-       ]),]
+        ('format', [None, ('Strip trailing whitespace', '<<do-rstrip>>'), ] ), ]
 
     def __init__(self, editwin):
         self.editwin = editwin
@@ -20,10 +16,18 @@
 
         undo.undo_block_start()
 
-        end_line = int(float(text.index('end'))) + 1
+        end_line = int(float(text.index('end')))
         for cur in range(1, end_line):
-            txt = text.get('%i.0' % cur, '%i.0 lineend' % cur)
+            txt = text.get('%i.0' % cur, '%i.end' % cur)
+            raw = len(txt)
             cut = len(txt.rstrip())
-            text.delete('%i.%i' % (cur, cut), '%i.0 lineend' % cur)
+            # Since text.delete() marks file as changed, even if not,
+            # only call it when needed to actually delete something.
+            if cut < raw:
+                text.delete('%i.%i' % (cur, cut), '%i.end' % cur)
 
         undo.undo_block_stop()
+
+if __name__ == "__main__":
+    import unittest
+    unittest.main('idlelib.idle_test.test_rstrip', verbosity=2, exit=False)
diff --git a/Lib/idlelib/ScriptBinding.py b/Lib/idlelib/ScriptBinding.py
index 01ac474..a024ebf 100644
--- a/Lib/idlelib/ScriptBinding.py
+++ b/Lib/idlelib/ScriptBinding.py
@@ -70,13 +70,13 @@
         f = open(filename, 'r')
         try:
             tabnanny.process_tokens(tokenize.generate_tokens(f.readline))
-        except tokenize.TokenError, msg:
+        except tokenize.TokenError as msg:
             msgtxt, (lineno, start) = msg
             self.editwin.gotoline(lineno)
             self.errorbox("Tabnanny Tokenizing Error",
                           "Token Error: %s" % msgtxt)
             return False
-        except tabnanny.NannyNag, nag:
+        except tabnanny.NannyNag as nag:
             # The error messages from tabnanny are too confusing...
             self.editwin.gotoline(nag.get_lineno())
             self.errorbox("Tab/space error", indent_message)
@@ -87,9 +87,8 @@
         self.shell = shell = self.flist.open_shell()
         saved_stream = shell.get_warning_stream()
         shell.set_warning_stream(shell.stderr)
-        f = open(filename, 'r')
-        source = f.read()
-        f.close()
+        with open(filename, 'r') as f:
+            source = f.read()
         if '\r' in source:
             source = re.sub(r"\r\n", "\n", source)
             source = re.sub(r"\r", "\n", source)
@@ -101,7 +100,7 @@
             try:
                 # If successful, return the compiled code
                 return compile(source, filename, "exec")
-            except (SyntaxError, OverflowError, ValueError), err:
+            except (SyntaxError, OverflowError, ValueError) as err:
                 try:
                     msg, (errorfilename, lineno, offset, line) = err
                     if not errorfilename:
@@ -152,16 +151,16 @@
         dirname = os.path.dirname(filename)
         # XXX Too often this discards arguments the user just set...
         interp.runcommand("""if 1:
-            _filename = %r
+            __file__ = {filename!r}
             import sys as _sys
             from os.path import basename as _basename
             if (not _sys.argv or
-                _basename(_sys.argv[0]) != _basename(_filename)):
-                _sys.argv = [_filename]
+                _basename(_sys.argv[0]) != _basename(__file__)):
+                _sys.argv = [__file__]
             import os as _os
-            _os.chdir(%r)
-            del _filename, _sys, _basename, _os
-            \n""" % (filename, dirname))
+            _os.chdir({dirname!r})
+            del _sys, _basename, _os
+            \n""".format(filename=filename, dirname=dirname))
         interp.prepend_syspath(filename)
         # XXX KBK 03Jul04 When run w/o subprocess, runtime warnings still
         #         go to __stderr__.  With subprocess, they go to the shell.
diff --git a/Lib/idlelib/SearchDialog.py b/Lib/idlelib/SearchDialog.py
index 7c70b84..3ba0cdb 100644
--- a/Lib/idlelib/SearchDialog.py
+++ b/Lib/idlelib/SearchDialog.py
@@ -24,13 +24,12 @@
 
     def create_widgets(self):
         f = SearchDialogBase.create_widgets(self)
-        self.make_button("Find", self.default_command, 1)
+        self.make_button("Find Next", self.default_command, 1)
 
     def default_command(self, event=None):
         if not self.engine.getprog():
             return
-        if self.find_again(self.text):
-            self.close()
+        self.find_again(self.text)
 
     def find_again(self, text):
         if not self.engine.getpat():
diff --git a/Lib/idlelib/SearchEngine.py b/Lib/idlelib/SearchEngine.py
index cc40a00..6745faf 100644
--- a/Lib/idlelib/SearchEngine.py
+++ b/Lib/idlelib/SearchEngine.py
@@ -66,7 +66,7 @@
             flags = flags | re.IGNORECASE
         try:
             prog = re.compile(pat, flags)
-        except re.error, what:
+        except re.error as what:
             try:
                 msg, col = what
             except:
diff --git a/Lib/idlelib/configSectionNameDialog.py b/Lib/idlelib/configSectionNameDialog.py
index 4f1b002..04fcd8c 100644
--- a/Lib/idlelib/configSectionNameDialog.py
+++ b/Lib/idlelib/configSectionNameDialog.py
@@ -1,97 +1,100 @@
 """
 Dialog that allows user to specify a new config file section name.
 Used to get new highlight theme and keybinding set names.
+The 'return value' for the dialog, used two placed in configDialog.py,
+is the .result attribute set in the Ok and Cancel methods.
 """
 from Tkinter import *
 import tkMessageBox
-
 class GetCfgSectionNameDialog(Toplevel):
-    def __init__(self,parent,title,message,usedNames):
+    def __init__(self, parent, title, message, used_names):
         """
         message - string, informational message to display
-        usedNames - list, list of names already in use for validity check
+        used_names - string collection, names already in use for validity check
         """
         Toplevel.__init__(self, parent)
         self.configure(borderwidth=5)
-        self.resizable(height=FALSE,width=FALSE)
+        self.resizable(height=FALSE, width=FALSE)
         self.title(title)
         self.transient(parent)
         self.grab_set()
         self.protocol("WM_DELETE_WINDOW", self.Cancel)
         self.parent = parent
-        self.message=message
-        self.usedNames=usedNames
-        self.result=''
-        self.CreateWidgets()
-        self.withdraw() #hide while setting geometry
+        self.message = message
+        self.used_names = used_names
+        self.create_widgets()
+        self.withdraw()  #hide while setting geometry
         self.update_idletasks()
         #needs to be done here so that the winfo_reqwidth is valid
         self.messageInfo.config(width=self.frameMain.winfo_reqwidth())
-        self.geometry("+%d+%d" %
-            ((parent.winfo_rootx()+((parent.winfo_width()/2)
-                -(self.winfo_reqwidth()/2)),
-              parent.winfo_rooty()+((parent.winfo_height()/2)
-                -(self.winfo_reqheight()/2)) )) ) #centre dialog over parent
-        self.deiconify() #geometry set, unhide
+        self.geometry(
+                "+%d+%d" % (
+                parent.winfo_rootx() +
+                (parent.winfo_width()/2 - self.winfo_reqwidth()/2),
+                parent.winfo_rooty() +
+                (parent.winfo_height()/2 - self.winfo_reqheight()/2)
+                ) )  #centre dialog over parent
+        self.deiconify()  #geometry set, unhide
         self.wait_window()
-
-    def CreateWidgets(self):
-        self.name=StringVar(self)
-        self.fontSize=StringVar(self)
-        self.frameMain = Frame(self,borderwidth=2,relief=SUNKEN)
-        self.frameMain.pack(side=TOP,expand=TRUE,fill=BOTH)
-        self.messageInfo=Message(self.frameMain,anchor=W,justify=LEFT,padx=5,pady=5,
-                text=self.message)#,aspect=200)
-        entryName=Entry(self.frameMain,textvariable=self.name,width=30)
+    def create_widgets(self):
+        self.name = StringVar(self.parent)
+        self.fontSize = StringVar(self.parent)
+        self.frameMain = Frame(self, borderwidth=2, relief=SUNKEN)
+        self.frameMain.pack(side=TOP, expand=TRUE, fill=BOTH)
+        self.messageInfo = Message(self.frameMain, anchor=W, justify=LEFT,
+                    padx=5, pady=5, text=self.message) #,aspect=200)
+        entryName = Entry(self.frameMain, textvariable=self.name, width=30)
         entryName.focus_set()
-        self.messageInfo.pack(padx=5,pady=5)#,expand=TRUE,fill=BOTH)
-        entryName.pack(padx=5,pady=5)
-        frameButtons=Frame(self)
-        frameButtons.pack(side=BOTTOM,fill=X)
-        self.buttonOk = Button(frameButtons,text='Ok',
-                width=8,command=self.Ok)
-        self.buttonOk.grid(row=0,column=0,padx=5,pady=5)
-        self.buttonCancel = Button(frameButtons,text='Cancel',
-                width=8,command=self.Cancel)
-        self.buttonCancel.grid(row=0,column=1,padx=5,pady=5)
+        self.messageInfo.pack(padx=5, pady=5) #, expand=TRUE, fill=BOTH)
+        entryName.pack(padx=5, pady=5)
+        frameButtons = Frame(self, pady=2)
+        frameButtons.pack(side=BOTTOM)
+        self.buttonOk = Button(frameButtons, text='Ok',
+                width=8, command=self.Ok)
+        self.buttonOk.pack(side=LEFT, padx=5)
+        self.buttonCancel = Button(frameButtons, text='Cancel',
+                width=8, command=self.Cancel)
+        self.buttonCancel.pack(side=RIGHT, padx=5)
 
-    def NameOk(self):
-        #simple validity check for a sensible
-        #ConfigParser file section name
-        nameOk=1
-        name=self.name.get()
-        name.strip()
+    def name_ok(self):
+        ''' After stripping entered name, check that it is a  sensible
+        ConfigParser file section name. Return it if it is, '' if not.
+        '''
+        name = self.name.get().strip()
         if not name: #no name specified
             tkMessageBox.showerror(title='Name Error',
                     message='No name specified.', parent=self)
-            nameOk=0
         elif len(name)>30: #name too long
             tkMessageBox.showerror(title='Name Error',
                     message='Name too long. It should be no more than '+
                     '30 characters.', parent=self)
-            nameOk=0
-        elif name in self.usedNames:
+            name = ''
+        elif name in self.used_names:
             tkMessageBox.showerror(title='Name Error',
                     message='This name is already in use.', parent=self)
-            nameOk=0
-        return nameOk
-
+            name = ''
+        return name
     def Ok(self, event=None):
-        if self.NameOk():
-            self.result=self.name.get().strip()
+        name = self.name_ok()
+        if name:
+            self.result = name
             self.destroy()
-
     def Cancel(self, event=None):
-        self.result=''
+        self.result = ''
         self.destroy()
-
 if __name__ == '__main__':
-    #test the dialog
-    root=Tk()
+    import unittest
+    unittest.main('idlelib.idle_test.test_config_name', verbosity=2, exit=False)
+
+    # also human test the dialog
+    root = Tk()
     def run():
-        keySeq=''
         dlg=GetCfgSectionNameDialog(root,'Get Name',
-                'The information here should need to be word wrapped. Test.')
+                "After the text entered with [Ok] is stripped, <nothing>, "
+                "'abc', or more that 30 chars are errors. "
+                "Close with a valid entry (printed), [Cancel], or [X]",
+                {'abc'})
         print dlg.result
-    Button(root,text='Dialog',command=run).pack()
+    Message(root, text='').pack()  # will be needed for oher dialog tests
+    Button(root, text='Click to begin dialog test', command=run).pack()
     root.mainloop()
diff --git a/Lib/idlelib/help.txt b/Lib/idlelib/help.txt
index 4b42e05..bd6822c 100644
--- a/Lib/idlelib/help.txt
+++ b/Lib/idlelib/help.txt
@@ -5,7 +5,7 @@
 
 File Menu:
 
-	New Window       -- Create a new editing window
+	New File         -- Create a new editing window
 	Open...          -- Open an existing file
 	Recent Files...  -- Open a list of recent files
 	Open Module...   -- Open an existing module (searches sys.path)
diff --git a/Lib/idlelib/idle_test/README.txt b/Lib/idlelib/idle_test/README.txt
new file mode 100644
index 0000000..a8d4dcb
--- /dev/null
+++ b/Lib/idlelib/idle_test/README.txt
@@ -0,0 +1,108 @@
+README FOR IDLE TESTS IN IDLELIB.IDLE_TEST
+
+
+1. Test Files
+
+The idle directory, idlelib, has over 60 xyz.py files. The idle_test
+subdirectory should contain a test_xyy.py for each. (For test modules, make
+'xyz' lower case, and possibly shorten it.) Each file should start with the
+something like the following template, with the blanks after after '.' and 'as',
+and before and after '_' filled in.
+---
+import unittest
+from test.support import requires
+import idlelib. as
+
+class _Test(unittest.TestCase):
+
+    def test_(self):
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=2)
+---
+Idle tests are run with unittest; do not use regrtest's test_main.
+
+Once test_xyy is written, the following should go at the end of xyy.py,
+with xyz (lowercased) added after 'test_'.
+---
+if __name__ == "__main__":
+    from test import support; support.use_resources = ['gui']
+    import unittest
+    unittest.main('idlelib.idle_test.test_', verbosity=2, exit=False)
+---
+
+
+2. Gui Tests
+
+Gui tests need 'requires' and 'use_resources' from test.support
+(test.test_support in 2.7). A test is a gui test if it creates a Tk root or
+master object either directly or indirectly by instantiating a tkinter or
+idle class. For the benefit of buildbot machines that do not have a graphics
+screen, gui tests must be 'guarded' by "requires('gui')" in a setUp
+function or method. This will typically be setUpClass.
+
+All gui objects must be destroyed by the end of the test, perhaps in a tearDown
+function. Creating the Tk root directly in a setUp allows a reference to be saved
+so it can be properly destroyed in the corresponding tearDown. 
+---
+    @classmethod
+    def setUpClass(cls):
+        requires('gui')
+        cls.root = tk.Tk()
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.root.destroy()
+---
+
+Support.requires('gui') returns true if it is either called in a main module
+(which never happens on buildbots) or if use_resources contains 'gui'.
+Use_resources is set by test.regrtest but not by unittest. So when running
+tests in another module with unittest, we set it ourselves, as in the xyz.py
+template above.
+
+Since non-gui tests always run, but gui tests only sometimes, tests of non-gui
+operations should best avoid needing a gui. Methods that make incidental use of
+tkinter (tk) variables and messageboxes can do this by using the mock classes in
+idle_test/mock_tk.py. There is also a mock text that will handle some uses of the
+tk Text widget.
+
+
+3. Running Tests
+
+Assume that xyz.py and test_xyz.py end with the "if __name__" statements given
+above. In Idle, pressing F5 in an editor window with either loaded will run all
+tests in the test_xyz file with the version of Python running Idle.  The test
+report and any tracebacks will appear in the Shell window. The options in these
+"if __name__" statements are appropriate for developers running (as opposed to
+importing) either of the files during development: verbosity=2 lists all test
+methods in the file; exit=False avoids a spurious sys.exit traceback that would
+otherwise occur when running in Idle. The following command lines also run
+all test methods, including gui tests, in test_xyz.py. (The exceptions are that
+idlelib and idlelib.idle start Idle and idlelib.PyShell should (issue 18330).)
+
+python -m idlelib.xyz  # With the capitalization of the xyz module
+python -m idlelib.idle_test.test_xyz
+
+To run all idle_test/test_*.py tests, either interactively
+('>>>', with unittest imported) or from a command line, use one of the
+following. (Notes: unittest does not run gui tests; in 2.7, 'test ' (with the
+space) is 'test.regrtest '; where present, -v and -ugui can be omitted.)
+
+>>> unittest.main('idlelib.idle_test', verbosity=2, exit=False)
+python -m unittest -v idlelib.idle_test
+python -m test -v -ugui test_idle
+python -m test.test_idle
+
+The idle tests are 'discovered' by idlelib.idle_test.__init__.load_tests,
+which is also imported into test.test_idle. Normally, neither file should be
+changed when working on individual test modules. The third command runs runs
+unittest indirectly through regrtest. The same happens when the entire test
+suite is run with 'python -m test'. So that command must work for buildbots
+to stay green. Idle tests must not disturb the environment in a way that
+makes other tests fail (issue 18081).
+
+To run an individual Testcase or test method, extend the dotted name given to
+unittest on the command line. (But gui tests will not this way.)
+
+python -m unittest -v idlelib.idle_test.text_xyz.Test_case.test_meth
diff --git a/Lib/idlelib/idle_test/__init__.py b/Lib/idlelib/idle_test/__init__.py
new file mode 100644
index 0000000..1bc9536
--- /dev/null
+++ b/Lib/idlelib/idle_test/__init__.py
@@ -0,0 +1,9 @@
+from os.path import dirname
+
+def load_tests(loader, standard_tests, pattern):
+    this_dir = dirname(__file__)
+    top_dir = dirname(dirname(this_dir))
+    package_tests = loader.discover(start_dir=this_dir, pattern='test*.py',
+                                    top_level_dir=top_dir)
+    standard_tests.addTests(package_tests)
+    return standard_tests
diff --git a/Lib/idlelib/idle_test/mock_idle.py b/Lib/idlelib/idle_test/mock_idle.py
new file mode 100644
index 0000000..54834de
--- /dev/null
+++ b/Lib/idlelib/idle_test/mock_idle.py
@@ -0,0 +1,27 @@
+'''Mock classes that imitate idlelib modules or classes.
+
+Attributes and methods will be added as needed for tests.
+'''
+
+from idlelib.idle_test.mock_tk import Text
+
+class Editor(object):
+    '''Minimally imitate EditorWindow.EditorWindow class.
+    '''
+    def __init__(self, flist=None, filename=None, key=None, root=None):
+        self.text = Text()
+        self.undo = UndoDelegator()
+
+    def get_selection_indices(self):
+        first = self.text.index('1.0')
+        last = self.text.index('end')
+        return first, last
+
+class UndoDelegator(object):
+    '''Minimally imitate UndoDelegator,UndoDelegator class.
+    '''
+    # A real undo block is only needed for user interaction.
+    def undo_block_start(*args):
+        pass
+    def undo_block_stop(*args):
+        pass
diff --git a/Lib/idlelib/idle_test/mock_tk.py b/Lib/idlelib/idle_test/mock_tk.py
new file mode 100644
index 0000000..d2ab038
--- /dev/null
+++ b/Lib/idlelib/idle_test/mock_tk.py
@@ -0,0 +1,279 @@
+"""Classes that replace tkinter gui objects used by an object being tested.
+
+A gui object is anything with a master or parent paramenter, which is typically
+required in spite of what the doc strings say.
+"""
+
+class Var(object):
+    "Use for String/Int/BooleanVar: incomplete"
+    def __init__(self, master=None, value=None, name=None):
+        self.master = master
+        self.value = value
+        self.name = name
+    def set(self, value):
+        self.value = value
+    def get(self):
+        return self.value
+
+class Mbox_func(object):
+    """Generic mock for messagebox functions, which all have the same signature.
+
+    Instead of displaying a message box, the mock's call method saves the
+    arguments as instance attributes, which test functions can then examime.
+    """
+    def __init__(self):
+        self.result = None  # The return for all show funcs
+    def __call__(self, title, message, *args, **kwds):
+        # Save all args for possible examination by tester
+        self.title = title
+        self.message = message
+        self.args = args
+        self.kwds = kwds
+        return self.result  # Set by tester for ask functions
+
+class Mbox(object):
+    """Mock for tkinter.messagebox with an Mbox_func for each function.
+
+    This module was 'tkMessageBox' in 2.x; hence the 'import as' in  3.x.
+    Example usage in test_module.py for testing functios in module.py:
+    ---
+from idlelib.idle_test.mock_tk import Mbox
+import module
+
+orig_mbox = module.tkMessageBox
+showerror = Mbox.showerror  # example, for attribute access in test methods
+
+class Test(unittest.TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        module.tkMessageBox = Mbox
+
+    @classmethod
+    def tearDownClass(cls):
+        module.tkMessageBox = orig_mbox
+    ---
+    For 'ask' functions, set func.result return value before calling the method
+    that uses the message function. When tkMessageBox functions are the
+    only gui alls in a method, this replacement makes the method gui-free,
+    """
+    askokcancel = Mbox_func()     # True or False
+    askquestion = Mbox_func()     # 'yes' or 'no'
+    askretrycancel = Mbox_func()  # True or False
+    askyesno = Mbox_func()        # True or False
+    askyesnocancel = Mbox_func()  # True, False, or None
+    showerror = Mbox_func()    # None
+    showinfo = Mbox_func()     # None
+    showwarning = Mbox_func()  # None
+
+from _tkinter import TclError
+
+class Text(object):
+    """A semi-functional non-gui replacement for tkinter.Text text editors.
+
+    The mock's data model is that a text is a list of \n-terminated lines.
+    The mock adds an empty string at  the beginning of the list so that the
+    index of actual lines start at 1, as with Tk. The methods never see this.
+    Tk initializes files with a terminal \n that cannot be deleted. It is
+    invisible in the sense that one cannot move the cursor beyond it.
+
+    This class is only tested (and valid) with strings of ascii chars.
+    For testing, we are not concerned with Tk Text's treatment of,
+    for instance, 0-width characters or character + accent.
+   """
+    def __init__(self, master=None, cnf={}, **kw):
+        '''Initialize mock, non-gui, text-only Text widget.
+
+        At present, all args are ignored. Almost all affect visual behavior.
+        There are just a few Text-only options that affect text behavior.
+        '''
+        self.data = ['', '\n']
+
+    def index(self, index):
+        "Return string version of index decoded according to current text."
+        return "%s.%s" % self._decode(index, endflag=1)
+
+    def _decode(self, index, endflag=0):
+        """Return a (line, char) tuple of int indexes into self.data.
+
+        This implements .index without converting the result back to a string.
+        The result is contrained by the number of lines and linelengths of
+        self.data. For many indexes, the result is initally (1, 0).
+
+        The input index may have any of several possible forms:
+        * line.char float: converted to 'line.char' string;
+        * 'line.char' string, where line and char are decimal integers;
+        * 'line.char lineend', where lineend='lineend' (and char is ignored);
+        * 'line.end', where end='end' (same as above);
+        * 'insert', the positions before terminal \n;
+        * 'end', whose meaning depends on the endflag passed to ._endex.
+        * 'sel.first' or 'sel.last', where sel is a tag -- not implemented.
+        """
+        if isinstance(index, (float, bytes)):
+            index = str(index)
+        try:
+            index=index.lower()
+        except AttributeError:
+            raise TclError('bad text index "%s"' % index)
+
+        lastline =  len(self.data) - 1  # same as number of text lines
+        if index == 'insert':
+            return lastline, len(self.data[lastline]) - 1
+        elif index == 'end':
+            return self._endex(endflag)
+
+        line, char = index.split('.')
+        line = int(line)
+
+        # Out of bounds line becomes first or last ('end') index
+        if line < 1:
+            return 1, 0
+        elif line > lastline:
+            return self._endex(endflag)
+
+        linelength = len(self.data[line])  -1  # position before/at \n
+        if char.endswith(' lineend') or char == 'end':
+            return line, linelength
+            # Tk requires that ignored chars before ' lineend' be valid int
+
+        # Out of bounds char becomes first or last index of line
+        char = int(char)
+        if char < 0:
+            char = 0
+        elif char > linelength:
+            char = linelength
+        return line, char
+
+    def _endex(self, endflag):
+        '''Return position for 'end' or line overflow corresponding to endflag.
+
+       -1: position before terminal \n; for .insert(), .delete
+       0: position after terminal \n; for .get, .delete index 1
+       1: same viewed as begininning of non-existent next line (for .index)
+       '''
+        n = len(self.data)
+        if endflag == 1:
+            return n, 0
+        else:
+            n -= 1
+            return n, len(self.data[n]) + endflag
+
+
+    def insert(self, index, chars):
+        "Insert chars before the character at index."
+
+        if not chars:  # ''.splitlines() is [], not ['']
+            return
+        chars = chars.splitlines(True)
+        if chars[-1][-1] == '\n':
+            chars.append('')
+        line, char = self._decode(index, -1)
+        before = self.data[line][:char]
+        after = self.data[line][char:]
+        self.data[line] = before + chars[0]
+        self.data[line+1:line+1] = chars[1:]
+        self.data[line+len(chars)-1] += after
+
+
+    def get(self, index1, index2=None):
+        "Return slice from index1 to index2 (default is 'index1+1')."
+
+        startline, startchar = self._decode(index1)
+        if index2 is None:
+            endline, endchar = startline, startchar+1
+        else:
+            endline, endchar = self._decode(index2)
+
+        if startline == endline:
+            return self.data[startline][startchar:endchar]
+        else:
+            lines = [self.data[startline][startchar:]]
+            for i in range(startline+1, endline):
+                lines.append(self.data[i])
+            lines.append(self.data[endline][:endchar])
+            return ''.join(lines)
+
+
+    def delete(self, index1, index2=None):
+        '''Delete slice from index1 to index2 (default is 'index1+1').
+
+        Adjust default index2 ('index+1) for line ends.
+        Do not delete the terminal \n at the very end of self.data ([-1][-1]).
+        '''
+        startline, startchar = self._decode(index1, -1)
+        if index2 is None:
+            if startchar < len(self.data[startline])-1:
+                # not deleting \n
+                endline, endchar = startline, startchar+1
+            elif startline < len(self.data) - 1:
+                # deleting non-terminal \n, convert 'index1+1 to start of next line
+                endline, endchar = startline+1, 0
+            else:
+                # do not delete terminal \n if index1 == 'insert'
+                return
+        else:
+            endline, endchar = self._decode(index2, -1)
+            # restricting end position to insert position excludes terminal \n
+
+        if startline == endline and startchar < endchar:
+            self.data[startline] = self.data[startline][:startchar] + \
+                                             self.data[startline][endchar:]
+        elif startline < endline:
+            self.data[startline] = self.data[startline][:startchar] + \
+                                   self.data[endline][endchar:]
+            startline += 1
+            for i in range(startline, endline+1):
+                del self.data[startline]
+
+    def compare(self, index1, op, index2):
+        line1, char1 = self._decode(index1)
+        line2, char2 = self._decode(index2)
+        if op == '<':
+            return line1 < line2 or line1 == line2 and char1 < char2
+        elif op == '<=':
+            return line1 < line2 or line1 == line2 and char1 <= char2
+        elif op == '>':
+            return line1 > line2 or line1 == line2 and char1 > char2
+        elif op == '>=':
+            return line1 > line2 or line1 == line2 and char1 >= char2
+        elif op == '==':
+            return line1 == line2 and char1 == char2
+        elif op == '!=':
+            return line1 != line2 or  char1 != char2
+        else:
+            raise TclError('''bad comparison operator "%s":'''
+                                  '''must be <, <=, ==, >=, >, or !=''' % op)
+
+    # The following Text methods normally do something and return None.
+    # Whether doing nothing is sufficient for a test will depend on the test.
+
+    def mark_set(self, name, index):
+        "Set mark *name* before the character at index."
+        pass
+
+    def mark_unset(self, *markNames):
+        "Delete all marks in markNames."
+
+    def tag_remove(self, tagName, index1, index2=None):
+        "Remove tag tagName from all characters between index1 and index2."
+        pass
+
+    # The following Text methods affect the graphics screen and return None.
+    # Doing nothing should always be sufficient for tests.
+
+    def scan_dragto(self, x, y):
+        "Adjust the view of the text according to scan_mark"
+
+    def scan_mark(self, x, y):
+        "Remember the current X, Y coordinates."
+
+    def see(self, index):
+        "Scroll screen to make the character at INDEX is visible."
+        pass
+
+    #  The following is a Misc method inheritet by Text.
+    # It should properly go in a Misc mock, but is included here for now.
+
+    def bind(sequence=None, func=None, add=None):
+        "Bind to this widget at event sequence a call to function func."
+        pass
diff --git a/Lib/idlelib/idle_test/test_calltips.py b/Lib/idlelib/idle_test/test_calltips.py
new file mode 100644
index 0000000..79884f2
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_calltips.py
@@ -0,0 +1,20 @@
+import unittest
+import idlelib.CallTips as ct
+CTi = ct.CallTips()
+
+class Get_entityTest(unittest.TestCase):
+    # In 3.x, get_entity changed from 'instance method' to module function
+    # since 'self' not used. Use dummy instance until change 2.7 also.
+    def test_bad_entity(self):
+        self.assertIsNone(CTi.get_entity('1/0'))
+    def test_good_entity(self):
+        self.assertIs(CTi.get_entity('int'), int)
+
+class Py2Test(unittest.TestCase):
+    def test_paramtuple_float(self):
+        # 18539: (a,b) becomes '.0' in code object; change that but not float
+        def f((a,b), c=0.0): pass
+        self.assertEqual(ct.get_arg_text(f), '(<tuple>, c=0.0)')
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_config_name.py b/Lib/idlelib/idle_test/test_config_name.py
new file mode 100644
index 0000000..4403f87
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_config_name.py
@@ -0,0 +1,75 @@
+"""Unit tests for idlelib.configSectionNameDialog"""
+import unittest
+from idlelib.idle_test.mock_tk import Var, Mbox
+from idlelib import configSectionNameDialog as name_dialog_module
+
+name_dialog = name_dialog_module.GetCfgSectionNameDialog
+
+class Dummy_name_dialog(object):
+    # Mock for testing the following methods of name_dialog
+    name_ok = name_dialog.name_ok.im_func
+    Ok = name_dialog.Ok.im_func
+    Cancel = name_dialog.Cancel.im_func
+    # Attributes, constant or variable, needed for tests
+    used_names = ['used']
+    name = Var()
+    result = None
+    destroyed = False
+    def destroy(self):
+        self.destroyed = True
+
+# name_ok calls Mbox.showerror if name is not ok
+orig_mbox = name_dialog_module.tkMessageBox
+showerror = Mbox.showerror
+
+class ConfigNameTest(unittest.TestCase):
+    dialog = Dummy_name_dialog()
+
+    @classmethod
+    def setUpClass(cls):
+        name_dialog_module.tkMessageBox = Mbox
+
+    @classmethod
+    def tearDownClass(cls):
+        name_dialog_module.tkMessageBox = orig_mbox
+
+    def test_blank_name(self):
+        self.dialog.name.set(' ')
+        self.assertEqual(self.dialog.name_ok(), '')
+        self.assertEqual(showerror.title, 'Name Error')
+        self.assertIn('No', showerror.message)
+
+    def test_used_name(self):
+        self.dialog.name.set('used')
+        self.assertEqual(self.dialog.name_ok(), '')
+        self.assertEqual(showerror.title, 'Name Error')
+        self.assertIn('use', showerror.message)
+
+    def test_long_name(self):
+        self.dialog.name.set('good'*8)
+        self.assertEqual(self.dialog.name_ok(), '')
+        self.assertEqual(showerror.title, 'Name Error')
+        self.assertIn('too long', showerror.message)
+
+    def test_good_name(self):
+        self.dialog.name.set('  good ')
+        showerror.title = 'No Error'  # should not be called
+        self.assertEqual(self.dialog.name_ok(), 'good')
+        self.assertEqual(showerror.title, 'No Error')
+
+    def test_ok(self):
+        self.dialog.destroyed = False
+        self.dialog.name.set('good')
+        self.dialog.Ok()
+        self.assertEqual(self.dialog.result, 'good')
+        self.assertTrue(self.dialog.destroyed)
+
+    def test_cancel(self):
+        self.dialog.destroyed = False
+        self.dialog.Cancel()
+        self.assertEqual(self.dialog.result, '')
+        self.assertTrue(self.dialog.destroyed)
+
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_delegator.py b/Lib/idlelib/idle_test/test_delegator.py
new file mode 100644
index 0000000..b8ae5ee
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_delegator.py
@@ -0,0 +1,37 @@
+import unittest
+from idlelib.Delegator import Delegator
+
+class DelegatorTest(unittest.TestCase):
+
+    def test_mydel(self):
+        # test a simple use scenario
+
+        # initialize
+        mydel = Delegator(int)
+        self.assertIs(mydel.delegate, int)
+        self.assertEqual(mydel._Delegator__cache, set())
+
+        # add an attribute:
+        self.assertRaises(AttributeError, mydel.__getattr__, 'xyz')
+        bl = mydel.bit_length
+        self.assertIs(bl, int.bit_length)
+        self.assertIs(mydel.__dict__['bit_length'], int.bit_length)
+        self.assertEqual(mydel._Delegator__cache, {'bit_length'})
+
+        # add a second attribute
+        mydel.numerator
+        self.assertEqual(mydel._Delegator__cache, {'bit_length', 'numerator'})
+
+        # delete the second (which, however, leaves it in the name cache)
+        del mydel.numerator
+        self.assertNotIn('numerator', mydel.__dict__)
+        self.assertIn('numerator', mydel._Delegator__cache)
+
+        # reset by calling .setdelegate, which calls .resetcache
+        mydel.setdelegate(float)
+        self.assertIs(mydel.delegate, float)
+        self.assertNotIn('bit_length', mydel.__dict__)
+        self.assertEqual(mydel._Delegator__cache, set())
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=2)
diff --git a/Lib/idlelib/idle_test/test_formatparagraph.py b/Lib/idlelib/idle_test/test_formatparagraph.py
new file mode 100644
index 0000000..2ec8232
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_formatparagraph.py
@@ -0,0 +1,374 @@
+# Test the functions and main class method of FormatParagraph.py
+import unittest
+from idlelib import FormatParagraph as fp
+from idlelib.EditorWindow import EditorWindow
+from tkinter import Tk, Text, TclError
+from test.support import requires
+
+
+class Is_Get_Test(unittest.TestCase):
+    """Test the is_ and get_ functions"""
+    test_comment = '# This is a comment'
+    test_nocomment = 'This is not a comment'
+    trailingws_comment = '# This is a comment   '
+    leadingws_comment = '    # This is a comment'
+    leadingws_nocomment = '    This is not a comment'
+
+    def test_is_all_white(self):
+        self.assertTrue(fp.is_all_white(''))
+        self.assertTrue(fp.is_all_white('\t\n\r\f\v'))
+        self.assertFalse(fp.is_all_white(self.test_comment))
+
+    def test_get_indent(self):
+        Equal = self.assertEqual
+        Equal(fp.get_indent(self.test_comment), '')
+        Equal(fp.get_indent(self.trailingws_comment), '')
+        Equal(fp.get_indent(self.leadingws_comment), '    ')
+        Equal(fp.get_indent(self.leadingws_nocomment), '    ')
+
+    def test_get_comment_header(self):
+        Equal = self.assertEqual
+        # Test comment strings
+        Equal(fp.get_comment_header(self.test_comment), '#')
+        Equal(fp.get_comment_header(self.trailingws_comment), '#')
+        Equal(fp.get_comment_header(self.leadingws_comment), '    #')
+        # Test non-comment strings
+        Equal(fp.get_comment_header(self.leadingws_nocomment), '    ')
+        Equal(fp.get_comment_header(self.test_nocomment), '')
+
+
+class FindTest(unittest.TestCase):
+    """Test the find_paragraph function in FormatParagraph.
+
+    Using the runcase() function, find_paragraph() is called with 'mark' set at
+    multiple indexes before and inside the test paragraph.
+
+    It appears that code with the same indentation as a quoted string is grouped
+    as part of the same paragraph, which is probably incorrect behavior.
+    """
+
+    @classmethod
+    def setUpClass(cls):
+        from idlelib.idle_test.mock_tk import Text
+        cls.text = Text()
+
+    def runcase(self, inserttext, stopline, expected):
+        # Check that find_paragraph returns the expected paragraph when
+        # the mark index is set to beginning, middle, end of each line
+        # up to but not including the stop line
+        text = self.text
+        text.insert('1.0', inserttext)
+        for line in range(1, stopline):
+            linelength = int(text.index("%d.end" % line).split('.')[1])
+            for col in (0, linelength//2, linelength):
+                tempindex = "%d.%d" % (line, col)
+                self.assertEqual(fp.find_paragraph(text, tempindex), expected)
+        text.delete('1.0', 'end')
+
+    def test_find_comment(self):
+        comment = (
+            "# Comment block with no blank lines before\n"
+            "# Comment line\n"
+            "\n")
+        self.runcase(comment, 3, ('1.0', '3.0', '#', comment[0:58]))
+
+        comment = (
+            "\n"
+            "# Comment block with whitespace line before and after\n"
+            "# Comment line\n"
+            "\n")
+        self.runcase(comment, 4, ('2.0', '4.0', '#', comment[1:70]))
+
+        comment = (
+            "\n"
+            "    # Indented comment block with whitespace before and after\n"
+            "    # Comment line\n"
+            "\n")
+        self.runcase(comment, 4, ('2.0', '4.0', '    #', comment[1:82]))
+
+        comment = (
+            "\n"
+            "# Single line comment\n"
+            "\n")
+        self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:23]))
+
+        comment = (
+            "\n"
+            "    # Single line comment with leading whitespace\n"
+            "\n")
+        self.runcase(comment, 3, ('2.0', '3.0', '    #', comment[1:51]))
+
+        comment = (
+            "\n"
+            "# Comment immediately followed by code\n"
+            "x = 42\n"
+            "\n")
+        self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:40]))
+
+        comment = (
+            "\n"
+            "    # Indented comment immediately followed by code\n"
+            "x = 42\n"
+            "\n")
+        self.runcase(comment, 3, ('2.0', '3.0', '    #', comment[1:53]))
+
+        comment = (
+            "\n"
+            "# Comment immediately followed by indented code\n"
+            "    x = 42\n"
+            "\n")
+        self.runcase(comment, 3, ('2.0', '3.0', '#', comment[1:49]))
+
+    def test_find_paragraph(self):
+        teststring = (
+            '"""String with no blank lines before\n'
+            'String line\n'
+            '"""\n'
+            '\n')
+        self.runcase(teststring, 4, ('1.0', '4.0', '', teststring[0:53]))
+
+        teststring = (
+            "\n"
+            '"""String with whitespace line before and after\n'
+            'String line.\n'
+            '"""\n'
+            '\n')
+        self.runcase(teststring, 5, ('2.0', '5.0', '', teststring[1:66]))
+
+        teststring = (
+            '\n'
+            '    """Indented string with whitespace before and after\n'
+            '    Comment string.\n'
+            '    """\n'
+            '\n')
+        self.runcase(teststring, 5, ('2.0', '5.0', '    ', teststring[1:85]))
+
+        teststring = (
+            '\n'
+            '"""Single line string."""\n'
+            '\n')
+        self.runcase(teststring, 3, ('2.0', '3.0', '', teststring[1:27]))
+
+        teststring = (
+            '\n'
+            '    """Single line string with leading whitespace."""\n'
+            '\n')
+        self.runcase(teststring, 3, ('2.0', '3.0', '    ', teststring[1:55]))
+
+
+class ReformatFunctionTest(unittest.TestCase):
+    """Test the reformat_paragraph function without the editor window."""
+
+    def test_reformat_paragrah(self):
+        Equal = self.assertEqual
+        reform = fp.reformat_paragraph
+        hw = "O hello world"
+        Equal(reform(' ', 1), ' ')
+        Equal(reform("Hello    world", 20), "Hello  world")
+
+        # Test without leading newline
+        Equal(reform(hw, 1), "O\nhello\nworld")
+        Equal(reform(hw, 6), "O\nhello\nworld")
+        Equal(reform(hw, 7), "O hello\nworld")
+        Equal(reform(hw, 12), "O hello\nworld")
+        Equal(reform(hw, 13), "O hello world")
+
+        # Test with leading newline
+        hw = "\nO hello world"
+        Equal(reform(hw, 1), "\nO\nhello\nworld")
+        Equal(reform(hw, 6), "\nO\nhello\nworld")
+        Equal(reform(hw, 7), "\nO hello\nworld")
+        Equal(reform(hw, 12), "\nO hello\nworld")
+        Equal(reform(hw, 13), "\nO hello world")
+
+
+class ReformatCommentTest(unittest.TestCase):
+    """Test the reformat_comment function without the editor window."""
+
+    def test_reformat_comment(self):
+        Equal = self.assertEqual
+
+        # reformat_comment formats to a minimum of 20 characters
+        test_string = (
+            "    \"\"\"this is a test of a reformat for a triple quoted string"
+            " will it reformat to less than 70 characters for me?\"\"\"")
+        result = fp.reformat_comment(test_string, 70, "    ")
+        expected = (
+            "    \"\"\"this is a test of a reformat for a triple quoted string will it\n"
+            "    reformat to less than 70 characters for me?\"\"\"")
+        Equal(result, expected)
+
+        test_comment = (
+            "# this is a test of a reformat for a triple quoted string will "
+            "it reformat to less than 70 characters for me?")
+        result = fp.reformat_comment(test_comment, 70, "#")
+        expected = (
+            "# this is a test of a reformat for a triple quoted string will it\n"
+            "# reformat to less than 70 characters for me?")
+        Equal(result, expected)
+
+
+class FormatClassTest(unittest.TestCase):
+    def test_init_close(self):
+        instance = fp.FormatParagraph('editor')
+        self.assertEqual(instance.editwin, 'editor')
+        instance.close()
+        self.assertEqual(instance.editwin, None)
+
+
+# For testing format_paragraph_event, Initialize FormatParagraph with
+# a mock Editor with .text and  .get_selection_indices.  The text must
+# be a Text wrapper that adds two methods
+
+# A real EditorWindow creates unneeded, time-consuming baggage and
+# sometimes emits shutdown warnings like this:
+# "warning: callback failed in WindowList <class '_tkinter.TclError'>
+# : invalid command name ".55131368.windows".
+# Calling EditorWindow._close in tearDownClass prevents this but causes
+# other problems (windows left open).
+
+class TextWrapper:
+    def __init__(self, master):
+        self.text = Text(master=master)
+    def __getattr__(self, name):
+        return getattr(self.text, name)
+    def undo_block_start(self): pass
+    def undo_block_stop(self): pass
+
+class Editor:
+    def __init__(self, root):
+        self.text = TextWrapper(root)
+    get_selection_indices = EditorWindow. get_selection_indices
+
+class FormatEventTest(unittest.TestCase):
+    """Test the formatting of text inside a Text widget.
+
+    This is done with FormatParagraph.format.paragraph_event,
+    which calls funtions in the module as appropriate.
+    """
+    test_string = (
+        "    '''this is a test of a reformat for a triple "
+        "quoted string will it reformat to less than 70 "
+        "characters for me?'''\n")
+    multiline_test_string = (
+        "    '''The first line is under the max width.\n"
+        "    The second line's length is way over the max width. It goes "
+        "on and on until it is over 100 characters long.\n"
+        "    Same thing with the third line. It is also way over the max "
+        "width, but FormatParagraph will fix it.\n"
+        "    '''\n")
+    multiline_test_comment = (
+        "# The first line is under the max width.\n"
+        "# The second line's length is way over the max width. It goes on "
+        "and on until it is over 100 characters long.\n"
+        "# Same thing with the third line. It is also way over the max "
+        "width, but FormatParagraph will fix it.\n"
+        "# The fourth line is short like the first line.")
+
+    @classmethod
+    def setUpClass(cls):
+        requires('gui')
+        cls.root = Tk()
+        editor = Editor(root=cls.root)
+        cls.text = editor.text.text  # Test code does not need the wrapper.
+        cls.formatter = fp.FormatParagraph(editor).format_paragraph_event
+        # Sets the insert mark just after the re-wrapped and inserted  text.
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.root.destroy()
+
+    def test_short_line(self):
+        self.text.insert('1.0', "Short line\n")
+        self.formatter("Dummy")
+        self.assertEqual(self.text.get('1.0', 'insert'), "Short line\n" )
+        self.text.delete('1.0', 'end')
+
+    def test_long_line(self):
+        text = self.text
+
+        # Set cursor ('insert' mark) to '1.0', within text.
+        text.insert('1.0', self.test_string)
+        text.mark_set('insert', '1.0')
+        self.formatter('ParameterDoesNothing')
+        result = text.get('1.0', 'insert')
+        # find function includes \n
+        expected = (
+"    '''this is a test of a reformat for a triple quoted string will it\n"
+"    reformat to less than 70 characters for me?'''\n")  # yes
+        self.assertEqual(result, expected)
+        text.delete('1.0', 'end')
+
+        # Select from 1.11 to line end.
+        text.insert('1.0', self.test_string)
+        text.tag_add('sel', '1.11', '1.end')
+        self.formatter('ParameterDoesNothing')
+        result = text.get('1.0', 'insert')
+        # selection excludes \n
+        expected = (
+"    '''this is a test of a reformat for a triple quoted string will it reformat\n"
+" to less than 70 characters for me?'''")  # no
+        self.assertEqual(result, expected)
+        text.delete('1.0', 'end')
+
+    def test_multiple_lines(self):
+        text = self.text
+        #  Select 2 long lines.
+        text.insert('1.0', self.multiline_test_string)
+        text.tag_add('sel', '2.0', '4.0')
+        self.formatter('ParameterDoesNothing')
+        result = text.get('2.0', 'insert')
+        expected = (
+"    The second line's length is way over the max width. It goes on and\n"
+"    on until it is over 100 characters long. Same thing with the third\n"
+"    line. It is also way over the max width, but FormatParagraph will\n"
+"    fix it.\n")
+        self.assertEqual(result, expected)
+        text.delete('1.0', 'end')
+
+    def test_comment_block(self):
+        text = self.text
+
+        # Set cursor ('insert') to '1.0', within block.
+        text.insert('1.0', self.multiline_test_comment)
+        self.formatter('ParameterDoesNothing')
+        result = text.get('1.0', 'insert')
+        expected = (
+"# The first line is under the max width. The second line's length is\n"
+"# way over the max width. It goes on and on until it is over 100\n"
+"# characters long. Same thing with the third line. It is also way over\n"
+"# the max width, but FormatParagraph will fix it. The fourth line is\n"
+"# short like the first line.\n")
+        self.assertEqual(result, expected)
+        text.delete('1.0', 'end')
+
+        # Select line 2, verify line 1 unaffected.
+        text.insert('1.0', self.multiline_test_comment)
+        text.tag_add('sel', '2.0', '3.0')
+        self.formatter('ParameterDoesNothing')
+        result = text.get('1.0', 'insert')
+        expected = (
+"# The first line is under the max width.\n"
+"# The second line's length is way over the max width. It goes on and\n"
+"# on until it is over 100 characters long.\n")
+        self.assertEqual(result, expected)
+        text.delete('1.0', 'end')
+
+# The following block worked with EditorWindow but fails with the mock.
+# Lines 2 and 3 get pasted together even though the previous block left
+# the previous line alone. More investigation is needed.
+##        # Select lines 3 and 4
+##        text.insert('1.0', self.multiline_test_comment)
+##        text.tag_add('sel', '3.0', '5.0')
+##        self.formatter('ParameterDoesNothing')
+##        result = text.get('3.0', 'insert')
+##        expected = (
+##"# Same thing with the third line. It is also way over the max width,\n"
+##"# but FormatParagraph will fix it. The fourth line is short like the\n"
+##"# first line.\n")
+##        self.assertEqual(result, expected)
+##        text.delete('1.0', 'end')
+
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=2)
diff --git a/Lib/idlelib/idle_test/test_grep.py b/Lib/idlelib/idle_test/test_grep.py
new file mode 100644
index 0000000..e9f4f22
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_grep.py
@@ -0,0 +1,82 @@
+""" !Changing this line will break Test_findfile.test_found!
+Non-gui unit tests for idlelib.GrepDialog methods.
+dummy_command calls grep_it calls findfiles.
+An exception raised in one method will fail callers.
+Otherwise, tests are mostly independent.
+*** Currently only test grep_it.
+"""
+import unittest
+from test.test_support import captured_stdout, findfile
+from idlelib.idle_test.mock_tk import Var
+from idlelib.GrepDialog import GrepDialog
+import re
+
+__file__ = findfile('idlelib/idle_test') + '/test_grep.py'
+
+class Dummy_searchengine:
+    '''GrepDialog.__init__ calls parent SearchDiabolBase which attaches the
+    passed in SearchEngine instance as attribute 'engine'. Only a few of the
+    many possible self.engine.x attributes are needed here.
+    '''
+    def getpat(self):
+        return self._pat
+
+searchengine = Dummy_searchengine()
+
+class Dummy_grep:
+    # Methods tested
+    #default_command = GrepDialog.default_command
+    grep_it = GrepDialog.grep_it.im_func
+    findfiles = GrepDialog.findfiles.im_func
+    # Other stuff needed
+    recvar = Var(False)
+    engine = searchengine
+    def close(self):  # gui method
+        pass
+
+grep = Dummy_grep()
+
+class FindfilesTest(unittest.TestCase):
+    # findfiles is really a function, not a method, could be iterator
+    # test that filename return filename
+    # test that idlelib has many .py files
+    # test that recursive flag adds idle_test .py files
+    pass
+
+class Grep_itTest(unittest.TestCase):
+    # Test captured reports with 0 and some hits.
+    # Should test file names, but Windows reports have mixed / and \ separators
+    # from incomplete replacement, so 'later'.
+
+    def report(self, pat):
+        grep.engine._pat = pat
+        with captured_stdout() as s:
+            grep.grep_it(re.compile(pat), __file__)
+        lines = s.getvalue().split('\n')
+        lines.pop()  # remove bogus '' after last \n
+        return lines
+
+    def test_unfound(self):
+        pat = 'xyz*'*7
+        lines = self.report(pat)
+        self.assertEqual(len(lines), 2)
+        self.assertIn(pat, lines[0])
+        self.assertEqual(lines[1], 'No hits.')
+
+    def test_found(self):
+
+        pat = '""" !Changing this line will break Test_findfile.test_found!'
+        lines = self.report(pat)
+        self.assertEqual(len(lines), 5)
+        self.assertIn(pat, lines[0])
+        self.assertIn('py: 1:', lines[1])  # line number 1
+        self.assertIn('2', lines[3])  # hits found 2
+        self.assertTrue(lines[4].startswith('(Hint:'))
+
+class Default_commandTest(unittest.TestCase):
+    # To write this, mode OutputWindow import to top of GrepDialog
+    # so it can be replaced by captured_stdout in class setup/teardown.
+    pass
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_pathbrowser.py b/Lib/idlelib/idle_test/test_pathbrowser.py
new file mode 100644
index 0000000..7ad7c97
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_pathbrowser.py
@@ -0,0 +1,12 @@
+import unittest
+import idlelib.PathBrowser as PathBrowser
+
+class PathBrowserTest(unittest.TestCase):
+
+    def test_DirBrowserTreeItem(self):
+        # Issue16226 - make sure that getting a sublist works
+        d = PathBrowser.DirBrowserTreeItem('')
+        d.GetSubList()
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_rstrip.py b/Lib/idlelib/idle_test/test_rstrip.py
new file mode 100644
index 0000000..1c90b93
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_rstrip.py
@@ -0,0 +1,49 @@
+import unittest
+import idlelib.RstripExtension as rs
+from idlelib.idle_test.mock_idle import Editor
+
+class rstripTest(unittest.TestCase):
+
+    def test_rstrip_line(self):
+        editor = Editor()
+        text = editor.text
+        do_rstrip = rs.RstripExtension(editor).do_rstrip
+
+        do_rstrip()
+        self.assertEqual(text.get('1.0', 'insert'), '')
+        text.insert('1.0', '     ')
+        do_rstrip()
+        self.assertEqual(text.get('1.0', 'insert'), '')
+        text.insert('1.0', '     \n')
+        do_rstrip()
+        self.assertEqual(text.get('1.0', 'insert'), '\n')
+
+    def test_rstrip_multiple(self):
+        editor = Editor()
+        #  Uncomment following to verify that test passes with real widgets.
+##        from idlelib.EditorWindow import EditorWindow as Editor
+##        from tkinter import Tk
+##        editor = Editor(root=Tk())
+        text = editor.text
+        do_rstrip = rs.RstripExtension(editor).do_rstrip
+
+        original = (
+            "Line with an ending tab    \n"
+            "Line ending in 5 spaces     \n"
+            "Linewithnospaces\n"
+            "    indented line\n"
+            "    indented line with trailing space \n"
+            "    ")
+        stripped = (
+            "Line with an ending tab\n"
+            "Line ending in 5 spaces\n"
+            "Linewithnospaces\n"
+            "    indented line\n"
+            "    indented line with trailing space\n")
+
+        text.insert('1.0', original)
+        do_rstrip()
+        self.assertEqual(text.get('1.0', 'insert'), stripped)
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_text.py b/Lib/idlelib/idle_test/test_text.py
new file mode 100644
index 0000000..3a0705b
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_text.py
@@ -0,0 +1,227 @@
+# Test mock_tk.Text class against tkinter.Text class by running same tests with both.
+import unittest
+from test.test_support import requires
+
+from _tkinter import TclError
+import Tkinter as tk
+
+class TextTest(object):
+
+    hw = 'hello\nworld'  # usual initial insert after initialization
+    hwn = hw+'\n'  # \n present at initialization, before insert
+
+    Text = None
+    def setUp(self):
+        self.text = self.Text()
+
+    def test_init(self):
+        self.assertEqual(self.text.get('1.0'), '\n')
+        self.assertEqual(self.text.get('end'), '')
+
+    def test_index_empty(self):
+        index = self.text.index
+
+        for dex in (-1.0, 0.3, '1.-1', '1.0', '1.0 lineend', '1.end', '1.33',
+                'insert'):
+            self.assertEqual(index(dex), '1.0')
+
+        for dex in 'end', 2.0, '2.1', '33.44':
+            self.assertEqual(index(dex), '2.0')
+
+    def test_index_data(self):
+        index = self.text.index
+        self.text.insert('1.0', self.hw)
+
+        for dex in -1.0, 0.3, '1.-1', '1.0':
+            self.assertEqual(index(dex), '1.0')
+
+        for dex in '1.0 lineend', '1.end', '1.33':
+            self.assertEqual(index(dex), '1.5')
+
+        for dex in 'end',  '33.44':
+            self.assertEqual(index(dex), '3.0')
+
+    def test_get(self):
+        get = self.text.get
+        Equal = self.assertEqual
+        self.text.insert('1.0', self.hw)
+
+        Equal(get('end'), '')
+        Equal(get('end', 'end'), '')
+        Equal(get('1.0'), 'h')
+        Equal(get('1.0', '1.1'), 'h')
+        Equal(get('1.0', '1.3'), 'hel')
+        Equal(get('1.1', '1.3'), 'el')
+        Equal(get('1.0', '1.0 lineend'), 'hello')
+        Equal(get('1.0', '1.10'), 'hello')
+        Equal(get('1.0 lineend'), '\n')
+        Equal(get('1.1', '2.3'), 'ello\nwor')
+        Equal(get('1.0', '2.5'), self.hw)
+        Equal(get('1.0', 'end'), self.hwn)
+        Equal(get('0.0', '5.0'), self.hwn)
+
+    def test_insert(self):
+        insert = self.text.insert
+        get = self.text.get
+        Equal = self.assertEqual
+
+        insert('1.0', self.hw)
+        Equal(get('1.0', 'end'), self.hwn)
+
+        insert('1.0', '')  # nothing
+        Equal(get('1.0', 'end'), self.hwn)
+
+        insert('1.0', '*')
+        Equal(get('1.0', 'end'), '*hello\nworld\n')
+
+        insert('1.0 lineend', '*')
+        Equal(get('1.0', 'end'), '*hello*\nworld\n')
+
+        insert('2.3', '*')
+        Equal(get('1.0', 'end'), '*hello*\nwor*ld\n')
+
+        insert('end', 'x')
+        Equal(get('1.0', 'end'), '*hello*\nwor*ldx\n')
+
+        insert('1.4', 'x\n')
+        Equal(get('1.0', 'end'), '*helx\nlo*\nwor*ldx\n')
+
+    def test_no_delete(self):
+        # if index1 == 'insert' or 'end' or >= end, there is no deletion
+        delete = self.text.delete
+        get = self.text.get
+        Equal = self.assertEqual
+        self.text.insert('1.0', self.hw)
+
+        delete('insert')
+        Equal(get('1.0', 'end'), self.hwn)
+
+        delete('end')
+        Equal(get('1.0', 'end'), self.hwn)
+
+        delete('insert', 'end')
+        Equal(get('1.0', 'end'), self.hwn)
+
+        delete('insert', '5.5')
+        Equal(get('1.0', 'end'), self.hwn)
+
+        delete('1.4', '1.0')
+        Equal(get('1.0', 'end'), self.hwn)
+
+        delete('1.4', '1.4')
+        Equal(get('1.0', 'end'), self.hwn)
+
+    def test_delete_char(self):
+        delete = self.text.delete
+        get = self.text.get
+        Equal = self.assertEqual
+        self.text.insert('1.0', self.hw)
+
+        delete('1.0')
+        Equal(get('1.0', '1.end'), 'ello')
+
+        delete('1.0', '1.1')
+        Equal(get('1.0', '1.end'), 'llo')
+
+        # delete \n and combine 2 lines into 1
+        delete('1.end')
+        Equal(get('1.0', '1.end'), 'lloworld')
+
+        self.text.insert('1.3', '\n')
+        delete('1.10')
+        Equal(get('1.0', '1.end'), 'lloworld')
+
+        self.text.insert('1.3', '\n')
+        delete('1.3', '2.0')
+        Equal(get('1.0', '1.end'), 'lloworld')
+
+    def test_delete_slice(self):
+        delete = self.text.delete
+        get = self.text.get
+        Equal = self.assertEqual
+        self.text.insert('1.0', self.hw)
+
+        delete('1.0', '1.0 lineend')
+        Equal(get('1.0', 'end'), '\nworld\n')
+
+        delete('1.0', 'end')
+        Equal(get('1.0', 'end'), '\n')
+
+        self.text.insert('1.0', self.hw)
+        delete('1.0', '2.0')
+        Equal(get('1.0', 'end'), 'world\n')
+
+        delete('1.0', 'end')
+        Equal(get('1.0', 'end'), '\n')
+
+        self.text.insert('1.0', self.hw)
+        delete('1.2', '2.3')
+        Equal(get('1.0', 'end'), 'held\n')
+
+    def test_multiple_lines(self):  # insert and delete
+        self.text.insert('1.0', 'hello')
+
+        self.text.insert('1.3', '1\n2\n3\n4\n5')
+        self.assertEqual(self.text.get('1.0', 'end'), 'hel1\n2\n3\n4\n5lo\n')
+
+        self.text.delete('1.3', '5.1')
+        self.assertEqual(self.text.get('1.0', 'end'), 'hello\n')
+
+    def test_compare(self):
+        compare = self.text.compare
+        Equal = self.assertEqual
+        # need data so indexes not squished to 1,0
+        self.text.insert('1.0', 'First\nSecond\nThird\n')
+
+        self.assertRaises(TclError, compare, '2.2', 'op', '2.2')
+
+        for op, less1, less0, equal, greater0, greater1 in (
+                ('<', True, True, False, False, False),
+                ('<=', True, True, True, False, False),
+                ('>', False, False, False, True, True),
+                ('>=', False, False, True, True, True),
+                ('==', False, False, True, False, False),
+                ('!=', True, True, False, True, True),
+                ):
+            Equal(compare('1.1', op, '2.2'), less1, op)
+            Equal(compare('2.1', op, '2.2'), less0, op)
+            Equal(compare('2.2', op, '2.2'), equal, op)
+            Equal(compare('2.3', op, '2.2'), greater0, op)
+            Equal(compare('3.3', op, '2.2'), greater1, op)
+
+
+class MockTextTest(TextTest, unittest.TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        from idlelib.idle_test.mock_tk import Text
+        cls.Text = Text
+
+    def test_decode(self):
+        # test endflags (-1, 0) not tested by test_index (which uses +1)
+        decode = self.text._decode
+        Equal = self.assertEqual
+        self.text.insert('1.0', self.hw)
+
+        Equal(decode('end', -1), (2, 5))
+        Equal(decode('3.1', -1), (2, 5))
+        Equal(decode('end',  0), (2, 6))
+        Equal(decode('3.1', 0), (2, 6))
+
+
+class TkTextTest(TextTest, unittest.TestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        requires('gui')
+        from Tkinter import Tk, Text
+        cls.Text = Text
+        cls.root = Tk()
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.root.destroy()
+
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/idle_test/test_warning.py b/Lib/idlelib/idle_test/test_warning.py
new file mode 100644
index 0000000..da1d8a1
--- /dev/null
+++ b/Lib/idlelib/idle_test/test_warning.py
@@ -0,0 +1,73 @@
+'''Test warnings replacement in PyShell.py and run.py.
+
+This file could be expanded to include traceback overrides
+(in same two modules). If so, change name.
+Revise if output destination changes (http://bugs.python.org/issue18318).
+Make sure warnings module is left unaltered (http://bugs.python.org/issue18081).
+'''
+
+import unittest
+from test.test_support import captured_stderr
+
+import warnings
+# Try to capture default showwarning before Idle modules are imported.
+showwarning = warnings.showwarning
+# But if we run this file within idle, we are in the middle of the run.main loop
+# and default showwarnings has already been replaced.
+running_in_idle = 'idle' in showwarning.__name__
+
+from idlelib import run
+from idlelib import PyShell as shell
+
+# The following was generated from PyShell.idle_formatwarning
+# and checked as matching expectation.
+idlemsg = '''
+Warning (from warnings module):
+  File "test_warning.py", line 99
+    Line of code
+UserWarning: Test
+'''
+shellmsg = idlemsg + ">>> "
+
+class RunWarnTest(unittest.TestCase):
+
+    @unittest.skipIf(running_in_idle, "Does not work when run within Idle.")
+    def test_showwarnings(self):
+        self.assertIs(warnings.showwarning, showwarning)
+        run.capture_warnings(True)
+        self.assertIs(warnings.showwarning, run.idle_showwarning_subproc)
+        run.capture_warnings(False)
+        self.assertIs(warnings.showwarning, showwarning)
+
+    def test_run_show(self):
+        with captured_stderr() as f:
+            run.idle_showwarning_subproc(
+                    'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
+            # The following uses .splitlines to erase line-ending differences
+            self.assertEqual(idlemsg.splitlines(), f.getvalue().splitlines())
+
+class ShellWarnTest(unittest.TestCase):
+
+    @unittest.skipIf(running_in_idle, "Does not work when run within Idle.")
+    def test_showwarnings(self):
+        self.assertIs(warnings.showwarning, showwarning)
+        shell.capture_warnings(True)
+        self.assertIs(warnings.showwarning, shell.idle_showwarning)
+        shell.capture_warnings(False)
+        self.assertIs(warnings.showwarning, showwarning)
+
+    def test_idle_formatter(self):
+        # Will fail if format changed without regenerating idlemsg
+        s = shell.idle_formatwarning(
+                'Test', UserWarning, 'test_warning.py', 99, 'Line of code')
+        self.assertEqual(idlemsg, s)
+
+    def test_shell_show(self):
+        with captured_stderr() as f:
+            shell.idle_showwarning(
+                    'Test', UserWarning, 'test_warning.py', 99, f, 'Line of code')
+            self.assertEqual(shellmsg.splitlines(), f.getvalue().splitlines())
+
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2, exit=False)
diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py
index 9cc009f..604c5cd 100644
--- a/Lib/idlelib/run.py
+++ b/Lib/idlelib/run.py
@@ -22,24 +22,38 @@
 
 LOCALHOST = '127.0.0.1'
 
-try:
-    import warnings
-except ImportError:
-    pass
-else:
-    def idle_formatwarning_subproc(message, category, filename, lineno,
-                                   line=None):
-        """Format warnings the IDLE way"""
-        s = "\nWarning (from warnings module):\n"
-        s += '  File \"%s\", line %s\n' % (filename, lineno)
-        if line is None:
-            line = linecache.getline(filename, lineno)
-        line = line.strip()
-        if line:
-            s += "    %s\n" % line
-        s += "%s: %s\n" % (category.__name__, message)
-        return s
-    warnings.formatwarning = idle_formatwarning_subproc
+import warnings
+
+def idle_showwarning_subproc(
+        message, category, filename, lineno, file=None, line=None):
+    """Show Idle-format warning after replacing warnings.showwarning.
+
+    The only difference is the formatter called.
+    """
+    if file is None:
+        file = sys.stderr
+    try:
+        file.write(PyShell.idle_formatwarning(
+                message, category, filename, lineno, line))
+    except IOError:
+        pass # the file (probably stderr) is invalid - this warning gets lost.
+
+_warnings_showwarning = None
+
+def capture_warnings(capture):
+    "Replace warning.showwarning with idle_showwarning_subproc, or reverse."
+
+    global _warnings_showwarning
+    if capture:
+        if _warnings_showwarning is None:
+            _warnings_showwarning = warnings.showwarning
+            warnings.showwarning = idle_showwarning_subproc
+    else:
+        if _warnings_showwarning is not None:
+            warnings.showwarning = _warnings_showwarning
+            _warnings_showwarning = None
+
+capture_warnings(True)
 
 # Thread shared globals: Establish a queue between a subthread (which handles
 # the socket) and the main thread (which runs user code), plus global
@@ -78,6 +92,8 @@
     except:
         print>>sys.stderr, "IDLE Subprocess: no IP port passed in sys.argv."
         return
+
+    capture_warnings(True)
     sys.argv[:] = [""]
     sockthread = threading.Thread(target=manage_socket,
                                   name='SockThread',
@@ -104,6 +120,7 @@
                 exit_now = True
             continue
         except SystemExit:
+            capture_warnings(False)
             raise
         except:
             type, value, tb = sys.exc_info()
@@ -123,7 +140,7 @@
         try:
             server = MyRPCServer(address, MyHandler)
             break
-        except socket.error, err:
+        except socket.error as err:
             print>>sys.__stderr__,"IDLE Subprocess: socket error: "\
                                         + err.args[1] + ", retrying...."
     else:
@@ -219,6 +236,7 @@
             del sys.exitfunc
         except AttributeError:
             pass
+    capture_warnings(False)
     sys.exit(0)
 
 class MyRPCServer(rpc.RPCServer):
@@ -352,3 +370,5 @@
         sys.last_value = val
         item = StackViewer.StackTreeItem(flist, tb)
         return RemoteObjectBrowser.remote_object_tree_item(item)
+
+capture_warnings(False)  # Make sure turned off; see issue 18081
diff --git a/Lib/lib-tk/Tkinter.py b/Lib/lib-tk/Tkinter.py
index fcb320f..6947bcc 100644
--- a/Lib/lib-tk/Tkinter.py
+++ b/Lib/lib-tk/Tkinter.py
@@ -1736,7 +1736,7 @@
         # ensure that self.tk is always _something_.
         self.tk = None
         if baseName is None:
-            import sys, os
+            import os
             baseName = os.path.basename(sys.argv[0])
             baseName, ext = os.path.splitext(baseName)
             if ext not in ('.py', '.pyc', '.pyo'):
diff --git a/Lib/lib-tk/test/test_ttk/test_widgets.py b/Lib/lib-tk/test/test_ttk/test_widgets.py
index 9d06a75..2fcb3fa 100644
--- a/Lib/lib-tk/test/test_ttk/test_widgets.py
+++ b/Lib/lib-tk/test/test_ttk/test_widgets.py
@@ -26,8 +26,8 @@
     def test_identify(self):
         self.widget.update_idletasks()
         self.assertEqual(self.widget.identify(
-            int(self.widget.winfo_width() / 2),
-            int(self.widget.winfo_height() / 2)
+            self.widget.winfo_width() // 2,
+            self.widget.winfo_height() // 2
             ), "label")
         self.assertEqual(self.widget.identify(-1, -1), "")
 
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
index 57bf811..1a29c36 100644
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -270,7 +270,14 @@
             self._unlink = None
 
     def accept(self):
-        s, self._last_accepted = self._socket.accept()
+        while True:
+            try:
+                s, self._last_accepted = self._socket.accept()
+            except socket.error as e:
+                if e.args[0] != errno.EINTR:
+                    raise
+            else:
+                break
         s.setblocking(True)
         fd = duplicate(s.fileno())
         conn = _multiprocessing.Connection(fd)
@@ -287,15 +294,16 @@
     '''
     Return a connection object connected to the socket given by `address`
     '''
-    family = address_type(address)
-    s = socket.socket( getattr(socket, family) )
-    s.setblocking(True)
+    family = getattr(socket, address_type(address))
     t = _init_timeout()
 
     while 1:
+        s = socket.socket(family)
+        s.setblocking(True)
         try:
             s.connect(address)
         except socket.error, e:
+            s.close()
             if e.args[0] != errno.ECONNREFUSED or _check_timeout(t):
                 debug('failed to connect to address %s', address)
                 raise
diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py
index ffe5812..08d35d8 100644
--- a/Lib/multiprocessing/managers.py
+++ b/Lib/multiprocessing/managers.py
@@ -763,6 +763,7 @@
         elif kind == '#PROXY':
             exposed, token = result
             proxytype = self._manager._registry[token.typeid][-1]
+            token.address = self._token.address
             proxy = proxytype(
                 token, self._serializer, manager=self._manager,
                 authkey=self._authkey, exposed=exposed
diff --git a/Lib/plat-mac/EasyDialogs.py b/Lib/plat-mac/EasyDialogs.py
index 129cf2c..1d3edb3 100644
--- a/Lib/plat-mac/EasyDialogs.py
+++ b/Lib/plat-mac/EasyDialogs.py
@@ -243,8 +243,15 @@
 
 
 
+# The deprecated Carbon QuickDraw APIs are no longer available as of
+# OS X 10.8.  Raise an ImportError here in that case so that callers
+# of EasyDialogs, like BuildApplet, will do the right thing.
 
-screenbounds = Qd.GetQDGlobalsScreenBits().bounds
+try:
+    screenbounds = Qd.GetQDGlobalsScreenBits().bounds
+except AttributeError:
+    raise ImportError("QuickDraw APIs not available")
+
 screenbounds = screenbounds[0]+4, screenbounds[1]+4, \
     screenbounds[2]-4, screenbounds[3]-4
 
diff --git a/Lib/robotparser.py b/Lib/robotparser.py
index 1722863..ad3be94 100644
--- a/Lib/robotparser.py
+++ b/Lib/robotparser.py
@@ -160,6 +160,7 @@
         if path == '' and not allowance:
             # an empty value means allow all
             allowance = True
+        path = urlparse.urlunparse(urlparse.urlparse(path))
         self.path = urllib.quote(path)
         self.allowance = allowance
 
diff --git a/Lib/smtplib.py b/Lib/smtplib.py
index a3213b3..7f07840 100755
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -238,10 +238,11 @@
         If specified, `host' is the name of the remote host to which to
         connect.  If specified, `port' specifies the port to which to connect.
         By default, smtplib.SMTP_PORT is used.  If a host is specified the
-        connect method is called, and if it returns anything other than
-        a success code an SMTPConnectError is raised.  If specified,
-        `local_hostname` is used as the FQDN of the local host.  By default,
-        the local hostname is found using socket.getfqdn().
+        connect method is called, and if it returns anything other than a
+        success code an SMTPConnectError is raised.  If specified,
+        `local_hostname` is used as the FQDN of the local host for the
+        HELO/EHLO command.  Otherwise, the local hostname is found using
+        socket.getfqdn().
 
         """
         self.timeout = timeout
@@ -759,12 +760,15 @@
 if _have_ssl:
 
     class SMTP_SSL(SMTP):
-        """ This is a subclass derived from SMTP that connects over an SSL encrypted
-        socket (to use this class you need a socket module that was compiled with SSL
-        support). If host is not specified, '' (the local host) is used. If port is
-        omitted, the standard SMTP-over-SSL port (465) is used. keyfile and certfile
-        are also optional - they can contain a PEM formatted private key and
-        certificate chain file for the SSL connection.
+        """ This is a subclass derived from SMTP that connects over an SSL
+        encrypted socket (to use this class you need a socket module that was
+        compiled with SSL support). If host is not specified, '' (the local
+        host) is used. If port is omitted, the standard SMTP-over-SSL port
+        (465) is used.  local_hostname has the same meaning as it does in the
+        SMTP class.  keyfile and certfile are also optional - they can contain
+        a PEM formatted private key and certificate chain file for the SSL
+        connection.
+
         """
 
         default_port = SMTP_SSL_PORT
@@ -795,9 +799,10 @@
     """LMTP - Local Mail Transfer Protocol
 
     The LMTP protocol, which is very similar to ESMTP, is heavily based
-    on the standard SMTP client. It's common to use Unix sockets for LMTP,
-    so our connect() method must support that as well as a regular
-    host:port server. To specify a Unix socket, you must use an absolute
+    on the standard SMTP client. It's common to use Unix sockets for
+    LMTP, so our connect() method must support that as well as a regular
+    host:port server.  local_hostname has the same meaning as it does in
+    the SMTP class.  To specify a Unix socket, you must use an absolute
     path as the host, starting with a '/'.
 
     Authentication is supported, using the regular SMTP mechanism. When
diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py
index 7cda2b6..425a1f8 100644
--- a/Lib/sre_compile.py
+++ b/Lib/sre_compile.py
@@ -355,8 +355,8 @@
 def _simple(av):
     # check if av is a "simple" operator
     lo, hi = av[2].getwidth()
-    if lo == 0 and hi == MAXREPEAT:
-        raise error, "nothing to repeat"
+    #if lo == 0 and hi == MAXREPEAT:
+    #    raise error, "nothing to repeat"
     return lo == hi == 1 and av[2][0][0] != SUBPATTERN
 
 def _compile_info(code, pattern, flags):
diff --git a/Lib/subprocess.py b/Lib/subprocess.py
index 309f9a3..7fd1b02 100644
--- a/Lib/subprocess.py
+++ b/Lib/subprocess.py
@@ -2,8 +2,6 @@
 #
 # For more information about this module, see PEP 324.
 #
-# This module should remain compatible with Python 2.2, see PEP 291.
-#
 # Copyright (c) 2003-2005 by Peter Astrand <astrand@lysator.liu.se>
 #
 # Licensed to PSF under a Contributor Agreement.
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index f2ddbb0..b5dce97 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -612,7 +612,7 @@
         return rv
 
     def xreadlines(self, *args):
-        try:
-            return self._file.xreadlines(*args)
-        except AttributeError:
+        if hasattr(self._file, 'xreadlines'):  # real file
+            return iter(self._file)
+        else:  # StringIO()
             return iter(self._file.readlines(*args))
diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py
index f6abe97..7fdd482 100644
--- a/Lib/test/test_cgi.py
+++ b/Lib/test/test_cgi.py
@@ -266,6 +266,29 @@
                 got = getattr(fs.list[x], k)
                 self.assertEqual(got, exp)
 
+    def test_fieldstorage_multipart_maxline(self):
+        # Issue #18167
+        maxline = 1 << 16
+        self.maxDiff = None
+        def check(content):
+            data = """
+---123
+Content-Disposition: form-data; name="upload"; filename="fake.txt"
+Content-Type: text/plain
+
+%s
+---123--
+""".replace('\n', '\r\n') % content
+            environ = {
+                'CONTENT_LENGTH':   str(len(data)),
+                'CONTENT_TYPE':     'multipart/form-data; boundary=-123',
+                'REQUEST_METHOD':   'POST',
+            }
+            self.assertEqual(gen_result(data, environ), {'upload': content})
+        check('x' * (maxline - 1))
+        check('x' * (maxline - 1) + '\r')
+        check('x' * (maxline - 1) + '\r' + 'y' * (maxline - 1))
+
     _qs_result = {
         'key1': 'value1',
         'key2': ['value2x', 'value2y'],
diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py
index 61c2df2..ecaf997 100644
--- a/Lib/test/test_codeccallbacks.py
+++ b/Lib/test/test_codeccallbacks.py
@@ -66,15 +66,34 @@
         # replace unencodable characters which numeric character entities.
         # For ascii, latin-1 and charmaps this is completely implemented
         # in C and should be reasonably fast.
-        s = u"\u30b9\u30d1\u30e2 \xe4nd eggs"
+        s = u"\u30b9\u30d1\u30e2 \xe4nd egg\u0161"
         self.assertEqual(
             s.encode("ascii", "xmlcharrefreplace"),
-            "&#12473;&#12497;&#12514; &#228;nd eggs"
+            "&#12473;&#12497;&#12514; &#228;nd egg&#353;"
         )
         self.assertEqual(
             s.encode("latin-1", "xmlcharrefreplace"),
-            "&#12473;&#12497;&#12514; \xe4nd eggs"
+            "&#12473;&#12497;&#12514; \xe4nd egg&#353;"
         )
+        self.assertEqual(
+            s.encode("iso-8859-15", "xmlcharrefreplace"),
+            "&#12473;&#12497;&#12514; \xe4nd egg\xa8"
+        )
+
+    def test_xmlcharrefreplace_with_surrogates(self):
+        tests = [(u'\U0001f49d', '&#128157;'),
+                 (u'\ud83d', '&#55357;'),
+                 (u'\udc9d', '&#56477;'),
+                 (u'\ud83d\udc9d', '&#128157;' if len(u'\U0001f49d') > 1 else
+                                   '&#55357;&#56477;'),
+                ]
+        for encoding in ['ascii', 'latin1', 'iso-8859-15']:
+            for s, exp in tests:
+                self.assertEqual(s.encode(encoding, 'xmlcharrefreplace'),
+                                 exp, msg='%r.encode(%r)' % (s, encoding))
+                self.assertEqual((s+'X').encode(encoding, 'xmlcharrefreplace'),
+                                 exp+'X',
+                                 msg='%r.encode(%r)' % (s + 'X', encoding))
 
     def test_xmlcharnamereplace(self):
         # This time use a named character entity for unencodable
diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py
index 8bdeb3d..3aaecb2 100644
--- a/Lib/test/test_collections.py
+++ b/Lib/test/test_collections.py
@@ -17,6 +17,43 @@
 
 TestNT = namedtuple('TestNT', 'x y z')    # type used for pickle tests
 
+py273_named_tuple_pickle = '''\
+ccopy_reg
+_reconstructor
+p0
+(ctest.test_collections
+TestNT
+p1
+c__builtin__
+tuple
+p2
+(I10
+I20
+I30
+tp3
+tp4
+Rp5
+ccollections
+OrderedDict
+p6
+((lp7
+(lp8
+S'x'
+p9
+aI10
+aa(lp10
+S'y'
+p11
+aI20
+aa(lp12
+S'z'
+p13
+aI30
+aatp14
+Rp15
+b.
+'''
+
 class TestNamedTuple(unittest.TestCase):
 
     def test_factory(self):
@@ -78,12 +115,12 @@
         self.assertRaises(TypeError, eval, 'Point(XXX=1, y=2)', locals())   # wrong keyword argument
         self.assertRaises(TypeError, eval, 'Point(x=1)', locals())          # missing keyword argument
         self.assertEqual(repr(p), 'Point(x=11, y=22)')
-        self.assertNotIn('__dict__', dir(p))                              # verify instance has no dict
         self.assertNotIn('__weakref__', dir(p))
         self.assertEqual(p, Point._make([11, 22]))                          # test _make classmethod
         self.assertEqual(p._fields, ('x', 'y'))                             # test _fields attribute
         self.assertEqual(p._replace(x=1), (1, 22))                          # test _replace method
         self.assertEqual(p._asdict(), dict(x=11, y=22))                     # test _asdict method
+        self.assertEqual(vars(p), p._asdict())                              # verify that vars() works
 
         try:
             p._replace(x=1, error=2)
@@ -215,6 +252,11 @@
         # test __getnewargs__
         self.assertEqual(t.__getnewargs__(), values)
 
+    def test_pickling_bug_18015(self):
+        # http://bugs.python.org/issue18015
+        pt = pickle.loads(py273_named_tuple_pickle)
+        self.assertEqual(pt.x, 10)
+
 class ABCTestCase(unittest.TestCase):
 
     def validate_abstract_methods(self, abc, *names):
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 53ca5ab..3f82665 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -914,7 +914,7 @@
 'Tommy''s Place':'Blue Island':'IL':'12/28/02':'Blue Sunday/White Crow'
 'Stonecutters ''Seafood'' and Chop House':'Lemont':'IL':'12/19/02':'Week Back'
 """
-    header = '''\
+    header1 = '''\
 "venue","city","state","date","performers"
 '''
     sample3 = '''\
@@ -933,10 +933,35 @@
     sample6 = "a|b|c\r\nd|e|f\r\n"
     sample7 = "'a'|'b'|'c'\r\n'd'|e|f\r\n"
 
+# Issue 18155: Use a delimiter that is a special char to regex:
+
+    header2 = '''\
+"venue"+"city"+"state"+"date"+"performers"
+'''
+    sample8 = """\
+Harry's+ Arlington Heights+ IL+ 2/1/03+ Kimi Hayes
+Shark City+ Glendale Heights+ IL+ 12/28/02+ Prezence
+Tommy's Place+ Blue Island+ IL+ 12/28/02+ Blue Sunday/White Crow
+Stonecutters Seafood and Chop House+ Lemont+ IL+ 12/19/02+ Week Back
+"""
+    sample9 = """\
+'Harry''s'+ Arlington Heights'+ 'IL'+ '2/1/03'+ 'Kimi Hayes'
+'Shark City'+ Glendale Heights'+' IL'+ '12/28/02'+ 'Prezence'
+'Tommy''s Place'+ Blue Island'+ 'IL'+ '12/28/02'+ 'Blue Sunday/White Crow'
+'Stonecutters ''Seafood'' and Chop House'+ 'Lemont'+ 'IL'+ '12/19/02'+ 'Week Back'
+"""
+
     def test_has_header(self):
         sniffer = csv.Sniffer()
         self.assertEqual(sniffer.has_header(self.sample1), False)
-        self.assertEqual(sniffer.has_header(self.header+self.sample1), True)
+        self.assertEqual(sniffer.has_header(self.header1 + self.sample1),
+                         True)
+
+    def test_has_header_regex_special_delimiter(self):
+        sniffer = csv.Sniffer()
+        self.assertEqual(sniffer.has_header(self.sample8), False)
+        self.assertEqual(sniffer.has_header(self.header2 + self.sample8),
+                         True)
 
     def test_sniff(self):
         sniffer = csv.Sniffer()
@@ -970,13 +995,24 @@
         dialect = sniffer.sniff(self.sample7)
         self.assertEqual(dialect.delimiter, "|")
         self.assertEqual(dialect.quotechar, "'")
+        dialect = sniffer.sniff(self.sample8)
+        self.assertEqual(dialect.delimiter, '+')
+        dialect = sniffer.sniff(self.sample9)
+        self.assertEqual(dialect.delimiter, '+')
+        self.assertEqual(dialect.quotechar, "'")
 
     def test_doublequote(self):
         sniffer = csv.Sniffer()
-        dialect = sniffer.sniff(self.header)
+        dialect = sniffer.sniff(self.header1)
+        self.assertFalse(dialect.doublequote)
+        dialect = sniffer.sniff(self.header2)
         self.assertFalse(dialect.doublequote)
         dialect = sniffer.sniff(self.sample2)
         self.assertTrue(dialect.doublequote)
+        dialect = sniffer.sniff(self.sample8)
+        self.assertFalse(dialect.doublequote)
+        dialect = sniffer.sniff(self.sample9)
+        self.assertTrue(dialect.doublequote)
 
 if not hasattr(sys, "gettotalrefcount"):
     if test_support.verbose: print "*** skipping leakage tests ***"
diff --git a/Lib/test/test_curses.py b/Lib/test/test_curses.py
index fa0d469..fcf9618 100644
--- a/Lib/test/test_curses.py
+++ b/Lib/test/test_curses.py
@@ -250,6 +250,26 @@
     except curses.panel.error:
         pass
 
+def test_userptr_memory_leak(stdscr):
+    w = curses.newwin(10, 10)
+    p = curses.panel.new_panel(w)
+    obj = object()
+    nrefs = sys.getrefcount(obj)
+    for i in range(100):
+        p.set_userptr(obj)
+
+    p.set_userptr(None)
+    if sys.getrefcount(obj) != nrefs:
+        raise RuntimeError, "set_userptr leaked references"
+
+def test_userptr_segfault(stdscr):
+    panel = curses.panel.new_panel(stdscr)
+    class A:
+        def __del__(self):
+            panel.set_userptr(None)
+    panel.set_userptr(A())
+    panel.set_userptr(None)
+
 def test_resize_term(stdscr):
     if hasattr(curses, 'resizeterm'):
         lines, cols = curses.LINES, curses.COLS
@@ -268,6 +288,8 @@
         module_funcs(stdscr)
         window_funcs(stdscr)
         test_userptr_without_set(stdscr)
+        test_userptr_memory_leak(stdscr)
+        test_userptr_segfault(stdscr)
         test_resize_term(stdscr)
         test_issue6243(stdscr)
     finally:
diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py
index b264ff2..30cfb93 100644
--- a/Lib/test/test_dictviews.py
+++ b/Lib/test/test_dictviews.py
@@ -112,6 +112,13 @@
         self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()),
                          {'a', 'b', 'd', 'e'})
 
+        self.assertEqual(d1.viewkeys() - d1.viewkeys(), set())
+        self.assertEqual(d1.viewkeys() - d2.viewkeys(), {'a'})
+        self.assertEqual(d1.viewkeys() - d3.viewkeys(), {'a', 'b'})
+        self.assertEqual(d1.viewkeys() - set(d1.viewkeys()), set())
+        self.assertEqual(d1.viewkeys() - set(d2.viewkeys()), {'a'})
+        self.assertEqual(d1.viewkeys() - set(d3.viewkeys()), {'a', 'b'})
+
     def test_items_set_operations(self):
         d1 = {'a': 1, 'b': 2}
         d2 = {'a': 2, 'b': 2}
@@ -144,6 +151,14 @@
         self.assertEqual(d1.viewitems() ^ d3.viewitems(),
                          {('a', 1), ('b', 2), ('d', 4), ('e', 5)})
 
+        self.assertEqual(d1.viewitems() - d1.viewitems(), set())
+        self.assertEqual(d1.viewitems() - d2.viewitems(), {('a', 1)})
+        self.assertEqual(d1.viewitems() - d3.viewitems(), {('a', 1), ('b', 2)})
+        self.assertEqual(d1.viewitems() - set(d1.viewitems()), set())
+        self.assertEqual(d1.viewitems() - set(d2.viewitems()), {('a', 1)})
+        self.assertEqual(d1.viewitems() - set(d3.viewitems()),
+                         {('a', 1), ('b', 2)})
+
     def test_recursive_repr(self):
         d = {}
         d[42] = d.viewvalues()
diff --git a/Lib/test/test_file.py b/Lib/test/test_file.py
index dffa4b5..ac2aafa 100644
--- a/Lib/test/test_file.py
+++ b/Lib/test/test_file.py
@@ -154,16 +154,6 @@
                 f.close()
                 self.fail('%r is an invalid file mode' % mode)
 
-    def testStdin(self):
-        # This causes the interpreter to exit on OSF1 v5.1.
-        if sys.platform != 'osf1V5':
-            self.assertRaises((IOError, ValueError), sys.stdin.seek, -1)
-        else:
-            print((
-                '  Skipping sys.stdin.seek(-1), it may crash the interpreter.'
-                ' Test manually.'), file=sys.__stdout__)
-        self.assertRaises((IOError, ValueError), sys.stdin.truncate)
-
     def testBadModeArgument(self):
         # verify that we get a sensible error message for bad mode argument
         bad_mode = "qwerty"
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index dd30efa..0ad8b02 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -302,6 +302,23 @@
 def test_main():
     test_support.run_unittest(FormatTest)
 
+    def test_precision(self):
+        INT_MAX = 2147483647
+
+        f = 1.2
+        self.assertEqual(format(f, ".0f"), "1")
+        self.assertEqual(format(f, ".3f"), "1.200")
+        with self.assertRaises(ValueError) as cm:
+            format(f, ".%sf" % (INT_MAX + 1))
+        self.assertEqual(str(cm.exception), "precision too big")
+
+        c = complex(f)
+        self.assertEqual(format(f, ".0f"), "1")
+        self.assertEqual(format(f, ".3f"), "1.200")
+        with self.assertRaises(ValueError) as cm:
+            format(f, ".%sf" % (INT_MAX + 1))
+        self.assertEqual(str(cm.exception), "precision too big")
+
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Lib/test/test_idle.py b/Lib/test/test_idle.py
new file mode 100644
index 0000000..8552e6f
--- /dev/null
+++ b/Lib/test/test_idle.py
@@ -0,0 +1,31 @@
+import unittest
+from test import test_support as support
+from test.test_support import import_module, use_resources
+
+# Skip test if _thread or _tkinter wasn't built or idlelib was deleted.
+import_module('threading')  # imported by idlelib.PyShell, imports _thread
+tk = import_module('Tkinter')  # imports _tkinter
+idletest = import_module('idlelib.idle_test')
+
+# If buildbot improperly sets gui resource (#18365, #18441), remove it
+# so requires('gui') tests are skipped while non-gui tests still run.
+# If there is a problem with Macs, see #18441, msg 193805
+if use_resources and 'gui' in use_resources:
+    try:
+        root = tk.Tk()
+        root.destroy()
+    except tk.TclError:
+        while 'gui' in use_resources:
+            use_resources.remove('gui')
+
+# Without test_main present, regrtest.runtest_inner (line1219) calls
+# unittest.TestLoader().loadTestsFromModule(this_module) which calls
+# load_tests() if it finds it. (Unittest.main does the same.)
+load_tests = idletest.load_tests
+
+if __name__ == '__main__':
+    # Until unittest supports resources, we emulate regrtest's -ugui
+    # so loaded tests run the same as if textually present here.
+    # If any Idle test ever needs another resource, add it to the list.
+    support.use_resources = ['gui']  # use_resources is initially None
+    unittest.main(verbosity=2, exit=False)
diff --git a/Lib/test/test_kqueue.py b/Lib/test/test_kqueue.py
index 2f8ff2f..3dffc73 100644
--- a/Lib/test/test_kqueue.py
+++ b/Lib/test/test_kqueue.py
@@ -70,13 +70,13 @@
         self.assertEqual(ev, ev)
         self.assertNotEqual(ev, other)
 
-        bignum = sys.maxsize * 2 + 1
-        ev = select.kevent(bignum, 1, 2, 3, sys.maxsize, bignum)
+        bignum = 0x7fff
+        ev = select.kevent(bignum, 1, 2, 3, bignum - 1, bignum)
         self.assertEqual(ev.ident, bignum)
         self.assertEqual(ev.filter, 1)
         self.assertEqual(ev.flags, 2)
         self.assertEqual(ev.fflags, 3)
-        self.assertEqual(ev.data, sys.maxsize)
+        self.assertEqual(ev.data, bignum - 1)
         self.assertEqual(ev.udata, bignum)
         self.assertEqual(ev, ev)
         self.assertNotEqual(ev, other)
diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py
index 88663ef..5ffe759 100644
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -2461,12 +2461,80 @@
         self.assertLessEqual(new_size, old_size)
 
 #
+# Issue #17097: EINTR should be ignored by recv(), send(), accept() etc
+#
+
+class TestIgnoreEINTR(unittest.TestCase):
+
+    @classmethod
+    def _test_ignore(cls, conn):
+        def handler(signum, frame):
+            pass
+        signal.signal(signal.SIGUSR1, handler)
+        conn.send('ready')
+        x = conn.recv()
+        conn.send(x)
+        conn.send_bytes(b'x'*(1024*1024))   # sending 1 MB should block
+
+    @unittest.skipUnless(hasattr(signal, 'SIGUSR1'), 'requires SIGUSR1')
+    def test_ignore(self):
+        conn, child_conn = multiprocessing.Pipe()
+        try:
+            p = multiprocessing.Process(target=self._test_ignore,
+                                        args=(child_conn,))
+            p.daemon = True
+            p.start()
+            child_conn.close()
+            self.assertEqual(conn.recv(), 'ready')
+            time.sleep(0.1)
+            os.kill(p.pid, signal.SIGUSR1)
+            time.sleep(0.1)
+            conn.send(1234)
+            self.assertEqual(conn.recv(), 1234)
+            time.sleep(0.1)
+            os.kill(p.pid, signal.SIGUSR1)
+            self.assertEqual(conn.recv_bytes(), b'x'*(1024*1024))
+            time.sleep(0.1)
+            p.join()
+        finally:
+            conn.close()
+
+    @classmethod
+    def _test_ignore_listener(cls, conn):
+        def handler(signum, frame):
+            pass
+        signal.signal(signal.SIGUSR1, handler)
+        l = multiprocessing.connection.Listener()
+        conn.send(l.address)
+        a = l.accept()
+        a.send('welcome')
+
+    @unittest.skipUnless(hasattr(signal, 'SIGUSR1'), 'requires SIGUSR1')
+    def test_ignore_listener(self):
+        conn, child_conn = multiprocessing.Pipe()
+        try:
+            p = multiprocessing.Process(target=self._test_ignore_listener,
+                                        args=(child_conn,))
+            p.daemon = True
+            p.start()
+            child_conn.close()
+            address = conn.recv()
+            time.sleep(0.1)
+            os.kill(p.pid, signal.SIGUSR1)
+            time.sleep(0.1)
+            client = multiprocessing.connection.Client(address)
+            self.assertEqual(client.recv(), 'welcome')
+            p.join()
+        finally:
+            conn.close()
+
+#
 #
 #
 
 testcases_other = [OtherTest, TestInvalidHandle, TestInitializers,
                    TestStdinBadfiledescriptor, TestTimeouts, TestNoForkBomb,
-                   TestFlags, TestForkAwareThreadLock]
+                   TestFlags, TestForkAwareThreadLock, TestIgnoreEINTR]
 
 #
 #
diff --git a/Lib/test/test_openpty.py b/Lib/test/test_openpty.py
index 20c4fe2..4b34b3a 100644
--- a/Lib/test/test_openpty.py
+++ b/Lib/test/test_openpty.py
@@ -10,6 +10,8 @@
 class OpenptyTest(unittest.TestCase):
     def test(self):
         master, slave = os.openpty()
+        self.addCleanup(os.close, master)
+        self.addCleanup(os.close, slave)
         if not os.isatty(slave):
             self.fail("Slave-end of pty is not a terminal.")
 
diff --git a/Lib/test/test_pep263.py b/Lib/test/test_pep263.py
index 9286467..4b60624 100644
--- a/Lib/test/test_pep263.py
+++ b/Lib/test/test_pep263.py
@@ -41,6 +41,24 @@
         # two bytes in common with the UTF-8 BOM
         self.assertRaises(SyntaxError, eval, '\xef\xbb\x20')
 
+    def test_error_message(self):
+        compile('# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
+        compile('\xef\xbb\xbf\n', 'dummy', 'exec')
+        compile('\xef\xbb\xbf# -*- coding: utf-8 -*-\n', 'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'fake'):
+            compile('# -*- coding: fake -*-\n', 'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'iso-8859-15'):
+            compile('\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
+                    'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'BOM'):
+            compile('\xef\xbb\xbf# -*- coding: iso-8859-15 -*-\n',
+                    'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'fake'):
+            compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
+        with self.assertRaisesRegexp(SyntaxError, 'BOM'):
+            compile('\xef\xbb\xbf# -*- coding: fake -*-\n', 'dummy', 'exec')
+
+
 def test_main():
     test_support.run_unittest(PEP263Test)
 
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 18a81a2..2be5f5c 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -897,6 +897,16 @@
         with self.assertRaisesRegexp(sre_constants.error, '\?foo'):
             re.compile('(?P<?foo>)')
 
+    def test_issue17998(self):
+        for reps in '*', '+', '?', '{1}':
+            for mod in '', '?':
+                pattern = '.' + reps + mod + 'yz'
+                self.assertEqual(re.compile(pattern, re.S).findall('xyz'),
+                                 ['xyz'], msg=pattern)
+                pattern = pattern.encode()
+                self.assertEqual(re.compile(pattern, re.S).findall(b'xyz'),
+                                 [b'xyz'], msg=pattern)
+
 
 def run_re_tests():
     from test.re_tests import tests, SUCCEED, FAIL, SYNTAX_ERROR
diff --git a/Lib/test/test_robotparser.py b/Lib/test/test_robotparser.py
index b3d4a46..651301b 100644
--- a/Lib/test/test_robotparser.py
+++ b/Lib/test/test_robotparser.py
@@ -228,6 +228,18 @@
 
 RobotTest(15, doc, good, bad)
 
+# 16. Empty query (issue #17403). Normalizing the url first.
+doc = """
+User-agent: *
+Allow: /some/path?
+Disallow: /another/path?
+"""
+
+good = ['/some/path?']
+bad = ['/another/path?']
+
+RobotTest(16, doc, good, bad)
+
 
 class NetworkTestCase(unittest.TestCase):
 
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 111e553..6e72180 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -665,6 +665,9 @@
         socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
                            socket.AI_PASSIVE)
 
+        # Issue 17269
+        if hasattr(socket, 'AI_NUMERICSERV'):
+            socket.getaddrinfo("localhost", None, 0, 0, 0, socket.AI_NUMERICSERV)
 
     def check_sendall_interrupted(self, with_timeout):
         # socketpair() is not stricly required, but it makes things easier.
diff --git a/Lib/test/test_stat.py b/Lib/test/test_stat.py
new file mode 100644
index 0000000..a71f599
--- /dev/null
+++ b/Lib/test/test_stat.py
@@ -0,0 +1,175 @@
+import unittest
+import os
+from test.test_support import TESTFN, run_unittest
+import stat
+
+class TestFilemode(unittest.TestCase):
+    file_flags = {'SF_APPEND', 'SF_ARCHIVED', 'SF_IMMUTABLE', 'SF_NOUNLINK',
+                  'SF_SNAPSHOT', 'UF_APPEND', 'UF_COMPRESSED', 'UF_HIDDEN',
+                  'UF_IMMUTABLE', 'UF_NODUMP', 'UF_NOUNLINK', 'UF_OPAQUE'}
+
+    formats = {'S_IFBLK', 'S_IFCHR', 'S_IFDIR', 'S_IFIFO', 'S_IFLNK',
+               'S_IFREG', 'S_IFSOCK'}
+
+    format_funcs = {'S_ISBLK', 'S_ISCHR', 'S_ISDIR', 'S_ISFIFO', 'S_ISLNK',
+                    'S_ISREG', 'S_ISSOCK'}
+
+    stat_struct = {
+        'ST_MODE': 0,
+        'ST_INO': 1,
+        'ST_DEV': 2,
+        'ST_NLINK': 3,
+        'ST_UID': 4,
+        'ST_GID': 5,
+        'ST_SIZE': 6,
+        'ST_ATIME': 7,
+        'ST_MTIME': 8,
+        'ST_CTIME': 9}
+
+    # permission bit value are defined by POSIX
+    permission_bits = {
+        'S_ISUID': 0o4000,
+        'S_ISGID': 0o2000,
+        'S_ENFMT': 0o2000,
+        'S_ISVTX': 0o1000,
+        'S_IRWXU': 0o700,
+        'S_IRUSR': 0o400,
+        'S_IREAD': 0o400,
+        'S_IWUSR': 0o200,
+        'S_IWRITE': 0o200,
+        'S_IXUSR': 0o100,
+        'S_IEXEC': 0o100,
+        'S_IRWXG': 0o070,
+        'S_IRGRP': 0o040,
+        'S_IWGRP': 0o020,
+        'S_IXGRP': 0o010,
+        'S_IRWXO': 0o007,
+        'S_IROTH': 0o004,
+        'S_IWOTH': 0o002,
+        'S_IXOTH': 0o001}
+
+    def setUp(self):
+        try:
+            os.remove(TESTFN)
+        except OSError:
+            try:
+                os.rmdir(TESTFN)
+            except OSError:
+                pass
+    tearDown = setUp
+
+    def get_mode(self, fname=TESTFN, lstat=True):
+        if lstat:
+            st_mode = os.lstat(fname).st_mode
+        else:
+            st_mode = os.stat(fname).st_mode
+        return st_mode
+
+    def assertS_IS(self, name, mode):
+        # test format, lstrip is for S_IFIFO
+        fmt = getattr(stat, "S_IF" + name.lstrip("F"))
+        self.assertEqual(stat.S_IFMT(mode), fmt)
+        # test that just one function returns true
+        testname = "S_IS" + name
+        for funcname in self.format_funcs:
+            func = getattr(stat, funcname, None)
+            if func is None:
+                if funcname == testname:
+                    raise ValueError(funcname)
+                continue
+            if funcname == testname:
+                self.assertTrue(func(mode))
+            else:
+                self.assertFalse(func(mode))
+
+    def test_mode(self):
+        with open(TESTFN, 'w'):
+            pass
+        if os.name == 'posix':
+            os.chmod(TESTFN, 0o700)
+            st_mode = self.get_mode()
+            self.assertS_IS("REG", st_mode)
+            self.assertEqual(stat.S_IMODE(st_mode),
+                             stat.S_IRWXU)
+
+            os.chmod(TESTFN, 0o070)
+            st_mode = self.get_mode()
+            self.assertS_IS("REG", st_mode)
+            self.assertEqual(stat.S_IMODE(st_mode),
+                             stat.S_IRWXG)
+
+            os.chmod(TESTFN, 0o007)
+            st_mode = self.get_mode()
+            self.assertS_IS("REG", st_mode)
+            self.assertEqual(stat.S_IMODE(st_mode),
+                             stat.S_IRWXO)
+
+            os.chmod(TESTFN, 0o444)
+            st_mode = self.get_mode()
+            self.assertS_IS("REG", st_mode)
+            self.assertEqual(stat.S_IMODE(st_mode), 0o444)
+        else:
+            os.chmod(TESTFN, 0o700)
+            st_mode = self.get_mode()
+            self.assertS_IS("REG", st_mode)
+            self.assertEqual(stat.S_IFMT(st_mode),
+                             stat.S_IFREG)
+
+    def test_directory(self):
+        os.mkdir(TESTFN)
+        os.chmod(TESTFN, 0o700)
+        st_mode = self.get_mode()
+        self.assertS_IS("DIR", st_mode)
+
+    @unittest.skipUnless(hasattr(os, 'symlink'), 'os.symlink not available')
+    def test_link(self):
+        try:
+            os.symlink(os.getcwd(), TESTFN)
+        except (OSError, NotImplementedError) as err:
+            raise unittest.SkipTest(str(err))
+        else:
+            st_mode = self.get_mode()
+            self.assertS_IS("LNK", st_mode)
+
+    @unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available')
+    def test_fifo(self):
+        os.mkfifo(TESTFN, 0o700)
+        st_mode = self.get_mode()
+        self.assertS_IS("FIFO", st_mode)
+
+    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
+    def test_devices(self):
+        if os.path.exists(os.devnull):
+            st_mode = self.get_mode(os.devnull, lstat=False)
+            self.assertS_IS("CHR", st_mode)
+        # Linux block devices, BSD has no block devices anymore
+        for blockdev in ("/dev/sda", "/dev/hda"):
+            if os.path.exists(blockdev):
+                st_mode = self.get_mode(blockdev, lstat=False)
+                self.assertS_IS("BLK", st_mode)
+                break
+
+    def test_module_attributes(self):
+        for key, value in self.stat_struct.items():
+            modvalue = getattr(stat, key)
+            self.assertEqual(value, modvalue, key)
+        for key, value in self.permission_bits.items():
+            modvalue = getattr(stat, key)
+            self.assertEqual(value, modvalue, key)
+        for key in self.file_flags:
+            modvalue = getattr(stat, key)
+            self.assertIsInstance(modvalue, int)
+        for key in self.formats:
+            modvalue = getattr(stat, key)
+            self.assertIsInstance(modvalue, int)
+        for key in self.format_funcs:
+            func = getattr(stat, key)
+            self.assertTrue(callable(func))
+            self.assertEqual(func(0), 0)
+
+
+def test_main():
+    run_unittest(TestFilemode)
+
+if __name__ == '__main__':
+    test_main()
diff --git a/Lib/test/test_subprocess.py b/Lib/test/test_subprocess.py
index e89d84f..f43b51c 100644
--- a/Lib/test/test_subprocess.py
+++ b/Lib/test/test_subprocess.py
@@ -806,7 +806,8 @@
                         self._testcase.assertNotIn(
                                 fd, (p2cwrite, c2pread, errread))
                 finally:
-                    map(os.close, devzero_fds)
+                    for fd in devzero_fds:
+                        os.close(fd)
 
     @unittest.skipIf(not os.path.exists("/dev/zero"), "/dev/zero required.")
     def test_preexec_errpipe_does_not_double_close_pipes(self):
diff --git a/Lib/test/test_tcl.py b/Lib/test/test_tcl.py
index a0e9e62..3da87d9 100644
--- a/Lib/test/test_tcl.py
+++ b/Lib/test/test_tcl.py
@@ -184,6 +184,66 @@
                 self.assertEqual(passValue(f), f)
         self.assertEqual(passValue((1, '2', (3.4,))), (1, '2', (3.4,)))
 
+    def test_splitlist(self):
+        splitlist = self.interp.tk.splitlist
+        call = self.interp.tk.call
+        self.assertRaises(TypeError, splitlist)
+        self.assertRaises(TypeError, splitlist, 'a', 'b')
+        self.assertRaises(TypeError, splitlist, 2)
+        testcases = [
+            ('2', ('2',)),
+            ('', ()),
+            ('{}', ('',)),
+            ('""', ('',)),
+            ('a\n b\t\r c\n ', ('a', 'b', 'c')),
+            (u'a\n b\t\r c\n ', ('a', 'b', 'c')),
+            ('a \xe2\x82\xac', ('a', '\xe2\x82\xac')),
+            (u'a \u20ac', ('a', '\xe2\x82\xac')),
+            ('a {b c}', ('a', 'b c')),
+            (r'a b\ c', ('a', 'b c')),
+            (('a', 'b c'), ('a', 'b c')),
+            ('a 2', ('a', '2')),
+            (('a', 2), ('a', 2)),
+            ('a 3.4', ('a', '3.4')),
+            (('a', 3.4), ('a', 3.4)),
+            ((), ()),
+            (call('list', 1, '2', (3.4,)), (1, '2', (3.4,))),
+        ]
+        for arg, res in testcases:
+            self.assertEqual(splitlist(arg), res)
+        self.assertRaises(TclError, splitlist, '{')
+
+    def test_split(self):
+        split = self.interp.tk.split
+        call = self.interp.tk.call
+        self.assertRaises(TypeError, split)
+        self.assertRaises(TypeError, split, 'a', 'b')
+        self.assertRaises(TypeError, split, 2)
+        testcases = [
+            ('2', '2'),
+            ('', ''),
+            ('{}', ''),
+            ('""', ''),
+            ('{', '{'),
+            ('a\n b\t\r c\n ', ('a', 'b', 'c')),
+            (u'a\n b\t\r c\n ', ('a', 'b', 'c')),
+            ('a \xe2\x82\xac', ('a', '\xe2\x82\xac')),
+            (u'a \u20ac', ('a', '\xe2\x82\xac')),
+            ('a {b c}', ('a', ('b', 'c'))),
+            (r'a b\ c', ('a', ('b', 'c'))),
+            (('a', 'b c'), ('a', ('b', 'c'))),
+            (('a', u'b c'), ('a', ('b', 'c'))),
+            ('a 2', ('a', '2')),
+            (('a', 2), ('a', 2)),
+            ('a 3.4', ('a', '3.4')),
+            (('a', 3.4), ('a', 3.4)),
+            (('a', (2, 3.4)), ('a', (2, 3.4))),
+            ((), ()),
+            (call('list', 1, '2', (3.4,)), (1, '2', (3.4,))),
+        ]
+        for arg, res in testcases:
+            self.assertEqual(split(arg), res)
+
 
 def test_main():
     test_support.run_unittest(TclTest, TkinterTest)
diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
index e44fe03..666cab8 100644
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -1658,6 +1658,18 @@
         self.assertEqual(unicode_encodedecimal(u"123\u20ac\u0660", "replace"),
                          b'123?0')
 
+    def test_encode_decimal_with_surrogates(self):
+        from _testcapi import unicode_encodedecimal
+        tests = [(u'\U0001f49d', '&#128157;'),
+                 (u'\ud83d', '&#55357;'),
+                 (u'\udc9d', '&#56477;'),
+                 (u'\ud83d\udc9d', '&#128157;' if len(u'\U0001f49d') > 1 else
+                                  '&#55357;&#56477;'),
+                ]
+        for s, exp in tests:
+            self.assertEqual(
+                    unicode_encodedecimal(u"123" + s, "xmlcharrefreplace"),
+                    '123' + exp)
 
 def test_main():
     test_support.run_unittest(__name__)
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 3a273f8..e60adb2 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -227,13 +227,13 @@
                 'file://localhost/a/missing/file.py')
         fd, tmp_file = tempfile.mkstemp()
         tmp_fileurl = 'file://localhost/' + tmp_file.replace(os.path.sep, '/')
+        self.assertTrue(os.path.exists(tmp_file))
         try:
-            self.assertTrue(os.path.exists(tmp_file))
             fp = urllib.urlopen(tmp_fileurl)
+            fp.close()
         finally:
             os.close(fd)
-            fp.close()
-        os.unlink(tmp_file)
+            os.unlink(tmp_file)
 
         self.assertFalse(os.path.exists(tmp_file))
         self.assertRaises(IOError, urllib.urlopen, tmp_fileurl)
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py
index e503084..9de3d78 100644
--- a/Lib/test/test_uuid.py
+++ b/Lib/test/test_uuid.py
@@ -1,5 +1,6 @@
-from unittest import TestCase
+import unittest
 from test import test_support
+import os
 import uuid
 
 def importable(name):
@@ -9,7 +10,7 @@
     except:
         return False
 
-class TestUUID(TestCase):
+class TestUUID(unittest.TestCase):
     last_node = None
     source2node = {}
 
@@ -299,24 +300,22 @@
         else:
             TestUUID.last_node = node
 
+    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
     def test_ifconfig_getnode(self):
-        import sys
-        import os
-        if os.name == 'posix':
-            node = uuid._ifconfig_getnode()
-            if node is not None:
-                self.check_node(node, 'ifconfig')
+        node = uuid._ifconfig_getnode()
+        if node is not None:
+            self.check_node(node, 'ifconfig')
 
+    @unittest.skipUnless(os.name == 'nt', 'requires Windows')
     def test_ipconfig_getnode(self):
-        import os
-        if os.name == 'nt':
-            node = uuid._ipconfig_getnode()
-            if node is not None:
-                self.check_node(node, 'ipconfig')
+        node = uuid._ipconfig_getnode()
+        if node is not None:
+            self.check_node(node, 'ipconfig')
 
+    @unittest.skipUnless(importable('win32wnet'), 'requires win32wnet')
+    @unittest.skipUnless(importable('netbios'), 'requires netbios')
     def test_netbios_getnode(self):
-        if importable('win32wnet') and importable('netbios'):
-            self.check_node(uuid._netbios_getnode(), 'netbios')
+        self.check_node(uuid._netbios_getnode(), 'netbios')
 
     def test_random_getnode(self):
         node = uuid._random_getnode()
@@ -324,22 +323,20 @@
         self.assertTrue(node & 0x010000000000)
         self.assertTrue(node < (1L << 48))
 
+    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
+    @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
     def test_unixdll_getnode(self):
-        import sys
-        import os
-        if importable('ctypes') and os.name == 'posix':
-            try: # Issues 1481, 3581: _uuid_generate_time() might be None.
-                self.check_node(uuid._unixdll_getnode(), 'unixdll')
-            except TypeError:
-                pass
+        try: # Issues 1481, 3581: _uuid_generate_time() might be None.
+            self.check_node(uuid._unixdll_getnode(), 'unixdll')
+        except TypeError:
+            pass
 
+    @unittest.skipUnless(os.name == 'nt', 'requires Windows')
+    @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
     def test_windll_getnode(self):
-        import os
-        if importable('ctypes') and os.name == 'nt':
-            self.check_node(uuid._windll_getnode(), 'windll')
+        self.check_node(uuid._windll_getnode(), 'windll')
 
     def test_getnode(self):
-        import sys
         node1 = uuid.getnode()
         self.check_node(node1, "getnode1")
 
@@ -349,13 +346,8 @@
 
         self.assertEqual(node1, node2)
 
+    @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
     def test_uuid1(self):
-        # uuid1 requires ctypes.
-        try:
-            import ctypes
-        except ImportError:
-            return
-
         equal = self.assertEqual
 
         # Make sure uuid1() generates UUIDs that are actually version 1.
@@ -408,13 +400,8 @@
             equal(u, uuid.UUID(v))
             equal(str(u), v)
 
+    @unittest.skipUnless(importable('ctypes'), 'requires ctypes')
     def test_uuid4(self):
-        # uuid4 requires ctypes.
-        try:
-            import ctypes
-        except ImportError:
-            return
-
         equal = self.assertEqual
 
         # Make sure uuid4() generates UUIDs that are actually version 4.
@@ -446,12 +433,8 @@
             equal(u, uuid.UUID(v))
             equal(str(u), v)
 
+    @unittest.skipUnless(os.name == 'posix', 'requires Posix')
     def testIssue8621(self):
-        import os
-        import sys
-        if os.name != 'posix':
-            return
-
         # On at least some versions of OSX uuid.uuid4 generates
         # the same sequence of UUIDs in the parent and any
         # children started using fork.
@@ -465,6 +448,7 @@
 
         else:
             os.close(fds[1])
+            self.addCleanup(os.close, fds[0])
             parent_value = uuid.uuid4().hex
             os.waitpid(pid, 0)
             child_value = os.read(fds[0], 100)
diff --git a/Lib/test/test_wait4.py b/Lib/test/test_wait4.py
index d04a11b..54580a9 100644
--- a/Lib/test/test_wait4.py
+++ b/Lib/test/test_wait4.py
@@ -3,6 +3,7 @@
 
 import os
 import time
+import sys
 from test.fork_wait import ForkWait
 from test.test_support import run_unittest, reap_children, get_attribute
 
@@ -13,10 +14,15 @@
 
 class Wait4Test(ForkWait):
     def wait_impl(self, cpid):
+        option = os.WNOHANG
+        if sys.platform.startswith('aix'):
+            # Issue #11185: wait4 is broken on AIX and will always return 0
+            # with WNOHANG.
+            option = 0
         for i in range(10):
             # wait4() shouldn't hang, but some of the buildbots seem to hang
             # in the forking tests.  This is an attempt to fix the problem.
-            spid, status, rusage = os.wait4(cpid, os.WNOHANG)
+            spid, status, rusage = os.wait4(cpid, option)
             if spid == cpid:
                 break
             time.sleep(1.0)
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 4e161ca..adb7852 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -883,6 +883,12 @@
     >>> check_encoding("iso-8859-15")
     >>> check_encoding("cp437")
     >>> check_encoding("mac-roman")
+    >>> check_encoding("gbk")
+    Traceback (most recent call last):
+    ValueError: multi-byte encodings are not supported
+    >>> check_encoding("cp037")
+    Traceback (most recent call last):
+    ParseError: unknown encoding: line 1, column 30
     """
     ET.XML("<?xml version='1.0' encoding='%s'?><xml />" % encoding)
 
@@ -1769,6 +1775,16 @@
 
     """
 
+def bug_18347():
+    """
+
+    >>> e = ET.XML('<html><CamelCase>text</CamelCase></html>')
+    >>> serialize(e)
+    '<html><CamelCase>text</CamelCase></html>'
+    >>> serialize(e, method="html")
+    '<html><CamelCase>text</CamelCase></html>'
+    """
+
 # --------------------------------------------------------------------
 # reported on bugs.python.org
 
diff --git a/Lib/unittest/test/test_assertions.py b/Lib/unittest/test/test_assertions.py
index e1ba614..e8f0f64 100644
--- a/Lib/unittest/test/test_assertions.py
+++ b/Lib/unittest/test/test_assertions.py
@@ -33,6 +33,10 @@
         self.assertNotAlmostEqual(1.1, 1.0, delta=0.05)
         self.assertNotAlmostEqual(1.0, 1.1, delta=0.05)
 
+        self.assertAlmostEqual(1.0, 1.0, delta=0.5)
+        self.assertRaises(self.failureException, self.assertNotAlmostEqual,
+                          1.0, 1.0, delta=0.5)
+
         self.assertRaises(self.failureException, self.assertAlmostEqual,
                           1.1, 1.0, delta=0.05)
         self.assertRaises(self.failureException, self.assertNotAlmostEqual,
diff --git a/Lib/urllib.py b/Lib/urllib.py
index f9655f9..244cb05 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -819,7 +819,10 @@
     """Return the IP address of the current host."""
     global _thishost
     if _thishost is None:
-        _thishost = socket.gethostbyname(socket.gethostname())
+        try:
+            _thishost = socket.gethostbyname(socket.gethostname())
+        except socket.gaierror:
+            _thishost = socket.gethostbyname('localhost')
     return _thishost
 
 _ftperrors = None
@@ -870,8 +873,8 @@
         self.ftp = ftplib.FTP()
         self.ftp.connect(self.host, self.port, self.timeout)
         self.ftp.login(self.user, self.passwd)
-        for dir in self.dirs:
-            self.ftp.cwd(dir)
+        _target = '/'.join(self.dirs)
+        self.ftp.cwd(_target)
 
     def retrfile(self, file, type):
         import ftplib
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index bb468cd..9f3e75d 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -988,15 +988,15 @@
                     # FIXME: handle boolean attributes
                     write(" %s=\"%s\"" % (qnames[k], v))
             write(">")
-            tag = tag.lower()
+            ltag = tag.lower()
             if text:
-                if tag == "script" or tag == "style":
+                if ltag == "script" or ltag == "style":
                     write(_encode(text, encoding))
                 else:
                     write(_escape_cdata(text, encoding))
             for e in elem:
                 _serialize_html(write, e, encoding, qnames, None)
-            if tag not in HTML_EMPTY:
+            if ltag not in HTML_EMPTY:
                 write("</" + tag + ">")
     if elem.tail:
         write(_escape_cdata(elem.tail, encoding))
diff --git a/Mac/Makefile.in b/Mac/Makefile.in
index 6e60b2a..a3e00b2 100644
--- a/Mac/Makefile.in
+++ b/Mac/Makefile.in
@@ -202,15 +202,22 @@
 	cd IDLE && make install
 
 install_BuildApplet:
-	$(RUNSHARED) @ARCH_RUN_32BIT@ $(BUILDPYTHON) $(srcdir)/scripts/BuildApplet.py \
-		--destroot "$(DESTDIR)" \
-		--python=$(prefix)/Resources/Python.app/Contents/MacOS/Python \
-		--output "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app" \
-		$(srcdir)/scripts/BuildApplet.py
-ifneq ($(LIPO_32BIT_FLAGS),)
-	rm "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app/Contents/MacOS/Python"
-	lipo $(LIPO_32BIT_FLAGS) -output "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app/Contents/MacOS/Python" $(BUILDPYTHON)
-endif   
+	if ! $(RUNSHARED) @ARCH_RUN_32BIT@ $(BUILDPYTHON) \
+			-c 'import EasyDialogs' 2>/dev/null ; then \
+		echo "EasyDialogs not available in this Python - skipping Build Applet.app" ; \
+	else \
+		$(RUNSHARED) @ARCH_RUN_32BIT@ $(BUILDPYTHON) $(srcdir)/scripts/BuildApplet.py \
+			--destroot "$(DESTDIR)" \
+			--python=$(prefix)/Resources/Python.app/Contents/MacOS/Python \
+			--output "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app" \
+			$(srcdir)/scripts/BuildApplet.py && \
+		if [ -n "$(LIPO_32BIT_FLAGS)" ] ; then \
+			rm "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app/Contents/MacOS/Python" && \
+			lipo $(LIPO_32BIT_FLAGS) \
+				-output "$(DESTDIR)$(PYTHONAPPSDIR)/Build Applet.app/Contents/MacOS/Python" \
+				$(BUILDPYTHON) ; \
+		fi \
+	fi
 
 MACLIBDEST=$(LIBDEST)/plat-mac
 MACTOOLSDEST=$(prefix)/Mac/Tools
diff --git a/Mac/PythonLauncher/FileSettings.h b/Mac/PythonLauncher/FileSettings.h
index d807bae..7b74a9b 100755
--- a/Mac/PythonLauncher/FileSettings.h
+++ b/Mac/PythonLauncher/FileSettings.h
@@ -45,18 +45,13 @@
 + (id)getFactorySettingsForFileType: (NSString *)filetype;
 + (id)newSettingsForFileType: (NSString *)filetype;
 
-//- (id)init;
 - (id)initForFileType: (NSString *)filetype;
 - (id)initForFSDefaultFileType: (NSString *)filetype;
 - (id)initForDefaultFileType: (NSString *)filetype;
-//- (id)initWithFileSettings: (FileSettings *)source;
 
 - (void)updateFromSource: (id <FileSettingsSource>)source;
 - (NSString *)commandLineForScript: (NSString *)script;
 
-//- (void)applyFactorySettingsForFileType: (NSString *)filetype;
-//- (void)saveDefaults;
-//- (void)applyUserDefaults: (NSString *)filetype;
 - (void)applyValuesFromDict: (NSDictionary *)dict;
 - (void)reset;
 - (NSArray *) interpreters;
diff --git a/Mac/PythonLauncher/FileSettings.m b/Mac/PythonLauncher/FileSettings.m
index 66b4fdc..3438870 100755
--- a/Mac/PythonLauncher/FileSettings.m
+++ b/Mac/PythonLauncher/FileSettings.m
@@ -14,7 +14,7 @@
 {
     static FileSettings *fsdefault_py, *fsdefault_pyw, *fsdefault_pyc;
     FileSettings **curdefault;
-    
+
     if ([filetype isEqualToString: @"Python Script"]) {
         curdefault = &fsdefault_py;
     } else if ([filetype isEqualToString: @"Python GUI Script"]) {
@@ -36,7 +36,7 @@
 {
     static FileSettings *default_py, *default_pyw, *default_pyc;
     FileSettings **curdefault;
-    
+
     if ([filetype isEqualToString: @"Python Script"]) {
         curdefault = &default_py;
     } else if ([filetype isEqualToString: @"Python GUI Script"]) {
@@ -57,7 +57,7 @@
 + (id)newSettingsForFileType: (NSString *)filetype
 {
     FileSettings *cur;
-    
+
     cur = [FileSettings new];
     [cur initForFileType: filetype];
     return [cur retain];
@@ -67,7 +67,7 @@
 {
     self = [super init];
     if (!self) return self;
-    
+
     interpreter = [source->interpreter retain];
     honourhashbang = source->honourhashbang;
     debug = source->debug;
@@ -81,36 +81,30 @@
     with_terminal = source->with_terminal;
     prefskey = source->prefskey;
     if (prefskey) [prefskey retain];
-    
+
     return self;
 }
 
 - (id)initForFileType: (NSString *)filetype
 {
     FileSettings *defaults;
-    
+
     defaults = [FileSettings getDefaultsForFileType: filetype];
     self = [self initWithFileSettings: defaults];
     origsource = [defaults retain];
     return self;
 }
 
-//- (id)init
-//{
-//    self = [self initForFileType: @"Python Script"];
-//    return self;
-//}
-
 - (id)initForFSDefaultFileType: (NSString *)filetype
 {
     int i;
     NSString *filename;
     NSDictionary *dict;
     static NSDictionary *factorySettings;
-    
+
     self = [super init];
     if (!self) return self;
-    
+
     if (factorySettings == NULL) {
         NSBundle *bdl = [NSBundle mainBundle];
         NSString *path = [ bdl pathForResource: @"factorySettings"
@@ -149,18 +143,18 @@
 {
     NSUserDefaults *defaults;
     NSDictionary *dict;
-    
+
     defaults = [NSUserDefaults standardUserDefaults];
     dict = [defaults dictionaryForKey: filetype];
     if (!dict)
         return;
     [self applyValuesFromDict: dict];
 }
-    
+
 - (id)initForDefaultFileType: (NSString *)filetype
 {
     FileSettings *fsdefaults;
-    
+
     fsdefaults = [FileSettings getFactorySettingsForFileType: filetype];
     self = [self initWithFileSettings: fsdefaults];
     if (!self) return self;
@@ -220,7 +214,7 @@
 - (void)applyValuesFromDict: (NSDictionary *)dict
 {
     id value;
-    
+
     value = [dict objectForKey: @"interpreter"];
     if (value) interpreter = [value retain];
     value = [dict objectForKey: @"honourhashbang"];
@@ -247,12 +241,12 @@
 
 - (NSString*)_replaceSingleQuotes: (NSString*)string
 {
-	/* Replace all single-quotes by '"'"', that way shellquoting will
-	 * be correct when the result value is delimited  using single quotes.
-	 */
-	NSArray* components = [string componentsSeparatedByString:@"'"];
+    /* Replace all single-quotes by '"'"', that way shellquoting will
+     * be correct when the result value is delimited  using single quotes.
+     */
+    NSArray* components = [string componentsSeparatedByString:@"'"];
 
-	return [components componentsJoinedByString:@"'\"'\"'"];
+    return [components componentsJoinedByString:@"'\"'\"'"];
 }
 
 - (NSString *)commandLineForScript: (NSString *)script
@@ -265,7 +259,7 @@
 
     script_dir = [script substringToIndex:
 	    [script length]-[[script lastPathComponent] length]];
-    
+
     if (honourhashbang &&
        (fp=fopen([script fileSystemRepresentation], "r")) &&
        fgets(hashbangbuf, sizeof(hashbangbuf), fp) &&
@@ -278,7 +272,7 @@
     }
     if (!cur_interp)
         cur_interp = interpreter;
-        
+
     return [NSString stringWithFormat:
         @"cd '%@' && '%@'%s%s%s%s%s%s %@ '%@' %@ %s",
     	[self _replaceSingleQuotes:script_dir],
@@ -297,7 +291,7 @@
 
 - (NSArray *) interpreters { return interpreters;};
 
-// FileSettingsSource protocol 
+// FileSettingsSource protocol
 - (NSString *) interpreter { return interpreter;};
 - (BOOL) honourhashbang { return honourhashbang; };
 - (BOOL) debug { return debug;};
diff --git a/Mac/PythonLauncher/MyAppDelegate.m b/Mac/PythonLauncher/MyAppDelegate.m
index a5ba751..e75fb06 100644
--- a/Mac/PythonLauncher/MyAppDelegate.m
+++ b/Mac/PythonLauncher/MyAppDelegate.m
@@ -33,7 +33,7 @@
 
 - (BOOL)shouldShowUI
 {
-    // if this call comes before applicationDidFinishLaunching: we 
+    // if this call comes before applicationDidFinishLaunching: we
     // should terminate immedeately after starting the script.
     if (!initial_action_done)
         should_terminate = YES;
@@ -62,7 +62,7 @@
     static NSString *extensions[] = { @"py", @"pyw", @"pyc", NULL};
     NSString **ext_p;
     int i;
-    
+
     if ([[NSUserDefaults standardUserDefaults] boolForKey: @"SkipFileBindingTest"])
         return;
     ourUrl = [NSURL fileURLWithPath: [[NSBundle mainBundle] bundlePath]];
@@ -92,5 +92,5 @@
         }
     }
 }
-        
+
 @end
diff --git a/Mac/PythonLauncher/MyDocument.m b/Mac/PythonLauncher/MyDocument.m
index 86112c4..90c5db9 100755
--- a/Mac/PythonLauncher/MyDocument.m
+++ b/Mac/PythonLauncher/MyDocument.m
@@ -16,7 +16,7 @@
 {
     self = [super init];
     if (self) {
-    
+
         // Add your subclass-specific initialization here.
         // If an error occurs here, send a [self dealloc] message and return nil.
         script = [@"<no script>.py" retain];
@@ -37,20 +37,17 @@
 {
     NSApplication *app = [NSApplication sharedApplication];
     [super close];
-    if ([[app delegate] shouldTerminate])
+    if ([(MyAppDelegate*)[app delegate] shouldTerminate])
         [app terminate: self];
 }
 
 - (void)load_defaults
 {
-//    if (settings) [settings release];
     settings = [FileSettings newSettingsForFileType: filetype];
 }
 
 - (void)update_display
 {
-//    [[self window] setTitle: script];
-    
     [interpreter setStringValue: [settings interpreter]];
     [honourhashbang setState: [settings honourhashbang]];
     [debug setState: [settings debug]];
@@ -62,7 +59,7 @@
     [others setStringValue: [settings others]];
     [scriptargs setStringValue: [settings scriptargs]];
     [with_terminal setState: [settings with_terminal]];
-    
+
     [commandline setStringValue: [settings commandLineForScript: script]];
 }
 
@@ -75,8 +72,8 @@
 {
     const char *cmdline;
     int sts;
-    
-     cmdline = [[settings commandLineForScript: script] cString];
+
+     cmdline = [[settings commandLineForScript: script] UTF8String];
    if ([settings with_terminal]) {
         sts = doscript(cmdline);
     } else {
@@ -107,14 +104,13 @@
 {
     // Insert code here to read your document from the given data.  You can also choose to override -loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
     BOOL show_ui;
-    
-    // ask the app delegate whether we should show the UI or not. 
-    show_ui = [[[NSApplication sharedApplication] delegate] shouldShowUI];
+
+    // ask the app delegate whether we should show the UI or not.
+    show_ui = [(MyAppDelegate*)[[NSApplication sharedApplication] delegate] shouldShowUI];
     [script release];
     script = [fileName retain];
     [filetype release];
     filetype = [type retain];
-//    if (settings) [settings release];
     settings = [FileSettings newSettingsForFileType: filetype];
     if (show_ui) {
         [self update_display];
@@ -152,7 +148,7 @@
     [self update_display];
 }
 
-// FileSettingsSource protocol 
+// FileSettingsSource protocol
 - (NSString *) interpreter { return [interpreter stringValue];};
 - (BOOL) honourhashbang { return [honourhashbang state];};
 - (BOOL) debug { return [debug state];};
diff --git a/Mac/PythonLauncher/PreferencesWindowController.m b/Mac/PythonLauncher/PreferencesWindowController.m
index 311c375..ec5bbe8 100644
--- a/Mac/PythonLauncher/PreferencesWindowController.m
+++ b/Mac/PythonLauncher/PreferencesWindowController.m
@@ -5,7 +5,7 @@
 + getPreferencesWindow
 {
     static PreferencesWindowController *_singleton;
-    
+
     if (!_singleton)
         _singleton = [[PreferencesWindowController alloc] init];
     [_singleton showWindow: _singleton];
@@ -21,15 +21,13 @@
 - (void)load_defaults
 {
     NSString *title = [filetype titleOfSelectedItem];
-    
+
     settings = [FileSettings getDefaultsForFileType: title];
 }
 
 - (void)update_display
 {
-//    [[self window] setTitle: script];
-    
-	[interpreter reloadData];
+    [interpreter reloadData];
     [interpreter setStringValue: [settings interpreter]];
     [honourhashbang setState: [settings honourhashbang]];
     [debug setState: [settings debug]];
@@ -41,7 +39,6 @@
     [others setStringValue: [settings others]];
     [with_terminal setState: [settings with_terminal]];
     // Not scriptargs, it isn't for preferences
-    
     [commandline setStringValue: [settings commandLineForScript: @"<your script here>"]];
 }
 
@@ -75,7 +72,7 @@
     [self update_display];
 }
 
-// FileSettingsSource protocol 
+// FileSettingsSource protocol
 - (NSString *) interpreter { return [interpreter stringValue];};
 - (BOOL) honourhashbang { return [honourhashbang state]; };
 - (BOOL) debug { return [debug state];};
@@ -98,23 +95,23 @@
 // NSComboBoxDataSource protocol
 - (unsigned int)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)aString
 {
-	NSArray *interp_list = [settings interpreters];
+    NSArray *interp_list = [settings interpreters];
     unsigned int rv = [interp_list indexOfObjectIdenticalTo: aString];
-	return rv;
+    return rv;
 }
 
 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index
 {
-	NSArray *interp_list = [settings interpreters];
+    NSArray *interp_list = [settings interpreters];
     id rv = [interp_list objectAtIndex: index];
-	return rv;
+    return rv;
 }
 
 - (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox
 {
-	NSArray *interp_list = [settings interpreters];
+    NSArray *interp_list = [settings interpreters];
     int rv = [interp_list count];
-	return rv;
+    return rv;
 }
 
 
diff --git a/Mac/PythonLauncher/doscript.h b/Mac/PythonLauncher/doscript.h
index eef0b56..3fd3187 100644
--- a/Mac/PythonLauncher/doscript.h
+++ b/Mac/PythonLauncher/doscript.h
@@ -9,4 +9,4 @@
 
 #include <Carbon/Carbon.h>
 
-extern int doscript(const char *command);
\ No newline at end of file
+extern int doscript(const char *command);
diff --git a/Mac/PythonLauncher/doscript.m b/Mac/PythonLauncher/doscript.m
index 024b883..cbb783b 100644
--- a/Mac/PythonLauncher/doscript.m
+++ b/Mac/PythonLauncher/doscript.m
@@ -11,49 +11,49 @@
 #import <ApplicationServices/ApplicationServices.h>
 #import "doscript.h"
 
-extern int 
+extern int
 doscript(const char *command)
 {
-	char *bundleID = "com.apple.Terminal";
-	AppleEvent evt, res;
-	AEDesc desc;
-	OSStatus err;
+    char *bundleID = "com.apple.Terminal";
+    AppleEvent evt, res;
+    AEDesc desc;
+    OSStatus err;
 
-	[[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Utilities/Terminal.app/"];
+    [[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Utilities/Terminal.app/"];
 
-	// Build event
-	err = AEBuildAppleEvent(kAECoreSuite, kAEDoScript,
-	                         typeApplicationBundleID,
-	                         bundleID, strlen(bundleID),
-	                         kAutoGenerateReturnID,
-	                         kAnyTransactionID,
-	                         &evt, NULL,
-	                         "'----':utf8(@)", strlen(command),
-	                         command);
-	if (err) {
-		NSLog(@"AEBuildAppleEvent failed: %d\n", err);
-		return err;
-	}
+    // Build event
+    err = AEBuildAppleEvent(kAECoreSuite, kAEDoScript,
+                             typeApplicationBundleID,
+                             bundleID, strlen(bundleID),
+                             kAutoGenerateReturnID,
+                             kAnyTransactionID,
+                             &evt, NULL,
+                             "'----':utf8(@)", strlen(command),
+                             command);
+    if (err) {
+        NSLog(@"AEBuildAppleEvent failed: %ld\n", (long)err);
+        return err;
+    }
 
-	// Send event and check for any Apple Event Manager errors
-	err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
-	AEDisposeDesc(&evt);
-	if (err) {
-		NSLog(@"AESendMessage failed: %d\n", err);
-		return err;
-	}
-	// Check for any application errors
-	err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &desc);
-	AEDisposeDesc(&res);
-	if (!err) {
-		AEGetDescData(&desc, &err, sizeof(err));
-		NSLog(@"Terminal returned an error: %d", err);
-		AEDisposeDesc(&desc);
-	} else if (err == errAEDescNotFound) {
-		err = noErr;
-	} else {
-		NSLog(@"AEGetPArmDesc returned an error: %d", err);
-	}
+    // Send event and check for any Apple Event Manager errors
+    err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
+    AEDisposeDesc(&evt);
+    if (err) {
+        NSLog(@"AESendMessage failed: %ld\n", (long)err);
+        return err;
+    }
+    // Check for any application errors
+    err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &desc);
+    AEDisposeDesc(&res);
+    if (!err) {
+        AEGetDescData(&desc, &err, sizeof(err));
+        NSLog(@"Terminal returned an error: %ld", (long)err);
+        AEDisposeDesc(&desc);
+    } else if (err == errAEDescNotFound) {
+        err = noErr;
+    } else {
+        NSLog(@"AEGetPArmDesc returned an error: %ld", (long)err);
+    }
 
-	return err;
+    return err;
 }
diff --git a/Mac/PythonLauncher/main.m b/Mac/PythonLauncher/main.m
index 6841433..04b4d73 100755
--- a/Mac/PythonLauncher/main.m
+++ b/Mac/PythonLauncher/main.m
@@ -11,7 +11,7 @@
 
 int main(int argc, const char *argv[])
 {
-	char *home = getenv("HOME");
-	if (home) chdir(home);
+    char *home = getenv("HOME");
+    if (home) chdir(home);
     return NSApplicationMain(argc, argv);
 }
diff --git a/Mac/README b/Mac/README
index 555b7ca..0a07b5e 100644
--- a/Mac/README
+++ b/Mac/README
@@ -209,9 +209,11 @@
 through PythonLauncher's preferences dialog.
 
 "BuildApplet.app" creates an applet from a Python script. Drop the script on it
-and out comes a full-featured MacOS application. There is much more to this,
-to be supplied later. Some useful (but outdated) info can be found in
-Mac/Demo.
+and out comes a full-featured MacOS application.  BuildApplet.app is now
+deprecated and has been removed in Python 3.  As of OS X 10.8, Xcode 4 no
+longer supplies the headers for the deprecated QuickDraw APIs used by
+the EasyDialogs module making BuildApplet unusable as an app.  It will
+not be built by the Mac/Makefile in this case.
 
 The commandline scripts /usr/local/bin/python and pythonw can be used to run
 non-GUI and GUI python scripts from the command line, respectively.
diff --git a/Makefile.pre.in b/Makefile.pre.in
index 9d55550..98711ae 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -1295,11 +1295,11 @@
 		Modules/ld_so_aix Modules/python.exp Misc/python.pc
 	-rm -f python*-gdb.py
 	-rm -f pybuilddir.txt
-	find $(srcdir) '(' -name '*.fdc' -o -name '*~' \
-			   -o -name '[@,#]*' -o -name '*.old' \
-			   -o -name '*.orig' -o -name '*.rej' \
-			   -o -name '*.bak' ')' \
-			   -exec rm -f {} ';'
+	find $(srcdir)/[a-zA-Z]* '(' -name '*.fdc' -o -name '*~' \
+				     -o -name '[@,#]*' -o -name '*.old' \
+				     -o -name '*.orig' -o -name '*.rej' \
+				     -o -name '*.bak' ')' \
+				     -exec rm -f {} ';'
 
 # Check for smelly exported symbols (not starting with Py/_Py)
 smelly: all
diff --git a/Misc/ACKS b/Misc/ACKS
index 21a1a49..fae831e 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -270,6 +270,7 @@
 Maxim Dzumanenko
 Walter Dörwald
 Hans Eckardt
+David Edelsohn
 Grant Edwards
 John Ehresman
 Eric Eisner
@@ -316,6 +317,7 @@
 John Fouhy
 Stefan Franke
 Martin Franklin
+Bruce Frederiksen
 Robin Friedrich
 Bradley Froehle
 Ivan Frohne
@@ -478,6 +480,7 @@
 Jack Jansen
 Bill Janssen
 Thomas Jarosch
+Rajagopalasarma Jayakrishnan
 Drew Jenkins
 Flemming Kjær Jensen
 Philip H. Jensen
@@ -501,6 +504,7 @@
 Sijin Joseph
 Andreas Jung
 Tattoo Mabonzo K.
+Sarah K.
 Bohuslav Kabrda
 Bob Kahn
 Kurt B. Kaiser
@@ -542,6 +546,7 @@
 Greg Kochanski
 Damon Kohler
 Marko Kohtala
+Vajrasky Kok
 Guido Kollerie
 Peter A. Koren
 Joseph Koshy
@@ -594,6 +599,7 @@
 Luke Kenneth Casson Leighton
 Tshepang Lekhonkhobe
 Marc-André Lemburg
+Mateusz Lenik
 John Lenton
 Kostyantyn Leschenko
 Christopher Tur Lesniewski-Laas
@@ -623,6 +629,7 @@
 Lukas Lueg
 Loren Luke
 Fredrik Lundh
+Zhongyue Luo
 Mark Lutz
 Jim Lynch
 Mikael Lyngvig
@@ -739,6 +746,7 @@
 Kevin O'Connor
 Tim O'Malley
 Zooko O'Whielacronx
+Elena Oat
 Pascal Oberndoerfer
 Jeffrey Ollie
 Adam Olsen
@@ -934,6 +942,7 @@
 Joel Shprentz
 Itamar Shtull-Trauring
 Yue Shuaijie
+Terrel Shumway
 Eric Siegerman
 Paul Sijben
 Tim Silk
@@ -1034,6 +1043,7 @@
 Stephen Turner
 Theodore Turocy
 Bill Tutt
+Fraser Tweedale
 Doobee R. Tzeck
 Eren Türkay
 Lionel Ulmer
@@ -1077,6 +1087,7 @@
 Henrik Weber
 Corran Webster
 Glyn Webster
+Phil Webster
 Stefan Wehr
 Zack Weinberg
 Bob Weiner
@@ -1138,6 +1149,7 @@
 Milan Zamazal
 Artur Zaprzala
 Mike Zarnstorff
+Yury V. Zaytsev
 Siebren van der Zee
 Nickolai Zeldovich
 Uwe Zessin
diff --git a/Misc/NEWS b/Misc/NEWS
index 9c472dd..5e4a1be 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,23 +9,141 @@
 Core and Builtins
 -----------------
 
+- Issue #15866: The xmlcharrefreplace error handler no more produces two XML
+  entities for a non-BMP character on narrow build.
+
+- Issue #18184: PyUnicode_FromFormat() and PyUnicode_FromFormatV() now raise
+  OverflowError when an argument of %c format is out of range.
+
+- Issue #18137: Detect integer overflow on precision in float.__format__()
+  and complex.__format__().
+
+- Issue #18038: SyntaxError raised during compilation sources with illegal
+  encoding now always contains an encoding name.
+
 - Issue #18019: Fix crash in the repr of dictionaries containing their own
   views.
 
+- Issue #18427: str.replace could crash the interpreter with huge strings.
+
 Library
 -------
 
+- Issue #18676: Change 'positive' to 'non-negative' in queue.py put and get
+  docstrings and ValueError messages. Patch by Zhongyue Luo
+
+- Issue #17998: Fix an internal error in regular expression engine.
+
+- Issue #17557: Fix os.getgroups() to work with the modified behavior of
+  getgroups(2) on OS X 10.8.  Original patch by Mateusz Lenik.
+
+- Issue #18455: multiprocessing should not retry connect() with same socket.
+
+- Issue #18513: Fix behaviour of cmath.rect w.r.t. signed zeros on OS X 10.8 +
+  gcc.
+
+- Issue #18101: Tcl.split() now process Unicode strings nested in a tuple as it
+  do with byte strings.
+
+- Issue #18347: ElementTree's html serializer now preserves the case of
+  closing tags.
+
+- Issue #17261: Ensure multiprocessing's proxies use proper address.
+
+- Issue #17097: Make multiprocessing ignore EINTR.
+
+- Issue #18155: The csv module now correctly handles csv files that use
+  a delimiter character that has a special meaning in regexes, instead of
+  throwing an exception.
+
+- Issue #18135: ssl.SSLSocket.write() now raises an OverflowError if the input
+  string in longer than 2 gigabytes. The ssl module does not support partial
+  write.
+
+- Issue #18167: cgi.FieldStorage no longer fails to handle multipart/form-data
+  when \r\n appears at end of 65535 bytes without other newlines.
+
+- Issue #17403: urllib.parse.robotparser normalizes the urls before adding to
+  ruleline. This helps in handling certain types invalid urls in a conservative
+  manner. Patch contributed by Mher Movsisyan.
+
 - Implement inequality on weakref.WeakSet.
 
 - Issue #17981: Closed socket on error in SysLogHandler.
 
+- Issue #18015: Fix unpickling of 2.7.3 and 2.7.4 namedtuples.
+
 - Issue #17754: Make ctypes.util.find_library() independent of the locale.
 
 - Fix typos in the multiprocessing module.
 
+- Issue #17269: Workaround for socket.getaddrinfo crash on MacOS X
+  with port None or "0" and flags AI_NUMERICSERV.
+
+- Issue #18080: When building a C extension module on OS X, if the compiler
+  is overriden with the CC environment variable, use the new compiler as
+  the default for linking if LDSHARED is not also overriden.  This restores
+  Distutils behavior introduced in 2.7.3 and inadvertently dropped in 2.7.4.
+
+- Issue #18071: C extension module builds on OS X could fail with TypeError
+  if the Xcode command line tools were not installed.
+
+- Issue #18113: Fixed a refcount leak in the curses.panel module's
+  set_userptr() method.  Reported by Atsuo Ishimoto.
+
+Tools/Demos
+-----------
+
+- Issue #18439: Make patchcheck work on Windows for ACKS, NEWS.
+
+- Issue #18448: Fix a typo in Demo/newmetaclasses/Eiffel.py.
+
+- Issue #12990: The "Python Launcher" on OSX could not launch python scripts
+  that have paths that include wide characters.
+
+Build
+-----
+
+- Issue #16067: Add description into MSI file to replace installer's temporary name.
+
+- Issue #18256: Compilation fix for recent AIX releases.  Patch by
+  David Edelsohn.
+
+- Issue #18098: The deprecated OS X Build Applet.app fails to build on
+  OS X 10.8 systems because the Apple-deprecated QuickDraw headers have
+  been removed from Xcode 4.  Skip building it in this case.
+
 IDLE
 ----
 
+- Issue #18429: Format / Format Paragraph, now works when comment blocks
+  are selected. As with text blocks, this works best when the selection
+  only includes complete lines.
+
+- Issue #18226: Add docstrings and unittests for FormatParagraph.py.
+  Original patches by Todd Rovito and Phil Webster.
+
+- Issue #18279: Format - Strip trailing whitespace no longer marks a file as
+  changed when it has not been changed. This fix followed the addition of a
+  test file originally written by Phil Webster (the issue's main goal).
+
+- Issue #18539: Calltips now work for float default arguments.
+
+- Issue #7136: In the Idle File menu, "New Window" is renamed "New File".
+  Patch by Tal Einat, Roget Serwy, and Todd Rovito.
+
+- Issue #8515: Set __file__ when run file in IDLE.
+  Initial patch by Bruce Frederiksen.
+
+- Issue #5492: Avoid traceback when exiting IDLE caused by a race condition.
+
+- Issue #17511: Keep IDLE find dialog open after clicking "Find Next".
+  Original patch by Sarah K.
+
+- Issue #15392: Create a unittest framework for IDLE.
+  Preliminary patch by Rajagopalasarma Jayakrishnan
+  See Lib/idlelib/idle_test/README.txt for how to run Idle tests.
+
 - Issue #14146: Highlight source line while debugging on Windows.
 
 - Issue #17532: Always include Options menu for IDLE on OS X.
@@ -34,11 +152,19 @@
 Tests
 -----
 
+- Issue #18357: add tests for dictview set difference.
+  Patch by Fraser Tweedale.
+
+- Issue #11185: Fix test_wait4 under AIX.  Patch by Sébastien Sablé.
+
+- Issue #18094: test_uuid no more reports skipped tests as passed.
+
 - Issue #11995: test_pydoc doesn't import all sys.path modules anymore.
 
 Documentation
 -------------
 
+- Issue #17701: Improving strftime documentation.
 - Issue #17844: Refactor a documentation of Python specific encodings.
   Add links to encoders and decoders for binary-to-binary codecs.
 
@@ -51,7 +177,7 @@
 Core and Builtins
 -----------------
 
-- Issue #15535: Fixed regression in the pickling of named tuples by 
+- Issue #15535: Fixed regression in the pickling of named tuples by
   removing the __dict__ property introduced in 2.7.4.
 
 - Issue #17857: Prevent build failures with pre-3.5.0 versions of sqlite3,
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c
index 371631c..26d8783 100644
--- a/Modules/_collectionsmodule.c
+++ b/Modules/_collectionsmodule.c
@@ -8,9 +8,13 @@
 */
 
 /* The block length may be set to any number over 1.  Larger numbers
- * reduce the number of calls to the memory allocator but take more
- * memory.  Ideally, BLOCKLEN should be set with an eye to the
- * length of a cache line.
+ * reduce the number of calls to the memory allocator, give faster
+ * indexing and rotation, and reduce the link::data overhead ratio.
+ *
+ * Ideally, the block length will be set to two less than some
+ * multiple of the cache-line length (so that the full block
+ * including the leftlink and rightlink will fit neatly into
+ * cache lines).
  */
 
 #define BLOCKLEN 62
@@ -46,9 +50,9 @@
  */
 
 typedef struct BLOCK {
-    struct BLOCK *leftlink;
-    struct BLOCK *rightlink;
     PyObject *data[BLOCKLEN];
+    struct BLOCK *rightlink;
+    struct BLOCK *leftlink;
 } block;
 
 #define MAXFREEBLOCKS 10
@@ -58,13 +62,8 @@
 static block *
 newblock(block *leftlink, block *rightlink, Py_ssize_t len) {
     block *b;
-    /* To prevent len from overflowing PY_SSIZE_T_MAX on 64-bit machines, we
-     * refuse to allocate new blocks if the current len is dangerously
-     * close.  There is some extra margin to prevent spurious arithmetic
-     * overflows at various places.  The following check ensures that
-     * the blocks allocated to the deque, in the worst case, can only
-     * have PY_SSIZE_T_MAX-2 entries in total.
-     */
+    /* To prevent len from overflowing PY_SSIZE_T_MAX on 32-bit machines, we
+     * refuse to allocate new blocks if the current len is nearing overflow. */
     if (len >= PY_SSIZE_T_MAX - 2*BLOCKLEN) {
         PyErr_SetString(PyExc_OverflowError,
                         "cannot add more blocks to the deque");
@@ -103,8 +102,8 @@
     Py_ssize_t leftindex;       /* in range(BLOCKLEN) */
     Py_ssize_t rightindex;      /* in range(BLOCKLEN) */
     Py_ssize_t len;
-    Py_ssize_t maxlen;
     long state;         /* incremented whenever the indices move */
+    Py_ssize_t maxlen;
     PyObject *weakreflist; /* List of weak references */
 } dequeobject;
 
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 6daf455..6642dc3 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -75,6 +75,10 @@
 
 #include <ffi.h>
 #include "ctypes.h"
+#ifdef HAVE_ALLOCA_H
+/* AIX needs alloca.h for alloca() */
+#include <alloca.h>
+#endif
 
 #if defined(_DEBUG) || defined(__MINGW32__)
 /* Don't use structured exception handling on Windows if this is defined.
diff --git a/Modules/_curses_panel.c b/Modules/_curses_panel.c
index 04a0a28..bdd5cf0 100644
--- a/Modules/_curses_panel.c
+++ b/Modules/_curses_panel.c
@@ -293,9 +293,18 @@
 static PyObject *
 PyCursesPanel_set_panel_userptr(PyCursesPanelObject *self, PyObject *obj)
 {
+    PyObject *oldobj;
+    int rc;
+    PyCursesInitialised;
     Py_INCREF(obj);
-    return PyCursesCheckERR(set_panel_userptr(self->pan, (void*)obj),
-                            "set_panel_userptr");
+    oldobj = (PyObject *) panel_userptr(self->pan);
+    rc = set_panel_userptr(self->pan, (void*)obj);
+    if (rc == ERR) {
+        /* In case of an ncurses error, decref the new object again */
+        Py_DECREF(obj);
+    }
+    Py_XDECREF(oldobj);
+    return PyCursesCheckERR(rc, "set_panel_userptr");
 }
 
 static PyObject *
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 379aa01..b9abcac 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2427,6 +2427,8 @@
 
     if (PyUnicode_GET_SIZE(u) != 256) {
         Py_DECREF(u);
+        PyErr_SetString(PyExc_ValueError,
+                        "multi-byte encodings are not supported");
         return XML_STATUS_ERROR;
     }
 
diff --git a/Modules/_multiprocessing/socket_connection.c b/Modules/_multiprocessing/socket_connection.c
index 66bc377..bdb0a32 100644
--- a/Modules/_multiprocessing/socket_connection.c
+++ b/Modules/_multiprocessing/socket_connection.c
@@ -23,6 +23,21 @@
 #endif
 
 /*
+ * Wrapper for PyErr_CheckSignals() which can be called without the GIL
+ */
+
+static int
+check_signals(void)
+{
+    PyGILState_STATE state;
+    int res;
+    state = PyGILState_Ensure();
+    res = PyErr_CheckSignals();
+    PyGILState_Release(state);
+    return res;
+}
+
+/*
  * Send string to file descriptor
  */
 
@@ -34,8 +49,14 @@
 
     while (length > 0) {
         res = WRITE(h, p, length);
-        if (res < 0)
+        if (res < 0) {
+            if (errno == EINTR) {
+                if (check_signals() < 0)
+                    return MP_EXCEPTION_HAS_BEEN_SET;
+                continue;
+            }
             return MP_SOCKET_ERROR;
+        }
         length -= res;
         p += res;
     }
@@ -56,12 +77,16 @@
 
     while (remaining > 0) {
         temp = READ(h, p, remaining);
-        if (temp <= 0) {
-            if (temp == 0)
-                return remaining == length ?
-                    MP_END_OF_FILE : MP_EARLY_END_OF_FILE;
-            else
-                return temp;
+        if (temp < 0) {
+            if (errno == EINTR) {
+                if (check_signals() < 0)
+                    return MP_EXCEPTION_HAS_BEEN_SET;
+                continue;
+            }
+            return temp;
+        }
+        else if (temp == 0) {
+            return remaining == length ? MP_END_OF_FILE : MP_EARLY_END_OF_FILE;
         }
         remaining -= temp;
         p += temp;
@@ -171,9 +196,16 @@
     p.revents = 0;
 
     if (timeout < 0) {
-        res = poll(&p, 1, -1);
+        do {
+            res = poll(&p, 1, -1);
+        } while (res < 0 && errno == EINTR);
     } else {
         res = poll(&p, 1, (int)(timeout * 1000 + 0.5));
+        if (res < 0 && errno == EINTR) {
+            /* We were interrupted by a signal.  Just indicate a
+               timeout even though we are early. */
+            return FALSE;
+        }
     }
 
     if (res < 0) {
@@ -209,12 +241,19 @@
     FD_SET((SOCKET)conn->handle, &rfds);
 
     if (timeout < 0.0) {
-        res = select((int)conn->handle+1, &rfds, NULL, NULL, NULL);
+        do {
+            res = select((int)conn->handle+1, &rfds, NULL, NULL, NULL);
+        } while (res < 0 && errno == EINTR);
     } else {
         struct timeval tv;
         tv.tv_sec = (long)timeout;
         tv.tv_usec = (long)((timeout - tv.tv_sec) * 1e6 + 0.5);
         res = select((int)conn->handle+1, &rfds, NULL, NULL, &tv);
+        if (res < 0 && errno == EINTR) {
+            /* We were interrupted by a signal.  Just indicate a
+               timeout even though we are early. */
+            return FALSE;
+        }
     }
 
     if (res < 0) {
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 478416f..c9e4f73 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -1028,7 +1028,7 @@
             TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
                    ctx->pattern[1], ctx->pattern[2]));
 
-            if (ctx->pattern[1] > end - ctx->ptr)
+            if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr)
                 RETURN_FAILURE; /* cannot match */
 
             state->ptr = ctx->ptr;
@@ -1111,7 +1111,7 @@
             TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
                    ctx->pattern[1], ctx->pattern[2]));
 
-            if (ctx->pattern[1] > end - ctx->ptr)
+            if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr)
                 RETURN_FAILURE; /* cannot match */
 
             state->ptr = ctx->ptr;
@@ -1210,7 +1210,7 @@
             TRACE(("|%p|%p|MAX_UNTIL %d\n", ctx->pattern,
                    ctx->ptr, ctx->count));
 
-            if (ctx->count < ctx->u.rep->pattern[1]) {
+            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
                 /* not enough matches */
                 ctx->u.rep->count = ctx->count;
                 DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
@@ -1224,7 +1224,7 @@
                 RETURN_FAILURE;
             }
 
-            if ((ctx->count < ctx->u.rep->pattern[2] ||
+            if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
                 ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
                 state->ptr != ctx->u.rep->last_ptr) {
                 /* we may have enough matches, but if we can
@@ -1273,7 +1273,7 @@
             TRACE(("|%p|%p|MIN_UNTIL %d %p\n", ctx->pattern,
                    ctx->ptr, ctx->count, ctx->u.rep->pattern));
 
-            if (ctx->count < ctx->u.rep->pattern[1]) {
+            if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
                 /* not enough matches */
                 ctx->u.rep->count = ctx->count;
                 DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
@@ -1302,7 +1302,7 @@
 
             LASTMARK_RESTORE();
 
-            if ((ctx->count >= ctx->u.rep->pattern[2]
+            if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
                 && ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
                 state->ptr == ctx->u.rep->last_ptr)
                 RETURN_FAILURE;
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 195e5b6..afcc017 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -1192,6 +1192,12 @@
     if (!PyArg_ParseTuple(args, "s*:write", &buf))
         return NULL;
 
+    if (buf.len > INT_MAX) {
+        PyErr_Format(PyExc_OverflowError,
+                     "string longer than %d bytes", INT_MAX);
+        goto error;
+    }
+
     /* just in case the blocking state of the socket has been changed */
     nonblocking = (self->Socket->sock_timeout >= 0.0);
     BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
@@ -1213,7 +1219,7 @@
     }
     do {
         PySSL_BEGIN_ALLOW_THREADS
-        len = SSL_write(self->ssl, buf.buf, buf.len);
+        len = SSL_write(self->ssl, buf.buf, (int)buf.len);
         err = SSL_get_error(self->ssl, len);
         PySSL_END_ALLOW_THREADS
         if (PyErr_CheckSignals()) {
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index b0386f0..4e7d47d 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -1118,7 +1118,7 @@
     if (!PyArg_ParseTuple(args, "u#|s", &unicode, &length, &errors))
         return NULL;
 
-    decimal_length = length * 7; /* len('&#8364;') */
+    decimal_length = length * 10; /* len('&#1114111;') */
     decimal = PyBytes_FromStringAndSize(NULL, decimal_length);
     if (decimal == NULL)
         return NULL;
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 7872df3..aed42ac 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -547,6 +547,33 @@
             return Split(PyString_AsString(arg));
         /* Fall through, returning arg. */
     }
+    else if (PyUnicode_Check(arg)) {
+        int argc;
+        char **argv;
+        char *list;
+        PyObject *s = PyUnicode_AsUTF8String(arg);
+
+        if (s == NULL) {
+            Py_INCREF(arg);
+            return arg;
+        }
+        list = PyString_AsString(s);
+
+        if (list == NULL ||
+            Tcl_SplitList((Tcl_Interp *)NULL, list, &argc, &argv) != TCL_OK) {
+            Py_DECREF(s);
+            Py_INCREF(arg);
+            return arg;
+        }
+        Tcl_Free(FREECAST argv);
+        if (argc > 1) {
+            PyObject *v = Split(list);
+            Py_DECREF(s);
+            return v;
+        }
+        Py_DECREF(s);
+        /* Fall through, returning arg. */
+    }
     Py_INCREF(arg);
     return arg;
 }
diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index 0dc6bdb..5720678 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -1006,6 +1006,13 @@
         else
             errno = 0;
     }
+    else if (phi == 0.0) {
+        /* Workaround for buggy results with phi=-0.0 on OS X 10.8.  See
+           bugs.python.org/issue18513. */
+        z.real = r;
+        z.imag = r * phi;
+        errno = 0;
+    }
     else {
         z.real = r * cos(phi);
         z.imag = r * sin(phi);
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index 55da9b8..040d8b0 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -11,7 +11,7 @@
    {"gr_name", "group name"},
    {"gr_passwd", "password"},
    {"gr_gid", "group id"},
-   {"gr_mem", "group memebers"},
+   {"gr_mem", "group members"},
    {0}
 };
 
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 65ade9f..8352dd3 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -4054,6 +4054,34 @@
     gid_t* alt_grouplist = grouplist;
     int n;
 
+#ifdef __APPLE__
+    /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
+     * there are more groups than can fit in grouplist.  Therefore, on OS X
+     * always first call getgroups with length 0 to get the actual number
+     * of groups.
+     */
+    n = getgroups(0, NULL);
+    if (n < 0) {
+        return posix_error();
+    } else if (n <= MAX_GROUPS) {
+        /* groups will fit in existing array */
+        alt_grouplist = grouplist;
+    } else {
+        alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
+        if (alt_grouplist == NULL) {
+            errno = EINVAL;
+            return posix_error();
+        }
+    }
+
+    n = getgroups(n, alt_grouplist);
+    if (n == -1) {
+        if (alt_grouplist != grouplist) {
+            PyMem_Free(alt_grouplist);
+        }
+        return posix_error();
+    }
+#else
     n = getgroups(MAX_GROUPS, grouplist);
     if (n < 0) {
         if (errno == EINVAL) {
@@ -4080,6 +4108,8 @@
             return posix_error();
         }
     }
+#endif
+
     result = PyList_New(n);
     if (result != NULL) {
         int i;
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index f269113..8de3fb1 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1252,6 +1252,13 @@
     if (_u_string == NULL)
         return result;
 
+    if (PyUnicode_GET_SIZE(_u_string) != 256) {
+        Py_DECREF(_u_string);
+        PyErr_SetString(PyExc_ValueError,
+                        "multi-byte encodings are not supported");
+        return result;
+    }
+
     for (i = 0; i < 256; i++) {
         /* Stupid to access directly, but fast */
         Py_UNICODE c = _u_string->str[i];
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index bdc055d..2735ecc 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -4179,6 +4179,15 @@
                         "getaddrinfo() argument 2 must be integer or string");
         goto err;
     }
+#if defined(__APPLE__) && defined(AI_NUMERICSERV)
+    if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
+        /* On OSX upto at least OSX 10.8 getaddrinfo crashes
+	 * if AI_NUMERICSERV is set and the servname is NULL or "0".
+	 * This workaround avoids a segfault in libsystem.
+	 */
+        pptr = "00";
+    }
+#endif
     memset(&hints, 0, sizeof(hints));
     hints.ai_family = family;
     hints.ai_socktype = socktype;
diff --git a/Modules/sre.h b/Modules/sre.h
index 15ed3d5..22a2d5d 100644
--- a/Modules/sre.h
+++ b/Modules/sre.h
@@ -20,14 +20,14 @@
 # if SIZEOF_SIZE_T > 4
 #  define SRE_MAXREPEAT (~(SRE_CODE)0)
 # else
-#  define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX + 1u)
+#  define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX)
 # endif
 #else
 # define SRE_CODE unsigned int
 # if SIZEOF_SIZE_T > SIZEOF_INT
 #  define SRE_MAXREPEAT (~(SRE_CODE)0)
 # else
-#  define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX + 1u)
+#  define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX)
 # endif
 #endif
 
diff --git a/Objects/setobject.c b/Objects/setobject.c
index af1ce16..db5cee2 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -212,7 +212,6 @@
 set_insert_key(register PySetObject *so, PyObject *key, long hash)
 {
     register setentry *entry;
-    typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
 
     assert(so->lookup != NULL);
     entry = so->lookup(so, key, hash);
diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h
index 6b28224..fd22751 100644
--- a/Objects/stringlib/formatter.h
+++ b/Objects/stringlib/formatter.h
@@ -928,7 +928,7 @@
     Py_ssize_t n_total;
     int has_decimal;
     double val;
-    Py_ssize_t precision = format->precision;
+    Py_ssize_t precision;
     Py_ssize_t default_precision = 6;
     STRINGLIB_CHAR type = format->type;
     int add_pct = 0;
@@ -947,6 +947,12 @@
        from a hard-code pseudo-locale */
     LocaleInfo locale;
 
+    if (format->precision > INT_MAX) {
+        PyErr_SetString(PyExc_ValueError, "precision too big");
+        goto done;
+    }
+    precision = (int)format->precision;
+
     /* Alternate is not allowed on floats. */
     if (format->alternate) {
         PyErr_SetString(PyExc_ValueError,
@@ -1078,7 +1084,7 @@
     Py_ssize_t n_im_total;
     int re_has_decimal;
     int im_has_decimal;
-    Py_ssize_t precision = format->precision;
+    Py_ssize_t precision;
     Py_ssize_t default_precision = 6;
     STRINGLIB_CHAR type = format->type;
     STRINGLIB_CHAR *p_re;
@@ -1107,6 +1113,12 @@
        from a hard-code pseudo-locale */
     LocaleInfo locale;
 
+    if (format->precision > INT_MAX) {
+        PyErr_SetString(PyExc_ValueError, "precision too big");
+        goto done;
+    }
+    precision = (int)format->precision;
+
     /* Alternate is not allowed on complex. */
     if (format->alternate) {
         PyErr_SetString(PyExc_ValueError,
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index 1209197..b80ef87 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -882,9 +882,9 @@
             size -= chunk_size;
         }
 #ifdef __VMS
-        if (size) fwrite(data, (int)size, 1, fp);
+        if (size) fwrite(data, (size_t)size, 1, fp);
 #else
-        fwrite(data, 1, (int)size, fp);
+        fwrite(data, 1, (size_t)size, fp);
 #endif
         Py_END_ALLOW_THREADS
         return 0;
@@ -2332,7 +2332,7 @@
 }
 
 Py_LOCAL_INLINE(Py_ssize_t)
-countchar(const char *target, int target_len, char c, Py_ssize_t maxcount)
+countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount)
 {
     Py_ssize_t count=0;
     const char *start=target;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 0ead06f..866eb9b 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -547,6 +547,37 @@
     return PyUnicode_FromStringAndSize(u, size);
 }
 
+/* _Py_UNICODE_NEXT is a private macro used to retrieve the character pointed
+ * by 'ptr', possibly combining surrogate pairs on narrow builds.
+ * 'ptr' and 'end' must be Py_UNICODE*, with 'ptr' pointing at the character
+ * that should be returned and 'end' pointing to the end of the buffer.
+ * ('end' is used on narrow builds to detect a lone surrogate at the
+ * end of the buffer that should be returned unchanged.)
+ * The ptr and end arguments should be side-effect free and ptr must an lvalue.
+ * The type of the returned char is always Py_UCS4.
+ *
+ * Note: the macro advances ptr to next char, so it might have side-effects
+ *       (especially if used with other macros).
+ */
+
+/* helper macros used by _Py_UNICODE_NEXT */
+#define _Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= ch && ch <= 0xDBFF)
+#define _Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= ch && ch <= 0xDFFF)
+/* Join two surrogate characters and return a single Py_UCS4 value. */
+#define _Py_UNICODE_JOIN_SURROGATES(high, low)  \
+    (((((Py_UCS4)(high) & 0x03FF) << 10) |      \
+      ((Py_UCS4)(low) & 0x03FF)) + 0x10000)
+
+#ifdef Py_UNICODE_WIDE
+#define _Py_UNICODE_NEXT(ptr, end) *(ptr)++
+#else
+#define _Py_UNICODE_NEXT(ptr, end)                                      \
+     (((_Py_UNICODE_IS_HIGH_SURROGATE(*(ptr)) && (ptr) < (end)) &&      \
+        _Py_UNICODE_IS_LOW_SURROGATE((ptr)[1])) ?                       \
+       ((ptr) += 2,_Py_UNICODE_JOIN_SURROGATES((ptr)[-2], (ptr)[-1])) : \
+       (Py_UCS4)*(ptr)++)
+#endif
+
 #ifdef HAVE_WCHAR_H
 
 #if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
@@ -740,8 +771,25 @@
 
             switch (*f) {
             case 'c':
-                (void)va_arg(count, int);
+            {
+                int ordinal = va_arg(count, int);
+#ifdef Py_UNICODE_WIDE
+                if (ordinal < 0 || ordinal > 0x10ffff) {
+                    PyErr_SetString(PyExc_OverflowError,
+                                    "%c arg not in range(0x110000) "
+                                    "(wide Python build)");
+                    goto fail;
+                }
+#else
+                if (ordinal < 0 || ordinal > 0xffff) {
+                    PyErr_SetString(PyExc_OverflowError,
+                                    "%c arg not in range(0x10000) "
+                                    "(narrow Python build)");
+                    goto fail;
+                }
+#endif
                 /* fall through... */
+            }
             case '%':
                 n++;
                 break;
@@ -3625,26 +3673,22 @@
             case 4: /* xmlcharrefreplace */
                 respos = str-PyString_AS_STRING(res);
                 /* determine replacement size (temporarily (mis)uses p) */
-                for (p = collstart, repsize = 0; p < collend; ++p) {
-                    if (*p<10)
+                for (p = collstart, repsize = 0; p < collend;) {
+                    Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend);
+                    if (ch < 10)
                         repsize += 2+1+1;
-                    else if (*p<100)
+                    else if (ch < 100)
                         repsize += 2+2+1;
-                    else if (*p<1000)
+                    else if (ch < 1000)
                         repsize += 2+3+1;
-                    else if (*p<10000)
+                    else if (ch < 10000)
                         repsize += 2+4+1;
-#ifndef Py_UNICODE_WIDE
-                    else
+                    else if (ch < 100000)
                         repsize += 2+5+1;
-#else
-                    else if (*p<100000)
-                        repsize += 2+5+1;
-                    else if (*p<1000000)
+                    else if (ch < 1000000)
                         repsize += 2+6+1;
                     else
                         repsize += 2+7+1;
-#endif
                 }
                 requiredsize = respos+repsize+(endp-collend);
                 if (requiredsize > ressize) {
@@ -3656,8 +3700,9 @@
                     ressize = requiredsize;
                 }
                 /* generate replacement (temporarily (mis)uses p) */
-                for (p = collstart; p < collend; ++p) {
-                    str += sprintf(str, "&#%d;", (int)*p);
+                for (p = collstart; p < collend;) {
+                    Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend);
+                    str += sprintf(str, "&#%d;", (int)ch);
                 }
                 p = collend;
                 break;
@@ -4632,11 +4677,20 @@
         *inpos = collendpos;
         break;
     case 4: /* xmlcharrefreplace */
-        /* generate replacement (temporarily (mis)uses p) */
-        for (collpos = collstartpos; collpos < collendpos; ++collpos) {
+        /* generate replacement */
+        for (collpos = collstartpos; collpos < collendpos;) {
             char buffer[2+29+1+1];
             char *cp;
-            sprintf(buffer, "&#%d;", (int)p[collpos]);
+            Py_UCS4 ch = p[collpos++];
+#ifndef Py_UNICODE_WIDE
+            if ((0xD800 <= ch && ch <= 0xDBFF) &&
+                (collpos < collendpos) &&
+                (0xDC00 <= p[collpos] && p[collpos] <= 0xDFFF)) {
+                ch = ((((ch & 0x03FF) << 10) |
+                       ((Py_UCS4)p[collpos++] & 0x03FF)) + 0x10000);
+            }
+#endif
+            sprintf(buffer, "&#%d;", (int)ch);
             for (cp = buffer; *cp; ++cp) {
                 x = charmapencode_output(*cp, mapping, res, respos);
                 if (x==enc_EXCEPTION)
@@ -5051,10 +5105,11 @@
                 break;
             case 4: /* xmlcharrefreplace */
                 /* generate replacement (temporarily (mis)uses p) */
-                for (p = collstart; p < collend; ++p) {
+                for (p = collstart; p < collend;) {
                     char buffer[2+29+1+1];
                     char *cp;
-                    sprintf(buffer, "&#%d;", (int)*p);
+                    Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend);
+                    sprintf(buffer, "&#%d;", (int)ch);
                     if (charmaptranslate_makespace(&res, &str,
                                                    (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))
                         goto onError;
@@ -5205,8 +5260,10 @@
             break;
         case 4: /* xmlcharrefreplace */
             /* generate replacement (temporarily (mis)uses p) */
-            for (p = collstart; p < collend; ++p)
-                output += sprintf(output, "&#%d;", (int)*p);
+            for (p = collstart; p < collend;) {
+                Py_UCS4 ch = _Py_UNICODE_NEXT(p, collend);
+                output += sprintf(output, "&#%d;", ch);
+            }
             p = collend;
             break;
         default:
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index ee6313b..46cf9b2 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -277,8 +277,11 @@
                     tok->encoding = cs;
                     tok->decoding_state = -1;
                 }
-                else
+                else {
+                    PyErr_Format(PyExc_SyntaxError,
+                                 "encoding problem: %s", cs);
                     PyMem_FREE(cs);
+                }
 #else
                 /* Without Unicode support, we cannot
                    process the coding spec. Since there
@@ -289,15 +292,12 @@
             }
         } else {                /* then, compare cs with BOM */
             r = (strcmp(tok->encoding, cs) == 0);
+            if (!r)
+                PyErr_Format(PyExc_SyntaxError,
+                             "encoding problem: %s with BOM", cs);
             PyMem_FREE(cs);
         }
     }
-    if (!r) {
-        cs = tok->encoding;
-        if (!cs)
-            cs = "with BOM";
-        PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
-    }
     return r;
 }
 
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index cdc9080..d22dca2 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -2434,9 +2434,9 @@
 PyDoc_STRVAR(sum_doc,
 "sum(sequence[, start]) -> value\n\
 \n\
-Returns the sum of a sequence of numbers (NOT strings) plus the value\n\
+Return the sum of a sequence of numbers (NOT strings) plus the value\n\
 of parameter 'start' (which defaults to 0).  When the sequence is\n\
-empty, returns start.");
+empty, return start.");
 
 
 static PyObject *
diff --git a/Python/codecs.c b/Python/codecs.c
index 7334eb3..91147a0 100644
--- a/Python/codecs.c
+++ b/Python/codecs.c
@@ -556,6 +556,7 @@
         PyObject *res;
         Py_UNICODE *p;
         Py_UNICODE *startp;
+        Py_UNICODE *e;
         Py_UNICODE *outp;
         int ressize;
         if (PyUnicodeEncodeError_GetStart(exc, &start))
@@ -565,26 +566,31 @@
         if (!(object = PyUnicodeEncodeError_GetObject(exc)))
             return NULL;
         startp = PyUnicode_AS_UNICODE(object);
-        for (p = startp+start, ressize = 0; p < startp+end; ++p) {
-            if (*p<10)
-                ressize += 2+1+1;
-            else if (*p<100)
-                ressize += 2+2+1;
-            else if (*p<1000)
-                ressize += 2+3+1;
-            else if (*p<10000)
-                ressize += 2+4+1;
+        e = startp + end;
+        for (p = startp+start, ressize = 0; p < e;) {
+            Py_UCS4 ch = *p++;
 #ifndef Py_UNICODE_WIDE
-            else
+            if ((0xD800 <= ch && ch <= 0xDBFF) &&
+                (p < e) &&
+                (0xDC00 <= *p && *p <= 0xDFFF)) {
+                ch = ((((ch & 0x03FF) << 10) |
+                       ((Py_UCS4)*p++ & 0x03FF)) + 0x10000);
+            }
+#endif
+            if (ch < 10)
+                ressize += 2+1+1;
+            else if (ch < 100)
+                ressize += 2+2+1;
+            else if (ch < 1000)
+                ressize += 2+3+1;
+            else if (ch < 10000)
+                ressize += 2+4+1;
+            else if (ch < 100000)
                 ressize += 2+5+1;
-#else
-            else if (*p<100000)
-                ressize += 2+5+1;
-            else if (*p<1000000)
+            else if (ch < 1000000)
                 ressize += 2+6+1;
             else
                 ressize += 2+7+1;
-#endif
         }
         /* allocate replacement */
         res = PyUnicode_FromUnicode(NULL, ressize);
@@ -593,40 +599,41 @@
             return NULL;
         }
         /* generate replacement */
-        for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
-            p < startp+end; ++p) {
-            Py_UNICODE c = *p;
+        for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); p < e;) {
             int digits;
             int base;
+            Py_UCS4 ch = *p++;
+#ifndef Py_UNICODE_WIDE
+            if ((0xD800 <= ch && ch <= 0xDBFF) &&
+                (p < startp+end) &&
+                (0xDC00 <= *p && *p <= 0xDFFF)) {
+                ch = ((((ch & 0x03FF) << 10) |
+                       ((Py_UCS4)*p++ & 0x03FF)) + 0x10000);
+            }
+#endif
             *outp++ = '&';
             *outp++ = '#';
-            if (*p<10) {
+            if (ch < 10) {
                 digits = 1;
                 base = 1;
             }
-            else if (*p<100) {
+            else if (ch < 100) {
                 digits = 2;
                 base = 10;
             }
-            else if (*p<1000) {
+            else if (ch < 1000) {
                 digits = 3;
                 base = 100;
             }
-            else if (*p<10000) {
+            else if (ch < 10000) {
                 digits = 4;
                 base = 1000;
             }
-#ifndef Py_UNICODE_WIDE
-            else {
+            else if (ch < 100000) {
                 digits = 5;
                 base = 10000;
             }
-#else
-            else if (*p<100000) {
-                digits = 5;
-                base = 10000;
-            }
-            else if (*p<1000000) {
+            else if (ch < 1000000) {
                 digits = 6;
                 base = 100000;
             }
@@ -634,10 +641,9 @@
                 digits = 7;
                 base = 1000000;
             }
-#endif
             while (digits-->0) {
-                *outp++ = '0' + c/base;
-                c %= base;
+                *outp++ = '0' + ch/base;
+                ch %= base;
                 base /= 10;
             }
             *outp++ = ';';
diff --git a/Python/marshal.c b/Python/marshal.c
index 33685b9..6b285aa 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -88,7 +88,7 @@
 }
 
 static void
-w_string(char *s, Py_ssize_t n, WFILE *p)
+w_string(const char *s, Py_ssize_t n, WFILE *p)
 {
     if (p->fp != NULL) {
         fwrite(s, 1, n, p->fp);
@@ -141,6 +141,13 @@
 # define W_SIZE  w_long
 #endif
 
+static void
+w_pstring(const char *s, Py_ssize_t n, WFILE *p)
+{
+        W_SIZE(n, p);
+        w_string(s, n, p);
+}
+
 /* We assume that Python longs are stored internally in base some power of
    2**15; for the sake of portability we'll always read and write them in base
    exactly 2**15. */
@@ -338,9 +345,7 @@
         else {
             w_byte(TYPE_STRING, p);
         }
-        n = PyString_GET_SIZE(v);
-        W_SIZE(n, p);
-        w_string(PyString_AS_STRING(v), n, p);
+        w_pstring(PyBytes_AS_STRING(v), PyString_GET_SIZE(v), p);
     }
 #ifdef Py_USING_UNICODE
     else if (PyUnicode_CheckExact(v)) {
@@ -352,9 +357,7 @@
             return;
         }
         w_byte(TYPE_UNICODE, p);
-        n = PyString_GET_SIZE(utf8);
-        W_SIZE(n, p);
-        w_string(PyString_AS_STRING(utf8), n, p);
+        w_pstring(PyString_AS_STRING(utf8), PyString_GET_SIZE(utf8), p);
         Py_DECREF(utf8);
     }
 #endif
@@ -441,8 +444,7 @@
         PyBufferProcs *pb = v->ob_type->tp_as_buffer;
         w_byte(TYPE_STRING, p);
         n = (*pb->bf_getreadbuffer)(v, 0, (void **)&s);
-        W_SIZE(n, p);
-        w_string(s, n, p);
+        w_pstring(s, n, p);
     }
     else {
         w_byte(TYPE_UNKNOWN, p);
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index c1c92d1..c9ed796 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -145,6 +145,7 @@
 PyThread__init_thread(void)
 {
 #if defined(_AIX) && defined(__GNUC__)
+    extern void pthread_init(void);
     pthread_init();
 #endif
 }
@@ -394,6 +395,7 @@
     pthread_lock *thelock = (pthread_lock *)lock;
     int status, error = 0;
 
+    (void) error; /* silence unused-but-set-variable warning */
     dprintf(("PyThread_free_lock(%p) called\n", lock));
 
     status = pthread_mutex_destroy( &thelock->mut );
@@ -445,6 +447,7 @@
     pthread_lock *thelock = (pthread_lock *)lock;
     int status, error = 0;
 
+    (void) error; /* silence unused-but-set-variable warning */
     dprintf(("PyThread_release_lock(%p) called\n", lock));
 
     status = pthread_mutex_lock( &thelock->mut );
diff --git a/Tools/msi/msi.py b/Tools/msi/msi.py
index d2caf34..2f0d963 100644
--- a/Tools/msi/msi.py
+++ b/Tools/msi/msi.py
@@ -1392,7 +1392,10 @@
 # certname (from config.py) should be (a substring of)
 # the certificate subject, e.g. "Python Software Foundation"
 if certname:
-    os.system('signtool sign /n "%s" /t http://timestamp.verisign.com/scripts/timestamp.dll %s' % (certname, msiname))
+    os.system('signtool sign /n "%s" '
+      '/t http://timestamp.verisign.com/scripts/timestamp.dll '
+      '/d "Python %s" '
+      '%s' % (certname, full_current_version, msiname))
 
 if pdbzip:
     build_pdbzip()
diff --git a/Tools/msi/uuids.py b/Tools/msi/uuids.py
index cf5bfd2..7c54e51 100644
--- a/Tools/msi/uuids.py
+++ b/Tools/msi/uuids.py
@@ -60,4 +60,7 @@
     '2.7.3150':'{C0C31BCC-56FB-42a7-8766-D29E1BD74C7C}', # 2.7.3
     '2.7.4121':'{47F45F45-72D7-4e54-AF41-26767EDE95CF}', # 2.7.4rc1
     '2.7.4150':'{84ADC96C-B7E0-4938-9D6E-2B640D5DA224}', # 2.7.4
+    '2.7.5150':'{DBDD570E-0952-475f-9453-AB88F3DD5659}', # 2.7.5
+    '2.7.6121':'{D1EBC07F-A7B1-4163-83DB-AE813CEF392F}', # 2.7.6rc1
+    '2.7.6150':'{C3CC4DF5-39A5-4027-B136-2B3E1F5AB6E2}', # 2.7.6
 }
diff --git a/Tools/scripts/patchcheck.py b/Tools/scripts/patchcheck.py
index fe2e291..418dd26 100755
--- a/Tools/scripts/patchcheck.py
+++ b/Tools/scripts/patchcheck.py
@@ -144,13 +144,13 @@
 @status("Misc/ACKS updated", modal=True)
 def credit_given(file_paths):
     """Check if Misc/ACKS has been changed."""
-    return 'Misc/ACKS' in file_paths
+    return os.path.join('Misc', 'ACKS') in file_paths
 
 
 @status("Misc/NEWS updated", modal=True)
 def reported_news(file_paths):
     """Check if Misc/NEWS has been changed."""
-    return 'Misc/NEWS' in file_paths
+    return os.path.join('Misc', 'NEWS') in file_paths
 
 
 def main():
@@ -158,7 +158,8 @@
     python_files = [fn for fn in file_paths if fn.endswith('.py')]
     c_files = [fn for fn in file_paths if fn.endswith(('.c', '.h'))]
     doc_files = [fn for fn in file_paths if fn.startswith('Doc')]
-    special_files = {'Misc/ACKS', 'Misc/NEWS'} & set(file_paths)
+    misc_files = {os.path.join('Misc', 'ACKS'), os.path.join('Misc', 'NEWS')}\
+            & set(file_paths)
     # PEP 8 whitespace rules enforcement.
     normalize_whitespace(python_files)
     # C rules enforcement.
@@ -168,9 +169,9 @@
     # Docs updated.
     docs_modified(doc_files)
     # Misc/ACKS changed.
-    credit_given(special_files)
+    credit_given(misc_files)
     # Misc/NEWS changed.
-    reported_news(special_files)
+    reported_news(misc_files)
 
     # Test suite run and passed.
     if python_files or c_files:
diff --git a/configure b/configure
index dc0dfd0..5f376af 100755
--- a/configure
+++ b/configure
@@ -2981,6 +2981,7 @@
 
 
 
+ARCH_RUN_32BIT=""
 
 UNIVERSAL_ARCHS="32-bit"
 
@@ -6653,7 +6654,7 @@
 sys/termio.h sys/time.h \
 sys/times.h sys/types.h sys/un.h sys/utsname.h sys/wait.h pty.h libutil.h \
 sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
-bluetooth/bluetooth.h linux/tipc.h spawn.h util.h
+bluetooth/bluetooth.h linux/tipc.h spawn.h util.h alloca.h
 do :
   as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
 ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default"
@@ -7996,7 +7997,6 @@
 esac
 
 
-ARCH_RUN_32BIT=""
 
 case $ac_sys_system/$ac_sys_release in
   Darwin/[01567]\..*)
diff --git a/configure.ac b/configure.ac
index 30f5bf4..b4ed010 100644
--- a/configure.ac
+++ b/configure.ac
@@ -145,6 +145,7 @@
 AC_SUBST(UNIVERSALSDK)
 
 AC_SUBST(ARCH_RUN_32BIT)
+ARCH_RUN_32BIT=""
 
 UNIVERSAL_ARCHS="32-bit"
 AC_SUBST(LIPO_32BIT_FLAGS)
@@ -1518,7 +1519,7 @@
 sys/termio.h sys/time.h \
 sys/times.h sys/types.h sys/un.h sys/utsname.h sys/wait.h pty.h libutil.h \
 sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
-bluetooth/bluetooth.h linux/tipc.h spawn.h util.h)
+bluetooth/bluetooth.h linux/tipc.h spawn.h util.h alloca.h)
 AC_HEADER_DIRENT
 AC_HEADER_MAJOR
 
@@ -1801,7 +1802,6 @@
 esac
 
 
-ARCH_RUN_32BIT=""
 AC_SUBST(LIBTOOL_CRUFT)
 case $ac_sys_system/$ac_sys_release in
   Darwin/@<:@01567@:>@\..*) 
diff --git a/pyconfig.h.in b/pyconfig.h.in
index 231b9c8..65df68a 100644
--- a/pyconfig.h.in
+++ b/pyconfig.h.in
@@ -55,6 +55,9 @@
 /* Define to 1 if you have the `alarm' function. */
 #undef HAVE_ALARM
 
+/* Define to 1 if you have the <alloca.h> header file. */
+#undef HAVE_ALLOCA_H
+
 /* Define this if your time.h defines altzone. */
 #undef HAVE_ALTZONE