libpython-linkcheck 0.1.0a1__py3-none-any.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,207 @@
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 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 read_needed(path: str | os.PathLike[str] | os.PathLike[bytes] | bytes) -> list[str]:
64
+ """Return the DT_NEEDED entries of the ELF object at *path*, in order.
65
+
66
+ Returns an empty list for an ELF object without a dynamic segment.
67
+ Raises :class:`NotELFError` if the file is not ELF, :class:`ELFError` if it
68
+ is malformed, and :class:`OSError` if it cannot be read.
69
+ """
70
+ with open(path, "rb") as f:
71
+ return _read_needed(f)
72
+
73
+
74
+ def libpython_dependencies(
75
+ path: str | os.PathLike[str] | os.PathLike[bytes] | bytes,
76
+ ) -> list[str]:
77
+ """Return the DT_NEEDED entries of the ELF object at *path* that name libpython."""
78
+ return [name for name in read_needed(path) if is_libpython(name)]
79
+
80
+
81
+ class _Reader:
82
+ def __init__(self, f: BinaryIO) -> None:
83
+ self._f = f
84
+ self.size = os.fstat(f.fileno()).st_size
85
+
86
+ def read(self, offset: int, length: int) -> bytes:
87
+ if offset < 0 or length < 0 or offset + length > self.size:
88
+ raise ELFError(
89
+ f"read of {length} bytes at offset {offset} is outside the file "
90
+ f"({self.size} bytes); truncated or corrupt ELF"
91
+ )
92
+ self._f.seek(offset)
93
+ data = self._f.read(length)
94
+ if len(data) != length:
95
+ raise ELFError("short read; file changed while reading")
96
+ return data
97
+
98
+
99
+ class _Layout:
100
+ """Struct layouts for one ELF class/byte order."""
101
+
102
+ def __init__(self, ident: bytes) -> None:
103
+ ei_class, ei_data = ident[4], ident[5]
104
+ if ei_data == _ELFDATA2LSB:
105
+ e = "<"
106
+ elif ei_data == _ELFDATA2MSB:
107
+ e = ">"
108
+ else:
109
+ raise ELFError(f"unknown ELF data encoding {ei_data}")
110
+ self.word = struct.Struct(e + "I")
111
+ if ei_class == _ELFCLASS64:
112
+ self.ehdr = struct.Struct(e + "HHIQQQIHHHHHH")
113
+ self.phdr = struct.Struct(e + "IIQQQQQQ")
114
+ self.dyn = struct.Struct(e + "qQ")
115
+ # positions of (p_type, p_offset, p_vaddr, p_filesz) in a phdr
116
+ self.ph_fields = (0, 2, 3, 5)
117
+ self.sh_info_offset = 44
118
+ elif ei_class == _ELFCLASS32:
119
+ self.ehdr = struct.Struct(e + "HHIIIIIHHHHHH")
120
+ self.phdr = struct.Struct(e + "IIIIIIII")
121
+ self.dyn = struct.Struct(e + "iI")
122
+ self.ph_fields = (0, 1, 2, 4)
123
+ self.sh_info_offset = 28
124
+ else:
125
+ raise ELFError(f"unknown ELF class {ei_class}")
126
+
127
+
128
+ def _read_needed(f: BinaryIO) -> list[str]:
129
+ ident = f.read(16)
130
+ if len(ident) < 16 or ident[:4] != ELF_MAGIC:
131
+ raise NotELFError("not an ELF file")
132
+ layout = _Layout(ident)
133
+ r = _Reader(f)
134
+ loads, dynamic = _segments(r, layout)
135
+ if dynamic is None:
136
+ return [] # statically linked: no DT_NEEDED
137
+ needed_offsets, strtab_vaddr, strsz = _dynamic_entries(r.read(*dynamic), layout)
138
+ if not needed_offsets:
139
+ return []
140
+ if strtab_vaddr is None:
141
+ raise ELFError("DT_NEEDED present but no DT_STRTAB")
142
+ strtab = _string_table(r, loads, strtab_vaddr, strsz)
143
+ names = []
144
+ for off in needed_offsets:
145
+ end = strtab.find(b"\0", off)
146
+ if off >= len(strtab) or end < 0:
147
+ raise ELFError(f"DT_NEEDED string offset {off} is outside DT_STRTAB")
148
+ names.append(os.fsdecode(strtab[off:end]))
149
+ return names
150
+
151
+
152
+ def _segments(
153
+ r: _Reader, layout: _Layout
154
+ ) -> tuple[list[tuple[int, int, int]], tuple[int, int] | None]:
155
+ """Return ([(vaddr, offset, filesz) for PT_LOAD], (offset, filesz) of PT_DYNAMIC)."""
156
+ (_, _, _, _, phoff, shoff, _, _, phentsize, phnum, shentsize, _, _) = layout.ehdr.unpack(
157
+ r.read(16, layout.ehdr.size)
158
+ )
159
+ if phnum == _PN_XNUM:
160
+ # The real count is in sh_info of section header 0.
161
+ if shoff == 0 or shentsize < layout.sh_info_offset + 4:
162
+ raise ELFError("e_phnum is PN_XNUM but there is no section header 0")
163
+ (phnum,) = layout.word.unpack(r.read(shoff + layout.sh_info_offset, 4))
164
+ if phnum == 0:
165
+ return [], None
166
+ if phentsize < layout.phdr.size:
167
+ raise ELFError(f"e_phentsize {phentsize} is smaller than a program header")
168
+ table = r.read(phoff, phentsize * phnum)
169
+ loads: list[tuple[int, int, int]] = []
170
+ dynamic: tuple[int, int] | None = None
171
+ t_i, off_i, va_i, sz_i = layout.ph_fields
172
+ for i in range(phnum):
173
+ fields = layout.phdr.unpack_from(table, i * phentsize)
174
+ if fields[t_i] == _PT_LOAD:
175
+ loads.append((fields[va_i], fields[off_i], fields[sz_i]))
176
+ elif fields[t_i] == _PT_DYNAMIC and dynamic is None:
177
+ dynamic = (fields[off_i], fields[sz_i])
178
+ return loads, dynamic
179
+
180
+
181
+ def _dynamic_entries(data: bytes, layout: _Layout) -> tuple[list[int], int | None, int | None]:
182
+ """Return (DT_NEEDED string offsets, DT_STRTAB address, DT_STRSZ)."""
183
+ needed: list[int] = []
184
+ strtab: int | None = None
185
+ strsz: int | None = None
186
+ for tag, val in layout.dyn.iter_unpack(data[: len(data) - len(data) % layout.dyn.size]):
187
+ if tag == _DT_NULL:
188
+ break
189
+ if tag == _DT_NEEDED:
190
+ needed.append(val)
191
+ elif tag == _DT_STRTAB:
192
+ strtab = val
193
+ elif tag == _DT_STRSZ:
194
+ strsz = val
195
+ return needed, strtab, strsz
196
+
197
+
198
+ def _string_table(
199
+ r: _Reader, loads: list[tuple[int, int, int]], vaddr: int, size: int | None
200
+ ) -> bytes:
201
+ for seg_vaddr, seg_offset, seg_filesz in loads:
202
+ if seg_vaddr <= vaddr < seg_vaddr + seg_filesz:
203
+ offset = vaddr - seg_vaddr + seg_offset
204
+ if size is None:
205
+ size = seg_offset + seg_filesz - offset
206
+ return r.read(offset, min(size, max(r.size - offset, 0)))
207
+ raise ELFError(f"DT_STRTAB address {vaddr:#x} is not in any PT_LOAD segment")
@@ -0,0 +1,187 @@
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 ._elf import libpython_dependencies
13
+
14
+ #: Audit event used to ask "is a libpython-linkcheck hook installed here?".
15
+ #: Every hook implementation appends an identifier to the list passed as the
16
+ #: event's only argument. This lets ``enable()`` detect a hook installed by
17
+ #: another copy of this module (or, in future, by native code) without relying
18
+ #: on module globals.
19
+ PROBE_EVENT = "libpython_linkcheck.probe"
20
+
21
+ _enable_lock = threading.Lock()
22
+
23
+
24
+ class LibpythonLinkWarning(RuntimeWarning):
25
+ """An extension module about to be loaded has a DT_NEEDED entry for libpython.
26
+
27
+ Instances raised by the hook carry three attributes:
28
+
29
+ ``module``
30
+ The fully qualified name of the extension module being imported.
31
+ ``filename``
32
+ The path of the shared object CPython is about to load.
33
+ ``needed``
34
+ A tuple of the object's own DT_NEEDED entries that name libpython.
35
+ """
36
+
37
+ module: str | None = None
38
+ filename: str | None = None
39
+ needed: tuple[str, ...] = ()
40
+
41
+
42
+ # Report the public location in tracebacks and warning filters.
43
+ LibpythonLinkWarning.__module__ = "libpython_linkcheck"
44
+
45
+
46
+ def _make_warning(module: str, filename: str, needed: list[str]) -> LibpythonLinkWarning:
47
+ warning = LibpythonLinkWarning(
48
+ f"extension module {module!r} links directly to libpython: "
49
+ f"{filename} has DT_NEEDED {', '.join(needed)}"
50
+ )
51
+ warning.module = module
52
+ warning.filename = filename
53
+ warning.needed = tuple(needed)
54
+ return warning
55
+
56
+
57
+ def _check_extension(module: Any, filename: Any) -> None:
58
+ try:
59
+ filename = os.fsdecode(filename)
60
+ needed = libpython_dependencies(filename)
61
+ except Exception:
62
+ # Not ELF, unreadable, or malformed: never let the checker itself
63
+ # break an import. CPython's own loader reports real problems.
64
+ return
65
+ if needed:
66
+ # Raised outside the try block so that warning filters set to "error"
67
+ # (e.g. -Werror) propagate and abort the import before dlopen().
68
+ # stacklevel=3 skips this function and the hook; the warnings
69
+ # machinery then skips importlib's frames to reach the import site.
70
+ warnings.warn(_make_warning(str(module), filename, needed), stacklevel=3)
71
+
72
+
73
+ def _audit_hook(event: str, args: tuple[Any, ...]) -> None:
74
+ if event == "import":
75
+ # _imp.create_dynamic raises ("import", name, filename, None, None, None)
76
+ # immediately before it dlopen()s an extension module. The importlib
77
+ # event for every import passes filename=None and sys.path instead.
78
+ if len(args) == 5 and args[1] is not None and args[2] is None:
79
+ _check_extension(args[0], args[1])
80
+ elif event == PROBE_EVENT and args and isinstance(args[0], list):
81
+ args[0].append("python")
82
+
83
+
84
+ # How -W / PYTHONWARNINGS entries can spell the category.
85
+ _CATEGORY_NAMES = frozenset(
86
+ {"libpython_linkcheck.LibpythonLinkWarning", "libpython_linkcheck._hook.LibpythonLinkWarning"}
87
+ )
88
+ _ACTIONS = ("default", "always", "all", "ignore", "module", "once", "error")
89
+
90
+
91
+ def _parse_warnoption(arg: str) -> tuple[str, str, str, int] | None:
92
+ """Parse a -W option naming LibpythonLinkWarning like warnings._setoption does.
93
+
94
+ Returns (action, message regex, module regex, lineno), or None if the
95
+ option does not name this category or is invalid.
96
+ """
97
+ parts = [s.strip() for s in arg.split(":")]
98
+ if len(parts) > 5:
99
+ return None
100
+ parts += [""] * (5 - len(parts))
101
+ action, message, category, module, lineno = parts
102
+ if category not in _CATEGORY_NAMES:
103
+ return None
104
+ if not action:
105
+ action = "default"
106
+ else:
107
+ action = next((a for a in _ACTIONS if a.startswith(action)), "")
108
+ if not action:
109
+ return None
110
+ if action == "all" and sys.version_info < (3, 14):
111
+ action = "always" # "all" was an alias before 3.14
112
+ try:
113
+ line = int(lineno) if lineno else 0
114
+ except ValueError:
115
+ return None
116
+ if line < 0:
117
+ return None
118
+ return (
119
+ action,
120
+ re.escape(message) if message else "",
121
+ re.escape(module) + r"\Z" if module else "",
122
+ line,
123
+ )
124
+
125
+
126
+ def _apply_dropped_warnoptions() -> None:
127
+ """Apply -W / PYTHONWARNINGS entries for our category that CPython dropped.
128
+
129
+ CPython resolves the categories named in warning options during startup,
130
+ before ``site`` puts site-packages on sys.path. For a normally installed
131
+ copy of this package that fails, and CPython prints "Invalid -W option
132
+ ignored: invalid module name: 'libpython_linkcheck'" and drops the entry.
133
+ Apply such entries now, in their original order. Entries that did take
134
+ effect (e.g. the package was on PYTHONPATH) are left alone.
135
+ """
136
+ for arg in sys.warnoptions:
137
+ parsed = _parse_warnoption(arg)
138
+ if parsed is None:
139
+ continue
140
+ action, message, module, lineno = parsed
141
+ item = (
142
+ action,
143
+ re.compile(message, re.I) if message else None,
144
+ LibpythonLinkWarning,
145
+ re.compile(module) if module else None,
146
+ lineno,
147
+ )
148
+ if item not in warnings.filters:
149
+ warnings.filterwarnings(action, message, LibpythonLinkWarning, module, lineno)
150
+
151
+
152
+ def _installed_hooks() -> list[str]:
153
+ found: list[str] = []
154
+ sys.audit(PROBE_EVENT, found)
155
+ return found
156
+
157
+
158
+ def is_enabled() -> bool:
159
+ """Return True if a libpython-linkcheck audit hook is active in this interpreter."""
160
+ return bool(_installed_hooks())
161
+
162
+
163
+ def enable() -> bool:
164
+ """Install the libpython-linkcheck audit hook in the current interpreter.
165
+
166
+ Idempotent: if a hook is already active (installed by any earlier call,
167
+ including one made through a different copy of this module), nothing is
168
+ installed and False is returned. Returns True if this call installed it.
169
+
170
+ When it installs the hook, it also applies any ``-W`` or ``PYTHONWARNINGS``
171
+ entries naming ``libpython_linkcheck.LibpythonLinkWarning`` that CPython
172
+ could not resolve at startup (see the README).
173
+
174
+ Audit hooks cannot be removed, so the hook stays active for the lifetime
175
+ of the interpreter. Raises RuntimeError if another audit hook prevented
176
+ the installation.
177
+ """
178
+ with _enable_lock:
179
+ if is_enabled():
180
+ return False
181
+ sys.addaudithook(_audit_hook)
182
+ if not is_enabled():
183
+ # sys.addaudithook() silently skips the hook if an existing hook
184
+ # raised a RuntimeError subclass for the "sys.addaudithook" event.
185
+ raise RuntimeError("another audit hook prevented libpython-linkcheck from installing")
186
+ _apply_dropped_warnoptions()
187
+ return True
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0a1"
File without changes
@@ -0,0 +1,208 @@
1
+ Metadata-Version: 2.4
2
+ Name: libpython-linkcheck
3
+ Version: 0.1.0a1
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 is pure Python, with no dependencies.
53
+
54
+ ## Usage
55
+
56
+ ```python
57
+ import libpython_linkcheck
58
+
59
+ libpython_linkcheck.enable() # once, as early as possible
60
+
61
+ import some_extension # warns if some_extension's .so has DT_NEEDED libpython
62
+ ```
63
+
64
+ The warning names the module, the file, and the matching dependency, and
65
+ points at the line that did the import:
66
+
67
+ ```text
68
+ 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
69
+ ```
70
+
71
+ `enable()` returns `True` if it installed the hook and `False` if a hook was
72
+ already active in this interpreter. Calling it again is harmless and never
73
+ produces duplicate warnings. `is_enabled()` reports whether the hook is
74
+ active. Audit hooks cannot be removed, so the hook stays active until the
75
+ interpreter exits.
76
+
77
+ ### Controlling the warning
78
+
79
+ `LibpythonLinkWarning` is a `RuntimeWarning` subclass, and all the normal
80
+ warning filters apply. With an `error` filter, the import raises
81
+ `LibpythonLinkWarning` before the shared object is loaded, so none of its code
82
+ (not even ELF constructors) runs.
83
+
84
+ In code (after `import libpython_linkcheck`):
85
+
86
+ ```python
87
+ import warnings
88
+ from libpython_linkcheck import LibpythonLinkWarning
89
+
90
+ # Fail such imports instead of warning:
91
+ warnings.filterwarnings("error", category=LibpythonLinkWarning)
92
+ # Silence one known extension (the message starts with "extension module '<name>'"):
93
+ warnings.filterwarnings(
94
+ "ignore", message=r"extension module 'legacy\._speedups'", category=LibpythonLinkWarning
95
+ )
96
+ ```
97
+
98
+ The warning instance carries `.module` (the fully qualified module name),
99
+ `.filename` (the shared object), and `.needed` (a tuple of the matching
100
+ `DT_NEEDED` entries).
101
+
102
+ On the command line:
103
+
104
+ | Option | Effect |
105
+ | --- | --- |
106
+ | `python -Werror ...` | every warning, including this one, is an error |
107
+ | `python -W error::RuntimeWarning ...` | runtime warnings, including this one, are errors |
108
+ | `python -W "error:extension module 'foo._ext'" ...` | this warning is an error for one module (message prefix, matched literally) |
109
+ | `python -W error::libpython_linkcheck.LibpythonLinkWarning ...` | this warning is an error (see the note below) |
110
+ | `PYTHONWARNINGS=...` | same as `-W` |
111
+
112
+ **Note:** CPython resolves the category of a `-W` or `PYTHONWARNINGS` entry
113
+ during startup, before site-packages is on `sys.path`. For a normally
114
+ installed package that fails, and CPython itself prints
115
+ `Invalid -W option ignored: invalid module name: 'libpython_linkcheck'`.
116
+ When `enable()` installs the hook, it applies those dropped entries, in their
117
+ original order, so the filter does take effect for every import after
118
+ `enable()`. These entries then take precedence over filters added before
119
+ `enable()` was called. The other forms in the table work without this step.
120
+
121
+ ### Checking files without importing them
122
+
123
+ ```sh
124
+ python -m libpython_linkcheck build/lib*/mypkg/*.so
125
+ ```
126
+
127
+ prints `FILE: DT_NEEDED libpython...` for each offending file. The exit status
128
+ is 0 if no file links libpython, 1 if any does, and 2 if a file could not be
129
+ read or is not ELF. From Python, `libpython_linkcheck.libpython_dependencies(path)`
130
+ returns the matching entries as a list.
131
+
132
+ ## Try it
133
+
134
+ This builds an extension that links libpython and imports it with the hook
135
+ enabled. Copy and paste into a shell in a `python:3.15` container
136
+ (`podman run --rm -it docker.io/library/python:3.15-trixie bash`, or use `docker`):
137
+
138
+ <!-- readme-demo: the block below is executed by tests/test_readme.py -->
139
+ ```sh
140
+ pip install libpython-linkcheck
141
+ mkdir demo && cd demo
142
+ cat > linked.c <<'EOF'
143
+ #include <Python.h>
144
+ static struct PyModuleDef def = {PyModuleDef_HEAD_INIT, "linked", NULL, 0, NULL};
145
+ PyMODINIT_FUNC PyInit_linked(void) { return PyModuleDef_Init(&def); }
146
+ EOF
147
+ cc -shared -fPIC $(python3-config --includes) linked.c \
148
+ -o linked$(python3-config --extension-suffix) \
149
+ -Wl,--no-as-needed $(python3-config --ldflags --embed)
150
+
151
+ # 1. Warns, then loads the module:
152
+ python -c 'import libpython_linkcheck; libpython_linkcheck.enable(); import linked; print("loaded", linked)'
153
+ # 2. With -Werror, the import fails before the shared object is loaded:
154
+ python -Werror -c 'import libpython_linkcheck; libpython_linkcheck.enable(); import linked' || echo "exit status $?"
155
+ # 3. Check the file without importing it:
156
+ python -m libpython_linkcheck linked*.so || echo "exit status $?"
157
+ ```
158
+
159
+ Expected output (paths and versions vary):
160
+
161
+ ```text
162
+ <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
163
+ loaded <module 'linked' from '/demo/linked.cpython-315-x86_64-linux-gnu.so'>
164
+ Traceback (most recent call last):
165
+ ...
166
+ 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
167
+ exit status 1
168
+ linked.cpython-315-x86_64-linux-gnu.so: DT_NEEDED libpython3.15.so.1.0
169
+ exit status 1
170
+ ```
171
+
172
+ ## What the 0.1.0a1 alpha supports
173
+
174
+ Supported and tested:
175
+
176
+ - CPython 3.11 through 3.15 on Linux (x86_64 and aarch64, glibc).
177
+ - Explicit activation: call `libpython_linkcheck.enable()` in each interpreter
178
+ that should be checked. Only extension modules imported **after** that call
179
+ are inspected.
180
+ - Every extension module loaded through the import system
181
+ (`importlib.machinery.ExtensionFileLoader`, which also serves
182
+ `importlib.util.spec_from_file_location` and similar).
183
+ - Only the extension's **own** `DT_NEEDED` entries; dependencies of its
184
+ dependencies are not inspected.
185
+ - libpython names recognized: versioned (`libpython3.12.so.1.0`,
186
+ `libpython3.12.so`), Stable ABI (`libpython3.so`), free-threaded
187
+ (`libpython3.13t.so.1.0`), and ABI-flagged (`libpython3.12d.so.1.0`)
188
+ names, including entries that carry a directory.
189
+ - Standard warning filters, `-Werror`, and `catch_warnings()`.
190
+ - Objects that cannot be read or parsed are skipped silently, so that
191
+ CPython's own loader reports the real error.
192
+
193
+ Not covered yet:
194
+
195
+ - Automatic activation (`sitecustomize`, `.pth` files, or a hook installed
196
+ by an embedding application before `Py_Initialize()`).
197
+ - Subinterpreters: a Python audit hook belongs to the interpreter that
198
+ installed it, so `enable()` must be called in each one. This is not yet tested.
199
+ - Free-threaded interpreters: their libpython names are recognized, but the
200
+ hook has not yet been tested running on a free-threaded build.
201
+ - Shared libraries opened with `ctypes` or `dlopen()` directly, and extension
202
+ modules that were already loaded before `enable()`.
203
+ - Other platforms: `enable()` works, but non-ELF files (Mach-O, PE) are never
204
+ reported. Other ELF platforms are untested.
205
+
206
+ ## License
207
+
208
+ MIT
@@ -0,0 +1,11 @@
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=pnkVjF0LDB0bvcR5-UcBT8rC5IzvVm1dVss42nFxAxI,7594
4
+ libpython_linkcheck/_hook.py,sha256=lYXM4A-P_91MaF6NrlKm-tW345glvfX7JfG7cxbhz3E,6896
5
+ libpython_linkcheck/_version.py,sha256=lmvUfjeqbjbsnD2PLlUjZbqEGZrq6yUD6t2LZsM61ak,24
6
+ libpython_linkcheck/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ libpython_linkcheck-0.1.0a1.dist-info/licenses/LICENSE,sha256=nxJKW9vh32DdsG7aSRztYSpEowhSV1m6PRAceUBXDGw,1068
8
+ libpython_linkcheck-0.1.0a1.dist-info/METADATA,sha256=jBX3p-pqLboSo0TfVePN8gdvacw83gQUklvz78xILn0,9065
9
+ libpython_linkcheck-0.1.0a1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
10
+ libpython_linkcheck-0.1.0a1.dist-info/top_level.txt,sha256=IZprL5FIqWd22Moutmpxn7-QKBZCsPfEGn2XhdxG7VE,20
11
+ libpython_linkcheck-0.1.0a1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
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