Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[3.14] gh-133982: Run unclosed file test on all io implementations (gh-134165)#134433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ambv merged 1 commit intopython:3.14frommiss-islington:backport-5b0e827-3.14
May 21, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletionsLib/_pyio.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -407,6 +407,9 @@ def __del__(self):
if closed:
return

if dealloc_warn := getattr(self, "_dealloc_warn", None):
dealloc_warn(self)

# If close() fails, the caller logs the exception with
# sys.unraisablehook. close() must be called at the end at __del__().
self.close()
Expand DownExpand Up@@ -853,6 +856,10 @@ def __repr__(self):
else:
return "<{}.{} name={!r}>".format(modname, clsname, name)

def _dealloc_warn(self, source):
if dealloc_warn := getattr(self.raw, "_dealloc_warn", None):
dealloc_warn(source)

### Lower-level APIs ###

def fileno(self):
Expand DownExpand Up@@ -1600,12 +1607,11 @@ def __init__(self, file, mode='r', closefd=True, opener=None):
raise
self._fd = fd

def__del__(self):
def_dealloc_warn(self, source):
if self._fd >= 0 and self._closefd and not self.closed:
import warnings
warnings.warn('unclosed file%r' % (self,), ResourceWarning,
warnings.warn(f'unclosed file{source!r}', ResourceWarning,
stacklevel=2, source=self)
self.close()

def __getstate__(self):
raise TypeError(f"cannot pickle {self.__class__.__name__!r} object")
Expand DownExpand Up@@ -1780,7 +1786,7 @@ def close(self):
if not self.closed:
self._stat_atopen = None
try:
if self._closefd:
if self._closefd and self._fd >= 0:
os.close(self._fd)
finally:
super().close()
Expand DownExpand Up@@ -2689,6 +2695,10 @@ def readline(self, size=None):
def newlines(self):
return self._decoder.newlines if self._decoder else None

def _dealloc_warn(self, source):
if dealloc_warn := getattr(self.buffer, "_dealloc_warn", None):
dealloc_warn(source)


class StringIO(TextIOWrapper):
"""Text I/O implementation using an in-memory buffer.
Expand Down
4 changes: 2 additions & 2 deletionsLib/test/test_io.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4417,7 +4417,7 @@ def test_abc_inheritance_official(self):
self._check_abc_inheritance(io)

def _check_warn_on_dealloc(self, *args, **kwargs):
f = open(*args, **kwargs)
f =self.open(*args, **kwargs)
r = repr(f)
with self.assertWarns(ResourceWarning) as cm:
f = None
Expand DownExpand Up@@ -4446,7 +4446,7 @@ def cleanup_fds():
r, w = os.pipe()
fds += r, w
with warnings_helper.check_no_resource_warning(self):
open(r, *args, closefd=False, **kwargs)
self.open(r, *args, closefd=False, **kwargs)

@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
def test_warn_on_dealloc_fd(self):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
Emit :exc:`RuntimeWarning` in the Python implementation of :mod:`io` when
the :term:`file-like object <file object>` is not closed explicitly in the
presence of multiple I/O layers.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp