bpo-39769: Fix compileall ddir for subpkgs. (GH-18676)

Fix compileall.compile_dir() ddir= behavior on sub-packages.

Fixes compileall.compile_dir's ddir parameter and compileall command
line flag `-d` to no longer write the wrong pathname to the generated
pyc file for submodules beneath the root of the directory tree being
compiled.  This fixes a regression introduced with Python 3.5.

Also marks the _new_ in 3.9 from PR #16012 parameters to compile_dir as keyword only (as that is the only way they will be used) and fixes an omission of them in one place from the docs.
This commit is contained in:
Gregory P. Smith 2020-02-28 17:28:37 -08:00 committed by GitHub
parent 03153dd145
commit 02673352b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import importlib
from importlib import machinery, util, invalidate_caches
from importlib.abc import ResourceReader
import io
import marshal
import os
import os.path
from pathlib import Path, PurePath
@ -118,6 +119,16 @@ def submodule(parent, name, pkg_dir, content=''):
return '{}.{}'.format(parent, name), path
def get_code_from_pyc(pyc_path):
"""Reads a pyc file and returns the unmarshalled code object within.
No header validation is performed.
"""
with open(pyc_path, 'rb') as pyc_f:
pyc_f.seek(16)
return marshal.load(pyc_f)
@contextlib.contextmanager
def uncache(*names):
"""Uncache a module from sys.modules.