diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 265e286101..121aef4401 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1148,6 +1148,9 @@ def __init__(self, filename, mode='a', encoding=None, delay=False, errors=None): self.encoding = encoding self.errors = errors self.delay = delay + # bpo-39513: Keep a reference to the builtin open() function to be + # able to call _open() at Python exit (during the Python finalization). + self._builtin_open = open if delay: #We don't open the stream, but we still need to call the #Handler constructor to set level, formatter, lock etc. @@ -1183,8 +1186,9 @@ def _open(self): Open the current base file with the (original) mode and encoding. Return the resulting stream. """ - return open(self.baseFilename, self.mode, encoding=self.encoding, - errors=self.errors) + open_func = self._builtin_open + return open_func(self.baseFilename, self.mode, + encoding=self.encoding, errors=self.errors) def emit(self, record): """