Skip to content

Commit 30deeac

Browse files
davidfstrJelleZijlstraAlexWaygood
authored
gh-92675: venv: Fix ensure_directories() to again accept a Path for env_dir (#92676)
Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
1 parent 6b51773 commit 30deeac

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Lib/test/test_venv.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import ensurepip
99
import os
1010
import os.path
11+
import pathlib
1112
import re
1213
import shutil
1314
import struct
@@ -99,12 +100,23 @@ def isdir(self, *args):
99100
fn = self.get_env_file(*args)
100101
self.assertTrue(os.path.isdir(fn))
101102

102-
def test_defaults(self):
103+
def test_defaults_with_str_path(self):
103104
"""
104-
Test the create function with default arguments.
105+
Test the create function with default arguments and a str path.
105106
"""
106107
rmtree(self.env_dir)
107108
self.run_with_capture(venv.create, self.env_dir)
109+
self._check_output_of_default_create()
110+
111+
def test_defaults_with_pathlib_path(self):
112+
"""
113+
Test the create function with default arguments and a pathlib.Path path.
114+
"""
115+
rmtree(self.env_dir)
116+
self.run_with_capture(venv.create, pathlib.Path(self.env_dir))
117+
self._check_output_of_default_create()
118+
119+
def _check_output_of_default_create(self):
108120
self.isdir(self.bindir)
109121
self.isdir(self.include)
110122
self.isdir(*self.lib)
@@ -474,7 +486,9 @@ def test_pathsep_error(self):
474486
the path separator.
475487
"""
476488
rmtree(self.env_dir)
477-
self.assertRaises(ValueError, venv.create, self.env_dir + os.pathsep)
489+
bad_itempath = self.env_dir + os.pathsep
490+
self.assertRaises(ValueError, venv.create, bad_itempath)
491+
self.assertRaises(ValueError, venv.create, pathlib.Path(bad_itempath))
478492

479493
@requireVenvCreate
480494
class EnsurePipTest(BaseTest):

Lib/venv/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def create_if_needed(d):
116116
elif os.path.islink(d) or os.path.isfile(d):
117117
raise ValueError('Unable to create directory %r' % d)
118118

119-
if os.pathsep in env_dir:
119+
if os.pathsep in os.fspath(env_dir):
120120
raise ValueError(f'Refusing to create a venv in {env_dir} because '
121121
f'it contains the PATH separator {os.pathsep}.')
122122
if os.path.exists(env_dir) and self.clear:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`venv.ensure_directories` to accept :class:`pathlib.Path` arguments
2+
in addition to :class:`str` paths. Patch by David Foster.

0 commit comments

Comments
 (0)