Skip to content

Commit 6b1510c

Browse files
authored
GH-83863: Drop support for using pathlib.Path objects as context managers (GH-104807)
In Python 3.8 and prior, `pathlib.Path.__exit__()` marked a path as closed; some 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 we made `__exit__()` a no-op, and in 3.11 `__enter__()` began raising deprecation warnings. Here we remove both methods.
1 parent e0b3078 commit 6b1510c

File tree

4 files changed

+7
-39
lines changed

4 files changed

+7
-39
lines changed

Doc/whatsnew/3.13.rst

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Removed
115115
are now removed. The items in those namespaces can be imported directly
116116
from :mod:`typing`. (Contributed by Sebastian Rittau in :gh:`92871`.)
117117

118+
* Remove support for using :class:`pathlib.Path` objects as context managers.
119+
This functionality was deprecated and made a no-op in Python 3.9.
120+
118121
Porting to Python 3.13
119122
======================
120123

Lib/pathlib.py

-19
Original file line numberDiff line numberDiff line change
@@ -1080,25 +1080,6 @@ def __new__(cls, *args, **kwargs):
10801080
cls = WindowsPath if os.name == 'nt' else PosixPath
10811081
return object.__new__(cls)
10821082

1083-
def __enter__(self):
1084-
# In previous versions of pathlib, __exit__() marked this path as
1085-
# closed; subsequent attempts to perform I/O would raise an IOError.
1086-
# This functionality was never documented, and had the effect of
1087-
# making Path objects mutable, contrary to PEP 428.
1088-
# In Python 3.9 __exit__() was made a no-op.
1089-
# In Python 3.11 __enter__() began emitting DeprecationWarning.
1090-
# In Python 3.13 __enter__() and __exit__() should be removed.
1091-
warnings.warn("pathlib.Path.__enter__() is deprecated and scheduled "
1092-
"for removal in Python 3.13; Path objects as a context "
1093-
"manager is a no-op",
1094-
DeprecationWarning, stacklevel=2)
1095-
return self
1096-
1097-
def __exit__(self, t, v, tb):
1098-
pass
1099-
1100-
# Public API
1101-
11021083
@classmethod
11031084
def cwd(cls):
11041085
"""Return a new path pointing to the current working directory."""

Lib/test/test_pathlib.py

-20
Original file line numberDiff line numberDiff line change
@@ -2080,26 +2080,6 @@ def test_resolve_nonexist_relative_issue38671(self):
20802080
finally:
20812081
os.chdir(old_cwd)
20822082

2083-
def test_with(self):
2084-
p = self.cls(BASE)
2085-
it = p.iterdir()
2086-
it2 = p.iterdir()
2087-
next(it2)
2088-
# bpo-46556: path context managers are deprecated in Python 3.11.
2089-
with self.assertWarns(DeprecationWarning):
2090-
with p:
2091-
pass
2092-
# Using a path as a context manager is a no-op, thus the following
2093-
# operations should still succeed after the context manage exits.
2094-
next(it)
2095-
next(it2)
2096-
p.exists()
2097-
p.resolve()
2098-
p.absolute()
2099-
with self.assertWarns(DeprecationWarning):
2100-
with p:
2101-
pass
2102-
21032083
@os_helper.skip_unless_working_chmod
21042084
def test_chmod(self):
21052085
p = self.cls(BASE) / 'fileA'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Support for using :class:`pathlib.Path` objects as context managers has been
2+
removed. Before Python 3.9, exiting the context manager marked a path as
3+
"closed", which caused some (but not all!) methods to raise when called.
4+
Since Python 3.9, using a path as a context manager does nothing.

0 commit comments

Comments
 (0)