-
-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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...
- Loading branch information
Showing
8 changed files
with
371 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.