[Python-checkins] cpython: Stop binding sys.path as default parameter value in packaging.

eric.araujo python-checkins at python.org
Fri Jun 17 19:41:36 CEST 2011


http://hg.python.org/cpython/rev/af4ad88e90b5
changeset:   70847:af4ad88e90b5
user:        Éric Araujo <merwok at netwok.org>
date:        Thu Jun 16 23:43:15 2011 +0200
summary:
  Stop binding sys.path as default parameter value in packaging.

The two public functions in database default to sys.path if the given
*paths* argument is None; the private functions don’t have default
values for their arguments anymore, which is fine as the public
functions that call them pass their arguments down.  Likewise in
install, the functions will pass down their *paths* arguments down to
database functions.

A one-line unneeded function in install was removed instead of being
changed, and the few remaining tests that used brute-force restoration
of sys.path have been cleaned up to use sys.path.remove.

files:
  Lib/packaging/database.py                     |  13 ++++--
  Lib/packaging/install.py                      |  19 +++------
  Lib/packaging/tests/test_command_build_ext.py |   4 +-
  Lib/packaging/tests/test_database.py          |  11 ++---
  4 files changed, 20 insertions(+), 27 deletions(-)


diff --git a/Lib/packaging/database.py b/Lib/packaging/database.py
--- a/Lib/packaging/database.py
+++ b/Lib/packaging/database.py
@@ -72,7 +72,7 @@
     _cache_generated_egg = False
 
 
-def _yield_distributions(include_dist, include_egg, paths=sys.path):
+def _yield_distributions(include_dist, include_egg, paths):
     """
     Yield .dist-info and .egg(-info) distributions, based on the arguments
 
@@ -92,7 +92,7 @@
                 yield EggInfoDistribution(dist_path)
 
 
-def _generate_cache(use_egg_info=False, paths=sys.path):
+def _generate_cache(use_egg_info, paths):
     global _cache_generated, _cache_generated_egg
 
     if _cache_generated_egg or (_cache_generated and not use_egg_info):
@@ -472,7 +472,7 @@
     return '-'.join([name, normalized_version]) + file_extension
 
 
-def get_distributions(use_egg_info=False, paths=sys.path):
+def get_distributions(use_egg_info=False, paths=None):
     """
     Provides an iterator that looks for ``.dist-info`` directories in
     ``sys.path`` and returns :class:`Distribution` instances for each one of
@@ -482,6 +482,9 @@
     :rtype: iterator of :class:`Distribution` and :class:`EggInfoDistribution`
             instances
     """
+    if paths is None:
+        paths = sys.path
+
     if not _cache_enabled:
         for dist in _yield_distributions(True, use_egg_info, paths):
             yield dist
@@ -513,7 +516,7 @@
 
     :rtype: :class:`Distribution` or :class:`EggInfoDistribution` or None
     """
-    if paths == None:
+    if paths is None:
         paths = sys.path
 
     if not _cache_enabled:
@@ -632,7 +635,7 @@
 def get_file_path(distribution_name, relative_path):
     """Return the path to a resource file."""
     dist = get_distribution(distribution_name)
-    if dist != None:
+    if dist is not None:
         return dist.get_resource_path(relative_path)
     raise LookupError('no distribution named %r found' % distribution_name)
 
diff --git a/Lib/packaging/install.py b/Lib/packaging/install.py
--- a/Lib/packaging/install.py
+++ b/Lib/packaging/install.py
@@ -54,10 +54,8 @@
         try:
             os.makedirs(os.path.dirname(new))
         except OSError as e:
-            if e.errno == errno.EEXIST:
-                pass
-            else:
-                raise e
+            if e.errno != errno.EEXIST:
+                raise
         os.rename(old, new)
         yield old, new
 
@@ -169,7 +167,7 @@
         os.chdir(old_dir)
 
 
-def install_dists(dists, path, paths=sys.path):
+def install_dists(dists, path, paths=None):
     """Install all distributions provided in dists, with the given prefix.
 
     If an error occurs while installing one of the distributions, uninstall all
@@ -196,13 +194,13 @@
             # reverting
             for installed_dist in installed_dists:
                 logger.info('Reverting %s', installed_dist)
-                _remove_dist(installed_dist, paths)
+                remove(installed_dist.name, paths)
             raise e
     return installed_dists
 
 
 def install_from_infos(install_path=None, install=[], remove=[], conflicts=[],
-                       paths=sys.path):
+                       paths=None):
     """Install and remove the given distributions.
 
     The function signature is made to be compatible with the one of get_infos.
@@ -383,11 +381,7 @@
             infos[key].extend(new_infos[key])
 
 
-def _remove_dist(dist, paths=sys.path):
-    remove(dist.name, paths)
-
-
-def remove(project_name, paths=sys.path, auto_confirm=True):
+def remove(project_name, paths=None, auto_confirm=True):
     """Removes a single project from the installation.
 
     Returns True on success
@@ -539,7 +533,6 @@
 
 def _main(**attrs):
     if 'script_args' not in attrs:
-        import sys
         attrs['requirements'] = sys.argv[1]
     get_infos(**attrs)
 
diff --git a/Lib/packaging/tests/test_command_build_ext.py b/Lib/packaging/tests/test_command_build_ext.py
--- a/Lib/packaging/tests/test_command_build_ext.py
+++ b/Lib/packaging/tests/test_command_build_ext.py
@@ -29,7 +29,6 @@
         # Note that we're making changes to sys.path
         super(BuildExtTestCase, self).setUp()
         self.tmp_dir = self.mkdtemp()
-        self.sys_path = sys.path, sys.path[:]
         sys.path.append(self.tmp_dir)
         filename = _get_source_filename()
         if os.path.exists(filename):
@@ -107,8 +106,7 @@
     def tearDown(self):
         # Get everything back to normal
         unload('xx')
-        sys.path = self.sys_path[0]
-        sys.path[:] = self.sys_path[1]
+        sys.path.remove(self.tmp_dir)
         if sys.version > "2.6":
             site.USER_BASE = self.old_user_base
             build_ext.USER_BASE = self.old_user_base
diff --git a/Lib/packaging/tests/test_database.py b/Lib/packaging/tests/test_database.py
--- a/Lib/packaging/tests/test_database.py
+++ b/Lib/packaging/tests/test_database.py
@@ -259,12 +259,11 @@
         disable_cache()
         # Setup the path environment with our fake distributions
         current_path = os.path.abspath(os.path.dirname(__file__))
-        self.sys_path = sys.path[:]
         self.fake_dists_path = os.path.join(current_path, 'fake_dists')
         sys.path.insert(0, self.fake_dists_path)
 
     def tearDown(self):
-        sys.path[:] = self.sys_path
+        sys.path.remove(self.fake_dists_path)
         enable_cache()
         super(TestDatabase, self).tearDown()
 
@@ -488,20 +487,20 @@
         dists = [('choxie', '2.0.0.9'), ('grammar', '1.0a4'),
                  ('towel-stuff', '0.1'), ('babar', '0.1')]
 
-        checkLists([], _yield_distributions(False, False))
+        checkLists([], _yield_distributions(False, False, sys.path))
 
         found = [(dist.name, dist.metadata['Version'])
-                 for dist in _yield_distributions(False, True)
+                 for dist in _yield_distributions(False, True, sys.path)
                  if dist.path.startswith(self.fake_dists_path)]
         checkLists(eggs, found)
 
         found = [(dist.name, dist.metadata['Version'])
-                 for dist in _yield_distributions(True, False)
+                 for dist in _yield_distributions(True, False, sys.path)
                  if dist.path.startswith(self.fake_dists_path)]
         checkLists(dists, found)
 
         found = [(dist.name, dist.metadata['Version'])
-                 for dist in _yield_distributions(True, True)
+                 for dist in _yield_distributions(True, True, sys.path)
                  if dist.path.startswith(self.fake_dists_path)]
         checkLists(dists + eggs, found)
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list