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

Skip to content

Commit 06e1701

Browse files
bpo-46556: emit DeprecationWarning from pathlib.Path.__enter__() (GH-30971)
In Python 3.9, Path.__exit__() was made a no-op and has never been documented. Co-authored-by: Brett Cannon <[email protected]>
1 parent 81c7204 commit 06e1701

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

Lib/pathlib.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -877,17 +877,20 @@ def _make_child_relpath(self, part):
877877
return self._from_parsed_parts(self._drv, self._root, parts)
878878

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

882893
def __exit__(self, t, v, tb):
883-
# https://bugs.python.org/issue39682
884-
# In previous versions of pathlib, this method marked this path as
885-
# closed; subsequent attempts to perform I/O would raise an IOError.
886-
# This functionality was never documented, and had the effect of
887-
# making Path objects mutable, contrary to PEP 428. In Python 3.9 the
888-
# _closed attribute was removed, and this method made a no-op.
889-
# This method and __enter__()/__exit__() should be deprecated and
890-
# removed in the future.
891894
pass
892895

893896
# Public API

Lib/test/test_pathlib.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,17 +1850,20 @@ def test_with(self):
18501850
it = p.iterdir()
18511851
it2 = p.iterdir()
18521852
next(it2)
1853-
with p:
1854-
pass
1853+
# bpo-46556: path context managers are deprecated in Python 3.11.
1854+
with self.assertWarns(DeprecationWarning):
1855+
with p:
1856+
pass
18551857
# Using a path as a context manager is a no-op, thus the following
18561858
# operations should still succeed after the context manage exits.
18571859
next(it)
18581860
next(it2)
18591861
p.exists()
18601862
p.resolve()
18611863
p.absolute()
1862-
with p:
1863-
pass
1864+
with self.assertWarns(DeprecationWarning):
1865+
with p:
1866+
pass
18641867

18651868
def test_chmod(self):
18661869
p = self.cls(BASE) / 'fileA'
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate undocumented support for using a :class:`pathlib.Path` object as a
2+
context manager.

0 commit comments

Comments
 (0)