sysconfig.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. """Access to Python's configuration information."""
  2. import os
  3. import sys
  4. import threading
  5. from os.path import realpath
  6. __all__ = [
  7. 'get_config_h_filename',
  8. 'get_config_var',
  9. 'get_config_vars',
  10. 'get_makefile_filename',
  11. 'get_path',
  12. 'get_path_names',
  13. 'get_paths',
  14. 'get_platform',
  15. 'get_python_version',
  16. 'get_scheme_names',
  17. 'parse_config_h',
  18. ]
  19. # Keys for get_config_var() that are never converted to Python integers.
  20. _ALWAYS_STR = {
  21. 'MACOSX_DEPLOYMENT_TARGET',
  22. }
  23. _INSTALL_SCHEMES = {
  24. 'posix_prefix': {
  25. 'stdlib': '{installed_base}/{platlibdir}/python{py_version_short}',
  26. 'platstdlib': '{platbase}/{platlibdir}/python{py_version_short}',
  27. 'purelib': '{base}/lib/python{py_version_short}/site-packages',
  28. 'platlib': '{platbase}/{platlibdir}/python{py_version_short}/site-packages',
  29. 'include':
  30. '{installed_base}/include/python{py_version_short}{abiflags}',
  31. 'platinclude':
  32. '{installed_platbase}/include/python{py_version_short}{abiflags}',
  33. 'scripts': '{base}/bin',
  34. 'data': '{base}',
  35. },
  36. 'posix_home': {
  37. 'stdlib': '{installed_base}/lib/python',
  38. 'platstdlib': '{base}/lib/python',
  39. 'purelib': '{base}/lib/python',
  40. 'platlib': '{base}/lib/python',
  41. 'include': '{installed_base}/include/python',
  42. 'platinclude': '{installed_base}/include/python',
  43. 'scripts': '{base}/bin',
  44. 'data': '{base}',
  45. },
  46. 'nt': {
  47. 'stdlib': '{installed_base}/Lib',
  48. 'platstdlib': '{base}/Lib',
  49. 'purelib': '{base}/Lib/site-packages',
  50. 'platlib': '{base}/Lib/site-packages',
  51. 'include': '{installed_base}/Include',
  52. 'platinclude': '{installed_base}/Include',
  53. 'scripts': '{base}/Scripts',
  54. 'data': '{base}',
  55. },
  56. # Downstream distributors can overwrite the default install scheme.
  57. # This is done to support downstream modifications where distributors change
  58. # the installation layout (eg. different site-packages directory).
  59. # So, distributors will change the default scheme to one that correctly
  60. # represents their layout.
  61. # This presents an issue for projects/people that need to bootstrap virtual
  62. # environments, like virtualenv. As distributors might now be customizing
  63. # the default install scheme, there is no guarantee that the information
  64. # returned by sysconfig.get_default_scheme/get_paths is correct for
  65. # a virtual environment, the only guarantee we have is that it is correct
  66. # for the *current* environment. When bootstrapping a virtual environment,
  67. # we need to know its layout, so that we can place the files in the
  68. # correct locations.
  69. # The "*_venv" install scheme is a scheme to bootstrap virtual environments,
  70. # essentially identical to the default posix_prefix/nt schemes.
  71. # Downstream distributors who patch posix_prefix/nt scheme are encouraged to
  72. # leave the following schemes unchanged
  73. 'posix_venv': {
  74. 'stdlib': '{installed_base}/{platlibdir}/python{py_version_short}',
  75. 'platstdlib': '{platbase}/{platlibdir}/python{py_version_short}',
  76. 'purelib': '{base}/lib/python{py_version_short}/site-packages',
  77. 'platlib': '{platbase}/{platlibdir}/python{py_version_short}/site-packages',
  78. 'include':
  79. '{installed_base}/include/python{py_version_short}{abiflags}',
  80. 'platinclude':
  81. '{installed_platbase}/include/python{py_version_short}{abiflags}',
  82. 'scripts': '{base}/bin',
  83. 'data': '{base}',
  84. },
  85. 'nt_venv': {
  86. 'stdlib': '{installed_base}/Lib',
  87. 'platstdlib': '{base}/Lib',
  88. 'purelib': '{base}/Lib/site-packages',
  89. 'platlib': '{base}/Lib/site-packages',
  90. 'include': '{installed_base}/Include',
  91. 'platinclude': '{installed_base}/Include',
  92. 'scripts': '{base}/Scripts',
  93. 'data': '{base}',
  94. },
  95. }
  96. # For the OS-native venv scheme, we essentially provide an alias:
  97. if os.name == 'nt':
  98. _INSTALL_SCHEMES['venv'] = _INSTALL_SCHEMES['nt_venv']
  99. else:
  100. _INSTALL_SCHEMES['venv'] = _INSTALL_SCHEMES['posix_venv']
  101. # NOTE: site.py has copy of this function.
  102. # Sync it when modify this function.
  103. def _getuserbase():
  104. env_base = os.environ.get("PYTHONUSERBASE", None)
  105. if env_base:
  106. return env_base
  107. # Emscripten, VxWorks, and WASI have no home directories
  108. if sys.platform in {"emscripten", "vxworks", "wasi"}:
  109. return None
  110. def joinuser(*args):
  111. return os.path.expanduser(os.path.join(*args))
  112. if os.name == "nt":
  113. base = os.environ.get("APPDATA") or "~"
  114. return joinuser(base, "Python")
  115. if sys.platform == "darwin" and sys._framework:
  116. return joinuser("~", "Library", sys._framework,
  117. f"{sys.version_info[0]}.{sys.version_info[1]}")
  118. return joinuser("~", ".local")
  119. _HAS_USER_BASE = (_getuserbase() is not None)
  120. if _HAS_USER_BASE:
  121. _INSTALL_SCHEMES |= {
  122. # NOTE: When modifying "purelib" scheme, update site._get_path() too.
  123. 'nt_user': {
  124. 'stdlib': '{userbase}/Python{py_version_nodot_plat}',
  125. 'platstdlib': '{userbase}/Python{py_version_nodot_plat}',
  126. 'purelib': '{userbase}/Python{py_version_nodot_plat}/site-packages',
  127. 'platlib': '{userbase}/Python{py_version_nodot_plat}/site-packages',
  128. 'include': '{userbase}/Python{py_version_nodot_plat}/Include',
  129. 'scripts': '{userbase}/Python{py_version_nodot_plat}/Scripts',
  130. 'data': '{userbase}',
  131. },
  132. 'posix_user': {
  133. 'stdlib': '{userbase}/{platlibdir}/python{py_version_short}',
  134. 'platstdlib': '{userbase}/{platlibdir}/python{py_version_short}',
  135. 'purelib': '{userbase}/lib/python{py_version_short}/site-packages',
  136. 'platlib': '{userbase}/lib/python{py_version_short}/site-packages',
  137. 'include': '{userbase}/include/python{py_version_short}',
  138. 'scripts': '{userbase}/bin',
  139. 'data': '{userbase}',
  140. },
  141. 'osx_framework_user': {
  142. 'stdlib': '{userbase}/lib/python',
  143. 'platstdlib': '{userbase}/lib/python',
  144. 'purelib': '{userbase}/lib/python/site-packages',
  145. 'platlib': '{userbase}/lib/python/site-packages',
  146. 'include': '{userbase}/include/python{py_version_short}',
  147. 'scripts': '{userbase}/bin',
  148. 'data': '{userbase}',
  149. },
  150. }
  151. _SCHEME_KEYS = ('stdlib', 'platstdlib', 'purelib', 'platlib', 'include',
  152. 'scripts', 'data')
  153. _PY_VERSION = sys.version.split()[0]
  154. _PY_VERSION_SHORT = f'{sys.version_info[0]}.{sys.version_info[1]}'
  155. _PY_VERSION_SHORT_NO_DOT = f'{sys.version_info[0]}{sys.version_info[1]}'
  156. _PREFIX = os.path.normpath(sys.prefix)
  157. _BASE_PREFIX = os.path.normpath(sys.base_prefix)
  158. _EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
  159. _BASE_EXEC_PREFIX = os.path.normpath(sys.base_exec_prefix)
  160. # Mutex guarding initialization of _CONFIG_VARS.
  161. _CONFIG_VARS_LOCK = threading.RLock()
  162. _CONFIG_VARS = None
  163. # True iff _CONFIG_VARS has been fully initialized.
  164. _CONFIG_VARS_INITIALIZED = False
  165. _USER_BASE = None
  166. # Regexes needed for parsing Makefile (and similar syntaxes,
  167. # like old-style Setup files).
  168. _variable_rx = r"([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)"
  169. _findvar1_rx = r"\$\(([A-Za-z][A-Za-z0-9_]*)\)"
  170. _findvar2_rx = r"\${([A-Za-z][A-Za-z0-9_]*)}"
  171. def _safe_realpath(path):
  172. try:
  173. return realpath(path)
  174. except OSError:
  175. return path
  176. if sys.executable:
  177. _PROJECT_BASE = os.path.dirname(_safe_realpath(sys.executable))
  178. else:
  179. # sys.executable can be empty if argv[0] has been changed and Python is
  180. # unable to retrieve the real program name
  181. _PROJECT_BASE = _safe_realpath(os.getcwd())
  182. # In a virtual environment, `sys._home` gives us the target directory
  183. # `_PROJECT_BASE` for the executable that created it when the virtual
  184. # python is an actual executable ('venv --copies' or Windows).
  185. _sys_home = getattr(sys, '_home', None)
  186. if _sys_home:
  187. _PROJECT_BASE = _sys_home
  188. if os.name == 'nt':
  189. # In a source build, the executable is in a subdirectory of the root
  190. # that we want (<root>\PCbuild\<platname>).
  191. # `_BASE_PREFIX` is used as the base installation is where the source
  192. # will be. The realpath is needed to prevent mount point confusion
  193. # that can occur with just string comparisons.
  194. if _safe_realpath(_PROJECT_BASE).startswith(
  195. _safe_realpath(f'{_BASE_PREFIX}\\PCbuild')):
  196. _PROJECT_BASE = _BASE_PREFIX
  197. # set for cross builds
  198. if "_PYTHON_PROJECT_BASE" in os.environ:
  199. _PROJECT_BASE = _safe_realpath(os.environ["_PYTHON_PROJECT_BASE"])
  200. def is_python_build(check_home=None):
  201. if check_home is not None:
  202. import warnings
  203. warnings.warn("check_home argument is deprecated and ignored.",
  204. DeprecationWarning, stacklevel=2)
  205. for fn in ("Setup", "Setup.local"):
  206. if os.path.isfile(os.path.join(_PROJECT_BASE, "Modules", fn)):
  207. return True
  208. return False
  209. _PYTHON_BUILD = is_python_build()
  210. if _PYTHON_BUILD:
  211. for scheme in ('posix_prefix', 'posix_home'):
  212. # On POSIX-y platforms, Python will:
  213. # - Build from .h files in 'headers' (which is only added to the
  214. # scheme when building CPython)
  215. # - Install .h files to 'include'
  216. scheme = _INSTALL_SCHEMES[scheme]
  217. scheme['headers'] = scheme['include']
  218. scheme['include'] = '{srcdir}/Include'
  219. scheme['platinclude'] = '{projectbase}/.'
  220. del scheme
  221. def _subst_vars(s, local_vars):
  222. try:
  223. return s.format(**local_vars)
  224. except KeyError as var:
  225. try:
  226. return s.format(**os.environ)
  227. except KeyError:
  228. raise AttributeError(f'{var}') from None
  229. def _extend_dict(target_dict, other_dict):
  230. target_keys = target_dict.keys()
  231. for key, value in other_dict.items():
  232. if key in target_keys:
  233. continue
  234. target_dict[key] = value
  235. def _expand_vars(scheme, vars):
  236. res = {}
  237. if vars is None:
  238. vars = {}
  239. _extend_dict(vars, get_config_vars())
  240. if os.name == 'nt':
  241. # On Windows we want to substitute 'lib' for schemes rather
  242. # than the native value (without modifying vars, in case it
  243. # was passed in)
  244. vars = vars | {'platlibdir': 'lib'}
  245. for key, value in _INSTALL_SCHEMES[scheme].items():
  246. if os.name in ('posix', 'nt'):
  247. value = os.path.expanduser(value)
  248. res[key] = os.path.normpath(_subst_vars(value, vars))
  249. return res
  250. def _get_preferred_schemes():
  251. if os.name == 'nt':
  252. return {
  253. 'prefix': 'nt',
  254. 'home': 'posix_home',
  255. 'user': 'nt_user',
  256. }
  257. if sys.platform == 'darwin' and sys._framework:
  258. return {
  259. 'prefix': 'posix_prefix',
  260. 'home': 'posix_home',
  261. 'user': 'osx_framework_user',
  262. }
  263. return {
  264. 'prefix': 'posix_prefix',
  265. 'home': 'posix_home',
  266. 'user': 'posix_user',
  267. }
  268. def get_preferred_scheme(key):
  269. if key == 'prefix' and sys.prefix != sys.base_prefix:
  270. return 'venv'
  271. scheme = _get_preferred_schemes()[key]
  272. if scheme not in _INSTALL_SCHEMES:
  273. raise ValueError(
  274. f"{key!r} returned {scheme!r}, which is not a valid scheme "
  275. f"on this platform"
  276. )
  277. return scheme
  278. def get_default_scheme():
  279. return get_preferred_scheme('prefix')
  280. def _parse_makefile(filename, vars=None, keep_unresolved=True):
  281. """Parse a Makefile-style file.
  282. A dictionary containing name/value pairs is returned. If an
  283. optional dictionary is passed in as the second argument, it is
  284. used instead of a new dictionary.
  285. """
  286. import re
  287. if vars is None:
  288. vars = {}
  289. done = {}
  290. notdone = {}
  291. with open(filename, encoding=sys.getfilesystemencoding(),
  292. errors="surrogateescape") as f:
  293. lines = f.readlines()
  294. for line in lines:
  295. if line.startswith('#') or line.strip() == '':
  296. continue
  297. m = re.match(_variable_rx, line)
  298. if m:
  299. n, v = m.group(1, 2)
  300. v = v.strip()
  301. # `$$' is a literal `$' in make
  302. tmpv = v.replace('$$', '')
  303. if "$" in tmpv:
  304. notdone[n] = v
  305. else:
  306. try:
  307. if n in _ALWAYS_STR:
  308. raise ValueError
  309. v = int(v)
  310. except ValueError:
  311. # insert literal `$'
  312. done[n] = v.replace('$$', '$')
  313. else:
  314. done[n] = v
  315. # do variable interpolation here
  316. variables = list(notdone.keys())
  317. # Variables with a 'PY_' prefix in the makefile. These need to
  318. # be made available without that prefix through sysconfig.
  319. # Special care is needed to ensure that variable expansion works, even
  320. # if the expansion uses the name without a prefix.
  321. renamed_variables = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS')
  322. while len(variables) > 0:
  323. for name in tuple(variables):
  324. value = notdone[name]
  325. m1 = re.search(_findvar1_rx, value)
  326. m2 = re.search(_findvar2_rx, value)
  327. if m1 and m2:
  328. m = m1 if m1.start() < m2.start() else m2
  329. else:
  330. m = m1 if m1 else m2
  331. if m is not None:
  332. n = m.group(1)
  333. found = True
  334. if n in done:
  335. item = str(done[n])
  336. elif n in notdone:
  337. # get it on a subsequent round
  338. found = False
  339. elif n in os.environ:
  340. # do it like make: fall back to environment
  341. item = os.environ[n]
  342. elif n in renamed_variables:
  343. if (name.startswith('PY_') and
  344. name[3:] in renamed_variables):
  345. item = ""
  346. elif 'PY_' + n in notdone:
  347. found = False
  348. else:
  349. item = str(done['PY_' + n])
  350. else:
  351. done[n] = item = ""
  352. if found:
  353. after = value[m.end():]
  354. value = value[:m.start()] + item + after
  355. if "$" in after:
  356. notdone[name] = value
  357. else:
  358. try:
  359. if name in _ALWAYS_STR:
  360. raise ValueError
  361. value = int(value)
  362. except ValueError:
  363. done[name] = value.strip()
  364. else:
  365. done[name] = value
  366. variables.remove(name)
  367. if name.startswith('PY_') \
  368. and name[3:] in renamed_variables:
  369. name = name[3:]
  370. if name not in done:
  371. done[name] = value
  372. else:
  373. # Adds unresolved variables to the done dict.
  374. # This is disabled when called from distutils.sysconfig
  375. if keep_unresolved:
  376. done[name] = value
  377. # bogus variable reference (e.g. "prefix=$/opt/python");
  378. # just drop it since we can't deal
  379. variables.remove(name)
  380. # strip spurious spaces
  381. for k, v in done.items():
  382. if isinstance(v, str):
  383. done[k] = v.strip()
  384. # save the results in the global dictionary
  385. vars.update(done)
  386. return vars
  387. def get_makefile_filename():
  388. """Return the path of the Makefile."""
  389. if _PYTHON_BUILD:
  390. return os.path.join(_PROJECT_BASE, "Makefile")
  391. if hasattr(sys, 'abiflags'):
  392. config_dir_name = f'config-{_PY_VERSION_SHORT}{sys.abiflags}'
  393. else:
  394. config_dir_name = 'config'
  395. if hasattr(sys.implementation, '_multiarch'):
  396. config_dir_name += f'-{sys.implementation._multiarch}'
  397. return os.path.join(get_path('stdlib'), config_dir_name, 'Makefile')
  398. def _get_sysconfigdata_name():
  399. multiarch = getattr(sys.implementation, '_multiarch', '')
  400. return os.environ.get(
  401. '_PYTHON_SYSCONFIGDATA_NAME',
  402. '_sysconfigdata_arcadia' # f'_sysconfigdata_{sys.abiflags}_{sys.platform}_{multiarch}',
  403. )
  404. def _generate_posix_vars():
  405. """Generate the Python module containing build-time variables."""
  406. import pprint
  407. vars = {}
  408. # load the installed Makefile:
  409. makefile = get_makefile_filename()
  410. try:
  411. _parse_makefile(makefile, vars)
  412. except OSError as e:
  413. msg = f"invalid Python installation: unable to open {makefile}"
  414. if hasattr(e, "strerror"):
  415. msg = f"{msg} ({e.strerror})"
  416. raise OSError(msg)
  417. # load the installed pyconfig.h:
  418. config_h = get_config_h_filename()
  419. try:
  420. with open(config_h, encoding="utf-8") as f:
  421. parse_config_h(f, vars)
  422. except OSError as e:
  423. msg = f"invalid Python installation: unable to open {config_h}"
  424. if hasattr(e, "strerror"):
  425. msg = f"{msg} ({e.strerror})"
  426. raise OSError(msg)
  427. # On AIX, there are wrong paths to the linker scripts in the Makefile
  428. # -- these paths are relative to the Python source, but when installed
  429. # the scripts are in another directory.
  430. if _PYTHON_BUILD:
  431. vars['BLDSHARED'] = vars['LDSHARED']
  432. # There's a chicken-and-egg situation on OS X with regards to the
  433. # _sysconfigdata module after the changes introduced by #15298:
  434. # get_config_vars() is called by get_platform() as part of the
  435. # `make pybuilddir.txt` target -- which is a precursor to the
  436. # _sysconfigdata.py module being constructed. Unfortunately,
  437. # get_config_vars() eventually calls _init_posix(), which attempts
  438. # to import _sysconfigdata, which we won't have built yet. In order
  439. # for _init_posix() to work, if we're on Darwin, just mock up the
  440. # _sysconfigdata module manually and populate it with the build vars.
  441. # This is more than sufficient for ensuring the subsequent call to
  442. # get_platform() succeeds.
  443. name = _get_sysconfigdata_name()
  444. if 'darwin' in sys.platform:
  445. import types
  446. module = types.ModuleType(name)
  447. module.build_time_vars = vars
  448. sys.modules[name] = module
  449. pybuilddir = f'build/lib.{get_platform()}-{_PY_VERSION_SHORT}'
  450. if hasattr(sys, "gettotalrefcount"):
  451. pybuilddir += '-pydebug'
  452. os.makedirs(pybuilddir, exist_ok=True)
  453. destfile = os.path.join(pybuilddir, name + '.py')
  454. with open(destfile, 'w', encoding='utf8') as f:
  455. f.write('# system configuration generated and used by'
  456. ' the sysconfig module\n')
  457. f.write('build_time_vars = ')
  458. pprint.pprint(vars, stream=f)
  459. # Create file used for sys.path fixup -- see Modules/getpath.c
  460. with open('pybuilddir.txt', 'w', encoding='utf8') as f:
  461. f.write(pybuilddir)
  462. def _init_posix(vars):
  463. """Initialize the module as appropriate for POSIX systems."""
  464. # _sysconfigdata is generated at build time, see _generate_posix_vars()
  465. name = _get_sysconfigdata_name()
  466. _temp = __import__(name, globals(), locals(), ['build_time_vars'], 0)
  467. build_time_vars = _temp.build_time_vars
  468. vars.update(build_time_vars)
  469. def _init_non_posix(vars):
  470. """Initialize the module as appropriate for NT"""
  471. # set basic install directories
  472. import _imp
  473. vars['LIBDEST'] = get_path('stdlib')
  474. vars['BINLIBDEST'] = get_path('platstdlib')
  475. vars['INCLUDEPY'] = get_path('include')
  476. try:
  477. # GH-99201: _imp.extension_suffixes may be empty when
  478. # HAVE_DYNAMIC_LOADING is not set. In this case, don't set EXT_SUFFIX.
  479. vars['EXT_SUFFIX'] = _imp.extension_suffixes()[0]
  480. except IndexError:
  481. pass
  482. vars['EXE'] = '.exe'
  483. vars['VERSION'] = _PY_VERSION_SHORT_NO_DOT
  484. vars['BINDIR'] = os.path.dirname(_safe_realpath(sys.executable))
  485. vars['TZPATH'] = ''
  486. #
  487. # public APIs
  488. #
  489. def parse_config_h(fp, vars=None):
  490. """Parse a config.h-style file.
  491. A dictionary containing name/value pairs is returned. If an
  492. optional dictionary is passed in as the second argument, it is
  493. used instead of a new dictionary.
  494. """
  495. if vars is None:
  496. vars = {}
  497. import re
  498. define_rx = re.compile("#define ([A-Z][A-Za-z0-9_]+) (.*)\n")
  499. undef_rx = re.compile("/[*] #undef ([A-Z][A-Za-z0-9_]+) [*]/\n")
  500. while True:
  501. line = fp.readline()
  502. if not line:
  503. break
  504. m = define_rx.match(line)
  505. if m:
  506. n, v = m.group(1, 2)
  507. try:
  508. if n in _ALWAYS_STR:
  509. raise ValueError
  510. v = int(v)
  511. except ValueError:
  512. pass
  513. vars[n] = v
  514. else:
  515. m = undef_rx.match(line)
  516. if m:
  517. vars[m.group(1)] = 0
  518. return vars
  519. def get_config_h_filename():
  520. """Return the path of pyconfig.h."""
  521. if _PYTHON_BUILD:
  522. if os.name == "nt":
  523. inc_dir = os.path.join(_PROJECT_BASE, "PC")
  524. else:
  525. inc_dir = _PROJECT_BASE
  526. else:
  527. inc_dir = get_path('platinclude')
  528. return os.path.join(inc_dir, 'pyconfig.h')
  529. def get_scheme_names():
  530. """Return a tuple containing the schemes names."""
  531. return tuple(sorted(_INSTALL_SCHEMES))
  532. def get_path_names():
  533. """Return a tuple containing the paths names."""
  534. return _SCHEME_KEYS
  535. def get_paths(scheme=get_default_scheme(), vars=None, expand=True):
  536. """Return a mapping containing an install scheme.
  537. ``scheme`` is the install scheme name. If not provided, it will
  538. return the default scheme for the current platform.
  539. """
  540. if expand:
  541. return _expand_vars(scheme, vars)
  542. else:
  543. return _INSTALL_SCHEMES[scheme]
  544. def get_path(name, scheme=get_default_scheme(), vars=None, expand=True):
  545. """Return a path corresponding to the scheme.
  546. ``scheme`` is the install scheme name.
  547. """
  548. return get_paths(scheme, vars, expand)[name]
  549. def _init_config_vars():
  550. global _CONFIG_VARS
  551. _CONFIG_VARS = {}
  552. # Normalized versions of prefix and exec_prefix are handy to have;
  553. # in fact, these are the standard versions used most places in the
  554. # Distutils.
  555. _CONFIG_VARS['prefix'] = _PREFIX
  556. _CONFIG_VARS['exec_prefix'] = _EXEC_PREFIX
  557. _CONFIG_VARS['py_version'] = _PY_VERSION
  558. _CONFIG_VARS['py_version_short'] = _PY_VERSION_SHORT
  559. _CONFIG_VARS['py_version_nodot'] = _PY_VERSION_SHORT_NO_DOT
  560. _CONFIG_VARS['installed_base'] = _BASE_PREFIX
  561. _CONFIG_VARS['base'] = _PREFIX
  562. _CONFIG_VARS['installed_platbase'] = _BASE_EXEC_PREFIX
  563. _CONFIG_VARS['platbase'] = _EXEC_PREFIX
  564. _CONFIG_VARS['projectbase'] = _PROJECT_BASE
  565. _CONFIG_VARS['platlibdir'] = sys.platlibdir
  566. try:
  567. _CONFIG_VARS['abiflags'] = sys.abiflags
  568. except AttributeError:
  569. # sys.abiflags may not be defined on all platforms.
  570. _CONFIG_VARS['abiflags'] = ''
  571. try:
  572. _CONFIG_VARS['py_version_nodot_plat'] = sys.winver.replace('.', '')
  573. except AttributeError:
  574. _CONFIG_VARS['py_version_nodot_plat'] = ''
  575. if os.name == 'nt':
  576. _init_non_posix(_CONFIG_VARS)
  577. _CONFIG_VARS['VPATH'] = sys._vpath
  578. if os.name == 'posix':
  579. _init_posix(_CONFIG_VARS)
  580. if _HAS_USER_BASE:
  581. # Setting 'userbase' is done below the call to the
  582. # init function to enable using 'get_config_var' in
  583. # the init-function.
  584. _CONFIG_VARS['userbase'] = _getuserbase()
  585. # Always convert srcdir to an absolute path
  586. srcdir = _CONFIG_VARS.get('srcdir', _PROJECT_BASE)
  587. if os.name == 'posix':
  588. if _PYTHON_BUILD:
  589. # If srcdir is a relative path (typically '.' or '..')
  590. # then it should be interpreted relative to the directory
  591. # containing Makefile.
  592. base = os.path.dirname(get_makefile_filename())
  593. srcdir = os.path.join(base, srcdir)
  594. else:
  595. # srcdir is not meaningful since the installation is
  596. # spread about the filesystem. We choose the
  597. # directory containing the Makefile since we know it
  598. # exists.
  599. srcdir = os.path.dirname(get_makefile_filename())
  600. _CONFIG_VARS['srcdir'] = _safe_realpath(srcdir)
  601. # OS X platforms require special customization to handle
  602. # multi-architecture, multi-os-version installers
  603. if sys.platform == 'darwin':
  604. import _osx_support
  605. _osx_support.customize_config_vars(_CONFIG_VARS)
  606. global _CONFIG_VARS_INITIALIZED
  607. _CONFIG_VARS_INITIALIZED = True
  608. def get_config_vars(*args):
  609. """With no arguments, return a dictionary of all configuration
  610. variables relevant for the current platform.
  611. On Unix, this means every variable defined in Python's installed Makefile;
  612. On Windows it's a much smaller set.
  613. With arguments, return a list of values that result from looking up
  614. each argument in the configuration variable dictionary.
  615. """
  616. # Avoid claiming the lock once initialization is complete.
  617. if not _CONFIG_VARS_INITIALIZED:
  618. with _CONFIG_VARS_LOCK:
  619. # Test again with the lock held to avoid races. Note that
  620. # we test _CONFIG_VARS here, not _CONFIG_VARS_INITIALIZED,
  621. # to ensure that recursive calls to get_config_vars()
  622. # don't re-enter init_config_vars().
  623. if _CONFIG_VARS is None:
  624. _init_config_vars()
  625. if args:
  626. vals = []
  627. for name in args:
  628. vals.append(_CONFIG_VARS.get(name))
  629. return vals
  630. else:
  631. return _CONFIG_VARS
  632. def get_config_var(name):
  633. """Return the value of a single variable using the dictionary returned by
  634. 'get_config_vars()'.
  635. Equivalent to get_config_vars().get(name)
  636. """
  637. return get_config_vars().get(name)
  638. def get_platform():
  639. """Return a string that identifies the current platform.
  640. This is used mainly to distinguish platform-specific build directories and
  641. platform-specific built distributions. Typically includes the OS name and
  642. version and the architecture (as supplied by 'os.uname()'), although the
  643. exact information included depends on the OS; on Linux, the kernel version
  644. isn't particularly important.
  645. Examples of returned values:
  646. linux-i586
  647. linux-alpha (?)
  648. solaris-2.6-sun4u
  649. Windows will return one of:
  650. win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
  651. win32 (all others - specifically, sys.platform is returned)
  652. For other non-POSIX platforms, currently just returns 'sys.platform'.
  653. """
  654. if os.name == 'nt':
  655. if 'amd64' in sys.version.lower():
  656. return 'win-amd64'
  657. if '(arm)' in sys.version.lower():
  658. return 'win-arm32'
  659. if '(arm64)' in sys.version.lower():
  660. return 'win-arm64'
  661. return sys.platform
  662. if os.name != "posix" or not hasattr(os, 'uname'):
  663. # XXX what about the architecture? NT is Intel or Alpha
  664. return sys.platform
  665. # Set for cross builds explicitly
  666. if "_PYTHON_HOST_PLATFORM" in os.environ:
  667. return os.environ["_PYTHON_HOST_PLATFORM"]
  668. # Try to distinguish various flavours of Unix
  669. osname, host, release, version, machine = os.uname()
  670. # Convert the OS name to lowercase, remove '/' characters, and translate
  671. # spaces (for "Power Macintosh")
  672. osname = osname.lower().replace('/', '')
  673. machine = machine.replace(' ', '_')
  674. machine = machine.replace('/', '-')
  675. if osname[:5] == "linux":
  676. # At least on Linux/Intel, 'machine' is the processor --
  677. # i386, etc.
  678. # XXX what about Alpha, SPARC, etc?
  679. return f"{osname}-{machine}"
  680. elif osname[:5] == "sunos":
  681. if release[0] >= "5": # SunOS 5 == Solaris 2
  682. osname = "solaris"
  683. release = f"{int(release[0]) - 3}.{release[2:]}"
  684. # We can't use "platform.architecture()[0]" because a
  685. # bootstrap problem. We use a dict to get an error
  686. # if some suspicious happens.
  687. bitness = {2147483647:"32bit", 9223372036854775807:"64bit"}
  688. machine += f".{bitness[sys.maxsize]}"
  689. # fall through to standard osname-release-machine representation
  690. elif osname[:3] == "aix":
  691. from _aix_support import aix_platform
  692. return aix_platform()
  693. elif osname[:6] == "cygwin":
  694. osname = "cygwin"
  695. import re
  696. rel_re = re.compile(r'[\d.]+')
  697. m = rel_re.match(release)
  698. if m:
  699. release = m.group()
  700. elif osname[:6] == "darwin":
  701. import _osx_support
  702. osname, release, machine = _osx_support.get_platform_osx(
  703. get_config_vars(),
  704. osname, release, machine)
  705. return f"{osname}-{release}-{machine}"
  706. def get_python_version():
  707. return _PY_VERSION_SHORT
  708. def expand_makefile_vars(s, vars):
  709. """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in
  710. 'string' according to 'vars' (a dictionary mapping variable names to
  711. values). Variables not present in 'vars' are silently expanded to the
  712. empty string. The variable values in 'vars' should not contain further
  713. variable expansions; if 'vars' is the output of 'parse_makefile()',
  714. you're fine. Returns a variable-expanded version of 's'.
  715. """
  716. import re
  717. # This algorithm does multiple expansion, so if vars['foo'] contains
  718. # "${bar}", it will expand ${foo} to ${bar}, and then expand
  719. # ${bar}... and so forth. This is fine as long as 'vars' comes from
  720. # 'parse_makefile()', which takes care of such expansions eagerly,
  721. # according to make's variable expansion semantics.
  722. while True:
  723. m = re.search(_findvar1_rx, s) or re.search(_findvar2_rx, s)
  724. if m:
  725. (beg, end) = m.span()
  726. s = s[0:beg] + vars.get(m.group(1)) + s[end:]
  727. else:
  728. break
  729. return s
  730. def _print_dict(title, data):
  731. for index, (key, value) in enumerate(sorted(data.items())):
  732. if index == 0:
  733. print(f'{title}: ')
  734. print(f'\t{key} = "{value}"')
  735. def _main():
  736. """Display all information sysconfig detains."""
  737. if '--generate-posix-vars' in sys.argv:
  738. _generate_posix_vars()
  739. return
  740. print(f'Platform: "{get_platform()}"')
  741. print(f'Python version: "{get_python_version()}"')
  742. print(f'Current installation scheme: "{get_default_scheme()}"')
  743. print()
  744. _print_dict('Paths', get_paths())
  745. print()
  746. _print_dict('Variables', get_config_vars())
  747. if __name__ == '__main__':
  748. _main()