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.
This commit is contained in:
Éric Araujo 2011-06-16 23:43:15 +02:00
parent b6be20ca33
commit 6f67765389
4 changed files with 20 additions and 27 deletions

View File

@ -72,7 +72,7 @@ def clear_cache():
_cache_generated_egg = False _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 Yield .dist-info and .egg(-info) distributions, based on the arguments
@ -92,7 +92,7 @@ def _yield_distributions(include_dist, include_egg, paths=sys.path):
yield EggInfoDistribution(dist_path) 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 global _cache_generated, _cache_generated_egg
if _cache_generated_egg or (_cache_generated and not use_egg_info): if _cache_generated_egg or (_cache_generated and not use_egg_info):
@ -472,7 +472,7 @@ def distinfo_dirname(name, version):
return '-'.join([name, normalized_version]) + file_extension 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 Provides an iterator that looks for ``.dist-info`` directories in
``sys.path`` and returns :class:`Distribution` instances for each one of ``sys.path`` and returns :class:`Distribution` instances for each one of
@ -482,6 +482,9 @@ def get_distributions(use_egg_info=False, paths=sys.path):
:rtype: iterator of :class:`Distribution` and :class:`EggInfoDistribution` :rtype: iterator of :class:`Distribution` and :class:`EggInfoDistribution`
instances instances
""" """
if paths is None:
paths = sys.path
if not _cache_enabled: if not _cache_enabled:
for dist in _yield_distributions(True, use_egg_info, paths): for dist in _yield_distributions(True, use_egg_info, paths):
yield dist yield dist
@ -513,7 +516,7 @@ def get_distribution(name, use_egg_info=False, paths=None):
:rtype: :class:`Distribution` or :class:`EggInfoDistribution` or None :rtype: :class:`Distribution` or :class:`EggInfoDistribution` or None
""" """
if paths == None: if paths is None:
paths = sys.path paths = sys.path
if not _cache_enabled: if not _cache_enabled:
@ -632,7 +635,7 @@ def get_file_users(path):
def get_file_path(distribution_name, relative_path): def get_file_path(distribution_name, relative_path):
"""Return the path to a resource file.""" """Return the path to a resource file."""
dist = get_distribution(distribution_name) dist = get_distribution(distribution_name)
if dist != None: if dist is not None:
return dist.get_resource_path(relative_path) return dist.get_resource_path(relative_path)
raise LookupError('no distribution named %r found' % distribution_name) raise LookupError('no distribution named %r found' % distribution_name)

View File

@ -54,10 +54,8 @@ def _move_files(files, destination):
try: try:
os.makedirs(os.path.dirname(new)) os.makedirs(os.path.dirname(new))
except OSError as e: except OSError as e:
if e.errno == errno.EEXIST: if e.errno != errno.EEXIST:
pass raise
else:
raise e
os.rename(old, new) os.rename(old, new)
yield old, new yield old, new
@ -169,7 +167,7 @@ def _run_install_from_dir(source_dir):
os.chdir(old_dir) 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. """Install all distributions provided in dists, with the given prefix.
If an error occurs while installing one of the distributions, uninstall all If an error occurs while installing one of the distributions, uninstall all
@ -196,13 +194,13 @@ def install_dists(dists, path, paths=sys.path):
# reverting # reverting
for installed_dist in installed_dists: for installed_dist in installed_dists:
logger.info('Reverting %s', installed_dist) logger.info('Reverting %s', installed_dist)
_remove_dist(installed_dist, paths) remove(installed_dist.name, paths)
raise e raise e
return installed_dists return installed_dists
def install_from_infos(install_path=None, install=[], remove=[], conflicts=[], def install_from_infos(install_path=None, install=[], remove=[], conflicts=[],
paths=sys.path): paths=None):
"""Install and remove the given distributions. """Install and remove the given distributions.
The function signature is made to be compatible with the one of get_infos. The function signature is made to be compatible with the one of get_infos.
@ -383,11 +381,7 @@ def _update_infos(infos, new_infos):
infos[key].extend(new_infos[key]) infos[key].extend(new_infos[key])
def _remove_dist(dist, paths=sys.path): def remove(project_name, paths=None, auto_confirm=True):
remove(dist.name, paths)
def remove(project_name, paths=sys.path, auto_confirm=True):
"""Removes a single project from the installation. """Removes a single project from the installation.
Returns True on success Returns True on success
@ -539,7 +533,6 @@ def install(project):
def _main(**attrs): def _main(**attrs):
if 'script_args' not in attrs: if 'script_args' not in attrs:
import sys
attrs['requirements'] = sys.argv[1] attrs['requirements'] = sys.argv[1]
get_infos(**attrs) get_infos(**attrs)

View File

@ -29,7 +29,6 @@ class BuildExtTestCase(support.TempdirManager,
# Note that we're making changes to sys.path # Note that we're making changes to sys.path
super(BuildExtTestCase, self).setUp() super(BuildExtTestCase, self).setUp()
self.tmp_dir = self.mkdtemp() self.tmp_dir = self.mkdtemp()
self.sys_path = sys.path, sys.path[:]
sys.path.append(self.tmp_dir) sys.path.append(self.tmp_dir)
filename = _get_source_filename() filename = _get_source_filename()
if os.path.exists(filename): if os.path.exists(filename):
@ -107,8 +106,7 @@ class BuildExtTestCase(support.TempdirManager,
def tearDown(self): def tearDown(self):
# Get everything back to normal # Get everything back to normal
unload('xx') unload('xx')
sys.path = self.sys_path[0] sys.path.remove(self.tmp_dir)
sys.path[:] = self.sys_path[1]
if sys.version > "2.6": if sys.version > "2.6":
site.USER_BASE = self.old_user_base site.USER_BASE = self.old_user_base
build_ext.USER_BASE = self.old_user_base build_ext.USER_BASE = self.old_user_base

View File

@ -259,12 +259,11 @@ class TestDatabase(support.LoggingCatcher,
disable_cache() disable_cache()
# Setup the path environment with our fake distributions # Setup the path environment with our fake distributions
current_path = os.path.abspath(os.path.dirname(__file__)) 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') self.fake_dists_path = os.path.join(current_path, 'fake_dists')
sys.path.insert(0, self.fake_dists_path) sys.path.insert(0, self.fake_dists_path)
def tearDown(self): def tearDown(self):
sys.path[:] = self.sys_path sys.path.remove(self.fake_dists_path)
enable_cache() enable_cache()
super(TestDatabase, self).tearDown() super(TestDatabase, self).tearDown()
@ -488,20 +487,20 @@ class TestDatabase(support.LoggingCatcher,
dists = [('choxie', '2.0.0.9'), ('grammar', '1.0a4'), dists = [('choxie', '2.0.0.9'), ('grammar', '1.0a4'),
('towel-stuff', '0.1'), ('babar', '0.1')] ('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']) 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)] if dist.path.startswith(self.fake_dists_path)]
checkLists(eggs, found) checkLists(eggs, found)
found = [(dist.name, dist.metadata['Version']) 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)] if dist.path.startswith(self.fake_dists_path)]
checkLists(dists, found) checkLists(dists, found)
found = [(dist.name, dist.metadata['Version']) 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)] if dist.path.startswith(self.fake_dists_path)]
checkLists(dists + eggs, found) checkLists(dists + eggs, found)