Fix packaging byte-compilation to comply with PEP 3147 (#11254).

I want to replace custom byte-compiling function with calls to
compileall before 3.3b1, but in the short term it’s good to have this
fixed.

Adapted from the distutils patch by Jeff Ramnani.  I tested with -B, -O
and -OO; test_util and test_mixin2to3 fail in -O mode because lib2to3
doesn’t support it.
diff --git a/Lib/packaging/tests/test_command_build_py.py b/Lib/packaging/tests/test_command_build_py.py
index 4eeb34e..a978c91 100644
--- a/Lib/packaging/tests/test_command_build_py.py
+++ b/Lib/packaging/tests/test_command_build_py.py
@@ -2,6 +2,7 @@
 
 import os
 import sys
+import imp
 
 from packaging.command.build_py import build_py
 from packaging.dist import Distribution
@@ -58,13 +59,15 @@
         self.assertEqual(len(cmd.get_outputs()), 3)
         pkgdest = os.path.join(destination, "pkg")
         files = os.listdir(pkgdest)
+        pycache_dir = os.path.join(pkgdest, "__pycache__")
         self.assertIn("__init__.py", files)
         self.assertIn("README.txt", files)
-        # XXX even with -O, distutils writes pyc, not pyo; bug?
         if sys.dont_write_bytecode:
-            self.assertNotIn("__init__.pyc", files)
+            self.assertFalse(os.path.exists(pycache_dir))
         else:
-            self.assertIn("__init__.pyc", files)
+            # XXX even with -O, packaging writes pyc, not pyo; bug?
+            pyc_files = os.listdir(pycache_dir)
+            self.assertIn("__init__.%s.pyc" % imp.get_tag(), pyc_files)
 
     def test_empty_package_dir(self):
         # See SF 1668596/1720897.
diff --git a/Lib/packaging/tests/test_command_install_lib.py b/Lib/packaging/tests/test_command_install_lib.py
index 9118e68..3f36822 100644
--- a/Lib/packaging/tests/test_command_install_lib.py
+++ b/Lib/packaging/tests/test_command_install_lib.py
@@ -1,6 +1,7 @@
 """Tests for packaging.command.install_data."""
-import sys
 import os
+import sys
+import imp
 
 from packaging.tests import unittest, support
 from packaging.command.install_lib import install_lib
@@ -36,6 +37,7 @@
     @unittest.skipIf(sys.dont_write_bytecode, 'byte-compile disabled')
     def test_byte_compile(self):
         pkg_dir, dist = self.create_dist()
+        os.chdir(pkg_dir)
         cmd = install_lib(dist)
         cmd.compile = True
         cmd.optimize = 1
@@ -43,8 +45,10 @@
         f = os.path.join(pkg_dir, 'foo.py')
         self.write_file(f, '# python file')
         cmd.byte_compile([f])
-        self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyc')))
-        self.assertTrue(os.path.exists(os.path.join(pkg_dir, 'foo.pyo')))
+        pyc_file = imp.cache_from_source('foo.py')
+        pyo_file = imp.cache_from_source('foo.py', debug_override=False)
+        self.assertTrue(os.path.exists(pyc_file))
+        self.assertTrue(os.path.exists(pyo_file))
 
     def test_get_outputs(self):
         pkg_dir, dist = self.create_dist()
diff --git a/Lib/packaging/util.py b/Lib/packaging/util.py
index 89f5389..f8a8058 100644
--- a/Lib/packaging/util.py
+++ b/Lib/packaging/util.py
@@ -3,6 +3,7 @@
 import os
 import re
 import csv
+import imp
 import sys
 import errno
 import shutil
@@ -296,7 +297,7 @@
 def byte_compile(py_files, optimize=0, force=False, prefix=None,
                  base_dir=None, verbose=0, dry_run=False, direct=None):
     """Byte-compile a collection of Python source files to either .pyc
-    or .pyo files in the same directory.
+    or .pyo files in a __pycache__ subdirectory.
 
     'py_files' is a list of files to compile; any files that don't end in
     ".py" are silently skipped. 'optimize' must be one of the following:
@@ -415,7 +416,10 @@
             # Terminology from the py_compile module:
             #   cfile - byte-compiled file
             #   dfile - purported source filename (same as 'file' by default)
-            cfile = file + (__debug__ and "c" or "o")
+            if optimize >= 0:
+                cfile = imp.cache_from_source(file, debug_override=not optimize)
+            else:
+                cfile = imp.cache_from_source(file)
             dfile = file
             if prefix:
                 if file[:len(prefix)] != prefix: