Make a number of small changes to ease the backport to distutils2
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 @@
 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 @@
             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/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 @@
         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/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 @@
         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/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 @@
     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 @@
             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/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 @@
 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 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 @@
         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 packaging.tests import unittest
 
+
 PYPI_DEFAULT_STATIC_PATH = os.path.join(
     os.path.dirname(os.path.abspath(__file__)), 'pypiserver')
 
@@ -219,7 +220,7 @@
                         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 @@
             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_build_ext.py b/Lib/packaging/tests/test_command_build_ext.py
index 59d09bf..a6ad6ac 100644
--- a/Lib/packaging/tests/test_command_build_ext.py
+++ b/Lib/packaging/tests/test_command_build_ext.py
@@ -4,6 +4,7 @@
 import sysconfig
 import textwrap
 from io import StringIO
+from sysconfig import _CONFIG_VARS
 from packaging.dist import Distribution
 from packaging.errors import (UnknownFileError, CompileError,
                               PackagingPlatformError)
@@ -78,7 +79,6 @@
         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
diff --git a/Lib/packaging/tests/test_command_install_lib.py b/Lib/packaging/tests/test_command_install_lib.py
index b46f3bd..c1066f6 100644
--- a/Lib/packaging/tests/test_command_install_lib.py
+++ b/Lib/packaging/tests/test_command_install_lib.py
@@ -40,7 +40,7 @@
         cmd.finalize_options()
         self.assertEqual(cmd.optimize, 2)
 
-    @unittest.skipIf(no_bytecode, 'byte-compile not supported')
+    @unittest.skipIf(no_bytecode, 'byte-compile disabled')
     def test_byte_compile(self):
         pkg_dir, dist = self.create_dist()
         cmd = install_lib(dist)
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 @@
         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 @@
         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 c00f5c1..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:
@@ -52,34 +51,27 @@
 
     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 @@
         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 @@
 
     @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 @@
         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 @@
         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 @@
         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 @@
         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 @@
         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 @@
         # 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 @@
         # 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 @@
         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 @@
     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 @@
     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 @@
     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 @@
 
         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 @@
 
         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 @@
 
         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 @@
     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 @@
 
     @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..8d91a92 100644
--- a/Lib/packaging/tests/test_dist.py
+++ b/Lib/packaging/tests/test_dist.py
@@ -3,13 +3,14 @@
 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 @@
 
     @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, '')
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_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 @@
 
             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 @@
             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 @@
     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..dcd210f 100644
--- a/Lib/packaging/tests/test_util.py
+++ b/Lib/packaging/tests/test_util.py
@@ -15,7 +15,7 @@
 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,
@@ -374,7 +374,7 @@
 
         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'))
@@ -416,7 +416,6 @@
         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()
@@ -432,7 +431,6 @@
         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()