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

Skip to content

Commit fb2c346

Browse files
authored
asyncio: __del__() keep reference to warnings.warn (pythonGH-11491)
* asyncio: __del__() keep reference to warnings.warn The __del__() methods of asyncio classes now keep a strong reference to the warnings.warn() to be able to display the ResourceWarning warning in more cases. Ensure that the function remains available if instances are destroyed late during Python shutdown (while module symbols are cleared). * Rename warn parameter to _warn "_warn" name is a hint that it's not the regular warnings.warn() function.
1 parent 9b07681 commit fb2c346

7 files changed

+16
-24
lines changed

Lib/asyncio/base_events.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,9 @@ def is_closed(self):
622622
"""Returns True if the event loop was closed."""
623623
return self._closed
624624

625-
def __del__(self):
625+
def __del__(self, _warn=warnings.warn):
626626
if not self.is_closed():
627-
warnings.warn(f"unclosed event loop {self!r}", ResourceWarning,
628-
source=self)
627+
_warn(f"unclosed event loop {self!r}", ResourceWarning, source=self)
629628
if not self.is_running():
630629
self.close()
631630

Lib/asyncio/base_subprocess.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,9 @@ def close(self):
120120

121121
# Don't clear the _proc reference yet: _post_init() may still run
122122

123-
def __del__(self):
123+
def __del__(self, _warn=warnings.warn):
124124
if not self._closed:
125-
warnings.warn(f"unclosed transport {self!r}", ResourceWarning,
126-
source=self)
125+
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
127126
self.close()
128127

129128
def get_pid(self):

Lib/asyncio/proactor_events.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,9 @@ def close(self):
8989
self._read_fut.cancel()
9090
self._read_fut = None
9191

92-
def __del__(self):
92+
def __del__(self, _warn=warnings.warn):
9393
if self._sock is not None:
94-
warnings.warn(f"unclosed transport {self!r}", ResourceWarning,
95-
source=self)
94+
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
9695
self.close()
9796

9897
def _fatal_error(self, exc, message='Fatal error on pipe transport'):

Lib/asyncio/selector_events.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,9 @@ def close(self):
658658
self._loop._remove_writer(self._sock_fd)
659659
self._loop.call_soon(self._call_connection_lost, None)
660660

661-
def __del__(self):
661+
def __del__(self, _warn=warnings.warn):
662662
if self._sock is not None:
663-
warnings.warn(f"unclosed transport {self!r}", ResourceWarning,
664-
source=self)
663+
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
665664
self._sock.close()
666665

667666
def _fatal_error(self, exc, message='Fatal error on transport'):

Lib/asyncio/sslproto.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,9 @@ def close(self):
316316
self._closed = True
317317
self._ssl_protocol._start_shutdown()
318318

319-
def __del__(self):
319+
def __del__(self, _warn=warnings.warn):
320320
if not self._closed:
321-
warnings.warn(f"unclosed transport {self!r}", ResourceWarning,
322-
source=self)
321+
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
323322
self.close()
324323

325324
def is_reading(self):

Lib/asyncio/unix_events.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,9 @@ def close(self):
511511
if not self._closing:
512512
self._close(None)
513513

514-
def __del__(self):
514+
def __del__(self, _warn=warnings.warn):
515515
if self._pipe is not None:
516-
warnings.warn(f"unclosed transport {self!r}", ResourceWarning,
517-
source=self)
516+
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
518517
self._pipe.close()
519518

520519
def _fatal_error(self, exc, message='Fatal error on pipe transport'):
@@ -707,10 +706,9 @@ def close(self):
707706
# write_eof is all what we needed to close the write pipe
708707
self.write_eof()
709708

710-
def __del__(self):
709+
def __del__(self, _warn=warnings.warn):
711710
if self._pipe is not None:
712-
warnings.warn(f"unclosed transport {self!r}", ResourceWarning,
713-
source=self)
711+
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
714712
self._pipe.close()
715713

716714
def abort(self):

Lib/asyncio/windows_utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ def close(self, *, CloseHandle=_winapi.CloseHandle):
107107
CloseHandle(self._handle)
108108
self._handle = None
109109

110-
def __del__(self):
110+
def __del__(self, _warn=warnings.warn):
111111
if self._handle is not None:
112-
warnings.warn(f"unclosed {self!r}", ResourceWarning,
113-
source=self)
112+
_warn(f"unclosed {self!r}", ResourceWarning, source=self)
114113
self.close()
115114

116115
def __enter__(self):

0 commit comments

Comments
 (0)