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:
parent
b6be20ca33
commit
6f67765389
|
@ -72,7 +72,7 @@ def clear_cache():
|
|||
_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 @@ def _yield_distributions(include_dist, include_egg, paths=sys.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
|
||||
|
||||
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
|
||||
|
||||
|
||||
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 @@ def get_distributions(use_egg_info=False, paths=sys.path):
|
|||
: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 @@ def get_distribution(name, use_egg_info=False, paths=None):
|
|||
|
||||
: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_users(path):
|
|||
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)
|
||||
|
||||
|
|
|
@ -54,10 +54,8 @@ def _move_files(files, destination):
|
|||
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 @@ def _run_install_from_dir(source_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.
|
||||
|
||||
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
|
||||
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 @@ def _update_infos(infos, new_infos):
|
|||
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 install(project):
|
|||
|
||||
def _main(**attrs):
|
||||
if 'script_args' not in attrs:
|
||||
import sys
|
||||
attrs['requirements'] = sys.argv[1]
|
||||
get_infos(**attrs)
|
||||
|
||||
|
|
|
@ -29,7 +29,6 @@ class BuildExtTestCase(support.TempdirManager,
|
|||
# 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 @@ class BuildExtTestCase(support.TempdirManager,
|
|||
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
|
||||
|
|
|
@ -259,12 +259,11 @@ class TestDatabase(support.LoggingCatcher,
|
|||
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 @@ class TestDatabase(support.LoggingCatcher,
|
|||
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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue