Commit afbcecb
committed
pyembed: support loading in-memory extension modules on Windows
There exists a dark arts mechanism for loading Windows PE files from
memory. A mechanism to facilitate this is implemented in the
MemoryModule library at https://github.com/fancycode/MemoryModule.
I have published a `memory-module-sys` Rust crate to expose
bindings to this library. This enables Rust to load DLLs from
memory.
Previously in PyOxidizer, we taught the embedded Python resources
data structure to define the contents of a shared library extension
module to be imported from memory.
This commit combines the two efforts and enables the `pyembed` crate
to import Python extension modules which reside in memory.
Getting this working took a fair amount of effort. There were
a handful of attempts that did not pan out. Some of the failed
attempts appeared to work. But they were subtly broken due to e.g.
the `LazyLoader` importer assuming that the `sys.modules()` entry
wouldn't be modified. In the end, the final implementation
emulates CPython's extension module loading mechanism as closely
as possible. This was the only way I was able to preserve compatibility
with `LazyLoader` (just implementing `exec_module()` without
`create_module()` appears impossible - at least without writing our
own lazy module implementation).
While this commit produces working results, it is far from feature
complete. We still do not handle library dependencies properly.
We will likely need to teach the embedded resources data structure
about the existence of shared library resources and dependencies
from extension modules so that shared libraries can be imported
from memory when an extension module is imported.
Because this commit utilizes some CPython APIs outside the paved
road of CPython APIs, we had to contribute support for these
symbols to python3-sys
(dgrunwald/rust-cpython#210). This is why
we now depend on a specific Git commit of python3-sys and the
cpython crates. This means we can't release pyembed to crates.io
until a new version of these crates is published... We're likely
a ways off from a new release, as I don't want to solidify the new
embedded resources format until it has more features. So hopefully
this isn't a problem...1 parent 0771233 commit afbcecb
8 files changed
Lines changed: 372 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
48 | 56 | | |
49 | 57 | | |
50 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
387 | 387 | | |
388 | 388 | | |
389 | 389 | | |
390 | | - | |
| 390 | + | |
| 391 | + | |
391 | 392 | | |
392 | 393 | | |
393 | 394 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
224 | 224 | | |
225 | 225 | | |
226 | 226 | | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
234 | 286 | | |
235 | 287 | | |
236 | 288 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
48 | | - | |
| 47 | + | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| |||
0 commit comments