fix: workaround method calls on attributes set to None (#4812)

This commit is contained in:
Gil Forsyth 2022-05-20 13:15:15 -04:00 committed by GitHub
parent 57f1e4bcbd
commit 400d2d6c56
Failed to generate hash of commit
2 changed files with 27 additions and 2 deletions

23
news/fix_exit_error.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* Silence spurious errors on exit due to out-of-order cleanup
**Security:**
* <news item>

View file

@ -191,8 +191,10 @@ class _TeeStd(io.TextIOBase):
def flush(self):
"""Flushes both the original stdout and the buffer."""
self.std.flush()
self.mem.flush()
# In certain cases, `std` or `mem` may already be `None` when we're
# cleaning up in which case we don't care whether `flush` does anything
getattr(getattr(self, "std", lambda: None), "flush", lambda: None)()
getattr(getattr(self, "mem", lambda: None), "flush", lambda: None)()
def fileno(self):
"""Tunnel fileno() calls to the std stream."""