Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45df61f

Browse files
authored
bpo-26789: Fix logging.FileHandler._open() at exit (GH-23053)
The logging.FileHandler class now keeps a reference to the builtin open() function to be able to open or reopen the file during Python finalization. Fix errors like: Exception ignored in: (...) Traceback (most recent call last): (...) File ".../logging/__init__.py", line 1463, in error File ".../logging/__init__.py", line 1577, in _log File ".../logging/__init__.py", line 1587, in handle File ".../logging/__init__.py", line 1649, in callHandlers File ".../logging/__init__.py", line 948, in handle File ".../logging/__init__.py", line 1182, in emit File ".../logging/__init__.py", line 1171, in _open NameError: name 'open' is not defined
1 parent 5cf4782 commit 45df61f

4 files changed

Lines changed: 50 additions & 8 deletions

File tree

Lib/logging/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,10 @@ def __init__(self, filename, mode='a', encoding=None, delay=False, errors=None):
11481148
self.encoding = encoding
11491149
self.errors = errors
11501150
self.delay = delay
1151+
# bpo-26789: FileHandler keeps a reference to the builtin open()
1152+
# function to be able to open or reopen the file during Python
1153+
# finalization.
1154+
self._builtin_open = open
11511155
if delay:
11521156
#We don't open the stream, but we still need to call the
11531157
#Handler constructor to set level, formatter, lock etc.
@@ -1183,8 +1187,9 @@ def _open(self):
11831187
Open the current base file with the (original) mode and encoding.
11841188
Return the resulting stream.
11851189
"""
1186-
return open(self.baseFilename, self.mode, encoding=self.encoding,
1187-
errors=self.errors)
1190+
open_func = self._builtin_open
1191+
return open_func(self.baseFilename, self.mode,
1192+
encoding=self.encoding, errors=self.errors)
11881193

11891194
def emit(self, record):
11901195
"""

Lib/test/test_logging.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4310,8 +4310,8 @@ def __init__(self, name='MyLogger', level=logging.NOTSET):
43104310
logging.setLoggerClass(logging.Logger)
43114311

43124312
def test_logging_at_shutdown(self):
4313-
# Issue #20037
4314-
code = """if 1:
4313+
# bpo-20037: Doing text I/O late at interpreter shutdown must not crash
4314+
code = textwrap.dedent("""
43154315
import logging
43164316
43174317
class A:
@@ -4321,22 +4321,55 @@ def __del__(self):
43214321
except Exception:
43224322
logging.exception("exception in __del__")
43234323
4324-
a = A()"""
4324+
a = A()
4325+
""")
43254326
rc, out, err = assert_python_ok("-c", code)
43264327
err = err.decode()
43274328
self.assertIn("exception in __del__", err)
43284329
self.assertIn("ValueError: some error", err)
43294330

4331+
def test_logging_at_shutdown_open(self):
4332+
# bpo-26789: FileHandler keeps a reference to the builtin open()
4333+
# function to be able to open or reopen the file during Python
4334+
# finalization.
4335+
filename = os_helper.TESTFN
4336+
self.addCleanup(os_helper.unlink, filename)
4337+
4338+
code = textwrap.dedent(f"""
4339+
import builtins
4340+
import logging
4341+
4342+
class A:
4343+
def __del__(self):
4344+
logging.error("log in __del__")
4345+
4346+
# basicConfig() opens the file, but logging.shutdown() closes
4347+
# it at Python exit. When A.__del__() is called,
4348+
# FileHandler._open() must be called again to re-open the file.
4349+
logging.basicConfig(filename={filename!r})
4350+
4351+
a = A()
4352+
4353+
# Simulate the Python finalization which removes the builtin
4354+
# open() function.
4355+
del builtins.open
4356+
""")
4357+
assert_python_ok("-c", code)
4358+
4359+
with open(filename) as fp:
4360+
self.assertEqual(fp.read().rstrip(), "ERROR:root:log in __del__")
4361+
43304362
def test_recursion_error(self):
43314363
# Issue 36272
4332-
code = """if 1:
4364+
code = textwrap.dedent("""
43334365
import logging
43344366
43354367
def rec():
43364368
logging.error("foo")
43374369
rec()
43384370
4339-
rec()"""
4371+
rec()
4372+
""")
43404373
rc, out, err = assert_python_failure("-c", code)
43414374
err = err.decode()
43424375
self.assertNotIn("Cannot recover from stack overflow.", err)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The :class:`logging.FileHandler` class now keeps a reference to the builtin
2+
:func:`open` function to be able to open or reopen the file during Python
3+
finalization. Fix errors like: ``NameError: name 'open' is not defined``. Patch
4+
by Victor Stinner.

Python/_warnings.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
852852
}
853853

854854
if (f == NULL) {
855-
globals = _PyInterpreterState_GET()->sysdict;
855+
globals = tstate->interp->sysdict;
856856
*filename = PyUnicode_FromString("sys");
857857
*lineno = 1;
858858
}

0 commit comments

Comments
 (0)