mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
bpo-33499: Add PYTHONPYCACHEPREFIX env var for alt bytecode cache location. (GH-6834)
In some development setups it is inconvenient or impossible to write bytecode caches to the code tree, but the bytecode caches are still useful. The PYTHONPYCACHEPREFIX environment variable allows specifying an alternate location for cached bytecode files, within which a directory tree mirroring the code tree will be created. This cache tree is then used (for both reading and writing) instead of the local `__pycache__` subdirectory within each source directory. Exposed at runtime as sys.pycache_prefix (defaulting to None), and can be set from the CLI as "-X pycache_prefix=path". Patch by Carl Meyer.
This commit is contained in:
parent
6868003514
commit
b193fa996a
12 changed files with 1386 additions and 1087 deletions
|
|
@ -519,6 +519,32 @@ class CmdLineTest(unittest.TestCase):
|
|||
with self.subTest(envar_value=value):
|
||||
assert_python_ok('-c', code, **env_vars)
|
||||
|
||||
def test_set_pycache_prefix(self):
|
||||
# sys.pycache_prefix can be set from either -X pycache_prefix or
|
||||
# PYTHONPYCACHEPREFIX env var, with the former taking precedence.
|
||||
NO_VALUE = object() # `-X pycache_prefix` with no `=PATH`
|
||||
cases = [
|
||||
# (PYTHONPYCACHEPREFIX, -X pycache_prefix, sys.pycache_prefix)
|
||||
(None, None, None),
|
||||
('foo', None, 'foo'),
|
||||
(None, 'bar', 'bar'),
|
||||
('foo', 'bar', 'bar'),
|
||||
('foo', '', None),
|
||||
('foo', NO_VALUE, None),
|
||||
]
|
||||
for envval, opt, expected in cases:
|
||||
exp_clause = "is None" if expected is None else f'== "{expected}"'
|
||||
code = f"import sys; sys.exit(not sys.pycache_prefix {exp_clause})"
|
||||
args = ['-c', code]
|
||||
env = {} if envval is None else {'PYTHONPYCACHEPREFIX': envval}
|
||||
if opt is NO_VALUE:
|
||||
args[:0] = ['-X', 'pycache_prefix']
|
||||
elif opt is not None:
|
||||
args[:0] = ['-X', f'pycache_prefix={opt}']
|
||||
with self.subTest(envval=envval, opt=opt):
|
||||
with support.temp_cwd():
|
||||
assert_python_ok(*args, **env)
|
||||
|
||||
def run_xdev(self, *args, check_exitcode=True, xdev=True):
|
||||
env = dict(os.environ)
|
||||
env.pop('PYTHONWARNINGS', None)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue