Skip to content

Commit 6525040

Browse files
committed
gh-100176: Remove redundant compat code for Python 3.2 and older
Python 3.2 has been EOL since 2016-02-20 and 2.7 since 2020-01-01, so we can remove this old compatibility check and unindent the old else-block. Also, in the unindented block, replace a .format() call with an f-string. Plus similar changes in the documentation.
1 parent 2e279e8 commit 6525040

File tree

2 files changed

+131
-146
lines changed

2 files changed

+131
-146
lines changed

Doc/library/venv.rst

+60-68
Original file line numberDiff line numberDiff line change
@@ -497,76 +497,68 @@ subclass which installs setuptools and pip into a created virtual environment::
497497
url = 'https://bootstrap.pypa.io/get-pip.py'
498498
self.install_script(context, 'pip', url)
499499

500+
500501
def main(args=None):
501-
compatible = True
502-
if sys.version_info < (3, 3):
503-
compatible = False
504-
elif not hasattr(sys, 'base_prefix'):
505-
compatible = False
506-
if not compatible:
507-
raise ValueError('This script is only for use with '
508-
'Python 3.3 or later')
502+
import argparse
503+
504+
parser = argparse.ArgumentParser(prog=__name__,
505+
description='Creates virtual Python '
506+
'environments in one or '
507+
'more target '
508+
'directories.')
509+
parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
510+
help='A directory in which to create the '
511+
'virtual environment.')
512+
parser.add_argument('--no-setuptools', default=False,
513+
action='store_true', dest='nodist',
514+
help="Don't install setuptools or pip in the "
515+
"virtual environment.")
516+
parser.add_argument('--no-pip', default=False,
517+
action='store_true', dest='nopip',
518+
help="Don't install pip in the virtual "
519+
"environment.")
520+
parser.add_argument('--system-site-packages', default=False,
521+
action='store_true', dest='system_site',
522+
help='Give the virtual environment access to the '
523+
'system site-packages dir.')
524+
if os.name == 'nt':
525+
use_symlinks = False
509526
else:
510-
import argparse
511-
512-
parser = argparse.ArgumentParser(prog=__name__,
513-
description='Creates virtual Python '
514-
'environments in one or '
515-
'more target '
516-
'directories.')
517-
parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
518-
help='A directory in which to create the '
519-
'virtual environment.')
520-
parser.add_argument('--no-setuptools', default=False,
521-
action='store_true', dest='nodist',
522-
help="Don't install setuptools or pip in the "
523-
"virtual environment.")
524-
parser.add_argument('--no-pip', default=False,
525-
action='store_true', dest='nopip',
526-
help="Don't install pip in the virtual "
527-
"environment.")
528-
parser.add_argument('--system-site-packages', default=False,
529-
action='store_true', dest='system_site',
530-
help='Give the virtual environment access to the '
531-
'system site-packages dir.')
532-
if os.name == 'nt':
533-
use_symlinks = False
534-
else:
535-
use_symlinks = True
536-
parser.add_argument('--symlinks', default=use_symlinks,
537-
action='store_true', dest='symlinks',
538-
help='Try to use symlinks rather than copies, '
539-
'when symlinks are not the default for '
540-
'the platform.')
541-
parser.add_argument('--clear', default=False, action='store_true',
542-
dest='clear', help='Delete the contents of the '
543-
'virtual environment '
544-
'directory if it already '
545-
'exists, before virtual '
546-
'environment creation.')
547-
parser.add_argument('--upgrade', default=False, action='store_true',
548-
dest='upgrade', help='Upgrade the virtual '
549-
'environment directory to '
550-
'use this version of '
551-
'Python, assuming Python '
552-
'has been upgraded '
553-
'in-place.')
554-
parser.add_argument('--verbose', default=False, action='store_true',
555-
dest='verbose', help='Display the output '
556-
'from the scripts which '
557-
'install setuptools and pip.')
558-
options = parser.parse_args(args)
559-
if options.upgrade and options.clear:
560-
raise ValueError('you cannot supply --upgrade and --clear together.')
561-
builder = ExtendedEnvBuilder(system_site_packages=options.system_site,
562-
clear=options.clear,
563-
symlinks=options.symlinks,
564-
upgrade=options.upgrade,
565-
nodist=options.nodist,
566-
nopip=options.nopip,
567-
verbose=options.verbose)
568-
for d in options.dirs:
569-
builder.create(d)
527+
use_symlinks = True
528+
parser.add_argument('--symlinks', default=use_symlinks,
529+
action='store_true', dest='symlinks',
530+
help='Try to use symlinks rather than copies, '
531+
'when symlinks are not the default for '
532+
'the platform.')
533+
parser.add_argument('--clear', default=False, action='store_true',
534+
dest='clear', help='Delete the contents of the '
535+
'virtual environment '
536+
'directory if it already '
537+
'exists, before virtual '
538+
'environment creation.')
539+
parser.add_argument('--upgrade', default=False, action='store_true',
540+
dest='upgrade', help='Upgrade the virtual '
541+
'environment directory to '
542+
'use this version of '
543+
'Python, assuming Python '
544+
'has been upgraded '
545+
'in-place.')
546+
parser.add_argument('--verbose', default=False, action='store_true',
547+
dest='verbose', help='Display the output '
548+
'from the scripts which '
549+
'install setuptools and pip.')
550+
options = parser.parse_args(args)
551+
if options.upgrade and options.clear:
552+
raise ValueError('you cannot supply --upgrade and --clear together.')
553+
builder = ExtendedEnvBuilder(system_site_packages=options.system_site,
554+
clear=options.clear,
555+
symlinks=options.symlinks,
556+
upgrade=options.upgrade,
557+
nodist=options.nodist,
558+
nopip=options.nopip,
559+
verbose=options.verbose)
560+
for d in options.dirs:
561+
builder.create(d)
570562

