]> granicus.if.org Git - python/commitdiff
Issue #20037: Avoid crashes when doing text I/O late at interpreter shutdown.
authorAntoine Pitrou <solipsis@pitrou.net>
Sat, 21 Dec 2013 14:51:54 +0000 (15:51 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sat, 21 Dec 2013 14:51:54 +0000 (15:51 +0100)
Lib/test/test_io.py
Lib/test/test_logging.py
Misc/NEWS
Modules/_io/_iomodule.c
Modules/_io/_iomodule.h
Modules/_io/bufferedio.c
Modules/_io/fileio.c
Modules/_io/iobase.c
Modules/_io/textio.c

index 344425b772dfa30a77f8465aa9a7191c745814d5..355a33e48e853afad1fcb37e5a3a6175858c46ec 100644 (file)
@@ -36,6 +36,7 @@ import _testcapi
 from collections import deque, UserList
 from itertools import cycle, count
 from test import support
+from test.script_helper import assert_python_ok
 
 import codecs
 import io  # C implementation of io
@@ -2589,8 +2590,46 @@ class TextIOWrapperTest(unittest.TestCase):
                                encoding='quopri_codec')
         self.assertRaises(TypeError, t.read)
 
+    def _check_create_at_shutdown(self, **kwargs):
+        # Issue #20037: creating a TextIOWrapper at shutdown
+        # shouldn't crash the interpreter.
+        iomod = self.io.__name__
+        code = """if 1:
+            import codecs
+            import {iomod} as io
+
+            # Avoid looking up codecs at shutdown
+            codecs.lookup('utf-8')
+
+            class C:
+                def __init__(self):
+                    self.buf = io.BytesIO()
+                def __del__(self):
+                    io.TextIOWrapper(self.buf, **{kwargs})
+                    print("ok")
+            c = C()
+            """.format(iomod=iomod, kwargs=kwargs)
+        return assert_python_ok("-c", code)
+
+    def test_create_at_shutdown_without_encoding(self):
+        rc, out, err = self._check_create_at_shutdown()
+        if err:
+            # Can error out with a RuntimeError if the module state
+            # isn't found.
+            self.assertIn("RuntimeError: could not find io module state",
+                          err.decode())
+        else:
+            self.assertEqual("ok", out.decode().strip())
+
+    def test_create_at_shutdown_with_encoding(self):
+        rc, out, err = self._check_create_at_shutdown(encoding='utf-8',
+                                                      errors='strict')
+        self.assertFalse(err)
+        self.assertEqual("ok", out.decode().strip())
+
 
 class CTextIOWrapperTest(TextIOWrapperTest):
+    io = io
 
     def test_initialization(self):
         r = self.BytesIO(b"\xc3\xa9\n\n")
@@ -2634,7 +2673,7 @@ class CTextIOWrapperTest(TextIOWrapperTest):
 
 
 class PyTextIOWrapperTest(TextIOWrapperTest):
-    pass
+    io = pyio
 
 
 class IncrementalNewlineDecoderTest(unittest.TestCase):
index ce3f84cf89d772c8e151f4273f3697de92d34004..3765f36aa6d5c49d1ce6cf284786fd9730c4793d 100644 (file)
@@ -41,6 +41,7 @@ import socket
 import struct
 import sys
 import tempfile
+from test.script_helper import assert_python_ok
 from test.support import (captured_stdout, run_with_locale, run_unittest,
                           patch, requires_zlib, TestHandler, Matcher)
 import textwrap
@@ -3397,6 +3398,25 @@ class ModuleLevelMiscTest(BaseTest):
         logging.setLoggerClass(logging.Logger)
         self.assertEqual(logging.getLoggerClass(), logging.Logger)
 
+    def test_logging_at_shutdown(self):
+        # Issue #20037
+        code = """if 1:
+            import logging
+
+            class A:
+                def __del__(self):
+                    try:
+                        raise ValueError("some error")
+                    except Exception:
+                        logging.exception("exception in __del__")
+
+            a = A()"""
+        rc, out, err = assert_python_ok("-c", code)
+        err = err.decode()
+        self.assertIn("exception in __del__", err)
+        self.assertIn("ValueError: some error", err)
+
+
 class LogRecordTest(BaseTest):
     def test_str_rep(self):
         r = logging.makeLogRecord({})
index c38a8c474d4b9f2f5f94146bdd5fdd944e87b9d0..b84b3cc4ab38574a58be7efc72b754cfca816321 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -44,6 +44,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #20037: Avoid crashes when opening a text file late at interpreter
+  shutdown.
+
 - Issue #19967: Thanks to the PEP 442, asyncio.Future now uses a
   destructor to log uncaught exceptions, instead of the dedicated
   _TracebackLogger class.
index 9866fbe82be949912921fc0a83152a4079e2250e..be0464c4fa33933578386d974eae754052a99cab 100644 (file)
@@ -539,6 +539,20 @@ _PyIO_ConvertSsize_t(PyObject *obj, void *result) {
 }
 
 
+_PyIO_State *
+_PyIO_get_module_state(void)
+{
+    PyObject *mod = PyState_FindModule(&_PyIO_Module);
+    _PyIO_State *state;
+    if (mod == NULL || (state = IO_MOD_STATE(mod)) == NULL) {
+        PyErr_SetString(PyExc_RuntimeError,
+                        "could not find io module state "
+                        "(interpreter shutdown?)");
+        return NULL;
+    }
+    return state;
+}
+
 PyObject *
 _PyIO_get_locale_module(_PyIO_State *state)
 {
index b90a658397dee2ec769a126e30c0e36fc76a4a52..8927864e8c002d2a2593287beb362a79c55e4898 100644 (file)
@@ -135,8 +135,9 @@ typedef struct {
 } _PyIO_State;
 
 #define IO_MOD_STATE(mod) ((_PyIO_State *)PyModule_GetState(mod))
-#define IO_STATE IO_MOD_STATE(PyState_FindModule(&_PyIO_Module))
+#define IO_STATE() _PyIO_get_module_state()
 
+extern _PyIO_State *_PyIO_get_module_state(void);
 extern PyObject *_PyIO_get_locale_module(_PyIO_State *);
 
 extern PyObject *_PyIO_str_close;
index a04b48dd3a98be781973901f8f791deba130969c..34c2bb97bd670cb8026fca8062eb173aa6cde9fa 100644 (file)
@@ -91,7 +91,9 @@ bufferediobase_readinto(PyObject *self, PyObject *args)
 static PyObject *
 bufferediobase_unsupported(const char *message)
 {
-    PyErr_SetString(IO_STATE->unsupported_operation, message);
+    _PyIO_State *state = IO_STATE();
+    if (state != NULL)
+        PyErr_SetString(state->unsupported_operation, message);
     return NULL;
 }
 
index 0e1e709efd9456a8ed2dfabbe8846803470be70e..cbb2daf5c291af71a4a82a410ef0ee21788d4415 100644 (file)
@@ -493,8 +493,10 @@ err_closed(void)
 static PyObject *
 err_mode(char *action)
 {
-    PyErr_Format(IO_STATE->unsupported_operation,
-                 "File not open for %s", action);
+    _PyIO_State *state = IO_STATE();
+    if (state != NULL)
+        PyErr_Format(state->unsupported_operation,
+                     "File not open for %s", action);
     return NULL;
 }
 
index 1b7cb0ff702793487f7f4b52cce6b2be7f057dcb..e3729902d88f78d79791d4e54d4d23bf0cee10e9 100644 (file)
@@ -69,7 +69,9 @@ _Py_IDENTIFIER(read);
 static PyObject *
 iobase_unsupported(const char *message)
 {
-    PyErr_SetString(IO_STATE->unsupported_operation, message);
+    _PyIO_State *state = IO_STATE();
+    if (state != NULL)
+        PyErr_SetString(state->unsupported_operation, message);
     return NULL;
 }
 
index fb89a17927cdd65ba33c80bbcad65d5249f99ea8..747f62323cbc16d3e2bcb1dfc4f1e17c48a6ce8c 100644 (file)
@@ -45,7 +45,9 @@ PyDoc_STRVAR(textiobase_doc,
 static PyObject *
 _unsupported(const char *message)
 {
-    PyErr_SetString(IO_STATE->unsupported_operation, message);
+    _PyIO_State *state = IO_STATE();
+    if (state != NULL)
+        PyErr_SetString(state->unsupported_operation, message);
     return NULL;
 }
 
@@ -852,7 +854,7 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
     char *errors = NULL;
     char *newline = NULL;
     int line_buffering = 0, write_through = 0;
-    _PyIO_State *state = IO_STATE;
+    _PyIO_State *state = NULL;
 
     PyObject *res;
     int r;
@@ -891,6 +893,9 @@ textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
     if (encoding == NULL) {
         /* Try os.device_encoding(fileno) */
         PyObject *fileno;
+        state = IO_STATE();
+        if (state == NULL)
+            goto error;
         fileno = _PyObject_CallMethodId(buffer, &PyId_fileno, NULL);
         /* Ignore only AttributeError and UnsupportedOperation */
         if (fileno == NULL) {