logging: close log_file_handler
While it should be closed in logging's shutdown [1], the following would
still issue a ResourceWarning:
```
import logging
log_file_handler = logging.FileHandler("temp.log", mode="w", encoding="UTF-8")
root_logger = logging.getLogger()
root_logger.addHandler(log_file_handler)
root_logger.removeHandler(log_file_handler)
root_logger.error("error")
del log_file_handler
```
It looks like the weakref might get lost for some reason.
See https://github.com/pytest-dev/pytest/pull/4981/commits/92ffe42b45 / #4981
for more information.
1: c1419578a1/Lib/logging/__init__.py (L2107-L2139)
This commit is contained in:
parent
ee96214a8d
commit
538efef1ba
|
@ -0,0 +1 @@
|
||||||
|
Close logging's file handler explicitly when the session finishes.
|
|
@ -577,8 +577,15 @@ class LoggingPlugin(object):
|
||||||
if self.log_cli_handler:
|
if self.log_cli_handler:
|
||||||
self.log_cli_handler.set_when("sessionfinish")
|
self.log_cli_handler.set_when("sessionfinish")
|
||||||
if self.log_file_handler is not None:
|
if self.log_file_handler is not None:
|
||||||
with catching_logs(self.log_file_handler, level=self.log_file_level):
|
try:
|
||||||
yield
|
with catching_logs(
|
||||||
|
self.log_file_handler, level=self.log_file_level
|
||||||
|
):
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
# Close the FileHandler explicitly.
|
||||||
|
# (logging.shutdown might have lost the weakref?!)
|
||||||
|
self.log_file_handler.close()
|
||||||
else:
|
else:
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue