diff options
author | Éric Araujo <merwok@netwok.org> | 2011-09-19 14:10:26 (GMT) |
---|---|---|
committer | Éric Araujo <merwok@netwok.org> | 2011-09-19 14:10:26 (GMT) |
commit | becf1c58572f29d4f35f448194e3ef9c535e3be0 (patch) | |
tree | 9fdee1cd0c48cbf29829bd2a3bfa94f2214ddec6 | |
parent | 2496f331a75c9dd30ef8588ae86179131227163a (diff) | |
parent | 28df8de6af5f859a212af75d818df68fce5a9377 (diff) | |
download | cpython-becf1c58572f29d4f35f448194e3ef9c535e3be0.zip cpython-becf1c58572f29d4f35f448194e3ef9c535e3be0.tar.gz cpython-becf1c58572f29d4f35f448194e3ef9c535e3be0.tar.bz2 |
Branch merge
44 files changed, 215 insertions, 283 deletions
diff --git a/Lib/packaging/command/bdist_dumb.py b/Lib/packaging/command/bdist_dumb.py index d5773f0..309f64f 100644 --- a/Lib/packaging/command/bdist_dumb.py +++ b/Lib/packaging/command/bdist_dumb.py @@ -5,9 +5,9 @@ sys.prefix or sys.exec_prefix. """ import os - from shutil import rmtree from sysconfig import get_python_version + from packaging.util import get_platform from packaging.command.cmd import Command from packaging.errors import PackagingPlatformError @@ -24,7 +24,7 @@ class bdist_dumb(Command): "platform name to embed in generated filenames " "(default: %s)" % get_platform()), ('format=', 'f', - "archive format to create (tar, gztar, zip)"), + "archive format to create (tar, gztar, bztar, zip)"), ('keep-temp', 'k', "keep the pseudo-installation tree around after " + "creating the distribution archive"), diff --git a/Lib/packaging/command/build_clib.py b/Lib/packaging/command/build_clib.py index 4a24996..5388ccd 100644 --- a/Lib/packaging/command/build_clib.py +++ b/Lib/packaging/command/build_clib.py @@ -16,7 +16,7 @@ distribution and needed by extension modules. import os from packaging.command.cmd import Command from packaging.errors import PackagingSetupError -from packaging.compiler import customize_compiler +from packaging.compiler import customize_compiler, new_compiler from packaging import logger @@ -93,7 +93,6 @@ class build_clib(Command): return # Yech -- this is cut 'n pasted from build_ext.py! - from packaging.compiler import new_compiler self.compiler = new_compiler(compiler=self.compiler, dry_run=self.dry_run, force=self.force) diff --git a/Lib/packaging/command/build_ext.py b/Lib/packaging/command/build_ext.py index 2bffae3..4051a2d 100644 --- a/Lib/packaging/command/build_ext.py +++ b/Lib/packaging/command/build_ext.py @@ -3,6 +3,7 @@ import os import re import sys +import site import logging import sysconfig @@ -15,9 +16,6 @@ from packaging.util import newer_group from packaging.compiler.extension import Extension from packaging import logger -import site -HAS_USER_SITE = True - if os.name == 'nt': from packaging.compiler.msvccompiler import get_build_version MSVC_VERSION = int(get_build_version()) @@ -62,6 +60,8 @@ class build_ext(Command): ('inplace', 'i', "ignore build-lib and put compiled extensions into the source " + "directory alongside your pure Python modules"), + ('user', None, + "add user include, library and rpath"), ('include-dirs=', 'I', "list of directories to search for header files" + sep_by), ('define=', 'D', @@ -88,12 +88,8 @@ class build_ext(Command): "path to the SWIG executable"), ] - boolean_options = ['inplace', 'debug', 'force'] + boolean_options = ['inplace', 'debug', 'force', 'user'] - if HAS_USER_SITE: - user_options.append(('user', None, - "add user include, library and rpath")) - boolean_options.append('user') help_options = [ ('help-compiler', None, @@ -120,8 +116,7 @@ class build_ext(Command): self.compiler = None self.swig = None self.swig_opts = None - if HAS_USER_SITE: - self.user = None + self.user = None def finalize_options(self): self.set_undefined_options('build', @@ -270,7 +265,7 @@ class build_ext(Command): self.swig_opts = self.swig_opts.split(' ') # Finally add the user include and library directories if requested - if HAS_USER_SITE and self.user: + if self.user: user_include = os.path.join(site.USER_BASE, "include") user_lib = os.path.join(site.USER_BASE, "lib") if os.path.isdir(user_include): diff --git a/Lib/packaging/command/build_py.py b/Lib/packaging/command/build_py.py index 7baa6e4..0eafffa 100644 --- a/Lib/packaging/command/build_py.py +++ b/Lib/packaging/command/build_py.py @@ -388,7 +388,7 @@ class build_py(Command, Mixin2to3): self.build_module(module, module_file, package) def byte_compile(self, files): - if hasattr(sys, 'dont_write_bytecode') and sys.dont_write_bytecode: + if sys.dont_write_bytecode: logger.warning('%s: byte-compiling is disabled, skipping.', self.get_command_name()) return diff --git a/Lib/packaging/command/build_scripts.py b/Lib/packaging/command/build_scripts.py index fe14e0a..d651ae0 100644 --- a/Lib/packaging/command/build_scripts.py +++ b/Lib/packaging/command/build_scripts.py @@ -3,7 +3,7 @@ import os import re import sysconfig -import tokenize +from tokenize import detect_encoding from packaging.command.cmd import Command from packaging.util import convert_path, newer @@ -83,7 +83,7 @@ class build_scripts(Command, Mixin2to3): raise f = None else: - encoding, lines = tokenize.detect_encoding(f.readline) + encoding, lines = detect_encoding(f.readline) f.seek(0) first_line = f.readline() if not first_line: diff --git a/Lib/packaging/command/install_dist.py b/Lib/packaging/command/install_dist.py index dfe6df2..4fbca7e 100644 --- a/Lib/packaging/command/install_dist.py +++ b/Lib/packaging/command/install_dist.py @@ -14,9 +14,6 @@ from packaging.util import convert_path, change_root, get_platform from packaging.errors import PackagingOptionError -HAS_USER_SITE = True - - class install_dist(Command): description = "install everything from build directory" @@ -27,6 +24,9 @@ class install_dist(Command): "installation prefix"), ('exec-prefix=', None, "(Unix only) prefix for platform-specific files"), + ('user', None, + "install in user site-packages directory [%s]" % + get_path('purelib', '%s_user' % os.name)), ('home=', None, "(Unix only) home directory to install under"), @@ -97,15 +97,7 @@ class install_dist(Command): ] boolean_options = ['compile', 'force', 'skip-build', 'no-distinfo', - 'requested', 'no-record'] - - if HAS_USER_SITE: - user_options.append( - ('user', None, - "install in user site-packages directory [%s]" % - get_path('purelib', '%s_user' % os.name))) - - boolean_options.append('user') + 'requested', 'no-record', 'user'] negative_opt = {'no-compile': 'compile', 'no-requested': 'requested'} @@ -115,8 +107,7 @@ class install_dist(Command): self.prefix = None self.exec_prefix = None self.home = None - if HAS_USER_SITE: - self.user = False + self.user = False # These select only the installation base; it's up to the user to # specify the installation scheme (currently, that means supplying @@ -135,9 +126,8 @@ class install_dist(Command): self.install_lib = None # set to either purelib or platlib self.install_scripts = None self.install_data = None - if HAS_USER_SITE: - self.install_userbase = get_config_var('userbase') - self.install_usersite = get_path('purelib', '%s_user' % os.name) + self.install_userbase = get_config_var('userbase') + self.install_usersite = get_path('purelib', '%s_user' % os.name) self.compile = None self.optimize = None @@ -219,9 +209,8 @@ class install_dist(Command): raise PackagingOptionError( "must supply either home or prefix/exec-prefix -- not both") - if HAS_USER_SITE and self.user and ( - self.prefix or self.exec_prefix or self.home or - self.install_base or self.install_platbase): + if self.user and (self.prefix or self.exec_prefix or self.home or + self.install_base or self.install_platbase): raise PackagingOptionError( "can't combine user with prefix/exec_prefix/home or " "install_base/install_platbase") @@ -274,11 +263,9 @@ class install_dist(Command): 'exec_prefix': exec_prefix, 'srcdir': srcdir, 'projectbase': projectbase, - } - - if HAS_USER_SITE: - self.config_vars['userbase'] = self.install_userbase - self.config_vars['usersite'] = self.install_usersite + 'userbase': self.install_userbase, + 'usersite': self.install_usersite, + } self.expand_basedirs() @@ -295,9 +282,9 @@ class install_dist(Command): self.dump_dirs("post-expand_dirs()") - # Create directories in the home dir: - if HAS_USER_SITE and self.user: - self.create_home_path() + # Create directories under USERBASE + if self.user: + self.create_user_dirs() # Pick the actual directory to install all modules to: either # install_purelib or install_platlib, depending on whether this @@ -311,10 +298,8 @@ class install_dist(Command): # Convert directories from Unix /-separated syntax to the local # convention. - self.convert_paths('lib', 'purelib', 'platlib', - 'scripts', 'data', 'headers') - if HAS_USER_SITE: - self.convert_paths('userbase', 'usersite') + self.convert_paths('lib', 'purelib', 'platlib', 'scripts', + 'data', 'headers', 'userbase', 'usersite') # Well, we're not actually fully completely finalized yet: we still # have to deal with 'extra_path', which is the hack for allowing @@ -355,7 +340,7 @@ class install_dist(Command): "installation scheme is incomplete") return - if HAS_USER_SITE and self.user: + if self.user: if self.install_userbase is None: raise PackagingPlatformError( "user base directory is not specified") @@ -383,7 +368,7 @@ class install_dist(Command): def finalize_other(self): """Finalize options for non-posix platforms""" - if HAS_USER_SITE and self.user: + if self.user: if self.install_userbase is None: raise PackagingPlatformError( "user base directory is not specified") @@ -494,10 +479,8 @@ class install_dist(Command): attr = "install_" + name setattr(self, attr, change_root(self.root, getattr(self, attr))) - def create_home_path(self): - """Create directories under ~.""" - if HAS_USER_SITE and not self.user: - return + def create_user_dirs(self): + """Create directories under USERBASE as needed.""" home = convert_path(os.path.expanduser("~")) for name, path in self.config_vars.items(): if path.startswith(home) and not os.path.isdir(path): diff --git a/Lib/packaging/command/install_distinfo.py b/Lib/packaging/command/install_distinfo.py index c1f85ed..39ad960 100644 --- a/Lib/packaging/command/install_distinfo.py +++ b/Lib/packaging/command/install_distinfo.py @@ -2,14 +2,13 @@ # Forked from the former install_egg_info command by Josip Djolonga -import csv import os -import re +import csv import hashlib +from shutil import rmtree -from packaging.command.cmd import Command from packaging import logger -from shutil import rmtree +from packaging.command.cmd import Command class install_distinfo(Command): @@ -28,7 +27,7 @@ class install_distinfo(Command): ('no-record', None, "do not generate a RECORD file"), ('no-resources', None, - "do not generate a RESSOURCES list installed file"), + "do not generate a RESOURCES file"), ] boolean_options = ['requested', 'no-record', 'no-resources'] diff --git a/Lib/packaging/command/install_lib.py b/Lib/packaging/command/install_lib.py index 978f0ef..5e81b41 100644 --- a/Lib/packaging/command/install_lib.py +++ b/Lib/packaging/command/install_lib.py @@ -114,7 +114,7 @@ class install_lib(Command): return outfiles def byte_compile(self, files): - if getattr(sys, 'dont_write_bytecode'): + if sys.dont_write_bytecode: # XXX do we want this? because a Python runs without bytecode # doesn't mean that the *dists should not contain bytecode #--or does it? diff --git a/Lib/packaging/command/upload_docs.py b/Lib/packaging/command/upload_docs.py index 03dd3ec..30e37b5 100644 --- a/Lib/packaging/command/upload_docs.py +++ b/Lib/packaging/command/upload_docs.py @@ -87,6 +87,7 @@ class upload_docs(Command): content_type, body = encode_multipart(fields, files) credentials = self.username + ':' + self.password + # FIXME should use explicit encoding auth = b"Basic " + base64.encodebytes(credentials.encode()).strip() logger.info("Submitting documentation to %s", self.repository) diff --git a/Lib/packaging/compat.py b/Lib/packaging/compat.py index a82efd3..dcb58f5 100644 --- a/Lib/packaging/compat.py +++ b/Lib/packaging/compat.py @@ -1,8 +1,4 @@ -"""Compatibility helpers. - -This module provides classes, variables and imports which are used to -support packaging across Python 2.x and 3.x. -""" +"""Compatibility helpers.""" from packaging import logger @@ -10,8 +6,6 @@ from packaging import logger # XXX Having two classes with the same name is not a good thing. # XXX 2to3-related code should move from util to this module -# TODO Move common code here: PY3 (bool indicating if we're on 3.x), any, etc. - try: from packaging.util import Mixin2to3 as _Mixin2to3 _CONVERT = True diff --git a/Lib/packaging/compiler/bcppcompiler.py b/Lib/packaging/compiler/bcppcompiler.py index 63b6d8b..477cf93 100644 --- a/Lib/packaging/compiler/bcppcompiler.py +++ b/Lib/packaging/compiler/bcppcompiler.py @@ -352,5 +352,4 @@ class BCPPCompiler(CCompiler) : try: self.spawn(pp_args) except PackagingExecError as msg: - print(msg) raise CompileError(msg) diff --git a/Lib/packaging/compiler/ccompiler.py b/Lib/packaging/compiler/ccompiler.py index d274327..02e22d2 100644 --- a/Lib/packaging/compiler/ccompiler.py +++ b/Lib/packaging/compiler/ccompiler.py @@ -5,7 +5,6 @@ interface for the compiler abstraction model used by packaging. """ import os -import sys from shutil import move from packaging import logger from packaging.util import split_quoted, execute, newer_group, spawn diff --git a/Lib/packaging/compiler/unixccompiler.py b/Lib/packaging/compiler/unixccompiler.py index c857d03..3458faa 100644 --- a/Lib/packaging/compiler/unixccompiler.py +++ b/Lib/packaging/compiler/unixccompiler.py @@ -127,7 +127,7 @@ class UnixCCompiler(CCompiler): executables['ranlib'] = ["ranlib"] # Needed for the filename generation methods provided by the base - # class, CCompiler. NB. whoever instantiates/uses a particular + # class, CCompiler. XXX whoever instantiates/uses a particular # UnixCCompiler instance should set 'shared_lib_ext' -- we set a # reasonable common default here, but it's not necessarily used on all # Unices! diff --git a/Lib/packaging/create.py b/Lib/packaging/create.py index ecabca0..34a8c82 100644 --- a/Lib/packaging/create.py +++ b/Lib/packaging/create.py @@ -25,11 +25,11 @@ import sys import glob import shutil import sysconfig -import tokenize from hashlib import md5 from textwrap import dedent -from functools import cmp_to_key +from tokenize import detect_encoding from configparser import RawConfigParser + # importing this with an underscore as it should be replaced by the # dict form or another structures for all purposes from packaging._trove import all_classifiers as _CLASSIFIERS_LIST @@ -112,7 +112,7 @@ def load_setup(): been loaded before, because we are monkey patching its setup function with a particular one""" with open("setup.py", "rb") as f: - encoding, lines = tokenize.detect_encoding(f.readline) + encoding, lines = detect_encoding(f.readline) with open("setup.py", encoding=encoding) as f: imp.load_module("setup", f, "setup.py", (".py", "r", imp.PY_SOURCE)) @@ -370,21 +370,9 @@ class MainProgram: dist.data_files = [('', dist.data_files)] # add tokens in the destination paths vars = {'distribution.name': data['name']} - path_tokens = list(sysconfig.get_paths(vars=vars).items()) - - # TODO replace this with a key function - def length_comparison(x, y): - len_x = len(x[1]) - len_y = len(y[1]) - if len_x == len_y: - return 0 - elif len_x < len_y: - return -1 - else: - return 1 - + path_tokens = sysconfig.get_paths(vars=vars).items() # sort tokens to use the longest one first - path_tokens.sort(key=cmp_to_key(length_comparison)) + path_tokens = sorted(path_tokens, key=lambda x: len(x[1])) for dest, srcs in (dist.data_files or []): dest = os.path.join(sys.prefix, dest) dest = dest.replace(os.path.sep, '/') diff --git a/Lib/packaging/database.py b/Lib/packaging/database.py index b606db6..b2fcb97 100644 --- a/Lib/packaging/database.py +++ b/Lib/packaging/database.py @@ -1,12 +1,13 @@ """PEP 376 implementation.""" -import io import os import re import csv import sys import zipimport +from io import StringIO from hashlib import md5 + from packaging import logger from packaging.errors import PackagingError from packaging.version import suggest_normalized_version, VersionPredicate @@ -173,7 +174,7 @@ class Distribution: def get_resource_path(self, relative_path): with self.get_distinfo_file('RESOURCES') as resources_file: resources_reader = csv.reader(resources_file, delimiter=',', - lineterminator='\n') + lineterminator='\n') for relative, destination in resources_reader: if relative == relative_path: return destination @@ -334,7 +335,7 @@ class EggInfoDistribution: else: # FIXME handle the case where zipfile is not available zipf = zipimport.zipimporter(path) - fileobj = io.StringIO( + fileobj = StringIO( zipf.get_data('EGG-INFO/PKG-INFO').decode('utf8')) self.metadata = Metadata(fileobj=fileobj) try: diff --git a/Lib/packaging/dist.py b/Lib/packaging/dist.py index de718cc..3019b7c 100644 --- a/Lib/packaging/dist.py +++ b/Lib/packaging/dist.py @@ -537,7 +537,7 @@ Common commands: (see '--help-commands' for more) def _get_command_groups(self): """Helper function to retrieve all the command class names divided into standard commands (listed in - packaging2.command.STANDARD_COMMANDS) and extra commands (given in + packaging.command.STANDARD_COMMANDS) and extra commands (given in self.cmdclass and not standard commands). """ extra_commands = [cmd for cmd in self.cmdclass @@ -547,7 +547,7 @@ Common commands: (see '--help-commands' for more) def print_commands(self): """Print out a help message listing all available commands with a description of each. The list is divided into standard commands - (listed in packaging2.command.STANDARD_COMMANDS) and extra commands + (listed in packaging.command.STANDARD_COMMANDS) and extra commands (given in self.cmdclass and not standard commands). The descriptions come from the command class attribute 'description'. diff --git a/Lib/packaging/metadata.py b/Lib/packaging/metadata.py index a3db51c..c13acbe 100644 --- a/Lib/packaging/metadata.py +++ b/Lib/packaging/metadata.py @@ -552,16 +552,17 @@ class Metadata: return data # Mapping API + # XXX these methods should return views or sets in 3.x def keys(self): - return _version2fieldlist(self['Metadata-Version']) + return list(_version2fieldlist(self['Metadata-Version'])) def __iter__(self): for key in self.keys(): yield key def values(self): - return [self[key] for key in list(self.keys())] + return [self[key] for key in self.keys()] def items(self): - return [(key, self[key]) for key in list(self.keys())] + return [(key, self[key]) for key in self.keys()] diff --git a/Lib/packaging/pypi/simple.py b/Lib/packaging/pypi/simple.py index 76aad02..9faabaa 100644 --- a/Lib/packaging/pypi/simple.py +++ b/Lib/packaging/pypi/simple.py @@ -23,12 +23,11 @@ from packaging.version import get_version_predicate from packaging import __version__ as packaging_version from packaging.pypi.base import BaseClient from packaging.pypi.dist import (ReleasesList, EXTENSIONS, - get_infos_from_url, MD5_HASH) + get_infos_from_url, MD5_HASH) from packaging.pypi.errors import (PackagingPyPIError, DownloadError, - UnableToDownload, CantParseArchiveName, - ReleaseNotFound, ProjectNotFound) + UnableToDownload, CantParseArchiveName, + ReleaseNotFound, ProjectNotFound) from packaging.pypi.mirrors import get_mirrors -from packaging.metadata import Metadata __all__ = ['Crawler', 'DEFAULT_SIMPLE_INDEX_URL'] diff --git a/Lib/packaging/tests/__init__.py b/Lib/packaging/tests/__init__.py index 0b0e3c5..56e487a 100644 --- a/Lib/packaging/tests/__init__.py +++ b/Lib/packaging/tests/__init__.py @@ -6,17 +6,15 @@ packaging.tests package. Each test module has a name starting with to return an initialized unittest.TestSuite instance. Utility code is included in packaging.tests.support. -""" -# Put this text back for the backport -#Always import unittest from this module, it will be the right version -#(standard library unittest for 3.2 and higher, third-party unittest2 -#elease for older versions). +Always import unittest from this module: it will be unittest from the +standard library for packaging tests and unittest2 for distutils2 tests. +""" import os import sys import unittest -from test.support import TESTFN +from io import StringIO # XXX move helpers to support, add tests for them, remove things that # duplicate test.support (or keep them for the backport; needs thinking) @@ -115,9 +113,8 @@ def reap_children(): def captured_stdout(func, *args, **kw): - import io orig_stdout = getattr(sys, 'stdout') - setattr(sys, 'stdout', io.StringIO()) + setattr(sys, 'stdout', StringIO()) try: res = func(*args, **kw) sys.stdout.seek(0) diff --git a/Lib/packaging/tests/__main__.py b/Lib/packaging/tests/__main__.py index 0f175cf..51daba4 100644 --- a/Lib/packaging/tests/__main__.py +++ b/Lib/packaging/tests/__main__.py @@ -14,6 +14,8 @@ def test_main(): start_dir = os.path.dirname(__file__) top_dir = os.path.dirname(os.path.dirname(start_dir)) test_loader = unittest.TestLoader() + # XXX find out how to use unittest.main, to get command-line options + # (failfast, catch, etc.) run_unittest(test_loader.discover(start_dir, top_level_dir=top_dir)) finally: reap_children() diff --git a/Lib/packaging/tests/pypi_server.py b/Lib/packaging/tests/pypi_server.py index 41bf9c4..72cf8be 100644 --- a/Lib/packaging/tests/pypi_server.py +++ b/Lib/packaging/tests/pypi_server.py @@ -40,6 +40,7 @@ from xmlrpc.server import SimpleXMLRPCServer from packaging.tests import unittest + PYPI_DEFAULT_STATIC_PATH = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'pypiserver') @@ -219,7 +220,7 @@ class PyPIRequestHandler(SimpleHTTPRequestHandler): relative_path += "index.html" if relative_path.endswith('.tar.gz'): - with open(fs_path + relative_path, 'br') as file: + with open(fs_path + relative_path, 'rb') as file: data = file.read() headers = [('Content-type', 'application/x-gtar')] else: @@ -260,8 +261,8 @@ class PyPIRequestHandler(SimpleHTTPRequestHandler): self.send_header(header, value) self.end_headers() - if type(data) is str: - data = data.encode() + if isinstance(data, str): + data = data.encode('utf-8') self.wfile.write(data) diff --git a/Lib/packaging/tests/test_command_bdist_dumb.py b/Lib/packaging/tests/test_command_bdist_dumb.py index cc03fa5..8e2d497 100644 --- a/Lib/packaging/tests/test_command_bdist_dumb.py +++ b/Lib/packaging/tests/test_command_bdist_dumb.py @@ -35,7 +35,7 @@ class BuildDumbTestCase(support.TempdirManager, dist = Distribution({'name': 'foo', 'version': '0.1', 'py_modules': ['foo'], - 'home-page': 'xxx', 'author': 'xxx', + 'home_page': 'xxx', 'author': 'xxx', 'author_email': 'xxx'}) os.chdir(pkg_dir) cmd = bdist_dumb(dist) diff --git a/Lib/packaging/tests/test_command_build_ext.py b/Lib/packaging/tests/test_command_build_ext.py index 59d09bf..2926c37 100644 --- a/Lib/packaging/tests/test_command_build_ext.py +++ b/Lib/packaging/tests/test_command_build_ext.py @@ -9,8 +9,8 @@ from packaging.errors import (UnknownFileError, CompileError, PackagingPlatformError) from packaging.command.build_ext import build_ext from packaging.compiler.extension import Extension -from test.script_helper import assert_python_ok +from test.script_helper import assert_python_ok from packaging.tests import support, unittest, verbose @@ -18,18 +18,13 @@ class BuildExtTestCase(support.TempdirManager, support.LoggingCatcher, unittest.TestCase): def setUp(self): - # Create a simple test environment - # Note that we're making changes to sys.path super(BuildExtTestCase, self).setUp() self.tmp_dir = self.mkdtemp() self.old_user_base = site.USER_BASE site.USER_BASE = self.mkdtemp() def tearDown(self): - # Get everything back to normal - if sys.version > "2.6": - site.USER_BASE = self.old_user_base - + site.USER_BASE = self.old_user_base super(BuildExtTestCase, self).tearDown() def test_build_ext(self): @@ -78,23 +73,21 @@ class BuildExtTestCase(support.TempdirManager, old = sys.platform sys.platform = 'sunos' # fooling finalize_options - from sysconfig import _CONFIG_VARS - old_var = _CONFIG_VARS.get('Py_ENABLE_SHARED') - _CONFIG_VARS['Py_ENABLE_SHARED'] = 1 + old_var = sysconfig.get_config_var('Py_ENABLE_SHARED') + sysconfig._CONFIG_VARS['Py_ENABLE_SHARED'] = 1 try: cmd.ensure_finalized() finally: sys.platform = old if old_var is None: - del _CONFIG_VARS['Py_ENABLE_SHARED'] + del sysconfig._CONFIG_VARS['Py_ENABLE_SHARED'] else: - _CONFIG_VARS['Py_ENABLE_SHARED'] = old_var + sysconfig._CONFIG_VARS['Py_ENABLE_SHARED'] = old_var # make sure we get some library dirs under solaris self.assertGreater(len(cmd.library_dirs), 0) - @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') def test_user_site(self): dist = Distribution({'name': 'xx'}) cmd = build_ext(dist) diff --git a/Lib/packaging/tests/test_command_build_py.py b/Lib/packaging/tests/test_command_build_py.py index 243a863..4eeb34e 100644 --- a/Lib/packaging/tests/test_command_build_py.py +++ b/Lib/packaging/tests/test_command_build_py.py @@ -99,8 +99,6 @@ class BuildPyTestCase(support.TempdirManager, os.chdir(cwd) sys.stdout = old_stdout - @unittest.skipUnless(hasattr(sys, 'dont_write_bytecode'), - 'sys.dont_write_bytecode not supported') def test_dont_write_bytecode(self): # makes sure byte_compile is not used pkg_dir, dist = self.create_dist() diff --git a/Lib/packaging/tests/test_command_install_dist.py b/Lib/packaging/tests/test_command_install_dist.py index 8f90aff..808b568 100644 --- a/Lib/packaging/tests/test_command_install_dist.py +++ b/Lib/packaging/tests/test_command_install_dist.py @@ -72,7 +72,6 @@ class InstallTestCase(support.TempdirManager, check_path(cmd.install_scripts, os.path.join(destination, "bin")) check_path(cmd.install_data, destination) - @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') def test_user_site(self): # test install with --user # preparing the environment for the test @@ -173,12 +172,11 @@ class InstallTestCase(support.TempdirManager, cmd.home = 'home' self.assertRaises(PackagingOptionError, cmd.finalize_options) - if sys.version >= '2.6': - # can't combine user with with prefix/exec_prefix/home or - # install_(plat)base - cmd.prefix = None - cmd.user = 'user' - self.assertRaises(PackagingOptionError, cmd.finalize_options) + # can't combine user with with prefix/exec_prefix/home or + # install_(plat)base + cmd.prefix = None + cmd.user = 'user' + self.assertRaises(PackagingOptionError, cmd.finalize_options) def test_old_record(self): # test pre-PEP 376 --record option (outside dist-info dir) diff --git a/Lib/packaging/tests/test_command_install_lib.py b/Lib/packaging/tests/test_command_install_lib.py index b46f3bd..9118e68 100644 --- a/Lib/packaging/tests/test_command_install_lib.py +++ b/Lib/packaging/tests/test_command_install_lib.py @@ -7,13 +7,6 @@ from packaging.command.install_lib import install_lib from packaging.compiler.extension import Extension from packaging.errors import PackagingOptionError -try: - no_bytecode = sys.dont_write_bytecode - bytecode_support = True -except AttributeError: - no_bytecode = False - bytecode_support = False - class InstallLibTestCase(support.TempdirManager, support.LoggingCatcher, @@ -40,7 +33,7 @@ class InstallLibTestCase(support.TempdirManager, cmd.finalize_options() self.assertEqual(cmd.optimize, 2) - @unittest.skipIf(no_bytecode, 'byte-compile not supported') + @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled') def test_byte_compile(self): pkg_dir, dist = self.create_dist() cmd = install_lib(dist) @@ -89,8 +82,6 @@ class InstallLibTestCase(support.TempdirManager, # get_input should return 2 elements self.assertEqual(len(cmd.get_inputs()), 2) - @unittest.skipUnless(bytecode_support, - 'sys.dont_write_bytecode not supported') def test_dont_write_bytecode(self): # makes sure byte_compile is not used pkg_dir, dist = self.create_dist() diff --git a/Lib/packaging/tests/test_command_register.py b/Lib/packaging/tests/test_command_register.py index 9c64e2d..9872e2e 100644 --- a/Lib/packaging/tests/test_command_register.py +++ b/Lib/packaging/tests/test_command_register.py @@ -99,7 +99,7 @@ class RegisterTestCase(support.TempdirManager, def _get_cmd(self, metadata=None): if metadata is None: - metadata = {'home-page': 'xxx', 'author': 'xxx', + metadata = {'home_page': 'xxx', 'author': 'xxx', 'author_email': 'xxx', 'name': 'xxx', 'version': 'xxx'} pkg_info, dist = self.create_dist(**metadata) diff --git a/Lib/packaging/tests/test_command_upload.py b/Lib/packaging/tests/test_command_upload.py index dbb4db7..82d7360 100644 --- a/Lib/packaging/tests/test_command_upload.py +++ b/Lib/packaging/tests/test_command_upload.py @@ -1,6 +1,5 @@ """Tests for packaging.command.upload.""" import os -import sys from packaging.command.upload import upload from packaging.dist import Distribution @@ -103,22 +102,23 @@ class UploadTestCase(support.TempdirManager, support.EnvironRestorer, command, pyversion, filename = 'xxx', '3.3', path dist_files = [(command, pyversion, filename)] - # lets run it - pkg_dir, dist = self.create_dist(dist_files=dist_files, author='dédé') + # let's run it + dist = self.create_dist(dist_files=dist_files, author='dédé')[1] cmd = upload(dist) cmd.ensure_finalized() cmd.repository = self.pypi.full_address cmd.run() - # what did we send ? + # what did we send? handler, request_data = self.pypi.requests[-1] headers = handler.headers - #self.assertIn('dédé', str(request_data)) + self.assertIn('dédé'.encode('utf-8'), request_data) self.assertIn(b'xxx', request_data) self.assertEqual(int(headers['content-length']), len(request_data)) self.assertLess(int(headers['content-length']), 2500) - self.assertTrue(headers['content-type'].startswith('multipart/form-data')) + self.assertTrue(headers['content-type'].startswith( + 'multipart/form-data')) self.assertEqual(handler.command, 'POST') self.assertNotIn('\n', headers['authorization']) @@ -132,20 +132,16 @@ class UploadTestCase(support.TempdirManager, support.EnvironRestorer, self.write_file(os.path.join(docs_path, "index.html"), "yellow") self.write_file(self.rc, PYPIRC) - # lets run it - pkg_dir, dist = self.create_dist(dist_files=dist_files, author='dédé') + # let's run it + dist = self.create_dist(dist_files=dist_files, author='dédé')[1] cmd = upload(dist) cmd.get_finalized_command("build").run() cmd.upload_docs = True cmd.ensure_finalized() cmd.repository = self.pypi.full_address - prev_dir = os.getcwd() - try: - os.chdir(self.tmp_dir) - cmd.run() - finally: - os.chdir(prev_dir) + os.chdir(self.tmp_dir) + cmd.run() handler, request_data = self.pypi.requests[-1] action, name, content = request_data.split( diff --git a/Lib/packaging/tests/test_command_upload_docs.py b/Lib/packaging/tests/test_command_upload_docs.py index 4070279..f443727 100644 --- a/Lib/packaging/tests/test_command_upload_docs.py +++ b/Lib/packaging/tests/test_command_upload_docs.py @@ -1,6 +1,5 @@ """Tests for packaging.command.upload_docs.""" import os -import sys import shutil import zipfile try: @@ -19,7 +18,7 @@ try: from packaging.tests.pypi_server import PyPIServerTestCase except ImportError: threading = None - PyPIServerTestCase = object + PyPIServerTestCase = unittest.TestCase PYPIRC = """\ @@ -52,34 +51,27 @@ class UploadDocsTestCase(support.TempdirManager, def test_default_uploaddir(self): sandbox = self.mkdtemp() - previous = os.getcwd() os.chdir(sandbox) - try: - os.mkdir("build") - self.prepare_sample_dir("build") - self.cmd.ensure_finalized() - self.assertEqual(self.cmd.upload_dir, os.path.join("build", "docs")) - finally: - os.chdir(previous) + os.mkdir("build") + self.prepare_sample_dir("build") + self.cmd.ensure_finalized() + self.assertEqual(self.cmd.upload_dir, os.path.join("build", "docs")) def test_default_uploaddir_looks_for_doc_also(self): sandbox = self.mkdtemp() - previous = os.getcwd() os.chdir(sandbox) - try: - os.mkdir("build") - self.prepare_sample_dir("build") - os.rename(os.path.join("build", "docs"), os.path.join("build", "doc")) - self.cmd.ensure_finalized() - self.assertEqual(self.cmd.upload_dir, os.path.join("build", "doc")) - finally: - os.chdir(previous) + os.mkdir("build") + self.prepare_sample_dir("build") + os.rename(os.path.join("build", "docs"), os.path.join("build", "doc")) + self.cmd.ensure_finalized() + self.assertEqual(self.cmd.upload_dir, os.path.join("build", "doc")) def prepare_sample_dir(self, sample_dir=None): if sample_dir is None: sample_dir = self.mkdtemp() os.mkdir(os.path.join(sample_dir, "docs")) - self.write_file(os.path.join(sample_dir, "docs", "index.html"), "Ce mortel ennui") + self.write_file(os.path.join(sample_dir, "docs", "index.html"), + "Ce mortel ennui") self.write_file(os.path.join(sample_dir, "index.html"), "Oh la la") return sample_dir @@ -108,9 +100,8 @@ class UploadDocsTestCase(support.TempdirManager, self.assertTrue(handler.headers['content-type'] .startswith('multipart/form-data;')) - action, name, version, content =\ - request_data.split("----------------GHSKFJDLGDS7543FJKLFHRE75642756743254".encode())[1:5] - + action, name, version, content = request_data.split( + b'----------------GHSKFJDLGDS7543FJKLFHRE75642756743254')[1:5] # check that we picked the right chunks self.assertIn(b'name=":action"', action) @@ -126,27 +117,25 @@ class UploadDocsTestCase(support.TempdirManager, @unittest.skipIf(_ssl is None, 'Needs SSL support') def test_https_connection(self): - https_called = False - - orig_https = upload_docs_mod.http.client.HTTPSConnection + self.https_called = False + self.addCleanup( + setattr, upload_docs_mod.http.client, 'HTTPSConnection', + upload_docs_mod.http.client.HTTPSConnection) def https_conn_wrapper(*args): - nonlocal https_called - https_called = True + self.https_called = True # the testing server is http return upload_docs_mod.http.client.HTTPConnection(*args) upload_docs_mod.http.client.HTTPSConnection = https_conn_wrapper - try: - self.prepare_command() - self.cmd.run() - self.assertFalse(https_called) - self.cmd.repository = self.cmd.repository.replace("http", "https") - self.cmd.run() - self.assertTrue(https_called) - finally: - upload_docs_mod.http.client.HTTPSConnection = orig_https + self.prepare_command() + self.cmd.run() + self.assertFalse(self.https_called) + + self.cmd.repository = self.cmd.repository.replace("http", "https") + self.cmd.run() + self.assertTrue(self.https_called) def test_handling_response(self): self.pypi.default_response_status = '403 Forbidden' @@ -155,7 +144,8 @@ class UploadDocsTestCase(support.TempdirManager, self.assertIn('Upload failed (403): Forbidden', self.get_logs()[-1]) self.pypi.default_response_status = '301 Moved Permanently' - self.pypi.default_response_headers.append(("Location", "brand_new_location")) + self.pypi.default_response_headers.append( + ("Location", "brand_new_location")) self.cmd.run() self.assertIn('brand_new_location', self.get_logs()[-1]) @@ -185,6 +175,7 @@ class UploadDocsTestCase(support.TempdirManager, self.assertTrue(record, "should report the response") self.assertIn(self.pypi.default_response_data, record) + def test_suite(): return unittest.makeSuite(UploadDocsTestCase) diff --git a/Lib/packaging/tests/test_create.py b/Lib/packaging/tests/test_create.py index a82ab43..b896619 100644 --- a/Lib/packaging/tests/test_create.py +++ b/Lib/packaging/tests/test_create.py @@ -1,8 +1,8 @@ """Tests for packaging.create.""" -import io import os import sys import sysconfig +from io import StringIO from textwrap import dedent from packaging.create import MainProgram, ask_yn, ask, main @@ -20,8 +20,8 @@ class CreateTestCase(support.TempdirManager, super(CreateTestCase, self).setUp() self._stdin = sys.stdin # TODO use Inputs self._stdout = sys.stdout - sys.stdin = io.StringIO() - sys.stdout = io.StringIO() + sys.stdin = StringIO() + sys.stdout = StringIO() self._cwd = os.getcwd() self.wdir = self.mkdtemp() os.chdir(self.wdir) @@ -135,7 +135,8 @@ class CreateTestCase(support.TempdirManager, sys.stdin.seek(0) main() - with open(os.path.join(self.wdir, 'setup.cfg'), encoding='utf-8') as fp: + path = os.path.join(self.wdir, 'setup.cfg') + with open(path, encoding='utf-8') as fp: contents = fp.read() self.assertEqual(contents, dedent("""\ @@ -210,7 +211,9 @@ ho, baby! sys.stdin.seek(0) # FIXME Out of memory error. main() - with open(os.path.join(self.wdir, 'setup.cfg'), encoding='utf-8') as fp: + + path = os.path.join(self.wdir, 'setup.cfg') + with open(path, encoding='utf-8') as fp: contents = fp.read() self.assertEqual(contents, dedent("""\ diff --git a/Lib/packaging/tests/test_database.py b/Lib/packaging/tests/test_database.py index b36e66d..6f63e19 100644 --- a/Lib/packaging/tests/test_database.py +++ b/Lib/packaging/tests/test_database.py @@ -49,8 +49,8 @@ class FakeDistsMixin: # distributions tmpdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tmpdir) - self.fake_dists_path = os.path.join(tmpdir, 'fake_dists') - self.fake_dists_path = os.path.realpath(self.fake_dists_path) + self.fake_dists_path = os.path.realpath( + os.path.join(tmpdir, 'fake_dists')) fake_dists_src = os.path.abspath( os.path.join(os.path.dirname(__file__), 'fake_dists')) shutil.copytree(fake_dists_src, self.fake_dists_path) @@ -58,6 +58,7 @@ class FakeDistsMixin: # back (to avoid getting a read-only copy of a read-only file). we # could pass a custom copy_function to change the mode of files, but # shutil gives no control over the mode of directories :( + # see http://bugs.python.org/issue1666318 for root, dirs, files in os.walk(self.fake_dists_path): os.chmod(root, 0o755) for f in files: diff --git a/Lib/packaging/tests/test_depgraph.py b/Lib/packaging/tests/test_depgraph.py index 64c22eb..8833302 100644 --- a/Lib/packaging/tests/test_depgraph.py +++ b/Lib/packaging/tests/test_depgraph.py @@ -1,10 +1,11 @@ """Tests for packaging.depgraph """ -import io import os import re import sys -import packaging.database +from io import StringIO + from packaging import depgraph +from packaging.database import get_distribution, enable_cache, disable_cache from packaging.tests import unittest, support from packaging.tests.support import requires_zlib @@ -30,13 +31,13 @@ class DepGraphTestCase(support.LoggingCatcher, path = os.path.abspath(path) sys.path.insert(0, path) self.addCleanup(sys.path.remove, path) - self.addCleanup(packaging.database.enable_cache) - packaging.database.disable_cache() + self.addCleanup(enable_cache) + disable_cache() def test_generate_graph(self): dists = [] for name in self.DISTROS_DIST: - dist = packaging.database.get_distribution(name) + dist = get_distribution(name) self.assertNotEqual(dist, None) dists.append(dist) @@ -61,7 +62,7 @@ class DepGraphTestCase(support.LoggingCatcher, def test_generate_graph_egg(self): dists = [] for name in self.DISTROS_DIST + self.DISTROS_EGG: - dist = packaging.database.get_distribution(name, use_egg_info=True) + dist = get_distribution(name, use_egg_info=True) self.assertNotEqual(dist, None) dists.append(dist) @@ -104,7 +105,7 @@ class DepGraphTestCase(support.LoggingCatcher, def test_dependent_dists(self): dists = [] for name in self.DISTROS_DIST: - dist = packaging.database.get_distribution(name) + dist = get_distribution(name) self.assertNotEqual(dist, None) dists.append(dist) @@ -123,7 +124,7 @@ class DepGraphTestCase(support.LoggingCatcher, def test_dependent_dists_egg(self): dists = [] for name in self.DISTROS_DIST + self.DISTROS_EGG: - dist = packaging.database.get_distribution(name, use_egg_info=True) + dist = get_distribution(name, use_egg_info=True) self.assertNotEqual(dist, None) dists.append(dist) @@ -158,12 +159,12 @@ class DepGraphTestCase(support.LoggingCatcher, dists = [] for name in self.DISTROS_DIST + self.DISTROS_EGG: - dist = packaging.database.get_distribution(name, use_egg_info=True) + dist = get_distribution(name, use_egg_info=True) self.assertNotEqual(dist, None) dists.append(dist) graph = depgraph.generate_graph(dists) - buf = io.StringIO() + buf = StringIO() depgraph.graph_to_dot(graph, buf) buf.seek(0) matches = [] @@ -189,12 +190,12 @@ class DepGraphTestCase(support.LoggingCatcher, dists = [] for name in self.DISTROS_DIST + self.DISTROS_EGG: - dist = packaging.database.get_distribution(name, use_egg_info=True) + dist = get_distribution(name, use_egg_info=True) self.assertNotEqual(dist, None) dists.append(dist) graph = depgraph.generate_graph(dists) - buf = io.StringIO() + buf = StringIO() depgraph.graph_to_dot(graph, buf, skip_disconnected=False) buf.seek(0) lines = buf.readlines() @@ -250,12 +251,12 @@ class DepGraphTestCase(support.LoggingCatcher, dists = [] for name in self.DISTROS_DIST + self.DISTROS_EGG + self.BAD_EGGS: - dist = packaging.database.get_distribution(name, use_egg_info=True) + dist = get_distribution(name, use_egg_info=True) self.assertNotEqual(dist, None) dists.append(dist) graph = depgraph.generate_graph(dists) - buf = io.StringIO() + buf = StringIO() depgraph.graph_to_dot(graph, buf) buf.seek(0) matches = [] @@ -273,7 +274,7 @@ class DepGraphTestCase(support.LoggingCatcher, def test_repr(self): dists = [] for name in self.DISTROS_DIST + self.DISTROS_EGG + self.BAD_EGGS: - dist = packaging.database.get_distribution(name, use_egg_info=True) + dist = get_distribution(name, use_egg_info=True) self.assertNotEqual(dist, None) dists.append(dist) @@ -282,7 +283,7 @@ class DepGraphTestCase(support.LoggingCatcher, @requires_zlib def test_main(self): - tempout = io.StringIO() + tempout = StringIO() old = sys.stdout sys.stdout = tempout oldargv = sys.argv[:] diff --git a/Lib/packaging/tests/test_dist.py b/Lib/packaging/tests/test_dist.py index bd86245..f912c6f 100644 --- a/Lib/packaging/tests/test_dist.py +++ b/Lib/packaging/tests/test_dist.py @@ -3,13 +3,14 @@ import os import sys import logging import textwrap + import packaging.dist from packaging.dist import Distribution from packaging.command import set_command from packaging.command.cmd import Command from packaging.errors import PackagingModuleError, PackagingOptionError -from packaging.tests import TESTFN, captured_stdout +from packaging.tests import captured_stdout from packaging.tests import support, unittest from packaging.tests.support import create_distribution from test.support import unload @@ -48,12 +49,13 @@ class DistributionTestCase(support.TempdirManager, @unittest.skip('needs to be updated') def test_debug_mode(self): - self.addCleanup(os.unlink, TESTFN) - with open(TESTFN, "w") as f: + tmpdir = self.mkdtemp() + setupcfg = os.path.join(tmpdir, 'setup.cfg') + with open(setupcfg, "w") as f: f.write("[global]\n") f.write("command_packages = foo.bar, splat") - files = [TESTFN] + files = [setupcfg] sys.argv.append("build") __, stdout = captured_stdout(create_distribution, files) self.assertEqual(stdout, '') @@ -70,7 +72,7 @@ class DistributionTestCase(support.TempdirManager, Distribution(attrs={'author': 'xxx', 'name': 'xxx', 'version': '1.2', - 'home-page': 'xxxx', + 'home_page': 'xxxx', 'badoptname': 'xxx'}) logs = self.get_logs(logging.WARNING) self.assertEqual(len(logs), 1) @@ -80,7 +82,7 @@ class DistributionTestCase(support.TempdirManager, # an empty options dictionary should not stay in the # list of attributes dist = Distribution(attrs={'author': 'xxx', 'name': 'xxx', - 'version': '1.2', 'home-page': 'xxxx', + 'version': '1.2', 'home_page': 'xxxx', 'options': {}}) self.assertEqual([], self.get_logs(logging.WARNING)) @@ -97,7 +99,7 @@ class DistributionTestCase(support.TempdirManager, dist = Distribution(attrs={'author': 'xxx', 'name': 'xxx', 'version': 'xxx', - 'home-page': 'xxxx', + 'home_page': 'xxxx', 'options': {'sdist': {'owner': 'root'}}}) self.assertIn('owner', dist.get_option_dict('sdist')) diff --git a/Lib/packaging/tests/test_install.py b/Lib/packaging/tests/test_install.py index cf0817c..cc1f5d3 100644 --- a/Lib/packaging/tests/test_install.py +++ b/Lib/packaging/tests/test_install.py @@ -1,8 +1,8 @@ """Tests for the packaging.install module.""" import os import logging -from sysconfig import is_python_build from tempfile import mkstemp +from sysconfig import is_python_build from packaging import install from packaging.pypi.xmlrpc import Client diff --git a/Lib/packaging/tests/test_metadata.py b/Lib/packaging/tests/test_metadata.py index 68b3d97..6b7dd38 100644 --- a/Lib/packaging/tests/test_metadata.py +++ b/Lib/packaging/tests/test_metadata.py @@ -101,7 +101,7 @@ class MetadataTestCase(LoggingCatcher, # XXX caveat: the keys method and friends are not 3.x-style views # should be changed or documented - self.assertEqual(list(metadata), list(metadata.keys())) + self.assertEqual(list(metadata), metadata.keys()) def test_read_metadata(self): fields = {'name': 'project', @@ -301,7 +301,7 @@ class MetadataTestCase(LoggingCatcher, Metadata(mapping={'author': 'xxx', 'name': 'xxx', 'version': 'xxx', - 'home-page': 'xxxx'}) + 'home_page': 'xxxx'}) logs = self.get_logs(logging.WARNING) self.assertEqual(1, len(logs)) self.assertIn('not a valid version', logs[0]) diff --git a/Lib/packaging/tests/test_mixin2to3.py b/Lib/packaging/tests/test_mixin2to3.py index 926f754..14a7487 100644 --- a/Lib/packaging/tests/test_mixin2to3.py +++ b/Lib/packaging/tests/test_mixin2to3.py @@ -9,7 +9,6 @@ class Mixin2to3TestCase(support.TempdirManager, support.LoggingCatcher, unittest.TestCase): - @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') def test_convert_code_only(self): # used to check if code gets converted properly. code = "print 'test'" @@ -26,7 +25,6 @@ class Mixin2to3TestCase(support.TempdirManager, self.assertEqual(expected, converted) - @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') def test_doctests_only(self): # used to check if doctests gets converted properly. doctest = textwrap.dedent('''\ @@ -57,7 +55,6 @@ class Mixin2to3TestCase(support.TempdirManager, self.assertEqual(expected, converted) - @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') def test_additional_fixers(self): # used to check if use_2to3_fixers works code = 'type(x) is not T' diff --git a/Lib/packaging/tests/test_pypi_server.py b/Lib/packaging/tests/test_pypi_server.py index 2c4ec0d..057c494 100644 --- a/Lib/packaging/tests/test_pypi_server.py +++ b/Lib/packaging/tests/test_pypi_server.py @@ -1,13 +1,12 @@ """Tests for packaging.command.bdist.""" -import sys - import urllib.request import urllib.parse import urllib.error try: import threading - from packaging.tests.pypi_server import PyPIServer, PYPI_DEFAULT_STATIC_PATH + from packaging.tests.pypi_server import ( + PyPIServer, PYPI_DEFAULT_STATIC_PATH) except ImportError: threading = None PyPIServer = None @@ -32,18 +31,19 @@ class PyPIServerTest(unittest.TestCase): headers = {"X-test-header": "Mister Iceberg"} - request = urllib.request.Request(server.full_address, data, headers) + request = urllib.request.Request( + server.full_address, data, headers) urllib.request.urlopen(request) self.assertEqual(len(server.requests), 1) handler, request_data = server.requests[-1] self.assertIn(data, request_data) self.assertIn("x-test-header", handler.headers) - self.assertEqual(handler.headers["x-test-header"], "Mister Iceberg") + self.assertEqual(handler.headers["x-test-header"], + "Mister Iceberg") finally: server.stop() - def test_serve_static_content(self): # PYPI Mocked server can serve static content from disk. @@ -74,7 +74,8 @@ class PyPIServerTest(unittest.TestCase): self.assertTrue(uses_local_files_for(server, "/simple/index.html")) # and another one in another root path - self.assertTrue(uses_local_files_for(server, "/external/index.html")) + self.assertTrue(uses_local_files_for(server, + "/external/index.html")) finally: server.stop() diff --git a/Lib/packaging/tests/test_run.py b/Lib/packaging/tests/test_run.py index 6a3b8fb..d9460cf 100644 --- a/Lib/packaging/tests/test_run.py +++ b/Lib/packaging/tests/test_run.py @@ -2,11 +2,10 @@ import os import sys -import shutil from io import StringIO from packaging import install -from packaging.tests import unittest, support, TESTFN +from packaging.tests import unittest, support from packaging.run import main from test.script_helper import assert_python_ok @@ -35,28 +34,14 @@ class RunTestCase(support.TempdirManager, def setUp(self): super(RunTestCase, self).setUp() self.old_stdout = sys.stdout - self.cleanup_testfn() self.old_argv = sys.argv, sys.argv[:] def tearDown(self): sys.stdout = self.old_stdout - self.cleanup_testfn() sys.argv = self.old_argv[0] sys.argv[:] = self.old_argv[1] super(RunTestCase, self).tearDown() - def cleanup_testfn(self): - path = TESTFN - if os.path.isfile(path): - os.remove(path) - elif os.path.isdir(path): - shutil.rmtree(path) - - def write_setup(self, text, path=TESTFN): - with open(path, "w") as fp: - fp.write(text) - return path - # TODO restore the tests removed six months ago and port them to pysetup def test_install(self): diff --git a/Lib/packaging/tests/test_util.py b/Lib/packaging/tests/test_util.py index c3d91aa..3d71a96 100644 --- a/Lib/packaging/tests/test_util.py +++ b/Lib/packaging/tests/test_util.py @@ -15,7 +15,7 @@ from packaging.errors import ( from packaging import util from packaging.dist import Distribution from packaging.util import ( - convert_path, change_root, split_quoted, strtobool, + convert_path, change_root, split_quoted, strtobool, run_2to3, get_compiler_versions, _MAC_OS_X_LD_VERSION, byte_compile, find_packages, spawn, get_pypirc_path, generate_pypirc, read_pypirc, resolve_name, iglob, RICH_GLOB, egginfo_to_distinfo, is_setuptools, is_distutils, is_packaging, @@ -319,8 +319,6 @@ class UtilTestCase(support.EnvironRestorer, res = get_compiler_versions() self.assertEqual(res[2], None) - @unittest.skipUnless(hasattr(sys, 'dont_write_bytecode'), - 'sys.dont_write_bytecode not supported') def test_dont_write_bytecode(self): # makes sure byte_compile raise a PackagingError # if sys.dont_write_bytecode is True @@ -374,7 +372,7 @@ class UtilTestCase(support.EnvironRestorer, res = find_packages([root], ['pkg1.pkg2']) self.assertEqual(set(res), set(['pkg1', 'pkg5', 'pkg1.pkg3', - 'pkg1.pkg3.pkg6'])) + 'pkg1.pkg3.pkg6'])) def test_resolve_name(self): self.assertIs(str, resolve_name('builtins.str')) @@ -407,7 +405,6 @@ class UtilTestCase(support.EnvironRestorer, finally: sys.path.remove(tmp_dir) - @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') def test_run_2to3_on_code(self): content = "print 'test'" converted_content = "print('test')" @@ -416,13 +413,11 @@ class UtilTestCase(support.EnvironRestorer, file_handle.write(content) file_handle.flush() file_handle.seek(0) - from packaging.util import run_2to3 run_2to3([file_name]) new_content = "".join(file_handle.read()) file_handle.close() self.assertEqual(new_content, converted_content) - @unittest.skipIf(sys.version < '2.6', 'requires Python 2.6 or higher') def test_run_2to3_on_doctests(self): # to check if text files containing doctests only get converted. content = ">>> print 'test'\ntest\n" @@ -432,7 +427,6 @@ class UtilTestCase(support.EnvironRestorer, file_handle.write(content) file_handle.flush() file_handle.seek(0) - from packaging.util import run_2to3 run_2to3([file_name], doctests_only=True) new_content = "".join(file_handle.readlines()) file_handle.close() diff --git a/Lib/packaging/util.py b/Lib/packaging/util.py index 49c5991..89f5389 100644 --- a/Lib/packaging/util.py +++ b/Lib/packaging/util.py @@ -326,7 +326,7 @@ def byte_compile(py_files, optimize=0, force=False, prefix=None, """ # nothing is done if sys.dont_write_bytecode is True # FIXME this should not raise an error - if hasattr(sys, 'dont_write_bytecode') and sys.dont_write_bytecode: + if sys.dont_write_bytecode: raise PackagingByteCompileError('byte-compiling is disabled.') # First, if the caller didn't force us into direct or indirect mode, @@ -346,8 +346,10 @@ def byte_compile(py_files, optimize=0, force=False, prefix=None, # run it with the appropriate flags. if not direct: from tempfile import mkstemp - # XXX script_fd may leak, use something better than mkstemp + # XXX use something better than mkstemp script_fd, script_name = mkstemp(".py") + os.close(script_fd) + script_fd = None logger.info("writing byte-compilation script '%s'", script_name) if not dry_run: if script_fd is not None: diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index 2dd20cd..c561c13 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -307,9 +307,9 @@ class ImpLoader: def get_filename(self, fullname=None): fullname = self._fix_name(fullname) mod_type = self.etc[2] - if self.etc[2]==imp.PKG_DIRECTORY: + if mod_type==imp.PKG_DIRECTORY: return self._get_delegate().get_filename() - elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION): + elif mod_type in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION): return self.filename return None diff --git a/Lib/shutil.py b/Lib/shutil.py index 0c096cb..2025ef8 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -391,7 +391,7 @@ def _make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0, compress_ext['bzip2'] = '.bz2' # flags for compression program, each element of list will be an argument - if compress is not None and compress not in compress_ext.keys(): + if compress is not None and compress not in compress_ext: raise ValueError("bad value for 'compress', or compression format not " "supported : {0}".format(compress)) @@ -497,7 +497,7 @@ _ARCHIVE_FORMATS = { 'gztar': (_make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"), 'bztar': (_make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"), 'tar': (_make_tarball, [('compress', None)], "uncompressed tar file"), - 'zip': (_make_zipfile, [],"ZIP file") + 'zip': (_make_zipfile, [], "ZIP file") } if _BZ2_SUPPORTED: @@ -530,7 +530,7 @@ def register_archive_format(name, function, extra_args=None, description=''): if not isinstance(extra_args, (tuple, list)): raise TypeError('extra_args needs to be a sequence') for element in extra_args: - if not isinstance(element, (tuple, list)) or len(element) !=2 : + if not isinstance(element, (tuple, list)) or len(element) !=2: raise TypeError('extra_args elements are : (arg_name, value)') _ARCHIVE_FORMATS[name] = (function, extra_args, description) @@ -682,7 +682,7 @@ def _unpack_zipfile(filename, extract_dir): if not name.endswith('/'): # file data = zip.read(info.filename) - f = open(target,'wb') + f = open(target, 'wb') try: f.write(data) finally: diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py index 65438af..e46f935 100755 --- a/Lib/test/regrtest.py +++ b/Lib/test/regrtest.py @@ -965,7 +965,9 @@ class saved_test_environment: 'warnings.filters', 'asyncore.socket_map', 'logging._handlers', 'logging._handlerList', 'sys.gettrace', 'sys.warnoptions', 'threading._dangling', - 'multiprocessing.process._dangling') + 'multiprocessing.process._dangling', + 'sysconfig._CONFIG_VARS', 'sysconfig._SCHEMES', + ) def get_sys_argv(self): return id(sys.argv), sys.argv, sys.argv[:] @@ -1083,6 +1085,27 @@ class saved_test_environment: multiprocessing.process._dangling.clear() multiprocessing.process._dangling.update(saved) + def get_sysconfig__CONFIG_VARS(self): + # make sure the dict is initialized + sysconfig.get_config_var('prefix') + return (id(sysconfig._CONFIG_VARS), sysconfig._CONFIG_VARS, + dict(sysconfig._CONFIG_VARS)) + def restore_sysconfig__CONFIG_VARS(self, saved): + sysconfig._CONFIG_VARS = saved[1] + sysconfig._CONFIG_VARS.clear() + sysconfig._CONFIG_VARS.update(saved[2]) + + def get_sysconfig__SCHEMES(self): + # it's mildly evil to look at the internal attribute, but it's easier + # than copying a RawConfigParser object + return (id(sysconfig._SCHEMES), sysconfig._SCHEMES._sections, + sysconfig._SCHEMES._sections.copy()) + def restore_sysconfig__SCHEMES(self, saved): + sysconfig._SCHEMES._sections = saved[1] + sysconfig._SCHEMES._sections.clear() + sysconfig._SCHEMES._sections.update(saved[2]) + + def resource_info(self): for name in self.resources: method_suffix = name.replace('.', '_') diff --git a/Lib/test/test_sysconfig.py b/Lib/test/test_sysconfig.py index 773d523..08b40a1 100644 --- a/Lib/test/test_sysconfig.py +++ b/Lib/test/test_sysconfig.py @@ -39,7 +39,7 @@ class TestSysConfig(unittest.TestCase): self._config_vars = copy(sysconfig._CONFIG_VARS) self._added_envvars = [] self._changed_envvars = [] - for var in ('MACOSX_DEPLOYMENT_TARGET', 'Path'): + for var in ('MACOSX_DEPLOYMENT_TARGET', 'PATH'): if var in os.environ: self._changed_envvars.append((var, os.environ[var])) else: @@ -87,21 +87,19 @@ class TestSysConfig(unittest.TestCase): scheme = get_paths() default_scheme = _get_default_scheme() wanted = _expand_vars(default_scheme, None) - wanted = list(wanted.items()) - wanted.sort() - scheme = list(scheme.items()) - scheme.sort() + wanted = sorted(wanted.items()) + scheme = sorted(scheme.items()) self.assertEqual(scheme, wanted) def test_get_path(self): - # xxx make real tests here + # XXX make real tests here for scheme in _SCHEMES: for name in _SCHEMES[scheme]: res = get_path(name, scheme) def test_get_config_vars(self): cvars = get_config_vars() - self.assertTrue(isinstance(cvars, dict)) + self.assertIsInstance(cvars, dict) self.assertTrue(cvars) def test_get_platform(self): @@ -236,8 +234,8 @@ class TestSysConfig(unittest.TestCase): # On Windows, the EXE needs to know where pythonXY.dll is at so we have # to add the directory to the path. if sys.platform == "win32": - os.environ["Path"] = "{};{}".format( - os.path.dirname(sys.executable), os.environ["Path"]) + os.environ["PATH"] = "{};{}".format( + os.path.dirname(sys.executable), os.environ["PATH"]) # Issue 7880 def get(python): |