Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fade04e

Browse filesBrowse files
[3.14] gh-133982: Run unclosed file test on all io implementations (gh-134165) (gh-134433)
Update `test_io` `_check_warn_on_dealloc` to use `self.` to dispatch to different I/O implementations. Update the `_pyio` implementation to match expected behavior, using the same `_dealloc_warn` design as the C implementation uses to report the topmost `__del__` object. The FileIO one now matches all the others, so can use IOBase. There was a missing check on closing (self._fd must be valid), add that check (cherry picked from commit 5b0e827) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
1 parent 74dde92 commit fade04e
Copy full SHA for fade04e

File tree

Expand file treeCollapse file tree

3 files changed

+19
-6
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+19
-6
lines changed

‎Lib/_pyio.py

Copy file name to clipboardExpand all lines: Lib/_pyio.py
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,9 @@ def __del__(self):
407407
if closed:
408408
return
409409

410+
if dealloc_warn := getattr(self, "_dealloc_warn", None):
411+
dealloc_warn(self)
412+
410413
# If close() fails, the caller logs the exception with
411414
# sys.unraisablehook. close() must be called at the end at __del__().
412415
self.close()
@@ -853,6 +856,10 @@ def __repr__(self):
853856
else:
854857
return "<{}.{} name={!r}>".format(modname, clsname, name)
855858

859+
def _dealloc_warn(self, source):
860+
if dealloc_warn := getattr(self.raw, "_dealloc_warn", None):
861+
dealloc_warn(source)
862+
856863
### Lower-level APIs ###
857864

858865
def fileno(self):
@@ -1601,12 +1608,11 @@ def __init__(self, file, mode='r', closefd=True, opener=None):
16011608
raise
16021609
self._fd = fd
16031610

1604-
def __del__(self):
1611+
def _dealloc_warn(self, source):
16051612
if self._fd >= 0 and self._closefd and not self.closed:
16061613
import warnings
1607-
warnings.warn('unclosed file %r' % (self,), ResourceWarning,
1614+
warnings.warn(f'unclosed file {source!r}', ResourceWarning,
16081615
stacklevel=2, source=self)
1609-
self.close()
16101616

16111617
def __getstate__(self):
16121618
raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
@@ -1781,7 +1787,7 @@ def close(self):
17811787
if not self.closed:
17821788
self._stat_atopen = None
17831789
try:
1784-
if self._closefd:
1790+
if self._closefd and self._fd >= 0:
17851791
os.close(self._fd)
17861792
finally:
17871793
super().close()
@@ -2690,6 +2696,10 @@ def readline(self, size=None):
26902696
def newlines(self):
26912697
return self._decoder.newlines if self._decoder else None
26922698

2699+
def _dealloc_warn(self, source):
2700+
if dealloc_warn := getattr(self.buffer, "_dealloc_warn", None):
2701+
dealloc_warn(source)
2702+
26932703

26942704
class StringIO(TextIOWrapper):
26952705
"""Text I/O implementation using an in-memory buffer.

‎Lib/test/test_io.py

Copy file name to clipboardExpand all lines: Lib/test/test_io.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4417,7 +4417,7 @@ def test_abc_inheritance_official(self):
44174417
self._check_abc_inheritance(io)
44184418

44194419
def _check_warn_on_dealloc(self, *args, **kwargs):
4420-
f = open(*args, **kwargs)
4420+
f = self.open(*args, **kwargs)
44214421
r = repr(f)
44224422
with self.assertWarns(ResourceWarning) as cm:
44234423
f = None
@@ -4446,7 +4446,7 @@ def cleanup_fds():
44464446
r, w = os.pipe()
44474447
fds += r, w
44484448
with warnings_helper.check_no_resource_warning(self):
4449-
open(r, *args, closefd=False, **kwargs)
4449+
self.open(r, *args, closefd=False, **kwargs)
44504450

44514451
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
44524452
def test_warn_on_dealloc_fd(self):
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Emit :exc:`RuntimeWarning` in the Python implementation of :mod:`io` when
2+
the :term:`file-like object <file object>` is not closed explicitly in the
3+
presence of multiple I/O layers.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.