Branch merge
diff --git a/Doc/library/http.client.rst b/Doc/library/http.client.rst
index 9ef7956..9b1ab0f 100644
--- a/Doc/library/http.client.rst
+++ b/Doc/library/http.client.rst
@@ -475,11 +475,10 @@
 .. method:: HTTPConnection.endheaders(message_body=None)
 
    Send a blank line to the server, signalling the end of the headers. The
-   optional message_body argument can be used to pass message body
-   associated with the request.  The message body will be sent in
-   the same packet as the message headers if possible.  The
-   message_body should be a string.
-
+   optional *message_body* argument can be used to pass a message body
+   associated with the request.  The message body will be sent in the same
+   packet as the message headers if it is string, otherwise it is sent in a
+   separate packet.
 
 .. method:: HTTPConnection.send(data)
 
diff --git a/Lib/http/client.py b/Lib/http/client.py
index 3f02729..8400914 100644
--- a/Lib/http/client.py
+++ b/Lib/http/client.py
@@ -947,11 +947,11 @@
     def endheaders(self, message_body=None):
         """Indicate that the last header line has been sent to the server.
 
-        This method sends the request to the server.  The optional
-        message_body argument can be used to pass message body
-        associated with the request.  The message body will be sent in
-        the same packet as the message headers if possible.  The
-        message_body should be a string.
+        This method sends the request to the server.  The optional message_body
+        argument can be used to pass a message body associated with the
+        request.  The message body will be sent in the same packet as the
+        message headers if it is a string, otherwise it is sent as a separate
+        packet.
         """
         if self.__state == _CS_REQ_STARTED:
             self.__state = _CS_REQ_SENT
diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py
index 98b7351..7c54032 100644
--- a/Lib/test/test_import.py
+++ b/Lib/test/test_import.py
@@ -4,6 +4,7 @@
 from importlib.test.import_ import util as importlib_util
 import marshal
 import os
+import platform
 import py_compile
 import random
 import stat
@@ -546,6 +547,8 @@
 
     @unittest.skipUnless(os.name == 'posix',
                          "test meaningful only on posix systems")
+    @unittest.skipIf(hasattr(os, 'geteuid') and os.geteuid() == 0,
+            "due to varying filesystem permission semantics (issue #11956)")
     def test_unwritable_directory(self):
         # When the umask causes the new __pycache__ directory to be
         # unwritable, the import still succeeds but no .pyc file is written.
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 53cabbb..eb2ac5f 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -2414,6 +2414,21 @@
         with self.open(support.TESTFN, "rb") as f:
             self.assertEqual(f.read(), b"456def")
 
+    def test_rwpair_cleared_before_textio(self):
+        # Issue 13070: TextIOWrapper's finalization would crash when called
+        # after the reference to the underlying BufferedRWPair's writer got
+        # cleared by the GC.
+        for i in range(1000):
+            b1 = self.BufferedRWPair(self.MockRawIO(), self.MockRawIO())
+            t1 = self.TextIOWrapper(b1, encoding="ascii")
+            b2 = self.BufferedRWPair(self.MockRawIO(), self.MockRawIO())
+            t2 = self.TextIOWrapper(b2, encoding="ascii")
+            # circular references
+            t1.buddy = t2
+            t2.buddy = t1
+        support.gc_collect()
+
+
 class PyTextIOWrapperTest(TextIOWrapperTest):
     pass
 
diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py
index 43134e9..cf3976c 100644
--- a/Lib/test/test_sys_settrace.py
+++ b/Lib/test/test_sys_settrace.py
@@ -282,11 +282,11 @@
         self.compare_events(func.__code__.co_firstlineno,
                             tracer.events, func.events)
 
-    def set_and_retrieve_none(self):
+    def test_set_and_retrieve_none(self):
         sys.settrace(None)
         assert sys.gettrace() is None
 
-    def set_and_retrieve_func(self):
+    def test_set_and_retrieve_func(self):
         def fn(*args):
             pass
 
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index c6dae17..8055dc2 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -1058,6 +1058,10 @@
         self.assertEqual(('user', 'a\vb'),urllib.parse.splitpasswd('user:a\vb'))
         self.assertEqual(('user', 'a:b'),urllib.parse.splitpasswd('user:a:b'))
 
+    def test_thishost(self):
+        """Test the urllib.request.thishost utility function returns a tuple"""
+        self.assertIsInstance(urllib.request.thishost(), tuple)
+
 
 class URLopener_Tests(unittest.TestCase):
     """Testcase to test the open method of URLopener class."""
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 773025a..d3b5c29 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -2116,7 +2116,7 @@
     """Return the IP addresses of the current host."""
     global _thishost
     if _thishost is None:
-        _thishost = tuple(socket.gethostbyname_ex(socket.gethostname()[2]))
+        _thishost = tuple(socket.gethostbyname_ex(socket.gethostname())[2])
     return _thishost
 
 _ftperrors = None
diff --git a/Misc/NEWS b/Misc/NEWS
index 6b558b4..c4aba0d 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,10 @@
 Extension Modules
 -----------------
 
+- Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
+  would be finalized after the reference to its underlying BufferedRWPair's
+  writer got cleared by the GC.
+
 - Issue #12881: ctypes: Fix segfault with large structure field names.
 
 - Issue #13058: ossaudiodev: fix a file descriptor leak on error. Patch by
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index bd1aae5..c979ac2 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -2212,6 +2212,11 @@
 static PyObject *
 bufferedrwpair_closed_get(rwpair *self, void *context)
 {
+    if (self->writer == NULL) {
+        PyErr_SetString(PyExc_RuntimeError,
+                "the BufferedRWPair object is being garbage-collected");
+        return NULL;
+    }
     return PyObject_GetAttr((PyObject *) self->writer, _PyIO_str_closed);
 }