diff -r 413c0b0a105f Doc/install/index.rst --- a/Doc/install/index.rst Mon Apr 15 09:53:49 2013 -0400 +++ b/Doc/install/index.rst Tue Apr 16 14:29:52 2013 -0400 @@ -645,6 +645,10 @@ the Distutils are the only ones you can use.) See section :ref:`inst-config-files` for details. +.. note:: When a virtual environment is activated, the `install`* options above + are ignored from all distutils configuration files to prevent inadvertently + installing packages outside of the virtual environment. + .. XXX need some Windows examples---when would custom installation schemes be needed on those platforms? diff -r 413c0b0a105f Doc/library/venv.rst --- a/Doc/library/venv.rst Mon Apr 15 09:53:49 2013 -0400 +++ b/Doc/library/venv.rst Tue Apr 16 14:29:52 2013 -0400 @@ -57,6 +57,10 @@ :attr:`sys.exec_prefix` is the same as :attr:`sys.base_exec_prefix` (they all point to a non-venv Python installation). + When a virtual environment is active `install`* options will be ignored in + distutils configuration files. This is to avoid packages being + inadvertently installed in the wrong place. + API --- diff -r 413c0b0a105f Lib/distutils/dist.py --- a/Lib/distutils/dist.py Mon Apr 15 09:53:49 2013 -0400 +++ b/Lib/distutils/dist.py Tue Apr 16 14:29:52 2013 -0400 @@ -343,6 +343,15 @@ def parse_config_files(self, filenames=None): from configparser import ConfigParser + # Ignore install directory options if we have a venv + if sys.prefix != sys.base_prefix: + ignore_options = [ + 'install-base', 'install-platbase', 'install-lib', + 'install-platlib', 'install-purelib', 'install-script', + 'install-headers', 'install-scripts', 'install-data', ] + else: + ignore_options = [] + if filenames is None: filenames = self.find_config_files() @@ -359,7 +368,7 @@ opt_dict = self.get_option_dict(section) for opt in options: - if opt != '__name__': + if opt != '__name__' and opt not in ignore_options: val = parser.get(section,opt) opt = opt.replace('-', '_') opt_dict[opt] = (filename, val) diff -r 413c0b0a105f Lib/distutils/tests/test_dist.py --- a/Lib/distutils/tests/test_dist.py Mon Apr 15 09:53:49 2013 -0400 +++ b/Lib/distutils/tests/test_dist.py Tue Apr 16 14:29:52 2013 -0400 @@ -18,7 +18,7 @@ user_options = [ ("sample-option=", "S", "help text"), - ] + ] def initialize_options(self): self.sample_option = None @@ -77,6 +77,57 @@ self.assertIsInstance(cmd, test_dist) self.assertEqual(cmd.sample_option, "sometext") + def test_venv_install_options(self): + sys.argv.append("install") + self.addCleanup(os.unlink, TESTFN) + f = open(TESTFN, "w") + + fakepath = '/somedir' + try: + print("[install]", file=f) + print("install-base = {0}".format(fakepath), file=f) + print("install-platbase = {0}".format(fakepath), file=f) + print("install-lib = {0}".format(fakepath), file=f) + print("install-platlib = {0}".format(fakepath), file=f) + print("install-purelib = {0}".format(fakepath), file=f) + print("install-script = {0}".format(fakepath), file=f) + print("install-headers = {0}".format(fakepath), file=f) + print("install-scripts = {0}".format(fakepath), file=f) + print("install-data = {0}".format(fakepath), file=f) + finally: + f.close() + + # Base case: Not in a Virtual Environment + sys.prefix = sys.base_prefix + d = self.create_distribution([TESTFN]) + + option_tuple = (TESTFN, fakepath) + + result_dict = { + 'install_base': option_tuple, + 'install_platbase': option_tuple, + 'install_lib': option_tuple, + 'install_platlib': option_tuple, + 'install_purelib': option_tuple, + 'install_script': option_tuple, + 'install_headers': option_tuple, + 'install_scripts': option_tuple, + 'install_data': option_tuple, + } + + self.assertEqual( + list(d.command_options.get('install').keys()).sort(), + list(result_dict.keys()).sort()) + + for (key, value) in d.command_options.get('install').items(): + self.assertEqual(value, result_dict[key]) + + # Test case: In a Virtual Environment + sys.prefix, sys.base_prefix = '/prefix', '/baseprefix' + d = self.create_distribution([TESTFN]) + for key in result_dict.keys(): + self.assertNotIn(key, d.command_options.get('install', {})) + def test_command_packages_configfile(self): sys.argv.append("build") self.addCleanup(os.unlink, TESTFN) @@ -304,7 +355,7 @@ os.environ['HOME'] = temp_dir files = dist.find_config_files() self.assertIn(user_filename, files, - '%r not found in %r' % (user_filename, files)) + '%r not found in %r' % (user_filename, files)) finally: os.remove(user_filename)