Skip to content

Commit 57de79f

Browse files
authored
fix #2737: pwnlib.shellcraft submodule imports on python 3.12+ (#2739)
* shellcraft: migrate lazy importer to find_spec for py3.12+ * shellcraft: changelog entry + mypy baseline sync for #2739
1 parent 5c37c24 commit 57de79f

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The table below shows which release corresponds to each branch, and what date th
7575

7676
## 5.0.0 (`dev`)
7777

78+
- [#2739][2739] shellcraft: migrate lazy importer to find_spec for Python 3.12+
7879
- [#2725][2725] feat(libcdb): add extra_mirrors arg + PWNLIB_EXTRA_LIBC_MIRRORS env to download_libraries
7980
- [#2677][2677] refactor: replace unsafe eval with safeeval.const in ROP cache loading
8081
- [#2675][2675] feat(term): add zellij support
@@ -195,6 +196,7 @@ The table below shows which release corresponds to each branch, and what date th
195196
[2725]: https://github.com/Gallopsled/pwntools/pull/2725
196197
[2730]: https://github.com/Gallopsled/pwntools/pull/2730
197198
[2733]: https://github.com/Gallopsled/pwntools/pull/2733
199+
[2739]: https://github.com/Gallopsled/pwntools/pull/2739
198200

199201
## 4.15.1
200202

mypy-baseline.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,4 @@ pwnlib/rop/call.py:0: error: Need type annotation for "args" (hint: "args: list[
1818
pwnlib/rop/call.py:0: error: Need type annotation for "values" (hint: "values: list[<type>] = ...") [var-annotated]
1919
pwnlib/rop/rop.py:0: error: Need type annotation for "descriptions" (hint: "descriptions: dict[<type>, <type>] = ...") [var-annotated]
2020
pwnlib/rop/srop.py:0: error: Need type annotation for "_regs" (hint: "_regs: list[<type>] = ...") [var-annotated]
21-
pwnlib/shellcraft/__init__.py:0: error: Argument 1 to "append" of "list" has incompatible type "LazyImporter"; expected "MetaPathFinderProtocol" [arg-type]
2221
pwnlib/update.py:0: error: "Callable[[bool], Version]" has no attribute "cached" [attr-defined]

pwnlib/shellcraft/__init__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib.abc
2+
import importlib.util
13
import itertools
24
import os
35
import re
@@ -167,8 +169,8 @@ def okay(self, s, *a, **kw):
167169
# Create the module structure
168170
shellcraft = module(__name__, '')
169171

170-
class LazyImporter:
171-
def find_module(self, fullname, path=None):
172+
class LazyImporter(importlib.abc.MetaPathFinder, importlib.abc.Loader):
173+
def find_spec(self, fullname, path=None, target=None):
172174
if not fullname.startswith('pwnlib.shellcraft.'):
173175
return None
174176

@@ -179,9 +181,15 @@ def find_module(self, fullname, path=None):
179181
if not isinstance(cur, ModuleType):
180182
return None
181183

182-
return self
184+
return importlib.util.spec_from_loader(fullname, self)
183185

184-
def load_module(self, fullname):
185-
return sys.modules[fullname]
186+
def create_module(self, spec):
187+
# The submodule was already built and registered in sys.modules by
188+
# the getattr walk in find_spec, so hand that object back instead
189+
# of letting the import system make a fresh empty module.
190+
return sys.modules.get(spec.name)
191+
192+
def exec_module(self, module):
193+
pass
186194

187195
sys.meta_path.append(LazyImporter())

0 commit comments

Comments
 (0)