[Python-checkins] gh-92675: venv: Fix ensure_directories() to again accept a Path for env_dir (GH-92676)

miss-islington webhook-mailer at python.org
Thu May 19 11:17:37 EDT 2022


https://github.com/python/cpython/commit/71cdf6a38af80bc1932c3a5f7343b589cc236226
commit: 71cdf6a38af80bc1932c3a5f7343b589cc236226
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-05-19T08:17:28-07:00
summary:

gh-92675: venv: Fix ensure_directories() to again accept a Path for env_dir (GH-92676)


Co-authored-by: Jelle Zijlstra <jelle.zijlstra at gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood at Gmail.com>
(cherry picked from commit 30deeac64925effe46cb5f1cd091ccb4c850ce83)

Co-authored-by: David Foster <david at dafoster.net>

files:
A Misc/NEWS.d/next/Library/2022-05-19-13-33-18.gh-issue-92675.ZeerMZ.rst
M Lib/test/test_venv.py
M Lib/venv/__init__.py

diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
index 6bfb6e9f336ef..199160e4d0e72 100644
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -8,6 +8,7 @@
 import ensurepip
 import os
 import os.path
+import pathlib
 import re
 import shutil
 import struct
@@ -98,12 +99,23 @@ def isdir(self, *args):
         fn = self.get_env_file(*args)
         self.assertTrue(os.path.isdir(fn))
 
-    def test_defaults(self):
+    def test_defaults_with_str_path(self):
         """
-        Test the create function with default arguments.
+        Test the create function with default arguments and a str path.
         """
         rmtree(self.env_dir)
         self.run_with_capture(venv.create, self.env_dir)
+        self._check_output_of_default_create()
+
+    def test_defaults_with_pathlib_path(self):
+        """
+        Test the create function with default arguments and a pathlib.Path path.
+        """
+        rmtree(self.env_dir)
+        self.run_with_capture(venv.create, pathlib.Path(self.env_dir))
+        self._check_output_of_default_create()
+
+    def _check_output_of_default_create(self):
         self.isdir(self.bindir)
         self.isdir(self.include)
         self.isdir(*self.lib)
@@ -473,7 +485,9 @@ def test_pathsep_error(self):
         the path separator.
         """
         rmtree(self.env_dir)
-        self.assertRaises(ValueError, venv.create, self.env_dir + os.pathsep)
+        bad_itempath = self.env_dir + os.pathsep
+        self.assertRaises(ValueError, venv.create, bad_itempath)
+        self.assertRaises(ValueError, venv.create, pathlib.Path(bad_itempath))
 
 @requireVenvCreate
 class EnsurePipTest(BaseTest):
diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py
index 7bfbadda7b497..6032f3648e15f 100644
--- a/Lib/venv/__init__.py
+++ b/Lib/venv/__init__.py
@@ -116,7 +116,7 @@ def create_if_needed(d):
             elif os.path.islink(d) or os.path.isfile(d):
                 raise ValueError('Unable to create directory %r' % d)
 
-        if os.pathsep in env_dir:
+        if os.pathsep in os.fspath(env_dir):
             raise ValueError(f'Refusing to create a venv in {env_dir} because '
                              f'it contains the PATH separator {os.pathsep}.')
         if os.path.exists(env_dir) and self.clear:
diff --git a/Misc/NEWS.d/next/Library/2022-05-19-13-33-18.gh-issue-92675.ZeerMZ.rst b/Misc/NEWS.d/next/Library/2022-05-19-13-33-18.gh-issue-92675.ZeerMZ.rst
new file mode 100644
index 0000000000000..6adc024fc5415
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-19-13-33-18.gh-issue-92675.ZeerMZ.rst
@@ -0,0 +1,2 @@
+Fix :func:`venv.ensure_directories` to accept :class:`pathlib.Path` arguments
+in addition to :class:`str` paths. Patch by David Foster.



More information about the Python-checkins mailing list