571563
if __name__ == '__main__':
572564
rc = 1

Lib/venv/__init__.py

+71-78
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def symlink_or_copy(self, src, dst, relative_symlinks_ok=False):
223223
force_copy = not self.symlinks
224224
if not force_copy:
225225
try:
226-
if not os.path.islink(dst): # can't link to itself!
226+
if not os.path.islink(dst): # can't link to itself!
227227
if relative_symlinks_ok:
228228
assert os.path.dirname(src) == os.path.dirname(dst)
229229
os.symlink(os.path.basename(src), dst)
@@ -418,11 +418,11 @@ def install_scripts(self, context, path):
418418
binpath = context.bin_path
419419
plen = len(path)
420420
for root, dirs, files in os.walk(path):
421-
if root == path: # at top-level, remove irrelevant dirs
421+
if root == path: # at top-level, remove irrelevant dirs
422422
for d in dirs[:]:
423423
if d not in ('common', os.name):
424424
dirs.remove(d)
425-
continue # ignore files in top level
425+
continue # ignore files in top level
426426
for f in files:
427427
if (os.name == 'nt' and f.startswith('python')
428428
and f.endswith(('.exe', '.pdb'))):
@@ -468,83 +468,76 @@ def create(env_dir, system_site_packages=False, clear=False,
468468
prompt=prompt, upgrade_deps=upgrade_deps)
469469
builder.create(env_dir)
470470

471+
471472
def main(args=None):
472-
compatible = True
473-
if sys.version_info < (3, 3):
474-
compatible = False
475-
elif not hasattr(sys, 'base_prefix'):
476-
compatible = False
477-
if not compatible:
478-
raise ValueError('This script is only for use with Python >= 3.3')
473+
import argparse
474+
475+
parser = argparse.ArgumentParser(prog=__name__,
476+
description='Creates virtual Python '
477+
'environments in one or '
478+
'more target '
479+
'directories.',
480+
epilog='Once an environment has been '
481+
'created, you may wish to '
482+
'activate it, e.g. by '
483+
'sourcing an activate script '
484+
'in its bin directory.')
485+
parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
486+
help='A directory to create the environment in.')
487+
parser.add_argument('--system-site-packages', default=False,
488+
action='store_true', dest='system_site',
489+
help='Give the virtual environment access to the '
490+
'system site-packages dir.')
491+
if os.name == 'nt':
492+
use_symlinks = False
479493
else:
480-
import argparse
481-
482-
parser = argparse.ArgumentParser(prog=__name__,
483-
description='Creates virtual Python '
484-
'environments in one or '
485-
'more target '
486-
'directories.',
487-
epilog='Once an environment has been '
488-
'created, you may wish to '
489-
'activate it, e.g. by '
490-
'sourcing an activate script '
491-
'in its bin directory.')
492-
parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
493-
help='A directory to create the environment in.')
494-
parser.add_argument('--system-site-packages', default=False,
495-
action='store_true', dest='system_site',
496-
help='Give the virtual environment access to the '
497-
'system site-packages dir.')
498-
if os.name == 'nt':
499-
use_symlinks = False
500-
else:
501-
use_symlinks = True
502-
group = parser.add_mutually_exclusive_group()
503-
group.add_argument('--symlinks', default=use_symlinks,
504-
action='store_true', dest='symlinks',
505-
help='Try to use symlinks rather than copies, '
506-
'when symlinks are not the default for '
507-
'the platform.')
508-
group.add_argument('--copies', default=not use_symlinks,
509-
action='store_false', dest='symlinks',
510-
help='Try to use copies rather than symlinks, '
511-
'even when symlinks are the default for '
512-
'the platform.')
513-
parser.add_argument('--clear', default=False, action='store_true',
514-
dest='clear', help='Delete the contents of the '
515-
'environment directory if it '
516-
'already exists, before '
517-
'environment creation.')
518-
parser.add_argument('--upgrade', default=False, action='store_true',
519-
dest='upgrade', help='Upgrade the environment '
520-
'directory to use this version '
521-
'of Python, assuming Python '
522-
'has been upgraded in-place.')
523-
parser.add_argument('--without-pip', dest='with_pip',
524-
default=True, action='store_false',
525-
help='Skips installing or upgrading pip in the '
526-
'virtual environment (pip is bootstrapped '
527-
'by default)')
528-
parser.add_argument('--prompt',
529-
help='Provides an alternative prompt prefix for '
530-
'this environment.')
531-
parser.add_argument('--upgrade-deps', default=False, action='store_true',
532-
dest='upgrade_deps',
533-
help='Upgrade core dependencies: {} to the latest '
534-
'version in PyPI'.format(
535-
' '.join(CORE_VENV_DEPS)))
536-
options = parser.parse_args(args)
537-
if options.upgrade and options.clear:
538-
raise ValueError('you cannot supply --upgrade and --clear together.')
539-
builder = EnvBuilder(system_site_packages=options.system_site,
540-
clear=options.clear,
541-
symlinks=options.symlinks,
542-
upgrade=options.upgrade,
543-
with_pip=options.with_pip,
544-
prompt=options.prompt,
545-
upgrade_deps=options.upgrade_deps)
546-
for d in options.dirs:
547-
builder.create(d)
494+
use_symlinks = True
495+
group = parser.add_mutually_exclusive_group()
496+
group.add_argument('--symlinks', default=use_symlinks,
497+
action='store_true', dest='symlinks',
498+
help='Try to use symlinks rather than copies, '
499+
'when symlinks are not the default for '
500+
'the platform.')
501+
group.add_argument('--copies', default=not use_symlinks,
502+
action='store_false', dest='symlinks',
503+
help='Try to use copies rather than symlinks, '
504+
'even when symlinks are the default for '
505+
'the platform.')
506+
parser.add_argument('--clear', default=False, action='store_true',
507+
dest='clear', help='Delete the contents of the '
508+
'environment directory if it '
509+
'already exists, before '
510+
'environment creation.')
511+
parser.add_argument('--upgrade', default=False, action='store_true',
512+
dest='upgrade', help='Upgrade the environment '
513+
'directory to use this version '
514+
'of Python, assuming Python '
515+
'has been upgraded in-place.')
516+
parser.add_argument('--without-pip', dest='with_pip',
517+
default=True, action='store_false',
518+
help='Skips installing or upgrading pip in the '
519+
'virtual environment (pip is bootstrapped '
520+
'by default)')
521+
parser.add_argument('--prompt',
522+
help='Provides an alternative prompt prefix for '
523+
'this environment.')
524+
parser.add_argument('--upgrade-deps', default=False, action='store_true',
525+
dest='upgrade_deps',
526+
help=f'Upgrade core dependencies: {" ".join(CORE_VENV_DEPS)} '
527+
'to the latest version in PyPI')
528+
options = parser.parse_args(args)
529+
if options.upgrade and options.clear:
530+
raise ValueError('you cannot supply --upgrade and --clear together.')
531+
builder = EnvBuilder(system_site_packages=options.system_site,
532+
clear=options.clear,
533+
symlinks=options.symlinks,
534+
upgrade=options.upgrade,
535+
with_pip=options.with_pip,
536+
prompt=options.prompt,
537+
upgrade_deps=options.upgrade_deps)
538+
for d in options.dirs:
539+
builder.create(d)
540+
548541

549542
if __name__ == '__main__':
550543
rc = 1

0 commit comments

Comments
 (0)