Skip to content

Commit 8037e7e

Browse files
committed
feat: add inflate shortcut
1 parent 470ac9f commit 8037e7e

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

libdestruct/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from libdestruct.common.enum import enum, enum_of
1818
from libdestruct.common.ptr import ptr
1919
from libdestruct.common.struct import ptr_to, ptr_to_self, struct
20-
from libdestruct.libdestruct import inflater
20+
from libdestruct.libdestruct import inflate, inflater
2121

2222
__all__ = [
2323
"array",
@@ -30,6 +30,7 @@
3030
"c_ulong",
3131
"enum",
3232
"enum_of",
33+
"inflate",
3334
"inflater",
3435
"struct",
3536
"ptr",

libdestruct/libdestruct.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,37 @@
66

77
from __future__ import annotations
88

9+
from collections.abc import Sequence
910
from typing import TYPE_CHECKING
1011

12+
from libdestruct.backing.resolver import Resolver
1113
from libdestruct.common.inflater import Inflater
1214

13-
if TYPE_CHECKING: # pragma: no cover
14-
from collections.abc import MutableSequence
15+
if TYPE_CHECKING: # pragma: no cover
1516

17+
from libdestruct.common.obj import obj
1618

17-
def inflater(memory: MutableSequence) -> Inflater:
19+
20+
def inflater(memory: Sequence) -> Inflater:
1821
"""Return a TypeInflater instance."""
22+
if not isinstance(memory, Sequence):
23+
raise TypeError(f"memory must be a MutableSequence, not {type(memory).__name__}")
24+
1925
return Inflater(memory)
26+
27+
28+
def inflate(item: type, memory: Sequence, address: int | Resolver) -> obj:
29+
"""Inflate a memory-referencing type.
30+
31+
Args:
32+
item: The type to inflate.
33+
memory: The memory view, which can be mutable or immutable.
34+
address: The address of the object in the memory view.
35+
36+
Returns:
37+
The inflated object.
38+
"""
39+
if not isinstance(address, int) and not isinstance(address, Resolver):
40+
raise TypeError(f"address must be an int or a Resolver, not {type(address).__name__}")
41+
42+
return inflater(memory).inflate(item, address)

0 commit comments

Comments
 (0)