libpython-linkcheck 0.1.0a1__tar.gz → 0.1.0a2__tar.gz
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.
- libpython_linkcheck-0.1.0a2/MANIFEST.in +7 -0
- {libpython_linkcheck-0.1.0a1/src/libpython_linkcheck.egg-info → libpython_linkcheck-0.1.0a2}/PKG-INFO +71 -12
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/README.md +70 -11
- libpython_linkcheck-0.1.0a2/autoenable/LICENSE +21 -0
- libpython_linkcheck-0.1.0a2/autoenable/README.md +17 -0
- libpython_linkcheck-0.1.0a2/autoenable/libpython_linkcheck_autoenable.pth +1 -0
- libpython_linkcheck-0.1.0a2/autoenable/libpython_linkcheck_autoenable.start +4 -0
- libpython_linkcheck-0.1.0a2/autoenable/pyproject.toml +31 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/pyproject.toml +31 -1
- libpython_linkcheck-0.1.0a2/setup.py +37 -0
- libpython_linkcheck-0.1.0a2/src/csrc/_nativemodule.c +144 -0
- libpython_linkcheck-0.1.0a2/src/csrc/libpython_linkcheck.h +51 -0
- libpython_linkcheck-0.1.0a2/src/csrc/linkcheck_elf.c +463 -0
- libpython_linkcheck-0.1.0a2/src/csrc/linkcheck_elf.h +51 -0
- libpython_linkcheck-0.1.0a2/src/csrc/linkcheck_hook.c +297 -0
- libpython_linkcheck-0.1.0a2/src/csrc/linkcheck_hook.h +24 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/src/libpython_linkcheck/_elf.py +42 -3
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/src/libpython_linkcheck/_hook.py +40 -13
- libpython_linkcheck-0.1.0a2/src/libpython_linkcheck/_version.py +1 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2/src/libpython_linkcheck.egg-info}/PKG-INFO +71 -12
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/src/libpython_linkcheck.egg-info/SOURCES.txt +18 -1
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/conftest.py +49 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/extbuild.py +41 -0
- libpython_linkcheck-0.1.0a2/tests/fixtures/launcher.c +49 -0
- libpython_linkcheck-0.1.0a2/tests/native/elfcheck.c +41 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/pyrun.py +51 -2
- libpython_linkcheck-0.1.0a2/tests/test_activation.py +176 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/test_elf.py +37 -37
- libpython_linkcheck-0.1.0a2/tests/test_embedded.py +208 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/test_hook.py +30 -5
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/test_linux_extensions.py +40 -3
- libpython_linkcheck-0.1.0a2/tests/test_native.py +198 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/test_readme.py +19 -1
- libpython_linkcheck-0.1.0a1/MANIFEST.in +0 -4
- libpython_linkcheck-0.1.0a1/src/libpython_linkcheck/_version.py +0 -1
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/LICENSE +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/setup.cfg +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/src/libpython_linkcheck/__init__.py +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/src/libpython_linkcheck/__main__.py +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/src/libpython_linkcheck/py.typed +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/src/libpython_linkcheck.egg-info/dependency_links.txt +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/src/libpython_linkcheck.egg-info/top_level.txt +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/elfbuild.py +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/fixtures/extmod.c +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/test_cli.py +0 -0
- {libpython_linkcheck-0.1.0a1 → libpython_linkcheck-0.1.0a2}/tests/test_version.py +0 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# The sdist must be enough to build *and* test: ship the C sources and the
|
|
2
|
+
# whole test tree, including C fixture sources, but no build products.
|
|
3
|
+
recursive-include src/csrc *.c *.h
|
|
4
|
+
graft tests
|
|
5
|
+
# The opt-in autoenable distribution's files (used by tests/test_activation.py).
|
|
6
|
+
graft autoenable
|
|
7
|
+
global-exclude __pycache__ *.py[cod] *.so *.o
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: libpython-linkcheck
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.0a2
|
|
4
4
|
Summary: Warn when an imported Python extension directly links to libpython on Linux.
|
|
5
5
|
Author: Itamar Oren
|
|
6
6
|
License-Expression: MIT
|
|
@@ -49,7 +49,9 @@ a `LibpythonLinkWarning` if the object's own `DT_NEEDED` entries name libpython.
|
|
|
49
49
|
pip install libpython-linkcheck
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
The import name is `libpython_linkcheck`. It
|
|
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.
|
|
53
55
|
|
|
54
56
|
## Usage
|
|
55
57
|
|
|
@@ -129,6 +131,59 @@ is 0 if no file links libpython, 1 if any does, and 2 if a file could not be
|
|
|
129
131
|
read or is not ELF. From Python, `libpython_linkcheck.libpython_dependencies(path)`
|
|
130
132
|
returns the matching entries as a list.
|
|
131
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
|
+
|
|
132
187
|
## Try it
|
|
133
188
|
|
|
134
189
|
This builds an extension that links libpython and imports it with the hook
|
|
@@ -169,14 +224,24 @@ linked.cpython-315-x86_64-linux-gnu.so: DT_NEEDED libpython3.15.so.1.0
|
|
|
169
224
|
exit status 1
|
|
170
225
|
```
|
|
171
226
|
|
|
172
|
-
## What
|
|
227
|
+
## What is supported
|
|
173
228
|
|
|
174
229
|
Supported and tested:
|
|
175
230
|
|
|
176
|
-
- CPython 3.11 through 3.15 on Linux (x86_64 and aarch64, glibc)
|
|
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.
|
|
177
241
|
- Explicit activation: call `libpython_linkcheck.enable()` in each interpreter
|
|
178
|
-
that should be checked
|
|
179
|
-
|
|
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.
|
|
180
245
|
- Every extension module loaded through the import system
|
|
181
246
|
(`importlib.machinery.ExtensionFileLoader`, which also serves
|
|
182
247
|
`importlib.util.spec_from_file_location` and similar).
|
|
@@ -192,12 +257,6 @@ Supported and tested:
|
|
|
192
257
|
|
|
193
258
|
Not covered yet:
|
|
194
259
|
|
|
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
260
|
- Shared libraries opened with `ctypes` or `dlopen()` directly, and extension
|
|
202
261
|
modules that were already loaded before `enable()`.
|
|
203
262
|
- Other platforms: `enable()` works, but non-ELF files (Mach-O, PE) are never
|
|
@@ -20,7 +20,9 @@ a `LibpythonLinkWarning` if the object's own `DT_NEEDED` entries name libpython.
|
|
|
20
20
|
pip install libpython-linkcheck
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
The import name is `libpython_linkcheck`. It
|
|
23
|
+
The import name is `libpython_linkcheck`. It has no dependencies. On Linux
|
|
24
|
+
(x86_64 and aarch64, glibc) pip installs a native wheel that runs the check
|
|
25
|
+
in C; elsewhere it installs a pure-Python wheel with the same behavior.
|
|
24
26
|
|
|
25
27
|
## Usage
|
|
26
28
|
|
|
@@ -100,6 +102,59 @@ is 0 if no file links libpython, 1 if any does, and 2 if a file could not be
|
|
|
100
102
|
read or is not ELF. From Python, `libpython_linkcheck.libpython_dependencies(path)`
|
|
101
103
|
returns the matching entries as a list.
|
|
102
104
|
|
|
105
|
+
## Activating it automatically
|
|
106
|
+
|
|
107
|
+
Installing `libpython-linkcheck` never activates anything by itself. To check
|
|
108
|
+
every program run by an interpreter, choose one of these explicitly:
|
|
109
|
+
|
|
110
|
+
| Activation | How | Covers | Not under |
|
|
111
|
+
| --- | --- | --- | --- |
|
|
112
|
+
| User code | `libpython_linkcheck.enable()` | the calling interpreter, from that call on | — |
|
|
113
|
+
| `sitecustomize` | `sitecustomize.py` in site-packages calling `enable()` | interpreters using that site-packages (a venv's own, or the base installation's) | `-S` |
|
|
114
|
+
| `usercustomize` | `usercustomize.py` in the user site directory calling `enable()` | that user's interpreters outside venvs | `-s`, `-I`, `-S`, venvs |
|
|
115
|
+
| `.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` |
|
|
116
|
+
| Executable | `LibpythonLinkcheck_Install()` in the program's `main()` (next section) | the whole runtime, including subinterpreters | — (works under `-I -S`) |
|
|
117
|
+
|
|
118
|
+
Every path can be combined with the others: whichever runs first installs the
|
|
119
|
+
hook, and later `enable()` calls detect it and return `False`, so each
|
|
120
|
+
extension module is reported once. A hook installed by `enable()` (any of the
|
|
121
|
+
first four rows) covers only the interpreter that installed it; a
|
|
122
|
+
subinterpreter needs its own `enable()` call. Extension modules imported
|
|
123
|
+
before activation, including by `.pth` files processed earlier, are not
|
|
124
|
+
inspected.
|
|
125
|
+
|
|
126
|
+
`libpython-linkcheck-autoenable` is not on PyPI; build it from the
|
|
127
|
+
repository with `python -m build --wheel autoenable`. On Python 3.11–3.14 its
|
|
128
|
+
`.pth` file's import line calls `enable()`; on 3.15 and later the matching
|
|
129
|
+
[PEP 829](https://peps.python.org/pep-0829/) `.start` file
|
|
130
|
+
(`libpython_linkcheck:enable`) does, and disables that import line.
|
|
131
|
+
|
|
132
|
+
## Embedding: a hook installed before Python starts
|
|
133
|
+
|
|
134
|
+
Applications that embed CPython, and CPython distributions that build their
|
|
135
|
+
own `python` executable, can register the check in C before the runtime
|
|
136
|
+
starts. No Python package is imported, it covers every interpreter
|
|
137
|
+
(including subinterpreters), and it works under `-I -S`:
|
|
138
|
+
|
|
139
|
+
```c
|
|
140
|
+
#include "libpython_linkcheck.h"
|
|
141
|
+
|
|
142
|
+
int main(int argc, char **argv) {
|
|
143
|
+
if (LibpythonLinkcheck_Install() < 0) {
|
|
144
|
+
return 1;
|
|
145
|
+
}
|
|
146
|
+
return Py_BytesMain(argc, argv);
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Compile `src/csrc/linkcheck_hook.c` and `src/csrc/linkcheck_elf.c` (from the
|
|
151
|
+
sdist or the repository) into the program, with `src/csrc` on the include
|
|
152
|
+
path; the program links libpython as embedders do. The warning category is
|
|
153
|
+
the same `libpython_linkcheck.LibpythonLinkWarning` that the package
|
|
154
|
+
exposes, and a later `libpython_linkcheck.enable()` call detects the hook
|
|
155
|
+
and returns `False` instead of installing a second one. See
|
|
156
|
+
`src/csrc/libpython_linkcheck.h` for the exact contract.
|
|
157
|
+
|
|
103
158
|
## Try it
|
|
104
159
|
|
|
105
160
|
This builds an extension that links libpython and imports it with the hook
|
|
@@ -140,14 +195,24 @@ linked.cpython-315-x86_64-linux-gnu.so: DT_NEEDED libpython3.15.so.1.0
|
|
|
140
195
|
exit status 1
|
|
141
196
|
```
|
|
142
197
|
|
|
143
|
-
## What
|
|
198
|
+
## What is supported
|
|
144
199
|
|
|
145
200
|
Supported and tested:
|
|
146
201
|
|
|
147
|
-
- CPython 3.11 through 3.15 on Linux (x86_64 and aarch64, glibc)
|
|
202
|
+
- CPython 3.11 through 3.15 on Linux (x86_64 and aarch64, glibc), and the
|
|
203
|
+
free-threaded builds of 3.14 and 3.15. Importing and enabling the
|
|
204
|
+
package keeps the GIL disabled.
|
|
205
|
+
- Distributions: native `manylinux_2_28` wheels (x86_64, aarch64) for
|
|
206
|
+
CPython 3.11–3.15 and free-threaded 3.14t/3.15t, which use the C
|
|
207
|
+
implementation; a pure-Python `py3-none-any` wheel for every other
|
|
208
|
+
platform; and an sdist, which compiles the C implementation on POSIX.
|
|
209
|
+
- Every activation path in [the table above](#activating-it-automatically),
|
|
210
|
+
each tested in a fresh venv against a real linked extension, alone and
|
|
211
|
+
combined.
|
|
148
212
|
- Explicit activation: call `libpython_linkcheck.enable()` in each interpreter
|
|
149
|
-
that should be checked
|
|
150
|
-
|
|
213
|
+
that should be checked; the hook it installs covers only that interpreter,
|
|
214
|
+
including when it is a subinterpreter. Only extension modules imported
|
|
215
|
+
**after** that call are inspected.
|
|
151
216
|
- Every extension module loaded through the import system
|
|
152
217
|
(`importlib.machinery.ExtensionFileLoader`, which also serves
|
|
153
218
|
`importlib.util.spec_from_file_location` and similar).
|
|
@@ -163,12 +228,6 @@ Supported and tested:
|
|
|
163
228
|
|
|
164
229
|
Not covered yet:
|
|
165
230
|
|
|
166
|
-
- Automatic activation (`sitecustomize`, `.pth` files, or a hook installed
|
|
167
|
-
by an embedding application before `Py_Initialize()`).
|
|
168
|
-
- Subinterpreters: a Python audit hook belongs to the interpreter that
|
|
169
|
-
installed it, so `enable()` must be called in each one. This is not yet tested.
|
|
170
|
-
- Free-threaded interpreters: their libpython names are recognized, but the
|
|
171
|
-
hook has not yet been tested running on a free-threaded build.
|
|
172
231
|
- Shared libraries opened with `ctypes` or `dlopen()` directly, and extension
|
|
173
232
|
modules that were already loaded before `enable()`.
|
|
174
233
|
- Other platforms: `enable()` works, but non-ELF files (Mach-O, PE) are never
|
|
@@ -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,17 @@
|
|
|
1
|
+
# libpython-linkcheck-autoenable
|
|
2
|
+
|
|
3
|
+
Installing this distribution enables
|
|
4
|
+
[libpython-linkcheck](https://pypi.org/project/libpython-linkcheck/) in every
|
|
5
|
+
interpreter that processes the `site-packages` directory it is installed in:
|
|
6
|
+
at startup, `libpython_linkcheck.enable()` runs before any user code.
|
|
7
|
+
|
|
8
|
+
It installs two files at the root of `site-packages`:
|
|
9
|
+
|
|
10
|
+
- `libpython_linkcheck_autoenable.pth`, whose `import` line runs `enable()`
|
|
11
|
+
on Python 3.11–3.14;
|
|
12
|
+
- `libpython_linkcheck_autoenable.start`, a [PEP 829](https://peps.python.org/pep-0829/)
|
|
13
|
+
entry point (`libpython_linkcheck:enable`) for Python 3.15 and later. Its
|
|
14
|
+
presence disables the `.pth` file's import line, so `enable()` runs once.
|
|
15
|
+
|
|
16
|
+
It does nothing under `-S` (no `site`), and it cannot inspect extension
|
|
17
|
+
modules imported by `.pth` files that are processed before it.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import libpython_linkcheck; libpython_linkcheck.enable()
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Opt-in automatic activation for libpython-linkcheck.
|
|
2
|
+
#
|
|
3
|
+
# Installing this distribution makes every interpreter that processes its
|
|
4
|
+
# site-packages directory (venvs included; not -S) call
|
|
5
|
+
# libpython_linkcheck.enable() at startup. It contains only two startup files
|
|
6
|
+
# at the root of site-packages; installing libpython-linkcheck by itself never
|
|
7
|
+
# activates anything.
|
|
8
|
+
#
|
|
9
|
+
# Not published to PyPI: build it with `python -m build --wheel autoenable`
|
|
10
|
+
# (or copy the two files into site-packages yourself). Its version must equal
|
|
11
|
+
# libpython-linkcheck's (checked by ci/build-dist.sh).
|
|
12
|
+
[build-system]
|
|
13
|
+
requires = ["hatchling==1.33.0"]
|
|
14
|
+
build-backend = "hatchling.build"
|
|
15
|
+
|
|
16
|
+
[project]
|
|
17
|
+
name = "libpython-linkcheck-autoenable"
|
|
18
|
+
version = "0.1.0a2"
|
|
19
|
+
description = "Enable libpython-linkcheck automatically at interpreter startup"
|
|
20
|
+
readme = "README.md"
|
|
21
|
+
license = "MIT"
|
|
22
|
+
license-files = ["LICENSE"]
|
|
23
|
+
requires-python = ">=3.11"
|
|
24
|
+
dependencies = ["libpython-linkcheck==0.1.0a2"]
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.wheel]
|
|
27
|
+
bypass-selection = true
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.wheel.force-include]
|
|
30
|
+
"libpython_linkcheck_autoenable.pth" = "libpython_linkcheck_autoenable.pth"
|
|
31
|
+
"libpython_linkcheck_autoenable.start" = "libpython_linkcheck_autoenable.start"
|
|
@@ -36,8 +36,9 @@ Issues = "https://github.com/itamaro/libpython-linkcheck/issues"
|
|
|
36
36
|
|
|
37
37
|
[dependency-groups]
|
|
38
38
|
test = ["pytest==9.2.0"]
|
|
39
|
-
lint = ["ruff==0.17.6"]
|
|
39
|
+
lint = ["ruff==0.17.6", "clang-format==23.1.5"]
|
|
40
40
|
dist = ["build==1.6.1", "twine==7.0.0"]
|
|
41
|
+
wheels = ["cibuildwheel==4.3.5"]
|
|
41
42
|
dev = [
|
|
42
43
|
{ include-group = "test" },
|
|
43
44
|
{ include-group = "lint" },
|
|
@@ -49,16 +50,45 @@ version = { attr = "libpython_linkcheck._version.__version__" }
|
|
|
49
50
|
|
|
50
51
|
[tool.setuptools.packages.find]
|
|
51
52
|
where = ["src"]
|
|
53
|
+
include = ["libpython_linkcheck*"]
|
|
54
|
+
namespaces = false
|
|
52
55
|
|
|
53
56
|
[tool.setuptools.package-data]
|
|
54
57
|
libpython_linkcheck = ["py.typed"]
|
|
55
58
|
|
|
59
|
+
[tool.cibuildwheel]
|
|
60
|
+
# Native wheels: manylinux_2_28 for every supported CPython, including the
|
|
61
|
+
# free-threaded builds. Other platforms (macOS, musl, ...) use the pure-Python
|
|
62
|
+
# wheel. See "Native wheels" in AGENTS.md.
|
|
63
|
+
build = [
|
|
64
|
+
"cp311-manylinux_*",
|
|
65
|
+
"cp312-manylinux_*",
|
|
66
|
+
"cp313-manylinux_*",
|
|
67
|
+
"cp314-manylinux_*",
|
|
68
|
+
"cp314t-manylinux_*",
|
|
69
|
+
"cp315-manylinux_*",
|
|
70
|
+
"cp315t-manylinux_*",
|
|
71
|
+
]
|
|
72
|
+
build-frontend = "build"
|
|
73
|
+
environment = { CFLAGS = "-Werror" }
|
|
74
|
+
test-command = "python {project}/ci/wheel_smoke.py {project}"
|
|
75
|
+
|
|
76
|
+
[tool.cibuildwheel.linux]
|
|
77
|
+
manylinux-x86_64-image = "manylinux_2_28"
|
|
78
|
+
manylinux-aarch64-image = "manylinux_2_28"
|
|
79
|
+
repair-wheel-command = [
|
|
80
|
+
"auditwheel show {wheel}",
|
|
81
|
+
"auditwheel repair --only-plat --plat manylinux_2_28_$(uname -m) -w {dest_dir} {wheel}",
|
|
82
|
+
"auditwheel show {dest_dir}/*.whl",
|
|
83
|
+
]
|
|
84
|
+
|
|
56
85
|
[tool.pytest.ini_options]
|
|
57
86
|
minversion = "8.0"
|
|
58
87
|
testpaths = ["tests"]
|
|
59
88
|
pythonpath = ["tests"]
|
|
60
89
|
addopts = ["-ra", "--strict-markers", "--strict-config", "--import-mode=importlib"]
|
|
61
90
|
xfail_strict = true
|
|
91
|
+
markers = ["requires_native: needs the C implementation; deselected when it is not built"]
|
|
62
92
|
filterwarnings = ["error"]
|
|
63
93
|
|
|
64
94
|
[tool.ruff]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Build configuration for the optional C implementation.
|
|
2
|
+
|
|
3
|
+
All metadata lives in pyproject.toml. This file only declares the
|
|
4
|
+
``libpython_linkcheck._native`` extension module, which is built on POSIX
|
|
5
|
+
platforms unless ``LIBPYTHON_LINKCHECK_PURE=1`` is set (then the package
|
|
6
|
+
is pure Python and uses its Python implementation).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
|
|
11
|
+
from setuptools import Extension, setup
|
|
12
|
+
|
|
13
|
+
CSRC = "src/csrc"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def ext_modules():
|
|
17
|
+
if os.environ.get("LIBPYTHON_LINKCHECK_PURE") == "1" or os.name != "posix":
|
|
18
|
+
return []
|
|
19
|
+
return [
|
|
20
|
+
Extension(
|
|
21
|
+
"libpython_linkcheck._native",
|
|
22
|
+
sources=[
|
|
23
|
+
f"{CSRC}/_nativemodule.c",
|
|
24
|
+
f"{CSRC}/linkcheck_hook.c",
|
|
25
|
+
f"{CSRC}/linkcheck_elf.c",
|
|
26
|
+
],
|
|
27
|
+
depends=[
|
|
28
|
+
f"{CSRC}/linkcheck_elf.h",
|
|
29
|
+
f"{CSRC}/linkcheck_hook.h",
|
|
30
|
+
f"{CSRC}/libpython_linkcheck.h",
|
|
31
|
+
],
|
|
32
|
+
extra_compile_args=["-Wall", "-Wextra", "-Wshadow", "-Wstrict-prototypes"],
|
|
33
|
+
)
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
setup(ext_modules=ext_modules())
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* libpython_linkcheck._native: the C implementation used by the package
|
|
3
|
+
* when available. The pure-Python modules remain as a fallback.
|
|
4
|
+
*/
|
|
5
|
+
#define PY_SSIZE_T_CLEAN
|
|
6
|
+
#include <Python.h>
|
|
7
|
+
|
|
8
|
+
#include <errno.h>
|
|
9
|
+
|
|
10
|
+
#include "linkcheck_elf.h"
|
|
11
|
+
#include "linkcheck_hook.h"
|
|
12
|
+
|
|
13
|
+
PyDoc_STRVAR(read_needed_doc,
|
|
14
|
+
"read_needed(path, /)\n--\n\n"
|
|
15
|
+
"Return (0, [names]) with the DT_NEEDED entries of the ELF object at path,\n"
|
|
16
|
+
"(2, detail) if it is not ELF, or (3, detail) if it is malformed.\n"
|
|
17
|
+
"Raises OSError if it cannot be read.");
|
|
18
|
+
|
|
19
|
+
static PyObject *
|
|
20
|
+
native_read_needed(PyObject *Py_UNUSED(module), PyObject *path)
|
|
21
|
+
{
|
|
22
|
+
PyObject *encoded = NULL;
|
|
23
|
+
if (!PyUnicode_FSConverter(path, &encoded)) {
|
|
24
|
+
return NULL;
|
|
25
|
+
}
|
|
26
|
+
lpc_needed needed;
|
|
27
|
+
const char *detail = NULL;
|
|
28
|
+
lpc_status st;
|
|
29
|
+
int saved_errno;
|
|
30
|
+
Py_BEGIN_ALLOW_THREADS
|
|
31
|
+
st = lpc_read_needed(PyBytes_AS_STRING(encoded), &needed, &detail);
|
|
32
|
+
saved_errno = errno;
|
|
33
|
+
Py_END_ALLOW_THREADS
|
|
34
|
+
Py_DECREF(encoded);
|
|
35
|
+
|
|
36
|
+
switch (st) {
|
|
37
|
+
case LPC_OK: {
|
|
38
|
+
PyObject *names = PyList_New(0);
|
|
39
|
+
if (names == NULL) {
|
|
40
|
+
lpc_needed_free(&needed);
|
|
41
|
+
return NULL;
|
|
42
|
+
}
|
|
43
|
+
const char *p = needed.data;
|
|
44
|
+
for (size_t i = 0; i < needed.count; i++) {
|
|
45
|
+
size_t len = strlen(p);
|
|
46
|
+
PyObject *name = PyUnicode_DecodeFSDefaultAndSize(p, (Py_ssize_t)len);
|
|
47
|
+
if (name == NULL || PyList_Append(names, name) < 0) {
|
|
48
|
+
Py_XDECREF(name);
|
|
49
|
+
Py_DECREF(names);
|
|
50
|
+
lpc_needed_free(&needed);
|
|
51
|
+
return NULL;
|
|
52
|
+
}
|
|
53
|
+
Py_DECREF(name);
|
|
54
|
+
p += len + 1;
|
|
55
|
+
}
|
|
56
|
+
lpc_needed_free(&needed);
|
|
57
|
+
return Py_BuildValue("(iN)", 0, names);
|
|
58
|
+
}
|
|
59
|
+
case LPC_ERR_OS:
|
|
60
|
+
errno = saved_errno;
|
|
61
|
+
return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
|
|
62
|
+
case LPC_ERR_NOMEM:
|
|
63
|
+
return PyErr_NoMemory();
|
|
64
|
+
case LPC_ERR_NOT_ELF:
|
|
65
|
+
case LPC_ERR_MALFORMED:
|
|
66
|
+
default:
|
|
67
|
+
return Py_BuildValue("(is)", (int)st, detail ? detail : "malformed ELF");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
PyDoc_STRVAR(is_libpython_doc,
|
|
72
|
+
"is_libpython(name, /)\n--\n\n"
|
|
73
|
+
"Return True if a DT_NEEDED entry names a libpython shared library.");
|
|
74
|
+
|
|
75
|
+
static PyObject *
|
|
76
|
+
native_is_libpython(PyObject *Py_UNUSED(module), PyObject *name)
|
|
77
|
+
{
|
|
78
|
+
PyObject *encoded = NULL;
|
|
79
|
+
if (!PyUnicode_FSConverter(name, &encoded)) {
|
|
80
|
+
return NULL;
|
|
81
|
+
}
|
|
82
|
+
int result = lpc_is_libpython(PyBytes_AS_STRING(encoded));
|
|
83
|
+
Py_DECREF(encoded);
|
|
84
|
+
return PyBool_FromLong(result);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
PyDoc_STRVAR(audit_hook_doc, "audit_hook(event, args, /)\n--\n\n"
|
|
88
|
+
"The libpython-linkcheck audit hook, for sys.addaudithook().");
|
|
89
|
+
|
|
90
|
+
static PyObject *
|
|
91
|
+
native_audit_hook(PyObject *Py_UNUSED(module), PyObject *const *args, Py_ssize_t nargs)
|
|
92
|
+
{
|
|
93
|
+
if (nargs != 2 || !PyUnicode_Check(args[0])) {
|
|
94
|
+
PyErr_SetString(PyExc_TypeError, "audit_hook(event: str, args: tuple)");
|
|
95
|
+
return NULL;
|
|
96
|
+
}
|
|
97
|
+
const char *event = PyUnicode_AsUTF8(args[0]);
|
|
98
|
+
if (event == NULL || lpc_audit(event, args[1], "native") < 0) {
|
|
99
|
+
return NULL;
|
|
100
|
+
}
|
|
101
|
+
Py_RETURN_NONE;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
PyDoc_STRVAR(category_doc, "category()\n--\n\n"
|
|
105
|
+
"Return this interpreter's LibpythonLinkWarning class.");
|
|
106
|
+
|
|
107
|
+
static PyObject *
|
|
108
|
+
native_category(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(ignored))
|
|
109
|
+
{
|
|
110
|
+
return lpc_get_category();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static PyMethodDef native_methods[] = {
|
|
114
|
+
{"audit_hook", (PyCFunction)(void (*)(void))native_audit_hook, METH_FASTCALL, audit_hook_doc},
|
|
115
|
+
{"category", native_category, METH_NOARGS, category_doc},
|
|
116
|
+
{"read_needed", native_read_needed, METH_O, read_needed_doc},
|
|
117
|
+
{"is_libpython", native_is_libpython, METH_O, is_libpython_doc},
|
|
118
|
+
{NULL, NULL, 0, NULL},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
static PyModuleDef_Slot native_slots[] = {
|
|
122
|
+
#if PY_VERSION_HEX >= 0x030C0000
|
|
123
|
+
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
|
|
124
|
+
#endif
|
|
125
|
+
#if PY_VERSION_HEX >= 0x030D0000
|
|
126
|
+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
|
|
127
|
+
#endif
|
|
128
|
+
{0, NULL},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
static struct PyModuleDef native_module = {
|
|
132
|
+
.m_base = PyModuleDef_HEAD_INIT,
|
|
133
|
+
.m_name = "libpython_linkcheck._native",
|
|
134
|
+
.m_doc = "C implementation of libpython-linkcheck.",
|
|
135
|
+
.m_size = 0,
|
|
136
|
+
.m_methods = native_methods,
|
|
137
|
+
.m_slots = native_slots,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
PyMODINIT_FUNC
|
|
141
|
+
PyInit__native(void)
|
|
142
|
+
{
|
|
143
|
+
return PyModuleDef_Init(&native_module);
|
|
144
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* libpython-linkcheck embedding API.
|
|
3
|
+
*
|
|
4
|
+
* Lets an application that embeds CPython (or a CPython distribution such as
|
|
5
|
+
* python-build-standalone, in its `python` executable) check every extension
|
|
6
|
+
* module before it is loaded, from the very start of the runtime and without
|
|
7
|
+
* importing any Python package.
|
|
8
|
+
*
|
|
9
|
+
* Build: compile these three files into the program, which links libpython:
|
|
10
|
+
*
|
|
11
|
+
* src/csrc/linkcheck_hook.c (includes Python.h)
|
|
12
|
+
* src/csrc/linkcheck_elf.c (plain C99 + POSIX)
|
|
13
|
+
* src/csrc/linkcheck_elf.h, linkcheck_hook.h, libpython_linkcheck.h
|
|
14
|
+
*
|
|
15
|
+
* Use:
|
|
16
|
+
*
|
|
17
|
+
* #include "libpython_linkcheck.h"
|
|
18
|
+
*
|
|
19
|
+
* int main(int argc, char **argv) {
|
|
20
|
+
* if (LibpythonLinkcheck_Install() < 0) { ... }
|
|
21
|
+
* return Py_BytesMain(argc, argv);
|
|
22
|
+
* }
|
|
23
|
+
*
|
|
24
|
+
* The hook is a runtime-wide C audit hook (PySys_AddAuditHook), so it covers
|
|
25
|
+
* the main interpreter and every subinterpreter, and it is active even under
|
|
26
|
+
* `-I -S`. It issues `libpython_linkcheck.LibpythonLinkWarning` (a
|
|
27
|
+
* RuntimeWarning subclass, created per interpreter and shared with the Python
|
|
28
|
+
* package) through the normal warnings machinery. A later
|
|
29
|
+
* `libpython_linkcheck.enable()` call detects it and installs nothing.
|
|
30
|
+
*/
|
|
31
|
+
#ifndef LIBPYTHON_LINKCHECK_H
|
|
32
|
+
#define LIBPYTHON_LINKCHECK_H
|
|
33
|
+
|
|
34
|
+
#ifdef __cplusplus
|
|
35
|
+
extern "C" {
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#define LIBPYTHON_LINKCHECK_API_VERSION 1
|
|
39
|
+
|
|
40
|
+
/* Register the hook. Call from the main thread before Py_Initialize*(),
|
|
41
|
+
Py_InitializeFromConfig(), Py_Main() or Py_BytesMain(). Calling it again
|
|
42
|
+
in the same runtime is a no-op; after Py_Finalize*() it may be called
|
|
43
|
+
again before the next initialization. Returns 0 on success, -1 on
|
|
44
|
+
failure (out of memory). */
|
|
45
|
+
int LibpythonLinkcheck_Install(void);
|
|
46
|
+
|
|
47
|
+
#ifdef __cplusplus
|
|
48
|
+
}
|
|
49
|
+
#endif
|
|
50
|
+
|
|
51
|
+
#endif /* LIBPYTHON_LINKCHECK_H */
|