]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-83863: Drop support for using `pathlib.Path` objects as context managers (GH-104807)
authorBarney Gale <barney.gale@gmail.com>
Tue, 23 May 2023 22:31:59 +0000 (23:31 +0100)
committerGitHub <noreply@github.com>
Tue, 23 May 2023 22:31:59 +0000 (22:31 +0000)
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.

Doc/whatsnew/3.13.rst
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2023-05-23-19-53-18.gh-issue-83863.eRI5JG.rst [new file with mode: 0644]

index e0c3c2a3592ec774b2add9b1f58d6004ca3b5dc5..309f26ea27dbfdb1be5a03a2bdc02968852aa11e 100644 (file)
@@ -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
 ======================
 
index 3d68c161603d0876d8e9a9c8c0665b9365960d5b..3a7a1241ba77f6d9d0aafe97eaf21237e5972e48 100644 (file)
@@ -1080,25 +1080,6 @@ class Path(PurePath):
             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."""
index ab2c2b232a04115fc68d8267019e9491b6a103c2..8b68cdc9b7d0036b47c9e3c30bd9016e386d5057 100644 (file)
@@ -2080,26 +2080,6 @@ class _BasePathTest(object):
         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'
diff --git a/Misc/NEWS.d/next/Library/2023-05-23-19-53-18.gh-issue-83863.eRI5JG.rst b/Misc/NEWS.d/next/Library/2023-05-23-19-53-18.gh-issue-83863.eRI5JG.rst
new file mode 100644 (file)
index 0000000..7a073aa
--- /dev/null
@@ -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.