2021-04-29 06:33:14 -03:00
|
|
|
"""Check the stable ABI manifest or generate files from it
|
2020-12-04 18:05:58 -04:00
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
By default, the tool only checks existing files/libraries.
|
|
|
|
Pass --generate to recreate auto-generated files instead.
|
|
|
|
|
|
|
|
For actions that take a FILENAME, the filename can be left out to use a default
|
|
|
|
(relative to the manifest file, as they appear in the CPython codebase).
|
|
|
|
"""
|
|
|
|
|
|
|
|
from functools import partial
|
|
|
|
from pathlib import Path
|
|
|
|
import dataclasses
|
|
|
|
import subprocess
|
|
|
|
import sysconfig
|
2020-12-04 18:05:58 -04:00
|
|
|
import argparse
|
2021-04-29 06:33:14 -03:00
|
|
|
import textwrap
|
2022-04-29 11:18:08 -03:00
|
|
|
import tomllib
|
2021-04-29 06:33:14 -03:00
|
|
|
import difflib
|
2022-04-29 11:18:08 -03:00
|
|
|
import pprint
|
2021-04-29 06:33:14 -03:00
|
|
|
import sys
|
|
|
|
import os
|
2021-02-17 06:14:42 -04:00
|
|
|
import os.path
|
2021-04-29 06:33:14 -03:00
|
|
|
import io
|
2021-02-17 06:14:42 -04:00
|
|
|
import re
|
2021-05-11 11:04:33 -03:00
|
|
|
import csv
|
2021-04-29 06:33:14 -03:00
|
|
|
|
2022-10-17 07:01:00 -03:00
|
|
|
SCRIPT_NAME = 'Tools/build/stable_abi.py'
|
2021-04-29 06:33:14 -03:00
|
|
|
MISSING = object()
|
2020-12-04 18:05:58 -04:00
|
|
|
|
|
|
|
EXCLUDED_HEADERS = {
|
|
|
|
"bytes_methods.h",
|
|
|
|
"cellobject.h",
|
|
|
|
"classobject.h",
|
|
|
|
"code.h",
|
|
|
|
"compile.h",
|
|
|
|
"datetime.h",
|
|
|
|
"dtoa.h",
|
|
|
|
"frameobject.h",
|
|
|
|
"genobject.h",
|
|
|
|
"longintrepr.h",
|
|
|
|
"parsetok.h",
|
|
|
|
"pyatomic.h",
|
|
|
|
"token.h",
|
|
|
|
"ucnhash.h",
|
|
|
|
}
|
2020-12-15 14:16:13 -04:00
|
|
|
MACOS = (sys.platform == "darwin")
|
2021-04-29 06:33:14 -03:00
|
|
|
UNIXY = MACOS or (sys.platform == "linux") # XXX should this be "not Windows"?
|
|
|
|
|
2022-04-08 09:35:11 -03:00
|
|
|
|
2022-04-29 11:18:08 -03:00
|
|
|
# The stable ABI manifest (Misc/stable_abi.toml) exists only to fill the
|
2021-04-29 06:33:14 -03:00
|
|
|
# following dataclasses.
|
|
|
|
# Feel free to change its syntax (and the `parse_manifest` function)
|
|
|
|
# to better serve that purpose (while keeping it human-readable).
|
|
|
|
|
|
|
|
class Manifest:
|
|
|
|
"""Collection of `ABIItem`s forming the stable ABI/limited API."""
|
2022-04-29 11:18:08 -03:00
|
|
|
def __init__(self):
|
|
|
|
self.contents = dict()
|
2021-04-29 06:33:14 -03:00
|
|
|
|
|
|
|
def add(self, item):
|
|
|
|
if item.name in self.contents:
|
|
|
|
# We assume that stable ABI items do not share names,
|
2021-10-06 14:55:16 -03:00
|
|
|
# even if they're different kinds (e.g. function vs. macro).
|
2021-04-29 06:33:14 -03:00
|
|
|
raise ValueError(f'duplicate ABI item {item.name}')
|
|
|
|
self.contents[item.name] = item
|
|
|
|
|
|
|
|
def select(self, kinds, *, include_abi_only=True, ifdef=None):
|
|
|
|
"""Yield selected items of the manifest
|
|
|
|
|
|
|
|
kinds: set of requested kinds, e.g. {'function', 'macro'}
|
|
|
|
include_abi_only: if True (default), include all items of the
|
|
|
|
stable ABI.
|
|
|
|
If False, include only items from the limited API
|
|
|
|
(i.e. items people should use today)
|
2022-04-29 11:18:08 -03:00
|
|
|
ifdef: set of feature macros (e.g. {'HAVE_FORK', 'MS_WINDOWS'}).
|
2021-04-29 06:33:14 -03:00
|
|
|
If None (default), items are not filtered by this. (This is
|
|
|
|
different from the empty set, which filters out all such
|
|
|
|
conditional items.)
|
|
|
|
"""
|
|
|
|
for name, item in sorted(self.contents.items()):
|
|
|
|
if item.kind not in kinds:
|
|
|
|
continue
|
|
|
|
if item.abi_only and not include_abi_only:
|
|
|
|
continue
|
|
|
|
if (ifdef is not None
|
|
|
|
and item.ifdef is not None
|
|
|
|
and item.ifdef not in ifdef):
|
|
|
|
continue
|
|
|
|
yield item
|
|
|
|
|
|
|
|
def dump(self):
|
|
|
|
"""Yield lines to recreate the manifest file (sans comments/newlines)"""
|
|
|
|
for item in self.contents.values():
|
2022-04-29 11:18:08 -03:00
|
|
|
fields = dataclasses.fields(item)
|
|
|
|
yield f"[{item.kind}.{item.name}]"
|
|
|
|
for field in fields:
|
|
|
|
if field.name in {'name', 'value', 'kind'}:
|
|
|
|
continue
|
|
|
|
value = getattr(item, field.name)
|
|
|
|
if value == field.default:
|
|
|
|
pass
|
|
|
|
elif value is True:
|
|
|
|
yield f" {field.name} = true"
|
|
|
|
elif value:
|
|
|
|
yield f" {field.name} = {value!r}"
|
|
|
|
|
|
|
|
|
|
|
|
itemclasses = {}
|
|
|
|
def itemclass(kind):
|
|
|
|
"""Register the decorated class in `itemclasses`"""
|
|
|
|
def decorator(cls):
|
|
|
|
itemclasses[kind] = cls
|
|
|
|
return cls
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
@itemclass('function')
|
|
|
|
@itemclass('macro')
|
|
|
|
@itemclass('data')
|
|
|
|
@itemclass('const')
|
|
|
|
@itemclass('typedef')
|
2021-04-29 06:33:14 -03:00
|
|
|
@dataclasses.dataclass
|
|
|
|
class ABIItem:
|
|
|
|
"""Information on one item (function, macro, struct, etc.)"""
|
|
|
|
|
|
|
|
name: str
|
2022-04-29 11:18:08 -03:00
|
|
|
kind: str
|
2021-04-29 06:33:14 -03:00
|
|
|
added: str = None
|
|
|
|
abi_only: bool = False
|
|
|
|
ifdef: str = None
|
2022-04-29 11:18:08 -03:00
|
|
|
|
|
|
|
@itemclass('feature_macro')
|
|
|
|
@dataclasses.dataclass(kw_only=True)
|
|
|
|
class FeatureMacro(ABIItem):
|
|
|
|
name: str
|
|
|
|
doc: str
|
2022-04-28 11:30:28 -03:00
|
|
|
windows: bool = False
|
2022-04-29 11:18:08 -03:00
|
|
|
abi_only: bool = True
|
2021-04-29 06:33:14 -03:00
|
|
|
|
2022-04-29 11:18:08 -03:00
|
|
|
@itemclass('struct')
|
|
|
|
@dataclasses.dataclass(kw_only=True)
|
|
|
|
class Struct(ABIItem):
|
|
|
|
struct_abi_kind: str
|
|
|
|
members: list = None
|
2021-04-29 06:33:14 -03:00
|
|
|
|
|
|
|
|
|
|
|
def parse_manifest(file):
|
|
|
|
"""Parse the given file (iterable of lines) to a Manifest"""
|
|
|
|
|
|
|
|
manifest = Manifest()
|
|
|
|
|
2022-04-29 11:18:08 -03:00
|
|
|
data = tomllib.load(file)
|
2021-04-29 06:33:14 -03:00
|
|
|
|
2022-04-29 11:18:08 -03:00
|
|
|
for kind, itemclass in itemclasses.items():
|
|
|
|
for name, item_data in data[kind].items():
|
|
|
|
try:
|
|
|
|
item = itemclass(name=name, kind=kind, **item_data)
|
|
|
|
manifest.add(item)
|
|
|
|
except BaseException as exc:
|
|
|
|
exc.add_note(f'in {kind} {name}')
|
|
|
|
raise
|
2022-04-28 11:30:28 -03:00
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
return manifest
|
|
|
|
|
|
|
|
# The tool can run individual "actions".
|
|
|
|
# Most actions are "generators", which generate a single file from the
|
|
|
|
# manifest. (Checking works by generating a temp file & comparing.)
|
|
|
|
# Other actions, like "--unixy-check", don't work on a single file.
|
|
|
|
|
|
|
|
generators = []
|
|
|
|
def generator(var_name, default_path):
|
|
|
|
"""Decorates a file generator: function that writes to a file"""
|
|
|
|
def _decorator(func):
|
|
|
|
func.var_name = var_name
|
|
|
|
func.arg_name = '--' + var_name.replace('_', '-')
|
|
|
|
func.default_path = default_path
|
|
|
|
generators.append(func)
|
|
|
|
return func
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
|
|
|
@generator("python3dll", 'PC/python3dll.c')
|
|
|
|
def gen_python3dll(manifest, args, outfile):
|
|
|
|
"""Generate/check the source for the Windows stable ABI library"""
|
|
|
|
write = partial(print, file=outfile)
|
2023-06-06 05:40:32 -03:00
|
|
|
content = f"""\
|
2021-04-29 06:33:14 -03:00
|
|
|
/* Re-export stable Python ABI */
|
|
|
|
|
2022-10-17 07:01:00 -03:00
|
|
|
/* Generated by {SCRIPT_NAME} */
|
|
|
|
"""
|
|
|
|
content += r"""
|
2021-04-29 06:33:14 -03:00
|
|
|
#ifdef _M_IX86
|
|
|
|
#define DECORATE "_"
|
|
|
|
#else
|
|
|
|
#define DECORATE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define EXPORT_FUNC(name) \
|
|
|
|
__pragma(comment(linker, "/EXPORT:" DECORATE #name "=" PYTHON_DLL_NAME "." #name))
|
|
|
|
#define EXPORT_DATA(name) \
|
|
|
|
__pragma(comment(linker, "/EXPORT:" DECORATE #name "=" PYTHON_DLL_NAME "." #name ",DATA"))
|
2022-10-17 07:01:00 -03:00
|
|
|
"""
|
|
|
|
write(textwrap.dedent(content))
|
2021-04-29 06:33:14 -03:00
|
|
|
|
|
|
|
def sort_key(item):
|
|
|
|
return item.name.lower()
|
|
|
|
|
2022-04-29 11:18:08 -03:00
|
|
|
windows_feature_macros = {
|
|
|
|
item.name for item in manifest.select({'feature_macro'}) if item.windows
|
2022-04-28 11:30:28 -03:00
|
|
|
}
|
2021-04-29 06:33:14 -03:00
|
|
|
for item in sorted(
|
|
|
|
manifest.select(
|
2022-04-29 11:18:08 -03:00
|
|
|
{'function'},
|
|
|
|
include_abi_only=True,
|
|
|
|
ifdef=windows_feature_macros),
|
2021-04-29 06:33:14 -03:00
|
|
|
key=sort_key):
|
|
|
|
write(f'EXPORT_FUNC({item.name})')
|
|
|
|
|
|
|
|
write()
|
|
|
|
|
|
|
|
for item in sorted(
|
|
|
|
manifest.select(
|
2022-04-29 11:18:08 -03:00
|
|
|
{'data'},
|
|
|
|
include_abi_only=True,
|
|
|
|
ifdef=windows_feature_macros),
|
2021-04-29 06:33:14 -03:00
|
|
|
key=sort_key):
|
|
|
|
write(f'EXPORT_DATA({item.name})')
|
|
|
|
|
2021-05-11 11:04:33 -03:00
|
|
|
REST_ROLES = {
|
|
|
|
'function': 'function',
|
|
|
|
'data': 'var',
|
|
|
|
'struct': 'type',
|
|
|
|
'macro': 'macro',
|
|
|
|
# 'const': 'const', # all undocumented
|
|
|
|
'typedef': 'type',
|
|
|
|
}
|
2021-04-29 06:33:14 -03:00
|
|
|
|
|
|
|
@generator("doc_list", 'Doc/data/stable_abi.dat')
|
|
|
|
def gen_doc_annotations(manifest, args, outfile):
|
|
|
|
"""Generate/check the stable ABI list for documentation annotations"""
|
2021-05-11 11:04:33 -03:00
|
|
|
writer = csv.DictWriter(
|
2022-04-06 11:50:45 -03:00
|
|
|
outfile,
|
|
|
|
['role', 'name', 'added', 'ifdef_note', 'struct_abi_kind'],
|
|
|
|
lineterminator='\n')
|
2021-05-11 11:04:33 -03:00
|
|
|
writer.writeheader()
|
|
|
|
for item in manifest.select(REST_ROLES.keys(), include_abi_only=False):
|
|
|
|
if item.ifdef:
|
2022-04-28 11:30:28 -03:00
|
|
|
ifdef_note = manifest.contents[item.ifdef].doc
|
2021-05-11 11:04:33 -03:00
|
|
|
else:
|
|
|
|
ifdef_note = None
|
2022-04-29 11:18:08 -03:00
|
|
|
row = {
|
2021-05-11 11:04:33 -03:00
|
|
|
'role': REST_ROLES[item.kind],
|
|
|
|
'name': item.name,
|
|
|
|
'added': item.added,
|
2022-04-29 11:18:08 -03:00
|
|
|
'ifdef_note': ifdef_note}
|
|
|
|
rows = [row]
|
|
|
|
if item.kind == 'struct':
|
|
|
|
row['struct_abi_kind'] = item.struct_abi_kind
|
|
|
|
for member_name in item.members or ():
|
|
|
|
rows.append({
|
|
|
|
'role': 'member',
|
|
|
|
'name': f'{item.name}.{member_name}',
|
|
|
|
'added': item.added})
|
|
|
|
writer.writerows(rows)
|
2021-04-29 06:33:14 -03:00
|
|
|
|
2021-10-22 05:12:06 -03:00
|
|
|
@generator("ctypes_test", 'Lib/test/test_stable_abi_ctypes.py')
|
|
|
|
def gen_ctypes_test(manifest, args, outfile):
|
|
|
|
"""Generate/check the ctypes-based test for exported symbols"""
|
|
|
|
write = partial(print, file=outfile)
|
2023-06-06 05:40:32 -03:00
|
|
|
write(textwrap.dedent(f'''\
|
|
|
|
# Generated by {SCRIPT_NAME}
|
2021-10-22 05:12:06 -03:00
|
|
|
|
|
|
|
"""Test that all symbols of the Stable ABI are accessible using ctypes
|
|
|
|
"""
|
|
|
|
|
2022-04-28 11:30:28 -03:00
|
|
|
import sys
|
2021-10-22 05:12:06 -03:00
|
|
|
import unittest
|
|
|
|
from test.support.import_helper import import_module
|
2024-04-03 10:11:36 -03:00
|
|
|
try:
|
|
|
|
from _testcapi import get_feature_macros
|
|
|
|
except ImportError:
|
|
|
|
raise unittest.SkipTest("requires _testcapi")
|
2021-10-22 05:12:06 -03:00
|
|
|
|
2022-04-28 11:30:28 -03:00
|
|
|
feature_macros = get_feature_macros()
|
2024-01-29 11:45:31 -04:00
|
|
|
|
|
|
|
# Stable ABI is incompatible with Py_TRACE_REFS builds due to PyObject
|
|
|
|
# layout differences.
|
|
|
|
# See https://github.com/python/cpython/issues/88299#issuecomment-1113366226
|
|
|
|
if feature_macros['Py_TRACE_REFS']:
|
|
|
|
raise unittest.SkipTest("incompatible with Py_TRACE_REFS.")
|
|
|
|
|
2021-10-22 05:12:06 -03:00
|
|
|
ctypes_test = import_module('ctypes')
|
|
|
|
|
|
|
|
class TestStableABIAvailability(unittest.TestCase):
|
|
|
|
def test_available_symbols(self):
|
2022-04-28 11:30:28 -03:00
|
|
|
|
2021-10-22 05:12:06 -03:00
|
|
|
for symbol_name in SYMBOL_NAMES:
|
|
|
|
with self.subTest(symbol_name):
|
|
|
|
ctypes_test.pythonapi[symbol_name]
|
|
|
|
|
2022-04-28 11:30:28 -03:00
|
|
|
def test_feature_macros(self):
|
2022-04-29 11:18:08 -03:00
|
|
|
self.assertEqual(
|
|
|
|
set(get_feature_macros()), EXPECTED_FEATURE_MACROS)
|
2022-04-28 11:30:28 -03:00
|
|
|
|
|
|
|
# The feature macros for Windows are used in creating the DLL
|
|
|
|
# definition, so they must be known on all platforms.
|
|
|
|
# If we are on Windows, we check that the hardcoded data matches
|
|
|
|
# the reality.
|
|
|
|
@unittest.skipIf(sys.platform != "win32", "Windows specific test")
|
|
|
|
def test_windows_feature_macros(self):
|
2022-04-29 11:18:08 -03:00
|
|
|
for name, value in WINDOWS_FEATURE_MACROS.items():
|
2022-04-28 11:30:28 -03:00
|
|
|
if value != 'maybe':
|
|
|
|
with self.subTest(name):
|
|
|
|
self.assertEqual(feature_macros[name], value)
|
|
|
|
|
2021-10-22 05:12:06 -03:00
|
|
|
SYMBOL_NAMES = (
|
|
|
|
'''))
|
|
|
|
items = manifest.select(
|
|
|
|
{'function', 'data'},
|
|
|
|
include_abi_only=True,
|
2022-04-28 11:30:28 -03:00
|
|
|
)
|
2024-01-29 11:45:31 -04:00
|
|
|
feature_macros = list(manifest.select({'feature_macro'}))
|
|
|
|
optional_items = {m.name: [] for m in feature_macros}
|
2021-10-22 05:12:06 -03:00
|
|
|
for item in items:
|
2022-04-28 11:30:28 -03:00
|
|
|
if item.ifdef:
|
2024-01-29 11:45:31 -04:00
|
|
|
optional_items[item.ifdef].append(item.name)
|
2022-04-28 11:30:28 -03:00
|
|
|
else:
|
|
|
|
write(f' "{item.name}",')
|
2021-10-22 05:12:06 -03:00
|
|
|
write(")")
|
2022-04-29 11:18:08 -03:00
|
|
|
for ifdef, names in optional_items.items():
|
2022-04-28 11:30:28 -03:00
|
|
|
write(f"if feature_macros[{ifdef!r}]:")
|
|
|
|
write(f" SYMBOL_NAMES += (")
|
|
|
|
for name in names:
|
|
|
|
write(f" {name!r},")
|
|
|
|
write(" )")
|
|
|
|
write("")
|
2022-04-29 11:18:08 -03:00
|
|
|
feature_names = sorted(m.name for m in feature_macros)
|
|
|
|
write(f"EXPECTED_FEATURE_MACROS = set({pprint.pformat(feature_names)})")
|
2022-04-28 11:30:28 -03:00
|
|
|
|
2022-04-29 11:18:08 -03:00
|
|
|
windows_feature_macros = {m.name: m.windows for m in feature_macros}
|
|
|
|
write(f"WINDOWS_FEATURE_MACROS = {pprint.pformat(windows_feature_macros)}")
|
2022-04-28 11:30:28 -03:00
|
|
|
|
|
|
|
|
|
|
|
@generator("testcapi_feature_macros", 'Modules/_testcapi_feature_macros.inc')
|
|
|
|
def gen_testcapi_feature_macros(manifest, args, outfile):
|
|
|
|
"""Generate/check the stable ABI list for documentation annotations"""
|
|
|
|
write = partial(print, file=outfile)
|
2023-06-06 05:40:32 -03:00
|
|
|
write(f'// Generated by {SCRIPT_NAME}')
|
2022-04-28 11:30:28 -03:00
|
|
|
write()
|
|
|
|
write('// Add an entry in dict `result` for each Stable ABI feature macro.')
|
|
|
|
write()
|
2022-04-29 11:18:08 -03:00
|
|
|
for macro in manifest.select({'feature_macro'}):
|
2022-04-28 11:30:28 -03:00
|
|
|
name = macro.name
|
|
|
|
write(f'#ifdef {name}')
|
|
|
|
write(f' res = PyDict_SetItemString(result, "{name}", Py_True);')
|
|
|
|
write('#else')
|
|
|
|
write(f' res = PyDict_SetItemString(result, "{name}", Py_False);')
|
|
|
|
write('#endif')
|
|
|
|
write('if (res) {')
|
|
|
|
write(' Py_DECREF(result); return NULL;')
|
|
|
|
write('}')
|
|
|
|
write()
|
2021-10-22 05:12:06 -03:00
|
|
|
|
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
def generate_or_check(manifest, args, path, func):
|
|
|
|
"""Generate/check a file with a single generator
|
|
|
|
|
|
|
|
Return True if successful; False if a comparison failed.
|
|
|
|
"""
|
|
|
|
|
|
|
|
outfile = io.StringIO()
|
|
|
|
func(manifest, args, outfile)
|
|
|
|
generated = outfile.getvalue()
|
|
|
|
existing = path.read_text()
|
|
|
|
|
|
|
|
if generated != existing:
|
|
|
|
if args.generate:
|
|
|
|
path.write_text(generated)
|
|
|
|
else:
|
|
|
|
print(f'File {path} differs from expected!')
|
|
|
|
diff = difflib.unified_diff(
|
|
|
|
generated.splitlines(), existing.splitlines(),
|
|
|
|
str(path), '<expected>',
|
|
|
|
lineterm='',
|
|
|
|
)
|
|
|
|
for line in diff:
|
|
|
|
print(line)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def do_unixy_check(manifest, args):
|
|
|
|
"""Check headers & library using "Unixy" tools (GCC/clang, binutils)"""
|
|
|
|
okay = True
|
2020-12-04 18:05:58 -04:00
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
# Get all macros first: we'll need feature macros like HAVE_FORK and
|
|
|
|
# MS_WINDOWS for everything else
|
|
|
|
present_macros = gcc_get_limited_api_macros(['Include/Python.h'])
|
2022-04-29 11:18:08 -03:00
|
|
|
feature_macros = set(m.name for m in manifest.select({'feature_macro'}))
|
|
|
|
feature_macros &= present_macros
|
2021-04-29 06:33:14 -03:00
|
|
|
|
2021-10-06 14:55:16 -03:00
|
|
|
# Check that we have all needed macros
|
2021-04-29 06:33:14 -03:00
|
|
|
expected_macros = set(
|
|
|
|
item.name for item in manifest.select({'macro'})
|
|
|
|
)
|
|
|
|
missing_macros = expected_macros - present_macros
|
|
|
|
okay &= _report_unexpected_items(
|
|
|
|
missing_macros,
|
|
|
|
'Some macros from are not defined from "Include/Python.h"'
|
|
|
|
+ 'with Py_LIMITED_API:')
|
|
|
|
|
|
|
|
expected_symbols = set(item.name for item in manifest.select(
|
2022-04-29 11:18:08 -03:00
|
|
|
{'function', 'data'}, include_abi_only=True, ifdef=feature_macros,
|
2021-04-29 06:33:14 -03:00
|
|
|
))
|
|
|
|
|
|
|
|
# Check the static library (*.a)
|
|
|
|
LIBRARY = sysconfig.get_config_var("LIBRARY")
|
|
|
|
if not LIBRARY:
|
|
|
|
raise Exception("failed to get LIBRARY variable from sysconfig")
|
|
|
|
if os.path.exists(LIBRARY):
|
|
|
|
okay &= binutils_check_library(
|
|
|
|
manifest, LIBRARY, expected_symbols, dynamic=False)
|
|
|
|
|
|
|
|
# Check the dynamic library (*.so)
|
|
|
|
LDLIBRARY = sysconfig.get_config_var("LDLIBRARY")
|
|
|
|
if not LDLIBRARY:
|
|
|
|
raise Exception("failed to get LDLIBRARY variable from sysconfig")
|
|
|
|
okay &= binutils_check_library(
|
|
|
|
manifest, LDLIBRARY, expected_symbols, dynamic=False)
|
|
|
|
|
|
|
|
# Check definitions in the header files
|
|
|
|
expected_defs = set(item.name for item in manifest.select(
|
2022-04-29 11:18:08 -03:00
|
|
|
{'function', 'data'}, include_abi_only=False, ifdef=feature_macros,
|
2021-04-29 06:33:14 -03:00
|
|
|
))
|
|
|
|
found_defs = gcc_get_limited_api_definitions(['Include/Python.h'])
|
|
|
|
missing_defs = expected_defs - found_defs
|
|
|
|
okay &= _report_unexpected_items(
|
|
|
|
missing_defs,
|
|
|
|
'Some expected declarations were not declared in '
|
|
|
|
+ '"Include/Python.h" with Py_LIMITED_API:')
|
|
|
|
|
|
|
|
# Some Limited API macros are defined in terms of private symbols.
|
|
|
|
# These are not part of Limited API (even though they're defined with
|
|
|
|
# Py_LIMITED_API). They must be part of the Stable ABI, though.
|
|
|
|
private_symbols = {n for n in expected_symbols if n.startswith('_')}
|
|
|
|
extra_defs = found_defs - expected_defs - private_symbols
|
|
|
|
okay &= _report_unexpected_items(
|
|
|
|
extra_defs,
|
|
|
|
'Some extra declarations were found in "Include/Python.h" '
|
|
|
|
+ 'with Py_LIMITED_API:')
|
|
|
|
|
|
|
|
return okay
|
|
|
|
|
|
|
|
|
|
|
|
def _report_unexpected_items(items, msg):
|
|
|
|
"""If there are any `items`, report them using "msg" and return false"""
|
|
|
|
if items:
|
|
|
|
print(msg, file=sys.stderr)
|
|
|
|
for item in sorted(items):
|
|
|
|
print(' -', item, file=sys.stderr)
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def binutils_get_exported_symbols(library, dynamic=False):
|
|
|
|
"""Retrieve exported symbols using the nm(1) tool from binutils"""
|
2020-12-04 18:05:58 -04:00
|
|
|
# Only look at dynamic symbols
|
|
|
|
args = ["nm", "--no-sort"]
|
|
|
|
if dynamic:
|
|
|
|
args.append("--dynamic")
|
|
|
|
args.append(library)
|
|
|
|
proc = subprocess.run(args, stdout=subprocess.PIPE, universal_newlines=True)
|
|
|
|
if proc.returncode:
|
|
|
|
sys.stdout.write(proc.stdout)
|
|
|
|
sys.exit(proc.returncode)
|
|
|
|
|
|
|
|
stdout = proc.stdout.rstrip()
|
|
|
|
if not stdout:
|
|
|
|
raise Exception("command output is empty")
|
|
|
|
|
|
|
|
for line in stdout.splitlines():
|
|
|
|
# Split line '0000000000001b80 D PyTextIOWrapper_Type'
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
parts = line.split(maxsplit=2)
|
|
|
|
if len(parts) < 3:
|
|
|
|
continue
|
|
|
|
|
|
|
|
symbol = parts[-1]
|
2020-12-15 14:16:13 -04:00
|
|
|
if MACOS and symbol.startswith("_"):
|
|
|
|
yield symbol[1:]
|
|
|
|
else:
|
|
|
|
yield symbol
|
2020-12-04 18:05:58 -04:00
|
|
|
|
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
def binutils_check_library(manifest, library, expected_symbols, dynamic):
|
|
|
|
"""Check that library exports all expected_symbols"""
|
|
|
|
available_symbols = set(binutils_get_exported_symbols(library, dynamic))
|
|
|
|
missing_symbols = expected_symbols - available_symbols
|
2020-12-04 18:05:58 -04:00
|
|
|
if missing_symbols:
|
2021-04-29 06:33:14 -03:00
|
|
|
print(textwrap.dedent(f"""\
|
|
|
|
Some symbols from the limited API are missing from {library}:
|
|
|
|
{', '.join(missing_symbols)}
|
2020-12-04 19:19:21 -04:00
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
This error means that there are some missing symbols among the
|
|
|
|
ones exported in the library.
|
|
|
|
This normally means that some symbol, function implementation or
|
|
|
|
a prototype belonging to a symbol in the limited API has been
|
|
|
|
deleted or is missing.
|
|
|
|
"""), file=sys.stderr)
|
|
|
|
return False
|
|
|
|
return True
|
2020-12-04 19:19:21 -04:00
|
|
|
|
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
def gcc_get_limited_api_macros(headers):
|
|
|
|
"""Get all limited API macros from headers.
|
2020-12-04 19:19:21 -04:00
|
|
|
|
2021-10-06 14:55:16 -03:00
|
|
|
Runs the preprocessor over all the header files in "Include" setting
|
2021-04-29 06:33:14 -03:00
|
|
|
"-DPy_LIMITED_API" to the correct value for the running version of the
|
|
|
|
interpreter and extracting all macro definitions (via adding -dM to the
|
|
|
|
compiler arguments).
|
2020-12-04 18:05:58 -04:00
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
Requires Python built with a GCC-compatible compiler. (clang might work)
|
|
|
|
"""
|
2020-12-04 18:05:58 -04:00
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
api_hexversion = sys.version_info.major << 24 | sys.version_info.minor << 16
|
2020-12-04 18:05:58 -04:00
|
|
|
|
2023-12-01 05:37:40 -04:00
|
|
|
preprocessor_output_with_macros = subprocess.check_output(
|
2021-04-29 06:33:14 -03:00
|
|
|
sysconfig.get_config_var("CC").split()
|
|
|
|
+ [
|
|
|
|
# Prevent the expansion of the exported macros so we can
|
|
|
|
# capture them later
|
|
|
|
"-DSIZEOF_WCHAR_T=4", # The actual value is not important
|
|
|
|
f"-DPy_LIMITED_API={api_hexversion}",
|
|
|
|
"-I.",
|
|
|
|
"-I./Include",
|
|
|
|
"-dM",
|
|
|
|
"-E",
|
|
|
|
]
|
|
|
|
+ [str(file) for file in headers],
|
|
|
|
text=True,
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
return {
|
|
|
|
target
|
|
|
|
for target in re.findall(
|
2023-12-01 05:37:40 -04:00
|
|
|
r"#define (\w+)", preprocessor_output_with_macros
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
2021-04-29 06:33:14 -03:00
|
|
|
}
|
2020-12-04 18:05:58 -04:00
|
|
|
|
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
def gcc_get_limited_api_definitions(headers):
|
|
|
|
"""Get all limited API definitions from headers.
|
2020-12-04 18:05:58 -04:00
|
|
|
|
2021-10-06 14:55:16 -03:00
|
|
|
Run the preprocessor over all the header files in "Include" setting
|
2021-04-29 06:33:14 -03:00
|
|
|
"-DPy_LIMITED_API" to the correct value for the running version of the
|
|
|
|
interpreter.
|
|
|
|
|
|
|
|
The limited API symbols will be extracted from the output of this command
|
|
|
|
as it includes the prototypes and definitions of all the exported symbols
|
|
|
|
that are in the limited api.
|
2020-12-04 18:05:58 -04:00
|
|
|
|
|
|
|
This function does *NOT* extract the macros defined on the limited API
|
2021-04-29 06:33:14 -03:00
|
|
|
|
|
|
|
Requires Python built with a GCC-compatible compiler. (clang might work)
|
2020-12-04 18:05:58 -04:00
|
|
|
"""
|
2021-04-29 06:33:14 -03:00
|
|
|
api_hexversion = sys.version_info.major << 24 | sys.version_info.minor << 16
|
2023-12-01 05:37:40 -04:00
|
|
|
preprocessor_output = subprocess.check_output(
|
2020-12-04 18:05:58 -04:00
|
|
|
sysconfig.get_config_var("CC").split()
|
|
|
|
+ [
|
2021-04-29 06:33:14 -03:00
|
|
|
# Prevent the expansion of the exported macros so we can capture
|
|
|
|
# them later
|
2020-12-04 18:05:58 -04:00
|
|
|
"-DPyAPI_FUNC=__PyAPI_FUNC",
|
|
|
|
"-DPyAPI_DATA=__PyAPI_DATA",
|
|
|
|
"-DEXPORT_DATA=__EXPORT_DATA",
|
|
|
|
"-D_Py_NO_RETURN=",
|
|
|
|
"-DSIZEOF_WCHAR_T=4", # The actual value is not important
|
2021-04-29 06:33:14 -03:00
|
|
|
f"-DPy_LIMITED_API={api_hexversion}",
|
2020-12-04 18:05:58 -04:00
|
|
|
"-I.",
|
|
|
|
"-I./Include",
|
|
|
|
"-E",
|
|
|
|
]
|
|
|
|
+ [str(file) for file in headers],
|
|
|
|
text=True,
|
|
|
|
stderr=subprocess.DEVNULL,
|
|
|
|
)
|
|
|
|
stable_functions = set(
|
2023-12-01 05:37:40 -04:00
|
|
|
re.findall(r"__PyAPI_FUNC\(.*?\)\s*(.*?)\s*\(", preprocessor_output)
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
|
|
|
stable_exported_data = set(
|
2023-12-01 05:37:40 -04:00
|
|
|
re.findall(r"__EXPORT_DATA\((.*?)\)", preprocessor_output)
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
|
|
|
stable_data = set(
|
2023-12-01 05:37:40 -04:00
|
|
|
re.findall(r"__PyAPI_DATA\(.*?\)[\s\*\(]*([^);]*)\)?.*;", preprocessor_output)
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
2021-04-29 06:33:14 -03:00
|
|
|
return stable_data | stable_exported_data | stable_functions
|
2020-12-04 18:05:58 -04:00
|
|
|
|
2021-06-16 06:22:36 -03:00
|
|
|
def check_private_names(manifest):
|
|
|
|
"""Ensure limited API doesn't contain private names
|
|
|
|
|
|
|
|
Names prefixed by an underscore are private by definition.
|
|
|
|
"""
|
|
|
|
for name, item in manifest.contents.items():
|
|
|
|
if name.startswith('_') and not item.abi_only:
|
|
|
|
raise ValueError(
|
|
|
|
f'`{name}` is private (underscore-prefixed) and should be '
|
2024-03-05 12:05:52 -04:00
|
|
|
+ 'removed from the stable ABI list or marked `abi_only`')
|
2020-12-04 18:05:58 -04:00
|
|
|
|
2022-04-29 11:18:08 -03:00
|
|
|
def check_dump(manifest, filename):
|
|
|
|
"""Check that manifest.dump() corresponds to the data.
|
|
|
|
|
|
|
|
Mainly useful when debugging this script.
|
|
|
|
"""
|
|
|
|
dumped = tomllib.loads('\n'.join(manifest.dump()))
|
|
|
|
with filename.open('rb') as file:
|
|
|
|
from_file = tomllib.load(file)
|
|
|
|
if dumped != from_file:
|
|
|
|
print(f'Dump differs from loaded data!', file=sys.stderr)
|
|
|
|
diff = difflib.unified_diff(
|
|
|
|
pprint.pformat(dumped).splitlines(),
|
|
|
|
pprint.pformat(from_file).splitlines(),
|
|
|
|
'<dumped>', str(filename),
|
|
|
|
lineterm='',
|
|
|
|
)
|
|
|
|
for line in diff:
|
|
|
|
print(line, file=sys.stderr)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2020-12-04 18:05:58 -04:00
|
|
|
def main():
|
2021-04-29 06:33:14 -03:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=__doc__,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"file", type=Path, metavar='FILE',
|
|
|
|
help="file with the stable abi manifest",
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
2021-04-29 06:33:14 -03:00
|
|
|
parser.add_argument(
|
|
|
|
"--generate", action='store_true',
|
|
|
|
help="generate file(s), rather than just checking them",
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
2021-04-29 06:33:14 -03:00
|
|
|
parser.add_argument(
|
|
|
|
"--generate-all", action='store_true',
|
|
|
|
help="as --generate, but generate all file(s) using default filenames."
|
|
|
|
+ " (unlike --all, does not run any extra checks)",
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
2021-04-29 06:33:14 -03:00
|
|
|
parser.add_argument(
|
|
|
|
"-a", "--all", action='store_true',
|
|
|
|
help="run all available checks using default filenames",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l", "--list", action='store_true',
|
|
|
|
help="list available generators and their default filenames; then exit",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--dump", action='store_true',
|
|
|
|
help="dump the manifest contents (used for debugging the parser)",
|
|
|
|
)
|
|
|
|
|
|
|
|
actions_group = parser.add_argument_group('actions')
|
|
|
|
for gen in generators:
|
|
|
|
actions_group.add_argument(
|
|
|
|
gen.arg_name, dest=gen.var_name,
|
|
|
|
type=str, nargs="?", default=MISSING,
|
|
|
|
metavar='FILENAME',
|
|
|
|
help=gen.__doc__,
|
|
|
|
)
|
|
|
|
actions_group.add_argument(
|
|
|
|
'--unixy-check', action='store_true',
|
|
|
|
help=do_unixy_check.__doc__,
|
2020-12-04 18:05:58 -04:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
base_path = args.file.parent.parent
|
|
|
|
|
|
|
|
if args.list:
|
|
|
|
for gen in generators:
|
|
|
|
print(f'{gen.arg_name}: {base_path / gen.default_path}')
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
run_all_generators = args.generate_all
|
|
|
|
|
|
|
|
if args.generate_all:
|
|
|
|
args.generate = True
|
|
|
|
|
|
|
|
if args.all:
|
|
|
|
run_all_generators = True
|
2023-05-30 12:45:56 -03:00
|
|
|
if UNIXY:
|
|
|
|
args.unixy_check = True
|
2021-04-29 06:33:14 -03:00
|
|
|
|
2022-04-29 11:18:08 -03:00
|
|
|
try:
|
|
|
|
file = args.file.open('rb')
|
|
|
|
except FileNotFoundError as err:
|
|
|
|
if args.file.suffix == '.txt':
|
|
|
|
# Provide a better error message
|
|
|
|
suggestion = args.file.with_suffix('.toml')
|
|
|
|
raise FileNotFoundError(
|
|
|
|
f'{args.file} not found. Did you mean {suggestion} ?') from err
|
|
|
|
raise
|
|
|
|
with file:
|
2021-04-29 06:33:14 -03:00
|
|
|
manifest = parse_manifest(file)
|
|
|
|
|
2021-06-16 06:22:36 -03:00
|
|
|
check_private_names(manifest)
|
|
|
|
|
2021-04-29 06:33:14 -03:00
|
|
|
# Remember results of all actions (as booleans).
|
|
|
|
# At the end we'll check that at least one action was run,
|
|
|
|
# and also fail if any are false.
|
|
|
|
results = {}
|
|
|
|
|
|
|
|
if args.dump:
|
|
|
|
for line in manifest.dump():
|
|
|
|
print(line)
|
2022-04-29 11:18:08 -03:00
|
|
|
results['dump'] = check_dump(manifest, args.file)
|
2021-04-29 06:33:14 -03:00
|
|
|
|
|
|
|
for gen in generators:
|
|
|
|
filename = getattr(args, gen.var_name)
|
|
|
|
if filename is None or (run_all_generators and filename is MISSING):
|
|
|
|
filename = base_path / gen.default_path
|
|
|
|
elif filename is MISSING:
|
|
|
|
continue
|
|
|
|
|
|
|
|
results[gen.var_name] = generate_or_check(manifest, args, filename, gen)
|
|
|
|
|
|
|
|
if args.unixy_check:
|
|
|
|
results['unixy_check'] = do_unixy_check(manifest, args)
|
|
|
|
|
|
|
|
if not results:
|
|
|
|
if args.generate:
|
|
|
|
parser.error('No file specified. Use --help for usage.')
|
|
|
|
parser.error('No check specified. Use --help for usage.')
|
|
|
|
|
|
|
|
failed_results = [name for name, result in results.items() if not result]
|
|
|
|
|
|
|
|
if failed_results:
|
|
|
|
raise Exception(f"""
|
|
|
|
These checks related to the stable ABI did not succeed:
|
|
|
|
{', '.join(failed_results)}
|
|
|
|
|
|
|
|
If you see diffs in the output, files derived from the stable
|
|
|
|
ABI manifest the were not regenerated.
|
|
|
|
Run `make regen-limited-abi` to fix this.
|
|
|
|
|
|
|
|
Otherwise, see the error(s) above.
|
|
|
|
|
|
|
|
The stable ABI manifest is at: {args.file}
|
|
|
|
Note that there is a process to follow when modifying it.
|
|
|
|
|
|
|
|
You can read more about the limited API and its contracts at:
|
|
|
|
|
|
|
|
https://docs.python.org/3/c-api/stable.html
|
|
|
|
|
|
|
|
And in PEP 384:
|
|
|
|
|
2022-03-30 08:00:27 -03:00
|
|
|
https://peps.python.org/pep-0384/
|
2021-04-29 06:33:14 -03:00
|
|
|
""")
|
2020-12-04 18:05:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|