Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ensurepip
import os
import os.path
import pathlib
import re
import shutil
import struct
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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.