diff -r c0d25de5919e Lib/test/test_venv.py --- a/Lib/test/test_venv.py Fri Jan 30 13:33:42 2015 -0500 +++ b/Lib/test/test_venv.py Sat Jan 31 22:17:38 2015 +0100 @@ -6,6 +6,7 @@ """ import ensurepip +import io import os import os.path import struct @@ -35,6 +36,7 @@ def failsOnWindows(f): return f + class BaseTest(unittest.TestCase): """Base class for venv tests.""" @@ -71,6 +73,7 @@ result = f.read() return result + class BasicTest(BaseTest): """Test venv module functionality.""" @@ -98,7 +101,7 @@ data = self.get_text_file_contents('pyvenv.cfg') if sys.platform == 'darwin' and ('__PYVENV_LAUNCHER__' in os.environ): - executable = os.environ['__PYVENV_LAUNCHER__'] + executable = os.environ['__PYVENV_LAUNCHER__'] else: executable = sys.executable path = os.path.dirname(executable) @@ -115,7 +118,7 @@ """ Test that the prefix values are as expected. """ - #check our prefixes + # check our prefixes self.assertEqual(sys.base_prefix, sys.prefix) self.assertEqual(sys.base_exec_prefix, sys.exec_prefix) @@ -124,11 +127,11 @@ self.run_with_capture(venv.create, self.env_dir) envpy = os.path.join(self.env_dir, self.bindir, self.exe) cmd = [envpy, '-c', None] - for prefix, expected in ( - ('prefix', self.env_dir), - ('prefix', self.env_dir), - ('base_prefix', sys.prefix), - ('base_exec_prefix', sys.exec_prefix)): + prefixes = (('prefix', self.env_dir), + ('prefix', self.env_dir), + ('base_prefix', sys.prefix), + ('base_exec_prefix', sys.exec_prefix)) + for prefix, expected in prefixes: cmd[2] = 'import sys; print(sys.%s)' % prefix p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -190,7 +193,7 @@ rmtree(fn) def test_unoverwritable_fails(self): - #create a file clashing with directories in the env dir + # create a file clashing with directories in the env dir for paths in self.ENV_SUBDIRS[:3]: fn = os.path.join(self.env_dir, *paths) with open(fn, 'wb') as f: @@ -255,7 +258,8 @@ """ rmtree(self.env_dir) self.run_with_capture(venv.create, self.env_dir) - envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) + envpy = os.path.join(os.path.realpath(self.env_dir), + self.bindir, self.exe) cmd = [envpy, '-c', 'import sys; print(sys.executable)'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -270,13 +274,43 @@ rmtree(self.env_dir) builder = venv.EnvBuilder(clear=True, symlinks=True) builder.create(self.env_dir) - envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) + envpy = os.path.join(os.path.realpath(self.env_dir), + self.bindir, self.exe) cmd = [envpy, '-c', 'import sys; print(sys.executable)'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() self.assertEqual(out.strip(), envpy.encode()) + def test_execute_as_pyvenv_without_arguments(self): + """ + Test that verifies that 'pyvenv' appears on the error message + """ + old_stderr = sys.stderr + sys.stderr = io.StringIO() + try: + with self.assertRaises(SystemExit): + venv.main(pyvenv=True) + + self.assertIn('pyvenv', sys.stderr.getvalue()) + finally: + sys.stderr = old_stderr + + def test_execute_as_module_without_arguments(self): + """ + Test that verifies that 'pyvenv' doesn't appear on the error message + """ + old_stderr = sys.stderr + sys.stderr = io.StringIO() + try: + with self.assertRaises(SystemExit): + venv.main() + + self.assertIn('venv', sys.stderr.getvalue()) + self.assertNotIn('pyvenv', sys.stderr.getvalue()) + finally: + sys.stderr = old_stderr + @skipInVenv class EnsurePipTest(BaseTest): @@ -291,12 +325,11 @@ out, err = p.communicate() # We force everything to text, so unittest gives the detailed diff # if we get unexpected results - err = err.decode("latin-1") # Force to text, prevent decoding errors + err = err.decode("latin-1") # Force to text, prevent decoding errors self.assertEqual(err, "") - out = out.decode("latin-1") # Force to text, prevent decoding errors + out = out.decode("latin-1") # Force to text, prevent decoding errors self.assertEqual(out.strip(), "OK") - def test_no_pip_by_default(self): rmtree(self.env_dir) self.run_with_capture(venv.create, self.env_dir) @@ -361,16 +394,17 @@ msg = "{}\n\n**Subprocess Output**\n{}" self.fail(msg.format(exc, details)) # Ensure pip is available in the virtual environment - envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) + envpy = os.path.join(os.path.realpath(self.env_dir), + self.bindir, self.exe) cmd = [envpy, '-Im', 'pip', '--version'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stderr=subprocess.PIPE) out, err = p.communicate() # We force everything to text, so unittest gives the detailed diff # if we get unexpected results - err = err.decode("latin-1") # Force to text, prevent decoding errors + err = err.decode("latin-1") # Force to text, prevent decoding errors self.assertEqual(err, "") - out = out.decode("latin-1") # Force to text, prevent decoding errors + out = out.decode("latin-1") # Force to text, prevent decoding errors expected_version = "pip {}".format(ensurepip.version()) self.assertEqual(out[:len(expected_version)], expected_version) env_dir = os.fsencode(self.env_dir).decode("latin-1") @@ -382,16 +416,16 @@ cmd = [envpy, '-Im', 'ensurepip._uninstall'] with EnvironmentVarGuard() as envvars: p = subprocess.Popen(cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) + stderr=subprocess.PIPE) out, err = p.communicate() # We force everything to text, so unittest gives the detailed diff # if we get unexpected results - err = err.decode("latin-1") # Force to text, prevent decoding errors + err = err.decode("latin-1") # Force to text, prevent decoding errors self.assertEqual(err, "") # Being fairly specific regarding the expected behaviour for the # initial bundling phase in Python 3.4. If the output changes in # future pip versions, this test can likely be relaxed further. - out = out.decode("latin-1") # Force to text, prevent decoding errors + out = out.decode("latin-1") # Force to text, prevent decoding errors self.assertIn("Successfully uninstalled pip", out) self.assertIn("Successfully uninstalled setuptools", out) # Check pip is now gone from the virtual environment diff -r c0d25de5919e Lib/venv/__init__.py --- a/Lib/venv/__init__.py Fri Jan 30 13:33:42 2015 -0500 +++ b/Lib/venv/__init__.py Sat Jan 31 22:17:38 2015 +0100 @@ -371,7 +371,7 @@ clear=clear, symlinks=symlinks, with_pip=with_pip) builder.create(env_dir) -def main(args=None): +def main(args=None, pyvenv=False): compatible = True if sys.version_info < (3, 3): compatible = False @@ -381,8 +381,9 @@ raise ValueError('This script is only for use with Python >= 3.3') else: import argparse + name = __name__ if not pyvenv else 'pyvenv' - parser = argparse.ArgumentParser(prog=__name__, + parser = argparse.ArgumentParser(prog=name, description='Creates virtual Python ' 'environments in one or ' 'more target ' diff -r c0d25de5919e Tools/scripts/pyvenv --- a/Tools/scripts/pyvenv Fri Jan 30 13:33:42 2015 -0500 +++ b/Tools/scripts/pyvenv Sat Jan 31 22:17:38 2015 +0100 @@ -4,7 +4,7 @@ rc = 1 try: import venv - venv.main() + venv.main(pyvenv=True) rc = 0 except Exception as e: print('Error: %s' % e, file=sys.stderr)