Skip to content

Commit 3bc6847

Browse files
committed
gh-98718: add sys.build_prefix
Signed-off-by: Filipe Laíns <lains@riseup.net>
1 parent c0f2a5e commit 3bc6847

File tree

8 files changed

+24
-5
lines changed

8 files changed

+24
-5
lines changed

Doc/library/sys.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ always available.
144144
.. versionadded:: 3.3
145145

146146

147+
.. data:: build_prefix
148+
149+
Set during Python startup, before ``site.py`` is run. If running Python from
150+
source, the value will be set to the build path, otherwise it will be set to
151+
:const:`None`.
152+
153+
.. versionadded:: 3.12
154+
155+
147156
.. data:: byteorder
148157

149158
An indicator of the native byte order. This will have the value ``'big'`` on

Include/cpython/initconfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ typedef struct PyConfig {
197197
wchar_t *base_prefix;
198198
wchar_t *exec_prefix;
199199
wchar_t *base_exec_prefix;
200+
wchar_t *build_prefix;
200201

201202
/* --- Parameter only used by Py_Main() ---------- */
202203
int skip_source_first_line;

Lib/sysconfig.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,9 @@ def is_python_build(check_home=None):
226226
import warnings
227227
warnings.warn("check_home argument is deprecated and ignored.",
228228
DeprecationWarning, stacklevel=2)
229-
for fn in ("Setup", "Setup.local"):
230-
if os.path.isfile(os.path.join(_PROJECT_BASE, "Modules", fn)):
231-
return True
232-
return False
229+
return bool(sys.build_prefix)
233230

234-
_PYTHON_BUILD = is_python_build()
231+
_PYTHON_BUILD = bool(sys.build_prefix)
235232

236233
if _PYTHON_BUILD:
237234
for scheme in ('posix_prefix', 'posix_home'):

Lib/test/test_sys.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ def test_attributes(self):
603603
self.assertIsInstance(sys.platform, str)
604604
self.assertIsInstance(sys.prefix, str)
605605
self.assertIsInstance(sys.base_prefix, str)
606+
self.assertIsInstance(sys.build_prefix, (str, None))
606607
self.assertIsInstance(sys.platlibdir, str)
607608
self.assertIsInstance(sys.version, str)
608609
vi = sys.version_info
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`sys` now exports :attr:`sys.build_prefix`, with the path of the Python
2+
build directory, if any.

Modules/getpath.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,9 @@ def search_up(prefix, *landmarks, test=isfile):
509509
config['_is_python_build'] = 1
510510

511511

512+
config['build_prefix'] = build_prefix
513+
514+
512515
# ******************************************************************************
513516
# CALCULATE prefix AND exec_prefix
514517
# ******************************************************************************

Python/initconfig.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ PyConfig_Clear(PyConfig *config)
731731
CLEAR(config->base_prefix);
732732
CLEAR(config->exec_prefix);
733733
CLEAR(config->base_exec_prefix);
734+
CLEAR(config->build_prefix);
734735
CLEAR(config->platlibdir);
735736

736737
CLEAR(config->filesystem_encoding);
@@ -985,6 +986,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
985986
COPY_WSTR_ATTR(base_prefix);
986987
COPY_WSTR_ATTR(exec_prefix);
987988
COPY_WSTR_ATTR(base_exec_prefix);
989+
COPY_WSTR_ATTR(build_prefix);
988990
COPY_WSTR_ATTR(platlibdir);
989991

990992
COPY_ATTR(site_import);
@@ -1094,6 +1096,7 @@ _PyConfig_AsDict(const PyConfig *config)
10941096
SET_ITEM_WSTR(base_prefix);
10951097
SET_ITEM_WSTR(exec_prefix);
10961098
SET_ITEM_WSTR(base_exec_prefix);
1099+
SET_ITEM_WSTR(build_prefix);
10971100
SET_ITEM_WSTR(platlibdir);
10981101
SET_ITEM_INT(site_import);
10991102
SET_ITEM_INT(bytes_warning);
@@ -1407,6 +1410,7 @@ _PyConfig_FromDict(PyConfig *config, PyObject *dict)
14071410
GET_WSTR_OPT(base_prefix);
14081411
GET_WSTR_OPT(exec_prefix);
14091412
GET_WSTR_OPT(base_exec_prefix);
1413+
GET_WSTR_OPT(build_prefix);
14101414

14111415
GET_UINT(skip_source_first_line);
14121416
GET_WSTR_OPT(run_command);
@@ -3172,6 +3176,7 @@ _Py_DumpPathConfig(PyThreadState *tstate)
31723176
DUMP_SYS(_base_executable);
31733177
DUMP_SYS(base_prefix);
31743178
DUMP_SYS(base_exec_prefix);
3179+
DUMP_SYS(build_prefix);
31753180
DUMP_SYS(platlibdir);
31763181
DUMP_SYS(executable);
31773182
DUMP_SYS(prefix);

Python/sysmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3317,6 +3317,7 @@ _PySys_UpdateConfig(PyThreadState *tstate)
33173317
COPY_WSTR("exec_prefix", config->exec_prefix);
33183318
COPY_WSTR("base_exec_prefix", config->base_exec_prefix);
33193319
COPY_WSTR("platlibdir", config->platlibdir);
3320+
COPY_WSTR("build_prefix", config->build_prefix);
33203321

33213322
if (config->pycache_prefix != NULL) {
33223323
SET_SYS_FROM_WSTR("pycache_prefix", config->pycache_prefix);

0 commit comments

Comments
 (0)