Generate the Python module containing build-time variables.
()
| 462 | |
| 463 | |
| 464 | def _generate_posix_vars(): |
| 465 | """Generate the Python module containing build-time variables.""" |
| 466 | import pprint |
| 467 | vars = {} |
| 468 | # load the installed Makefile: |
| 469 | makefile = get_makefile_filename() |
| 470 | try: |
| 471 | _parse_makefile(makefile, vars) |
| 472 | except OSError as e: |
| 473 | msg = f"invalid Python installation: unable to open {makefile}" |
| 474 | if hasattr(e, "strerror"): |
| 475 | msg = f"{msg} ({e.strerror})" |
| 476 | raise OSError(msg) |
| 477 | # load the installed pyconfig.h: |
| 478 | config_h = get_config_h_filename() |
| 479 | try: |
| 480 | with open(config_h, encoding="utf-8") as f: |
| 481 | parse_config_h(f, vars) |
| 482 | except OSError as e: |
| 483 | msg = f"invalid Python installation: unable to open {config_h}" |
| 484 | if hasattr(e, "strerror"): |
| 485 | msg = f"{msg} ({e.strerror})" |
| 486 | raise OSError(msg) |
| 487 | # On AIX, there are wrong paths to the linker scripts in the Makefile |
| 488 | # -- these paths are relative to the Python source, but when installed |
| 489 | # the scripts are in another directory. |
| 490 | if _PYTHON_BUILD: |
| 491 | vars['BLDSHARED'] = vars['LDSHARED'] |
| 492 | |
| 493 | # There's a chicken-and-egg situation on OS X with regards to the |
| 494 | # _sysconfigdata module after the changes introduced by #15298: |
| 495 | # get_config_vars() is called by get_platform() as part of the |
| 496 | # `make pybuilddir.txt` target -- which is a precursor to the |
| 497 | # _sysconfigdata.py module being constructed. Unfortunately, |
| 498 | # get_config_vars() eventually calls _init_posix(), which attempts |
| 499 | # to import _sysconfigdata, which we won't have built yet. In order |
| 500 | # for _init_posix() to work, if we're on Darwin, just mock up the |
| 501 | # _sysconfigdata module manually and populate it with the build vars. |
| 502 | # This is more than sufficient for ensuring the subsequent call to |
| 503 | # get_platform() succeeds. |
| 504 | name = _get_sysconfigdata_name() |
| 505 | if 'darwin' in sys.platform: |
| 506 | import types |
| 507 | module = types.ModuleType(name) |
| 508 | module.build_time_vars = vars |
| 509 | sys.modules[name] = module |
| 510 | |
| 511 | pybuilddir = f'build/lib.{get_platform()}-{_PY_VERSION_SHORT}' |
| 512 | if hasattr(sys, "gettotalrefcount"): |
| 513 | pybuilddir += '-pydebug' |
| 514 | os.makedirs(pybuilddir, exist_ok=True) |
| 515 | destfile = os.path.join(pybuilddir, name + '.py') |
| 516 | |
| 517 | with open(destfile, 'w', encoding='utf8') as f: |
| 518 | f.write('# system configuration generated and used by' |
| 519 | ' the sysconfig module\n') |
| 520 | f.write('build_time_vars = ') |
| 521 | pprint.pprint(vars, stream=f) |
no test coverage detected