Skip to content

Commit 1b2fa7e

Browse files
committed
Rename to PYTHONPYCACHEPREFIX / pycache_prefix.
1 parent e61917e commit 1b2fa7e

File tree

11 files changed

+2349
-2352
lines changed

11 files changed

+2349
-2352
lines changed

Doc/library/compileall.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ There is no command-line option to control the optimization level used by the
109109
:func:`compile` function, because the Python interpreter itself already
110110
provides the option: :program:`python -O -m compileall`.
111111

112-
Similarly, the :func:`compile` function respects the :attr:`sys.bytecode_prefix`
112+
Similarly, the :func:`compile` function respects the :attr:`sys.pycache_prefix`
113113
setting. The generated bytecode cache will only be useful if :func:`compile` is
114-
run with the same :attr:`sys.bytecode_prefix` (if any) that will be used at
114+
run with the same :attr:`sys.pycache_prefix` (if any) that will be used at
115115
runtime.
116116

117117
Public functions

Doc/library/sys.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ always available.
209209
yourself to control bytecode file generation.
210210

211211

212-
.. data:: bytecode_prefix
212+
.. data:: pycache_prefix
213213

214214
If this is set (not ``None``), Python will write bytecode-cache ``.pyc``
215215
files to (and read them from) a parallel directory tree rooted at this
@@ -222,8 +222,8 @@ always available.
222222
A relative path is interpreted relative to the current working directory.
223223

224224
This value is initially set based on the value of the :option:`-X`
225-
``bytecode_prefix=PATH`` command-line option or the
226-
:envvar:`PYTHONBYTECODEPREFIX` environment variable (command-line takes
225+
``pycache_prefix=PATH`` command-line option or the
226+
:envvar:`PYTHONPYCACHEPREFIX` environment variable (command-line takes
227227
precedence). If neither are set, it is ``None``.
228228

229229
.. versionadded:: 3.8

Doc/using/cmdline.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,9 @@ Miscellaneous options
442442
the default locale-aware mode. ``-X utf8=0`` explicitly disables UTF-8
443443
mode (even when it would otherwise activate automatically).
444444
See :envvar:`PYTHONUTF8` for more details.
445-
* ``-X bytecode_prefix=PATH`` enables writing ``.pyc`` files to a parallel
445+
* ``-X pycache_prefix=PATH`` enables writing ``.pyc`` files to a parallel
446446
tree rooted at the given directory instead of to the code tree. See also
447-
:envvar:`PYTHONBYTECODEPREFIX`.
447+
:envvar:`PYTHONPYCACHEPREFIX`.
448448

449449
It also allows passing arbitrary values and retrieving them through the
450450
:data:`sys._xoptions` dictionary.
@@ -465,7 +465,7 @@ Miscellaneous options
465465
The ``-X importtime``, ``-X dev`` and ``-X utf8`` options.
466466

467467
.. versionadded:: 3.8
468-
The ``-X bytecode_prefix`` option.
468+
The ``-X pycache_prefix`` option.
469469

470470

471471
Options you shouldn't use
@@ -593,12 +593,12 @@ conflict.
593593
specifying the :option:`-B` option.
594594

595595

596-
.. envvar:: PYTHONBYTECODEPREFIX
596+
.. envvar:: PYTHONPYCACHEPREFIX
597597

598598
If this is set, Python will write ``.pyc`` files in a mirror directory tree
599599
at this path, instead of in ``__pycache__`` directories within the source
600600
tree. This is equivalent to specifying the :option:`-X`
601-
``bytecode_prefix=PATH`` option.
601+
``pycache_prefix=PATH`` option.
602602

603603
.. versionadded:: 3.8
604604

Include/pystate.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/* Thread and interpreter state structures and their interfaces */
32

43

@@ -44,7 +43,7 @@ typedef struct {
4443
int coerce_c_locale; /* PYTHONCOERCECLOCALE, -1 means unknown */
4544
int coerce_c_locale_warn; /* PYTHONCOERCECLOCALE=warn */
4645
int utf8_mode; /* PYTHONUTF8, -X utf8; -1 means unknown */
47-
wchar_t *bytecode_prefix; /* PYTHONBYTECODEPREFIX; -X bytecode_prefix=PATH */
46+
wchar_t *pycache_prefix; /* PYTHONPYCACHEPREFIX, -X pycache_prefix=PATH */
4847

4948
wchar_t *program_name; /* Program name, see also Py_GetProgramName() */
5049
int argc; /* Number of command line arguments,
@@ -102,7 +101,7 @@ typedef struct {
102101
PyObject *warnoptions; /* sys.warnoptions list, can be NULL */
103102
PyObject *xoptions; /* sys._xoptions dict, can be NULL */
104103
PyObject *module_search_path; /* sys.path list */
105-
PyObject *bytecode_prefix; /* sys.bytecode_prefix str, can be NULL */
104+
PyObject *pycache_prefix; /* sys.pycache_prefix str, can be NULL */
106105
} _PyMainInterpreterConfig;
107106

108107
#define _PyMainInterpreterConfig_INIT \

Lib/importlib/_bootstrap_external.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,15 +322,15 @@ def cache_from_source(path, debug_override=None, *, optimization=None):
322322
raise ValueError('{!r} is not alphanumeric'.format(optimization))
323323
almost_filename = '{}.{}{}'.format(almost_filename, _OPT, optimization)
324324
filename = almost_filename + BYTECODE_SUFFIXES[0]
325-
if sys.bytecode_prefix is not None:
325+
if sys.pycache_prefix is not None:
326326
# We need an absolute path to the py file to avoid the possibility of
327-
# collisions within sys.bytecode_prefix, if someone has two different
327+
# collisions within sys.pycache_prefix, if someone has two different
328328
# `foo/bar.py` on their system and they import both of them using the
329-
# same sys.bytecode_prefix. Let's say sys.bytecode_prefix is
329+
# same sys.pycache_prefix. Let's say sys.pycache_prefix is
330330
# `C:\Bytecode`; the idea here is that if we get `Foo\Bar`, we first
331331
# make it absolute (`C:\Somewhere\Foo\Bar`), then make it root-relative
332-
# (`Somewhere\Foo\Bar`), so we end up placing the bytecode file in
333-
# an unambiguous `C:\Bytecode\Somewhere\Foo\Bar\`.
332+
# (`Somewhere\Foo\Bar`), so we end up placing the bytecode file in an
333+
# unambiguous `C:\Bytecode\Somewhere\Foo\Bar\`.
334334
if not _path_isabs(head):
335335
head = _path_join(_os.getcwd(), head)
336336

@@ -343,7 +343,7 @@ def cache_from_source(path, debug_override=None, *, optimization=None):
343343
# Strip initial path separator from `head` to complete the conversion
344344
# back to a root-relative path before joining.
345345
return _path_join(
346-
sys.bytecode_prefix,
346+
sys.pycache_prefix,
347347
head.lstrip(path_separators),
348348
filename,
349349
)
@@ -364,8 +364,8 @@ def source_from_cache(path):
364364
path = _os.fspath(path)
365365
head, pycache_filename = _path_split(path)
366366
found_in_bytecode_prefix = False
367-
if sys.bytecode_prefix is not None:
368-
stripped_path = sys.bytecode_prefix.rstrip(path_separators)
367+
if sys.pycache_prefix is not None:
368+
stripped_path = sys.pycache_prefix.rstrip(path_separators)
369369
if head.startswith(stripped_path + path_sep):
370370
head = head[len(stripped_path):]
371371
found_in_bytecode_prefix = True

Lib/test/test_cmd_line.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,12 @@ def test_sys_flags_set(self):
519519
with self.subTest(envar_value=value):
520520
assert_python_ok('-c', code, **env_vars)
521521

522-
def test_set_bytecode_prefix(self):
523-
# sys.bytecode_prefix can be set from either -X bytecode_prefix or
524-
# PYTHONBYTECODEPREFIX env var, with the former taking precedence.
525-
NO_VALUE = object() # `-X bytecode_prefix` with no `=PATH`
522+
def test_set_pycache_prefix(self):
523+
# sys.pycache_prefix can be set from either -X pycache_prefix or
524+
# PYTHONPYCACHEPREFIX env var, with the former taking precedence.
525+
NO_VALUE = object() # `-X pycache_prefix` with no `=PATH`
526526
cases = [
527-
# (PYTHONBYTECODEPREFIX, -X bytecode_prefix, sys.bytecode_prefix)
527+
# (PYTHONPYCACHEPREFIX, -X pycache_prefix, sys.pycache_prefix)
528528
(None, None, None),
529529
('foo', None, 'foo'),
530530
(None, 'bar', 'bar'),
@@ -534,15 +534,13 @@ def test_set_bytecode_prefix(self):
534534
]
535535
for envval, opt, expected in cases:
536536
exp_clause = "is None" if expected is None else f'== "{expected}"'
537-
code = f"import sys; sys.exit(not sys.bytecode_prefix {exp_clause})"
537+
code = f"import sys; sys.exit(not sys.pycache_prefix {exp_clause})"
538538
args = ['-c', code]
539-
env = {}
540-
if envval is not None:
541-
env = {'PYTHONBYTECODEPREFIX': envval}
539+
env = {} if envval is None else {'PYTHONPYCACHEPREFIX': envval}
542540
if opt is NO_VALUE:
543-
args[:0] = ['-X', 'bytecode_prefix']
541+
args[:0] = ['-X', 'pycache_prefix']
544542
elif opt is not None:
545-
args[:0] = ['-X', f'bytecode_prefix={opt}']
543+
args[:0] = ['-X', f'pycache_prefix={opt}']
546544
with self.subTest(envval=envval, opt=opt):
547545
with support.temp_cwd():
548546
assert_python_ok(*args, **env)

Lib/test/test_importlib/test_util.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -760,75 +760,75 @@ def test_source_from_cache_path_like_arg(self):
760760

761761
@unittest.skipIf(sys.implementation.cache_tag is None,
762762
'requires sys.implementation.cache_tag to not be None')
763-
def test_cache_from_source_respects_bytecode_prefix(self):
764-
# If bytecode_prefix is set, cache_from_source will return a bytecode
763+
def test_cache_from_source_respects_pycache_prefix(self):
764+
# If pycache_prefix is set, cache_from_source will return a bytecode
765765
# path inside that directory (in a subdirectory mirroring the .py file's
766766
# path) rather than in a __pycache__ dir next to the py file.
767-
bytecode_prefixes = [
767+
pycache_prefixes = [
768768
os.path.join(os.path.sep, 'tmp', 'bytecode'),
769769
os.path.join(os.path.sep, 'tmp', '\u2603'), # non-ASCII in path!
770770
os.path.join(os.path.sep, 'tmp', 'trailing-slash') + os.path.sep,
771771
]
772772
drive = ''
773773
if os.name == 'nt':
774774
drive = 'C:'
775-
bytecode_prefixes = [
776-
f'{drive}{prefix}' for prefix in bytecode_prefixes]
777-
bytecode_prefixes += [r'\\?\C:\foo', r'\\localhost\c$\bar']
778-
for bytecode_prefix in bytecode_prefixes:
779-
with self.subTest(path=bytecode_prefix):
775+
pycache_prefixes = [
776+
f'{drive}{prefix}' for prefix in pycache_prefixes]
777+
pycache_prefixes += [r'\\?\C:\foo', r'\\localhost\c$\bar']
778+
for pycache_prefix in pycache_prefixes:
779+
with self.subTest(path=pycache_prefix):
780780
path = drive + os.path.join(
781781
os.path.sep, 'foo', 'bar', 'baz', 'qux.py')
782782
expect = os.path.join(
783-
bytecode_prefix, 'foo', 'bar', 'baz',
783+
pycache_prefix, 'foo', 'bar', 'baz',
784784
'qux.{}.pyc'.format(self.tag))
785-
with util.temporary_bytecode_prefix(bytecode_prefix):
785+
with util.temporary_pycache_prefix(pycache_prefix):
786786
self.assertEqual(
787787
self.util.cache_from_source(path, optimization=''),
788788
expect)
789789

790790
@unittest.skipIf(sys.implementation.cache_tag is None,
791791
'requires sys.implementation.cache_tag to not be None')
792-
def test_cache_from_source_respects_bytecode_prefix_relative(self):
792+
def test_cache_from_source_respects_pycache_prefix_relative(self):
793793
# If the .py path we are given is relative, we will resolve to an
794-
# absolute path before prefixing with bytecode_prefix, to avoid any
794+
# absolute path before prefixing with pycache_prefix, to avoid any
795795
# possible ambiguity.
796-
bytecode_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
796+
pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
797797
path = os.path.join('foo', 'bar', 'baz', 'qux.py')
798798
root = os.path.splitdrive(os.getcwd())[0] + os.path.sep
799799
expect = os.path.join(
800-
bytecode_prefix,
800+
pycache_prefix,
801801
os.path.relpath(os.getcwd(), root),
802802
'foo', 'bar', 'baz', f'qux.{self.tag}.pyc')
803-
with util.temporary_bytecode_prefix(bytecode_prefix):
803+
with util.temporary_pycache_prefix(pycache_prefix):
804804
self.assertEqual(
805805
self.util.cache_from_source(path, optimization=''),
806806
expect)
807807

808808
@unittest.skipIf(sys.implementation.cache_tag is None,
809809
'requires sys.implementation.cache_tag to not be None')
810-
def test_source_from_cache_inside_bytecode_prefix(self):
811-
# If bytecode_prefix is set and the cache path we get is inside it,
810+
def test_source_from_cache_inside_pycache_prefix(self):
811+
# If pycache_prefix is set and the cache path we get is inside it,
812812
# we return an absolute path to the py file based on the remainder of
813-
# the path within bytecode_prefix.
814-
bytecode_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
815-
path = os.path.join(bytecode_prefix, 'foo', 'bar', 'baz',
813+
# the path within pycache_prefix.
814+
pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
815+
path = os.path.join(pycache_prefix, 'foo', 'bar', 'baz',
816816
f'qux.{self.tag}.pyc')
817817
expect = os.path.join(os.path.sep, 'foo', 'bar', 'baz', 'qux.py')
818-
with util.temporary_bytecode_prefix(bytecode_prefix):
818+
with util.temporary_pycache_prefix(pycache_prefix):
819819
self.assertEqual(self.util.source_from_cache(path), expect)
820820

821821
@unittest.skipIf(sys.implementation.cache_tag is None,
822822
'requires sys.implementation.cache_tag to not be None')
823-
def test_source_from_cache_outside_bytecode_prefix(self):
824-
# If bytecode_prefix is set but the cache path we get is not inside
823+
def test_source_from_cache_outside_pycache_prefix(self):
824+
# If pycache_prefix is set but the cache path we get is not inside
825825
# it, just ignore it and handle the cache path according to the default
826826
# behavior.
827-
bytecode_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
827+
pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
828828
path = os.path.join('foo', 'bar', 'baz', '__pycache__',
829829
f'qux.{self.tag}.pyc')
830830
expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
831-
with util.temporary_bytecode_prefix(bytecode_prefix):
831+
with util.temporary_pycache_prefix(pycache_prefix):
832832
self.assertEqual(self.util.source_from_cache(path), expect)
833833

834834

Lib/test/test_importlib/util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,14 @@ def ensure_bytecode_path(bytecode_path):
320320

321321

322322
@contextlib.contextmanager
323-
def temporary_bytecode_prefix(prefix):
324-
"""Adjust and restore sys.bytecode_prefix."""
325-
_orig_prefix = sys.bytecode_prefix
326-
sys.bytecode_prefix = prefix
323+
def temporary_pycache_prefix(prefix):
324+
"""Adjust and restore sys.pycache_prefix."""
325+
_orig_prefix = sys.pycache_prefix
326+
sys.pycache_prefix = prefix
327327
try:
328328
yield
329329
finally:
330-
sys.bytecode_prefix = _orig_prefix
330+
sys.pycache_prefix = _orig_prefix
331331

332332

333333
@contextlib.contextmanager

Modules/main.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static const char usage_6[] =
145145
" coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of\n"
146146
" locale coercion and locale compatibility warnings on stderr.\n"
147147
"PYTHONDEVMODE: enable the development mode.\n"
148-
"PYTHONBYTECODEPREFIX: root directory for bytecode cache (pyc) files.\n";
148+
"PYTHONPYCACHEPREFIX: root directory for bytecode cache (pyc) files.\n";
149149

150150
static void
151151
pymain_usage(int error, const wchar_t* program)
@@ -1677,29 +1677,29 @@ pymain_init_tracemalloc(_PyCoreConfig *config)
16771677

16781678

16791679
static _PyInitError
1680-
pymain_init_bytecode_prefix(_PyCoreConfig *config)
1680+
pymain_init_pycache_prefix(_PyCoreConfig *config)
16811681
{
16821682
wchar_t *env;
16831683

16841684
int res = config_get_env_var_dup(
1685-
&env, L"PYTHONBYTECODEPREFIX", "PYTHONBYTECODEPREFIX");
1685+
&env, L"PYTHONPYCACHEPREFIX", "PYTHONPYCACHEPREFIX");
16861686
if (res < 0) {
1687-
return DECODE_LOCALE_ERR("PYTHONBYTECODEPREFIX", res);
1687+
return DECODE_LOCALE_ERR("PYTHONPYCACHEPREFIX", res);
16881688
} else if (env) {
1689-
config->bytecode_prefix = env;
1689+
config->pycache_prefix = env;
16901690
}
16911691

1692-
const wchar_t *xoption = config_get_xoption(config, L"bytecode_prefix");
1692+
const wchar_t *xoption = config_get_xoption(config, L"pycache_prefix");
16931693
if (xoption) {
16941694
const wchar_t *sep = wcschr(xoption, L'=');
16951695
if (sep && wcslen(sep) > 1) {
1696-
config->bytecode_prefix = _PyMem_RawWcsdup(sep + 1);
1697-
if (config->bytecode_prefix == NULL) {
1696+
config->pycache_prefix = _PyMem_RawWcsdup(sep + 1);
1697+
if (config->pycache_prefix == NULL) {
16981698
return _Py_INIT_NO_MEMORY();
16991699
}
17001700
} else {
1701-
// -X bytecode_prefix= can cancel the env var
1702-
config->bytecode_prefix = NULL;
1701+
// -X pycache_prefix= can cancel the env var
1702+
config->pycache_prefix = NULL;
17031703
}
17041704
}
17051705

@@ -1901,7 +1901,7 @@ config_read_complex_options(_PyCoreConfig *config)
19011901
return err;
19021902
}
19031903

1904-
err = pymain_init_bytecode_prefix(config);
1904+
err = pymain_init_pycache_prefix(config);
19051905
if (_Py_INIT_FAILED(err)) {
19061906
return err;
19071907
}
@@ -2275,7 +2275,7 @@ _PyCoreConfig_Clear(_PyCoreConfig *config)
22752275
LIST = NULL; \
22762276
} while (0)
22772277

2278-
CLEAR(config->bytecode_prefix);
2278+
CLEAR(config->pycache_prefix);
22792279
CLEAR(config->module_search_path_env);
22802280
CLEAR(config->home);
22812281
CLEAR(config->program_name);
@@ -2340,7 +2340,7 @@ _PyCoreConfig_Copy(_PyCoreConfig *config, const _PyCoreConfig *config2)
23402340
COPY_ATTR(malloc_stats);
23412341
COPY_ATTR(utf8_mode);
23422342

2343-
COPY_STR_ATTR(bytecode_prefix);
2343+
COPY_STR_ATTR(pycache_prefix);
23442344
COPY_STR_ATTR(module_search_path_env);
23452345
COPY_STR_ATTR(home);
23462346
COPY_STR_ATTR(program_name);
@@ -2376,7 +2376,7 @@ _PyMainInterpreterConfig_Clear(_PyMainInterpreterConfig *config)
23762376
Py_CLEAR(config->warnoptions);
23772377
Py_CLEAR(config->xoptions);
23782378
Py_CLEAR(config->module_search_path);
2379-
Py_CLEAR(config->bytecode_prefix);
2379+
Py_CLEAR(config->pycache_prefix);
23802380
}
23812381

23822382

@@ -2429,7 +2429,7 @@ _PyMainInterpreterConfig_Copy(_PyMainInterpreterConfig *config,
24292429
COPY_ATTR(warnoptions);
24302430
COPY_ATTR(xoptions);
24312431
COPY_ATTR(module_search_path);
2432-
COPY_ATTR(bytecode_prefix);
2432+
COPY_ATTR(pycache_prefix);
24332433
#undef COPY_ATTR
24342434
return 0;
24352435
}
@@ -2488,10 +2488,10 @@ _PyMainInterpreterConfig_Read(_PyMainInterpreterConfig *main_config,
24882488
COPY_WSTRLIST(main_config->module_search_path,
24892489
config->nmodule_search_path, config->module_search_paths);
24902490

2491-
if (config->bytecode_prefix != NULL) {
2492-
COPY_WSTR(bytecode_prefix);
2491+
if (config->pycache_prefix != NULL) {
2492+
COPY_WSTR(pycache_prefix);
24932493
} else {
2494-
main_config->bytecode_prefix = NULL;
2494+
main_config->pycache_prefix = NULL;
24952495
}
24962496

24972497
}

0 commit comments

Comments
 (0)