Repository navigation
Commit 27909fe
Install the finder only after the module is initialized.
`install` appended a `MetadataPathFinder` to `sys.meta_path` while the
module body was still executing, 36 lines before `PathDistribution` is
bound, and `PathDistribution` is the only global the installed finder has
yet to see (`Prepared`, `FastPath` and `DistributionFinder` all precede
it). Anything walking `sys.meta_path` in that window resolves it against
incomplete globals and raises `NameError: name 'PathDistribution' is not
defined`.
Two conditions reach the window. Another thread calling `entry_points()`
takes no module lock, as it imports nothing and only iterates
`sys.meta_path`. And an import aborted in the window, as an async
exception such as a signal handler or a soft time limit can do, leaves
the half-built finder installed for the life of the process: the module
is dropped from `sys.modules`, `install` has already deleted
`PathFinder.find_distributions` so the orphan is the only provider, and
even a later successful import does not displace it.
Installing at the end of the module, rather than just after
`PathDistribution`, states the invariant the finder depends on and so
survives any later reordering.
The stdlib copy is unaffected, where `install` is stripped and
`PathFinder.find_distributions` imports `MetadataPathFinder` lazily.
Closes #525.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>1 parent d196fdb commit 27909fe
3 files changed
Lines changed: 45 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1030 | 1030 | | |
1031 | 1031 | | |
1032 | 1032 | | |
1033 | | - | |
1034 | 1033 | | |
1035 | 1034 | | |
1036 | 1035 | | |
| |||
1309 | 1308 | | |
1310 | 1309 | | |
1311 | 1310 | | |
| 1311 | + | |
| 1312 | + | |
| 1313 | + | |
| 1314 | + | |
| 1315 | + | |
| 1316 | + | |
| 1317 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
| 8 | + | |
6 | 9 | | |
| 10 | + | |
7 | 11 | | |
8 | 12 | | |
9 | 13 | | |
| |||
14 | 18 | | |
15 | 19 | | |
16 | 20 | | |
| 21 | + | |
17 | 22 | | |
18 | 23 | | |
19 | 24 | | |
| |||
108 | 113 | | |
109 | 114 | | |
110 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
111 | 143 | | |
112 | 144 | | |
113 | 145 | | |
| |||
0 commit comments