Generate the Python module containing build-time variables.
()
| 148 | |
| 149 | |
| 150 | def _generate_posix_vars(): |
| 151 | """Generate the Python module containing build-time variables.""" |
| 152 | vars = {} |
| 153 | # load the installed Makefile: |
| 154 | makefile = get_makefile_filename() |
| 155 | try: |
| 156 | _parse_makefile(makefile, vars) |
| 157 | except OSError as e: |
| 158 | msg = f"invalid Python installation: unable to open {makefile}" |
| 159 | if hasattr(e, "strerror"): |
| 160 | msg = f"{msg} ({e.strerror})" |
| 161 | raise OSError(msg) |
| 162 | # load the installed pyconfig.h: |
| 163 | config_h = get_config_h_filename() |
| 164 | try: |
| 165 | with open(config_h, encoding="utf-8") as f: |
| 166 | parse_config_h(f, vars) |
| 167 | except OSError as e: |
| 168 | msg = f"invalid Python installation: unable to open {config_h}" |
| 169 | if hasattr(e, "strerror"): |
| 170 | msg = f"{msg} ({e.strerror})" |
| 171 | raise OSError(msg) |
| 172 | # On AIX, there are wrong paths to the linker scripts in the Makefile |
| 173 | # -- these paths are relative to the Python source, but when installed |
| 174 | # the scripts are in another directory. |
| 175 | if _PYTHON_BUILD: |
| 176 | vars['BLDSHARED'] = vars['LDSHARED'] |
| 177 | |
| 178 | name = _get_sysconfigdata_name() |
| 179 | |
| 180 | # There's a chicken-and-egg situation on OS X with regards to the |
| 181 | # _sysconfigdata module after the changes introduced by #15298: |
| 182 | # get_config_vars() is called by get_platform() as part of the |
| 183 | # `make pybuilddir.txt` target -- which is a precursor to the |
| 184 | # _sysconfigdata.py module being constructed. Unfortunately, |
| 185 | # get_config_vars() eventually calls _init_posix(), which attempts |
| 186 | # to import _sysconfigdata, which we won't have built yet. In order |
| 187 | # for _init_posix() to work, if we're on Darwin, just mock up the |
| 188 | # _sysconfigdata module manually and populate it with the build vars. |
| 189 | # This is more than sufficient for ensuring the subsequent call to |
| 190 | # get_platform() succeeds. |
| 191 | # GH-127178: Since we started generating a .json file, we also need this to |
| 192 | # be able to run sysconfig.get_config_vars(). |
| 193 | module = types.ModuleType(name) |
| 194 | module.build_time_vars = vars |
| 195 | sys.modules[name] = module |
| 196 | |
| 197 | pybuilddir = _get_pybuilddir() |
| 198 | os.makedirs(pybuilddir, exist_ok=True) |
| 199 | destfile = os.path.join(pybuilddir, name + '.py') |
| 200 | |
| 201 | with open(destfile, 'w', encoding='utf8') as f: |
| 202 | f.write('# system configuration generated and used by' |
| 203 | ' the sysconfig module\n') |
| 204 | f.write('build_time_vars = ') |
| 205 | _print_config_dict(vars, stream=f) |
| 206 | |
| 207 | print(f'Written {destfile}') |
no test coverage detected
searching dependent graphs…