libpython-linkcheck 0.1.0a2__cp314-cp314t-manylinux_2_28_aarch64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ """Warn when an imported Python extension directly links to libpython.
2
+
3
+ Call :func:`enable` to install an audit hook in the current interpreter.
4
+ Before CPython loads an extension module, the hook reads the shared object's
5
+ own DT_NEEDED entries and issues a :class:`LibpythonLinkWarning` if one of
6
+ them names libpython. The warning goes through the normal warnings
7
+ machinery, so filters apply; with ``-Werror`` the import fails before the
8
+ object is loaded.
9
+ """
10
+
11
+ from ._elf import ELFError, libpython_dependencies
12
+ from ._hook import LibpythonLinkWarning, enable, is_enabled
13
+ from ._version import __version__
14
+
15
+ __all__ = [
16
+ "ELFError",
17
+ "LibpythonLinkWarning",
18
+ "__version__",
19
+ "enable",
20
+ "is_enabled",
21
+ "libpython_dependencies",
22
+ ]
@@ -0,0 +1,42 @@
1
+ """Command line: report ELF objects whose own DT_NEEDED entries name libpython.
2
+
3
+ Exit status: 0 if no file links libpython, 1 if at least one does, 2 if any
4
+ file could not be read or is not a valid ELF object.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import argparse
10
+ import sys
11
+ from collections.abc import Sequence
12
+
13
+ from ._elf import ELFError, libpython_dependencies
14
+
15
+
16
+ def main(argv: Sequence[str] | None = None) -> int:
17
+ parser = argparse.ArgumentParser(
18
+ prog="python -m libpython_linkcheck",
19
+ description=(
20
+ "Report ELF shared objects (e.g. extension modules) whose own DT_NEEDED "
21
+ "entries name libpython. Exit status: 0 none found, 1 found, 2 error."
22
+ ),
23
+ )
24
+ parser.add_argument("files", nargs="+", metavar="FILE", help="ELF object to inspect")
25
+ args = parser.parse_args(argv)
26
+
27
+ found = error = False
28
+ for path in args.files:
29
+ try:
30
+ needed = libpython_dependencies(path)
31
+ except (OSError, ELFError) as e:
32
+ print(f"{path}: error: {e}", file=sys.stderr)
33
+ error = True
34
+ continue
35
+ if needed:
36
+ print(f"{path}: DT_NEEDED {', '.join(needed)}")
37
+ found = True
38
+ return 2 if error else 1 if found else 0
39
+
40
+
41
+ if __name__ == "__main__":
42
+ sys.exit(main())
@@ -0,0 +1,246 @@
1
+ """Minimal, dependency-free reader for an ELF object's own DT_NEEDED entries.
2
+
3
+ The reader follows the dynamic loader's view of the file: program headers,
4
+ the PT_DYNAMIC segment, and DT_STRTAB translated to a file offset through the
5
+ PT_LOAD segments. It does not use section headers, so it works on objects
6
+ whose section table was stripped. Every read is bounds-checked against the
7
+ file size; malformed input raises :class:`ELFError`, never ``struct.error``
8
+ or ``IndexError``.
9
+
10
+ Only the object's *own* DT_NEEDED entries are reported. Transitive
11
+ dependencies are deliberately ignored.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import os
17
+ import re
18
+ import struct
19
+ from typing import BinaryIO
20
+
21
+ ELF_MAGIC = b"\x7fELF"
22
+
23
+ _ELFCLASS32 = 1
24
+ _ELFCLASS64 = 2
25
+ _ELFDATA2LSB = 1
26
+ _ELFDATA2MSB = 2
27
+
28
+ _PT_LOAD = 1
29
+ _PT_DYNAMIC = 2
30
+ _PN_XNUM = 0xFFFF
31
+
32
+ _DT_NULL = 0
33
+ _DT_NEEDED = 1
34
+ _DT_STRTAB = 5
35
+ _DT_STRSZ = 10
36
+
37
+ # Matches the file name of every libpython variant CPython has shipped:
38
+ # libpython3.so Stable ABI forwarding library
39
+ # libpython3.12.so.1.0 versioned (the SONAME of --enable-shared builds)
40
+ # libpython3.13t.so.1.0 free-threaded
41
+ # libpython3.12d.so, ...7m.so ABI-flagged (debug, pymalloc) builds
42
+ # libpython2.7.so.1.0 legacy
43
+ _LIBPYTHON_RE = re.compile(r"libpython[0-9]+(?:\.[0-9]+)?[a-z]*\.so(?:\.[0-9]+)*")
44
+
45
+
46
+ class ELFError(ValueError):
47
+ """The file is not a well-formed ELF object."""
48
+
49
+
50
+ class NotELFError(ELFError):
51
+ """The file does not start with the ELF magic number."""
52
+
53
+
54
+ def py_is_libpython(needed: str) -> bool:
55
+ """Return True if a DT_NEEDED entry names a libpython shared library.
56
+
57
+ Entries containing a path (``/usr/lib/libpython3.12.so.1.0``) are matched
58
+ on their final component.
59
+ """
60
+ return _LIBPYTHON_RE.fullmatch(needed.rpartition("/")[2]) is not None
61
+
62
+
63
+ def py_read_needed(path: str | os.PathLike[str] | os.PathLike[bytes] | bytes) -> list[str]:
64
+ """Pure-Python implementation of :func:`read_needed`."""
65
+ with open(path, "rb") as f:
66
+ return _read_needed(f)
67
+
68
+
69
+ def _native_read_needed(path: str | os.PathLike[str] | os.PathLike[bytes] | bytes) -> list[str]:
70
+ status, value = _native.read_needed(path)
71
+ if status == 0:
72
+ return value
73
+ if status == 2:
74
+ raise NotELFError(value)
75
+ raise ELFError(value)
76
+
77
+
78
+ if os.environ.get("LIBPYTHON_LINKCHECK_IMPLEMENTATION") == "python":
79
+ _native = None # forced pure-Python implementation (debugging/testing)
80
+ else:
81
+ try:
82
+ from . import _native # type: ignore[attr-defined]
83
+ except ImportError:
84
+ _native = None
85
+
86
+ #: "native" when the C implementation is in use, else "python".
87
+ IMPLEMENTATION = "python" if _native is None else "native"
88
+
89
+
90
+ def read_needed(path: str | os.PathLike[str] | os.PathLike[bytes] | bytes) -> list[str]:
91
+ """Return the DT_NEEDED entries of the ELF object at *path*, in order.
92
+
93
+ Returns an empty list for an ELF object without a dynamic segment.
94
+ Raises :class:`NotELFError` if the file is not ELF, :class:`ELFError` if it
95
+ is malformed, and :class:`OSError` if it cannot be read.
96
+ """
97
+ if _native is None:
98
+ return py_read_needed(path)
99
+ return _native_read_needed(path)
100
+
101
+
102
+ def is_libpython(needed: str) -> bool:
103
+ """Return True if a DT_NEEDED entry names a libpython shared library.
104
+
105
+ Entries containing a path (``/usr/lib/libpython3.12.so.1.0``) are matched
106
+ on their final component.
107
+ """
108
+ if _native is None:
109
+ return py_is_libpython(needed)
110
+ return _native.is_libpython(needed)
111
+
112
+
113
+ def libpython_dependencies(
114
+ path: str | os.PathLike[str] | os.PathLike[bytes] | bytes,
115
+ ) -> list[str]:
116
+ """Return the DT_NEEDED entries of the ELF object at *path* that name libpython."""
117
+ return [name for name in read_needed(path) if is_libpython(name)]
118
+
119
+
120
+ class _Reader:
121
+ def __init__(self, f: BinaryIO) -> None:
122
+ self._f = f
123
+ self.size = os.fstat(f.fileno()).st_size
124
+
125
+ def read(self, offset: int, length: int) -> bytes:
126
+ if offset < 0 or length < 0 or offset + length > self.size:
127
+ raise ELFError(
128
+ f"read of {length} bytes at offset {offset} is outside the file "
129
+ f"({self.size} bytes); truncated or corrupt ELF"
130
+ )
131
+ self._f.seek(offset)
132
+ data = self._f.read(length)
133
+ if len(data) != length:
134
+ raise ELFError("short read; file changed while reading")
135
+ return data
136
+
137
+
138
+ class _Layout:
139
+ """Struct layouts for one ELF class/byte order."""
140
+
141
+ def __init__(self, ident: bytes) -> None:
142
+ ei_class, ei_data = ident[4], ident[5]
143
+ if ei_data == _ELFDATA2LSB:
144
+ e = "<"
145
+ elif ei_data == _ELFDATA2MSB:
146
+ e = ">"
147
+ else:
148
+ raise ELFError(f"unknown ELF data encoding {ei_data}")
149
+ self.word = struct.Struct(e + "I")
150
+ if ei_class == _ELFCLASS64:
151
+ self.ehdr = struct.Struct(e + "HHIQQQIHHHHHH")
152
+ self.phdr = struct.Struct(e + "IIQQQQQQ")
153
+ self.dyn = struct.Struct(e + "qQ")
154
+ # positions of (p_type, p_offset, p_vaddr, p_filesz) in a phdr
155
+ self.ph_fields = (0, 2, 3, 5)
156
+ self.sh_info_offset = 44
157
+ elif ei_class == _ELFCLASS32:
158
+ self.ehdr = struct.Struct(e + "HHIIIIIHHHHHH")
159
+ self.phdr = struct.Struct(e + "IIIIIIII")
160
+ self.dyn = struct.Struct(e + "iI")
161
+ self.ph_fields = (0, 1, 2, 4)
162
+ self.sh_info_offset = 28
163
+ else:
164
+ raise ELFError(f"unknown ELF class {ei_class}")
165
+
166
+
167
+ def _read_needed(f: BinaryIO) -> list[str]:
168
+ ident = f.read(16)
169
+ if len(ident) < 16 or ident[:4] != ELF_MAGIC:
170
+ raise NotELFError("not an ELF file")
171
+ layout = _Layout(ident)
172
+ r = _Reader(f)
173
+ loads, dynamic = _segments(r, layout)
174
+ if dynamic is None:
175
+ return [] # statically linked: no DT_NEEDED
176
+ needed_offsets, strtab_vaddr, strsz = _dynamic_entries(r.read(*dynamic), layout)
177
+ if not needed_offsets:
178
+ return []
179
+ if strtab_vaddr is None:
180
+ raise ELFError("DT_NEEDED present but no DT_STRTAB")
181
+ strtab = _string_table(r, loads, strtab_vaddr, strsz)
182
+ names = []
183
+ for off in needed_offsets:
184
+ end = strtab.find(b"\0", off)
185
+ if off >= len(strtab) or end < 0:
186
+ raise ELFError(f"DT_NEEDED string offset {off} is outside DT_STRTAB")
187
+ names.append(os.fsdecode(strtab[off:end]))
188
+ return names
189
+
190
+
191
+ def _segments(
192
+ r: _Reader, layout: _Layout
193
+ ) -> tuple[list[tuple[int, int, int]], tuple[int, int] | None]:
194
+ """Return ([(vaddr, offset, filesz) for PT_LOAD], (offset, filesz) of PT_DYNAMIC)."""
195
+ (_, _, _, _, phoff, shoff, _, _, phentsize, phnum, shentsize, _, _) = layout.ehdr.unpack(
196
+ r.read(16, layout.ehdr.size)
197
+ )
198
+ if phnum == _PN_XNUM:
199
+ # The real count is in sh_info of section header 0.
200
+ if shoff == 0 or shentsize < layout.sh_info_offset + 4:
201
+ raise ELFError("e_phnum is PN_XNUM but there is no section header 0")
202
+ (phnum,) = layout.word.unpack(r.read(shoff + layout.sh_info_offset, 4))
203
+ if phnum == 0:
204
+ return [], None
205
+ if phentsize < layout.phdr.size:
206
+ raise ELFError(f"e_phentsize {phentsize} is smaller than a program header")
207
+ table = r.read(phoff, phentsize * phnum)
208
+ loads: list[tuple[int, int, int]] = []
209
+ dynamic: tuple[int, int] | None = None
210
+ t_i, off_i, va_i, sz_i = layout.ph_fields
211
+ for i in range(phnum):
212
+ fields = layout.phdr.unpack_from(table, i * phentsize)
213
+ if fields[t_i] == _PT_LOAD:
214
+ loads.append((fields[va_i], fields[off_i], fields[sz_i]))
215
+ elif fields[t_i] == _PT_DYNAMIC and dynamic is None:
216
+ dynamic = (fields[off_i], fields[sz_i])
217
+ return loads, dynamic
218
+
219
+
220
+ def _dynamic_entries(data: bytes, layout: _Layout) -> tuple[list[int], int | None, int | None]:
221
+ """Return (DT_NEEDED string offsets, DT_STRTAB address, DT_STRSZ)."""
222
+ needed: list[int] = []
223
+ strtab: int | None = None
224
+ strsz: int | None = None
225
+ for tag, val in layout.dyn.iter_unpack(data[: len(data) - len(data) % layout.dyn.size]):
226
+ if tag == _DT_NULL:
227
+ break
228
+ if tag == _DT_NEEDED:
229
+ needed.append(val)
230
+ elif tag == _DT_STRTAB:
231
+ strtab = val
232
+ elif tag == _DT_STRSZ:
233
+ strsz = val
234
+ return needed, strtab, strsz
235
+
236
+
237
+ def _string_table(
238
+ r: _Reader, loads: list[tuple[int, int, int]], vaddr: int, size: int | None
239
+ ) -> bytes:
240
+ for seg_vaddr, seg_offset, seg_filesz in loads:
241
+ if seg_vaddr <= vaddr < seg_vaddr + seg_filesz:
242
+ offset = vaddr - seg_vaddr + seg_offset
243
+ if size is None:
244
+ size = seg_offset + seg_filesz - offset
245
+ return r.read(offset, min(size, max(r.size - offset, 0)))
246
+ raise ELFError(f"DT_STRTAB address {vaddr:#x} is not in any PT_LOAD segment")
@@ -0,0 +1,214 @@
1
+ """The audit hook that inspects extension modules before they are loaded."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import re
7
+ import sys
8
+ import threading
9
+ import warnings
10
+ from typing import Any
11
+
12
+ from . import _elf
13
+ from ._elf import libpython_dependencies
14
+
15
+ #: Audit event used to ask "is a libpython-linkcheck hook installed here?".
16
+ #: Every hook appends ``(kind, category)`` to the list passed as the event's
17
+ #: only argument: kind is "python" (this module's hook), "native" (the C hook
18
+ #: installed by enable()) or "embedded" (the runtime-wide C hook registered by
19
+ #: an embedding application with LibpythonLinkcheck_Install()). This lets
20
+ #: enable() detect any of them, and lets every hook in an interpreter share
21
+ #: one warning category, without relying on module globals.
22
+ PROBE_EVENT = "libpython_linkcheck.probe"
23
+
24
+ _enable_lock = threading.Lock()
25
+
26
+
27
+ def _probe() -> list[tuple[str, Any]]:
28
+ found: list[Any] = []
29
+ sys.audit(PROBE_EVENT, found)
30
+ # 0.1.0a1 answered with a bare "python" string.
31
+ return [e if isinstance(e, tuple) and len(e) == 2 else (str(e), None) for e in found]
32
+
33
+
34
+ def _adopted_category() -> type[RuntimeWarning] | None:
35
+ for _, category in _probe():
36
+ if isinstance(category, type) and issubclass(category, RuntimeWarning):
37
+ return category
38
+ return None
39
+
40
+
41
+ class _PyLibpythonLinkWarning(RuntimeWarning):
42
+ """An extension module about to be loaded has a DT_NEEDED entry for libpython.
43
+
44
+ Instances raised by the hook carry three attributes:
45
+
46
+ ``module``
47
+ The fully qualified name of the extension module being imported.
48
+ ``filename``
49
+ The path of the shared object CPython is about to load.
50
+ ``needed``
51
+ A tuple of the object's own DT_NEEDED entries that name libpython.
52
+ """
53
+
54
+ module: str | None = None
55
+ filename: str | None = None
56
+ needed: tuple[str, ...] = ()
57
+
58
+
59
+ # Report the public location in tracebacks and warning filters.
60
+ _PyLibpythonLinkWarning.__module__ = "libpython_linkcheck"
61
+ _PyLibpythonLinkWarning.__qualname__ = _PyLibpythonLinkWarning.__name__ = "LibpythonLinkWarning"
62
+
63
+ # One class per interpreter, shared by every hook: the C implementation keeps
64
+ # it in the interpreter's state dict; without it, adopt the class of a hook
65
+ # that is already installed (e.g. by an embedding application).
66
+ LibpythonLinkWarning: type[RuntimeWarning]
67
+ if _elf._native is not None:
68
+ LibpythonLinkWarning = _elf._native.category()
69
+ else:
70
+ LibpythonLinkWarning = _adopted_category() or _PyLibpythonLinkWarning
71
+
72
+
73
+ def _make_warning(module: str, filename: str, needed: list[str]) -> RuntimeWarning:
74
+ warning: Any = LibpythonLinkWarning(
75
+ f"extension module {module!r} links directly to libpython: "
76
+ f"{filename} has DT_NEEDED {', '.join(needed)}"
77
+ )
78
+ warning.module = module
79
+ warning.filename = filename
80
+ warning.needed = tuple(needed)
81
+ return warning
82
+
83
+
84
+ def _check_extension(module: Any, filename: Any) -> None:
85
+ try:
86
+ filename = os.fsdecode(filename)
87
+ needed = libpython_dependencies(filename)
88
+ except Exception:
89
+ # Not ELF, unreadable, or malformed: never let the checker itself
90
+ # break an import. CPython's own loader reports real problems.
91
+ return
92
+ if needed:
93
+ # Raised outside the try block so that warning filters set to "error"
94
+ # (e.g. -Werror) propagate and abort the import before dlopen().
95
+ # stacklevel=3 skips this function and the hook; the warnings
96
+ # machinery then skips importlib's frames to reach the import site.
97
+ warnings.warn(_make_warning(str(module), filename, needed), stacklevel=3)
98
+
99
+
100
+ def _audit_hook(event: str, args: tuple[Any, ...]) -> None:
101
+ if event == "import":
102
+ # _imp.create_dynamic raises ("import", name, filename, None, None, None)
103
+ # immediately before it dlopen()s an extension module. The importlib
104
+ # event for every import passes filename=None and sys.path instead.
105
+ if len(args) == 5 and args[1] is not None and args[2] is None:
106
+ _check_extension(args[0], args[1])
107
+ elif event == PROBE_EVENT and args and isinstance(args[0], list):
108
+ args[0].append(("python", LibpythonLinkWarning))
109
+
110
+
111
+ # How -W / PYTHONWARNINGS entries can spell the category.
112
+ _CATEGORY_NAMES = frozenset(
113
+ {"libpython_linkcheck.LibpythonLinkWarning", "libpython_linkcheck._hook.LibpythonLinkWarning"}
114
+ )
115
+ _ACTIONS = ("default", "always", "all", "ignore", "module", "once", "error")
116
+
117
+
118
+ def _parse_warnoption(arg: str) -> tuple[str, str, str, int] | None:
119
+ """Parse a -W option naming LibpythonLinkWarning like warnings._setoption does.
120
+
121
+ Returns (action, message regex, module regex, lineno), or None if the
122
+ option does not name this category or is invalid.
123
+ """
124
+ parts = [s.strip() for s in arg.split(":")]
125
+ if len(parts) > 5:
126
+ return None
127
+ parts += [""] * (5 - len(parts))
128
+ action, message, category, module, lineno = parts
129
+ if category not in _CATEGORY_NAMES:
130
+ return None
131
+ if not action:
132
+ action = "default"
133
+ else:
134
+ action = next((a for a in _ACTIONS if a.startswith(action)), "")
135
+ if not action:
136
+ return None
137
+ if action == "all" and sys.version_info < (3, 14):
138
+ action = "always" # "all" was an alias before 3.14
139
+ try:
140
+ line = int(lineno) if lineno else 0
141
+ except ValueError:
142
+ return None
143
+ if line < 0:
144
+ return None
145
+ return (
146
+ action,
147
+ re.escape(message) if message else "",
148
+ re.escape(module) + r"\Z" if module else "",
149
+ line,
150
+ )
151
+
152
+
153
+ def _apply_dropped_warnoptions() -> None:
154
+ """Apply -W / PYTHONWARNINGS entries for our category that CPython dropped.
155
+
156
+ CPython resolves the categories named in warning options during startup,
157
+ before ``site`` puts site-packages on sys.path. For a normally installed
158
+ copy of this package that fails, and CPython prints "Invalid -W option
159
+ ignored: invalid module name: 'libpython_linkcheck'" and drops the entry.
160
+ Apply such entries now, in their original order. Entries that did take
161
+ effect (e.g. the package was on PYTHONPATH) are left alone.
162
+ """
163
+ for arg in sys.warnoptions:
164
+ parsed = _parse_warnoption(arg)
165
+ if parsed is None:
166
+ continue
167
+ action, message, module, lineno = parsed
168
+ item = (
169
+ action,
170
+ re.compile(message, re.I) if message else None,
171
+ LibpythonLinkWarning,
172
+ re.compile(module) if module else None,
173
+ lineno,
174
+ )
175
+ if item not in warnings.filters:
176
+ warnings.filterwarnings(action, message, LibpythonLinkWarning, module, lineno)
177
+
178
+
179
+ def _installed_hooks() -> list[str]:
180
+ """Kinds of the libpython-linkcheck hooks active in this interpreter."""
181
+ return [kind for kind, _ in _probe()]
182
+
183
+
184
+ def is_enabled() -> bool:
185
+ """Return True if a libpython-linkcheck audit hook is active in this interpreter."""
186
+ return bool(_installed_hooks())
187
+
188
+
189
+ def enable() -> bool:
190
+ """Install the libpython-linkcheck audit hook in the current interpreter.
191
+
192
+ Idempotent: if a hook is already active (installed by any earlier call,
193
+ including one made through a different copy of this module), nothing is
194
+ installed and False is returned. Returns True if this call installed it.
195
+
196
+ When it installs the hook, it also applies any ``-W`` or ``PYTHONWARNINGS``
197
+ entries naming ``libpython_linkcheck.LibpythonLinkWarning`` that CPython
198
+ could not resolve at startup (see the README).
199
+
200
+ Audit hooks cannot be removed, so the hook stays active for the lifetime
201
+ of the interpreter. Raises RuntimeError if another audit hook prevented
202
+ the installation.
203
+ """
204
+ with _enable_lock:
205
+ if is_enabled():
206
+ return False
207
+ native = _elf._native
208
+ sys.addaudithook(_audit_hook if native is None else native.audit_hook)
209
+ if not is_enabled():
210
+ # sys.addaudithook() silently skips the hook if an existing hook
211
+ # raised a RuntimeError subclass for the "sys.addaudithook" event.
212
+ raise RuntimeError("another audit hook prevented libpython-linkcheck from installing")
213
+ _apply_dropped_warnoptions()
214
+ return True
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0a2"
File without changes
@@ -0,0 +1,267 @@
1
+ Metadata-Version: 2.4
2
+ Name: libpython-linkcheck
3
+ Version: 0.1.0a2
4
+ Summary: Warn when an imported Python extension directly links to libpython on Linux.
5
+ Author: Itamar Oren
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/itamaro/libpython-linkcheck
8
+ Project-URL: Source, https://github.com/itamaro/libpython-linkcheck
9
+ Project-URL: Issues, https://github.com/itamaro/libpython-linkcheck/issues
10
+ Keywords: libpython,elf,dt_needed,extension,audit-hook,linker
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: POSIX :: Linux
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3 :: Only
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Programming Language :: Python :: 3.15
21
+ Classifier: Programming Language :: Python :: Implementation :: CPython
22
+ Classifier: Topic :: Software Development :: Build Tools
23
+ Classifier: Topic :: Software Development :: Quality Assurance
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.11
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Dynamic: license-file
29
+
30
+ # libpython-linkcheck
31
+
32
+ Warn when an imported Python extension directly links to libpython on Linux.
33
+
34
+ On Linux, CPython extension modules should **not** link against libpython:
35
+ the interpreter that loads them already provides every `Py*` symbol. An
36
+ extension with `libpython3.X.so.1.0` in its `DT_NEEDED` entries requires that
37
+ exact library at load time. Under an interpreter without a shared libpython (a
38
+ statically linked build, such as Debian's and Ubuntu's `/usr/bin/python3`), it
39
+ either fails with "cannot open shared object file" or loads a second, separate
40
+ copy of the runtime. The manylinux policies forbid such links for these reasons.
41
+
42
+ `libpython-linkcheck` installs a CPython audit hook that inspects each
43
+ extension module's shared object right **before** CPython loads it, and issues
44
+ a `LibpythonLinkWarning` if the object's own `DT_NEEDED` entries name libpython.
45
+
46
+ ## Install
47
+
48
+ ```sh
49
+ pip install libpython-linkcheck
50
+ ```
51
+
52
+ The import name is `libpython_linkcheck`. It has no dependencies. On Linux
53
+ (x86_64 and aarch64, glibc) pip installs a native wheel that runs the check
54
+ in C; elsewhere it installs a pure-Python wheel with the same behavior.
55
+
56
+ ## Usage
57
+
58
+ ```python
59
+ import libpython_linkcheck
60
+
61
+ libpython_linkcheck.enable() # once, as early as possible
62
+
63
+ import some_extension # warns if some_extension's .so has DT_NEEDED libpython
64
+ ```
65
+
66
+ The warning names the module, the file, and the matching dependency, and
67
+ points at the line that did the import:
68
+
69
+ ```text
70
+ app.py:5: LibpythonLinkWarning: extension module 'some_extension' links directly to libpython: /venv/lib/python3.15/site-packages/some_extension.cpython-315-x86_64-linux-gnu.so has DT_NEEDED libpython3.15.so.1.0
71
+ ```
72
+
73
+ `enable()` returns `True` if it installed the hook and `False` if a hook was
74
+ already active in this interpreter. Calling it again is harmless and never
75
+ produces duplicate warnings. `is_enabled()` reports whether the hook is
76
+ active. Audit hooks cannot be removed, so the hook stays active until the
77
+ interpreter exits.
78
+
79
+ ### Controlling the warning
80
+
81
+ `LibpythonLinkWarning` is a `RuntimeWarning` subclass, and all the normal
82
+ warning filters apply. With an `error` filter, the import raises
83
+ `LibpythonLinkWarning` before the shared object is loaded, so none of its code
84
+ (not even ELF constructors) runs.
85
+
86
+ In code (after `import libpython_linkcheck`):
87
+
88
+ ```python
89
+ import warnings
90
+ from libpython_linkcheck import LibpythonLinkWarning
91
+
92
+ # Fail such imports instead of warning:
93
+ warnings.filterwarnings("error", category=LibpythonLinkWarning)
94
+ # Silence one known extension (the message starts with "extension module '<name>'"):
95
+ warnings.filterwarnings(
96
+ "ignore", message=r"extension module 'legacy\._speedups'", category=LibpythonLinkWarning
97
+ )
98
+ ```
99
+
100
+ The warning instance carries `.module` (the fully qualified module name),
101
+ `.filename` (the shared object), and `.needed` (a tuple of the matching
102
+ `DT_NEEDED` entries).
103
+
104
+ On the command line:
105
+
106
+ | Option | Effect |
107
+ | --- | --- |
108
+ | `python -Werror ...` | every warning, including this one, is an error |
109
+ | `python -W error::RuntimeWarning ...` | runtime warnings, including this one, are errors |
110
+ | `python -W "error:extension module 'foo._ext'" ...` | this warning is an error for one module (message prefix, matched literally) |
111
+ | `python -W error::libpython_linkcheck.LibpythonLinkWarning ...` | this warning is an error (see the note below) |
112
+ | `PYTHONWARNINGS=...` | same as `-W` |
113
+
114
+ **Note:** CPython resolves the category of a `-W` or `PYTHONWARNINGS` entry
115
+ during startup, before site-packages is on `sys.path`. For a normally
116
+ installed package that fails, and CPython itself prints
117
+ `Invalid -W option ignored: invalid module name: 'libpython_linkcheck'`.
118
+ When `enable()` installs the hook, it applies those dropped entries, in their
119
+ original order, so the filter does take effect for every import after
120
+ `enable()`. These entries then take precedence over filters added before
121
+ `enable()` was called. The other forms in the table work without this step.
122
+
123
+ ### Checking files without importing them
124
+
125
+ ```sh
126
+ python -m libpython_linkcheck build/lib*/mypkg/*.so
127
+ ```
128
+
129
+ prints `FILE: DT_NEEDED libpython...` for each offending file. The exit status
130
+ is 0 if no file links libpython, 1 if any does, and 2 if a file could not be
131
+ read or is not ELF. From Python, `libpython_linkcheck.libpython_dependencies(path)`
132
+ returns the matching entries as a list.
133
+
134
+ ## Activating it automatically
135
+
136
+ Installing `libpython-linkcheck` never activates anything by itself. To check
137
+ every program run by an interpreter, choose one of these explicitly:
138
+
139
+ | Activation | How | Covers | Not under |
140
+ | --- | --- | --- | --- |
141
+ | User code | `libpython_linkcheck.enable()` | the calling interpreter, from that call on | — |
142
+ | `sitecustomize` | `sitecustomize.py` in site-packages calling `enable()` | interpreters using that site-packages (a venv's own, or the base installation's) | `-S` |
143
+ | `usercustomize` | `usercustomize.py` in the user site directory calling `enable()` | that user's interpreters outside venvs | `-s`, `-I`, `-S`, venvs |
144
+ | `.pth` + `.start` | install `libpython-linkcheck-autoenable` (see `autoenable/`), or copy its two files into site-packages | interpreters using that site-packages, before `sitecustomize` and user code | `-S` |
145
+ | Executable | `LibpythonLinkcheck_Install()` in the program's `main()` (next section) | the whole runtime, including subinterpreters | — (works under `-I -S`) |
146
+
147
+ Every path can be combined with the others: whichever runs first installs the
148
+ hook, and later `enable()` calls detect it and return `False`, so each
149
+ extension module is reported once. A hook installed by `enable()` (any of the
150
+ first four rows) covers only the interpreter that installed it; a
151
+ subinterpreter needs its own `enable()` call. Extension modules imported
152
+ before activation, including by `.pth` files processed earlier, are not
153
+ inspected.
154
+
155
+ `libpython-linkcheck-autoenable` is not on PyPI; build it from the
156
+ repository with `python -m build --wheel autoenable`. On Python 3.11–3.14 its
157
+ `.pth` file's import line calls `enable()`; on 3.15 and later the matching
158
+ [PEP 829](https://peps.python.org/pep-0829/) `.start` file
159
+ (`libpython_linkcheck:enable`) does, and disables that import line.
160
+
161
+ ## Embedding: a hook installed before Python starts
162
+
163
+ Applications that embed CPython, and CPython distributions that build their
164
+ own `python` executable, can register the check in C before the runtime
165
+ starts. No Python package is imported, it covers every interpreter
166
+ (including subinterpreters), and it works under `-I -S`:
167
+
168
+ ```c
169
+ #include "libpython_linkcheck.h"
170
+
171
+ int main(int argc, char **argv) {
172
+ if (LibpythonLinkcheck_Install() < 0) {
173
+ return 1;
174
+ }
175
+ return Py_BytesMain(argc, argv);
176
+ }
177
+ ```
178
+
179
+ Compile `src/csrc/linkcheck_hook.c` and `src/csrc/linkcheck_elf.c` (from the
180
+ sdist or the repository) into the program, with `src/csrc` on the include
181
+ path; the program links libpython as embedders do. The warning category is
182
+ the same `libpython_linkcheck.LibpythonLinkWarning` that the package
183
+ exposes, and a later `libpython_linkcheck.enable()` call detects the hook
184
+ and returns `False` instead of installing a second one. See
185
+ `src/csrc/libpython_linkcheck.h` for the exact contract.
186
+
187
+ ## Try it
188
+
189
+ This builds an extension that links libpython and imports it with the hook
190
+ enabled. Copy and paste into a shell in a `python:3.15` container
191
+ (`podman run --rm -it docker.io/library/python:3.15-trixie bash`, or use `docker`):
192
+
193
+ <!-- readme-demo: the block below is executed by tests/test_readme.py -->
194
+ ```sh
195
+ pip install libpython-linkcheck
196
+ mkdir demo && cd demo
197
+ cat > linked.c <<'EOF'
198
+ #include <Python.h>
199
+ static struct PyModuleDef def = {PyModuleDef_HEAD_INIT, "linked", NULL, 0, NULL};
200
+ PyMODINIT_FUNC PyInit_linked(void) { return PyModuleDef_Init(&def); }
201
+ EOF
202
+ cc -shared -fPIC $(python3-config --includes) linked.c \
203
+ -o linked$(python3-config --extension-suffix) \
204
+ -Wl,--no-as-needed $(python3-config --ldflags --embed)
205
+
206
+ # 1. Warns, then loads the module:
207
+ python -c 'import libpython_linkcheck; libpython_linkcheck.enable(); import linked; print("loaded", linked)'
208
+ # 2. With -Werror, the import fails before the shared object is loaded:
209
+ python -Werror -c 'import libpython_linkcheck; libpython_linkcheck.enable(); import linked' || echo "exit status $?"
210
+ # 3. Check the file without importing it:
211
+ python -m libpython_linkcheck linked*.so || echo "exit status $?"
212
+ ```
213
+
214
+ Expected output (paths and versions vary):
215
+
216
+ ```text
217
+ <string>:1: LibpythonLinkWarning: extension module 'linked' links directly to libpython: /demo/linked.cpython-315-x86_64-linux-gnu.so has DT_NEEDED libpython3.15.so.1.0
218
+ loaded <module 'linked' from '/demo/linked.cpython-315-x86_64-linux-gnu.so'>
219
+ Traceback (most recent call last):
220
+ ...
221
+ libpython_linkcheck.LibpythonLinkWarning: extension module 'linked' links directly to libpython: /demo/linked.cpython-315-x86_64-linux-gnu.so has DT_NEEDED libpython3.15.so.1.0
222
+ exit status 1
223
+ linked.cpython-315-x86_64-linux-gnu.so: DT_NEEDED libpython3.15.so.1.0
224
+ exit status 1
225
+ ```
226
+
227
+ ## What is supported
228
+
229
+ Supported and tested:
230
+
231
+ - CPython 3.11 through 3.15 on Linux (x86_64 and aarch64, glibc), and the
232
+ free-threaded builds of 3.14 and 3.15. Importing and enabling the
233
+ package keeps the GIL disabled.
234
+ - Distributions: native `manylinux_2_28` wheels (x86_64, aarch64) for
235
+ CPython 3.11–3.15 and free-threaded 3.14t/3.15t, which use the C
236
+ implementation; a pure-Python `py3-none-any` wheel for every other
237
+ platform; and an sdist, which compiles the C implementation on POSIX.
238
+ - Every activation path in [the table above](#activating-it-automatically),
239
+ each tested in a fresh venv against a real linked extension, alone and
240
+ combined.
241
+ - Explicit activation: call `libpython_linkcheck.enable()` in each interpreter
242
+ that should be checked; the hook it installs covers only that interpreter,
243
+ including when it is a subinterpreter. Only extension modules imported
244
+ **after** that call are inspected.
245
+ - Every extension module loaded through the import system
246
+ (`importlib.machinery.ExtensionFileLoader`, which also serves
247
+ `importlib.util.spec_from_file_location` and similar).
248
+ - Only the extension's **own** `DT_NEEDED` entries; dependencies of its
249
+ dependencies are not inspected.
250
+ - libpython names recognized: versioned (`libpython3.12.so.1.0`,
251
+ `libpython3.12.so`), Stable ABI (`libpython3.so`), free-threaded
252
+ (`libpython3.13t.so.1.0`), and ABI-flagged (`libpython3.12d.so.1.0`)
253
+ names, including entries that carry a directory.
254
+ - Standard warning filters, `-Werror`, and `catch_warnings()`.
255
+ - Objects that cannot be read or parsed are skipped silently, so that
256
+ CPython's own loader reports the real error.
257
+
258
+ Not covered yet:
259
+
260
+ - Shared libraries opened with `ctypes` or `dlopen()` directly, and extension
261
+ modules that were already loaded before `enable()`.
262
+ - Other platforms: `enable()` works, but non-ELF files (Mach-O, PE) are never
263
+ reported. Other ELF platforms are untested.
264
+
265
+ ## License
266
+
267
+ MIT
@@ -0,0 +1,12 @@
1
+ libpython_linkcheck/__init__.py,sha256=oQduYDgMqyw1VM_3LvOxeMx0J7LZN4F4awrQhQwsKFQ,746
2
+ libpython_linkcheck/__main__.py,sha256=PJp3ryjaSJevsvwJsA-4h3gv_2yTuRREnpg2OK-_wJQ,1300
3
+ libpython_linkcheck/_elf.py,sha256=FIJSqbddY-PMiu4uLnsJhcNJJs9V9Ev7QGE42Fgxpxw,8842
4
+ libpython_linkcheck/_hook.py,sha256=0W2OpQYjrYle13KAV3kV8Q9ZcQqTCvgjwxgJ6eD7EBU,8224
5
+ libpython_linkcheck/_native.cpython-314t-aarch64-linux-gnu.so,sha256=aXXh90Z9CjV_-3b31uv2VHLdNSzf2VCXHddOIWJy1vw,207472
6
+ libpython_linkcheck/_version.py,sha256=MVC3xG9U8KXXTiZ2FMZ9ARASAJV2A-Qu7UcliE-N_hI,24
7
+ libpython_linkcheck/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ libpython_linkcheck-0.1.0a2.dist-info/METADATA,sha256=5QqK6k3Pn93SycZ8kSrEtKgWikQbpWV158ceWoJKlYk,12250
9
+ libpython_linkcheck-0.1.0a2.dist-info/WHEEL,sha256=Om130trSqCzQ8cy3WeqU3UZtEYJUqeE3QflkBYQi8ww,115
10
+ libpython_linkcheck-0.1.0a2.dist-info/top_level.txt,sha256=IZprL5FIqWd22Moutmpxn7-QKBZCsPfEGn2XhdxG7VE,20
11
+ libpython_linkcheck-0.1.0a2.dist-info/RECORD,,
12
+ libpython_linkcheck-0.1.0a2.dist-info/licenses/LICENSE,sha256=nxJKW9vh32DdsG7aSRztYSpEowhSV1m6PRAceUBXDGw,1068
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: false
4
+ Tag: cp314-cp314t-manylinux_2_28_aarch64
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Itamar Oren
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ libpython_linkcheck