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

Skip to content

GH-83863: Drop support for using pathlib.Path objects as context managers #104807

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ Removed
are now removed. The items in those namespaces can be imported directly
from :mod:`typing`. (Contributed by Sebastian Rittau in :gh:`92871`.)

* Remove support for using :class:`pathlib.Path` objects as context managers.
This functionality was deprecated and made a no-op in Python 3.9.

Porting to Python 3.13
======================

Expand Down
19 changes: 0 additions & 19 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1080,25 +1080,6 @@ def __new__(cls, *args, **kwargs):
cls = WindowsPath if os.name == 'nt' else PosixPath
return object.__new__(cls)

def __enter__(self):
# In previous versions of pathlib, __exit__() marked this path as
# closed; subsequent attempts to perform I/O would raise an IOError.
# This functionality was never documented, and had the effect of
# making Path objects mutable, contrary to PEP 428.
# In Python 3.9 __exit__() was made a no-op.
# In Python 3.11 __enter__() began emitting DeprecationWarning.
# In Python 3.13 __enter__() and __exit__() should be removed.
warnings.warn("pathlib.Path.__enter__() is deprecated and scheduled "
"for removal in Python 3.13; Path objects as a context "
"manager is a no-op",
DeprecationWarning, stacklevel=2)
return self

def __exit__(self, t, v, tb):
pass

# Public API

@classmethod
def cwd(cls):
"""Return a new path pointing to the current working directory."""
Expand Down
20 changes: 0 additions & 20 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2080,26 +2080,6 @@ def test_resolve_nonexist_relative_issue38671(self):
finally:
os.chdir(old_cwd)

def test_with(self):
p = self.cls(BASE)
it = p.iterdir()
it2 = p.iterdir()
next(it2)
# bpo-46556: path context managers are deprecated in Python 3.11.
with self.assertWarns(DeprecationWarning):
with p:
pass
# Using a path as a context manager is a no-op, thus the following
# operations should still succeed after the context manage exits.
next(it)
next(it2)
p.exists()
p.resolve()
p.absolute()
with self.assertWarns(DeprecationWarning):
with p:
pass

@os_helper.skip_unless_working_chmod
def test_chmod(self):
p = self.cls(BASE) / 'fileA'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Support for using :class:`pathlib.Path` objects as context managers has been
removed. Before Python 3.9, exiting the context manager marked a path as
"closed", which caused some (but not all!) methods to raise when called.
Since Python 3.9, using a path as a context manager does nothing.