Skip to content

Commit

Permalink
Merge pull request #49 from ionite34/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ionite34 committed Feb 2, 2023
2 parents df9338d + f56cf29 commit 932462e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
project = "einspect"
copyright = "2023, Ionite"
author = "Ionite"
release = "v0.5.6"
release = "v0.5.7"


# -- General configuration ---------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "einspect"
version = "0.5.6"
version = "0.5.7"
packages = [{ include = "einspect", from = "src" }]
description = "Extended Inspect - view and modify memory structs of runtime objects."
authors = ["ionite34 <dev@ionite.io>"]
Expand Down
2 changes: 1 addition & 1 deletion src/einspect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

__all__ = ("view", "unsafe", "impl", "orig", "ptr", "NULL")

__version__ = "0.5.6"
__version__ = "0.5.7"

unsafe: ContextManager[None] = global_unsafe
36 changes: 21 additions & 15 deletions src/einspect/views/view_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,30 @@ def _try_alloc(self, slot: Slot):
new = slot.ptr_type()
ptr.contents = new

def __getitem__(self, key: str):
def __getitem__(self, key: str) -> Any:
"""Get an attribute from the type object."""
return self._pyobject.GetAttr(key)

def __setitem__(self, key: str, value):
"""Set an attribute on the type object."""
# Cache original implementation
base = self.base
if not in_cache(base, key):
if (attr := getattr(base, key, MISSING)) is not MISSING:
add_cache(base, key, attr)
# Check if this is a slots attr
if slot := get_slot(key):
# Allocate sub-struct if needed
self._try_alloc(slot)

with self.as_mutable():
self._pyobject.setattr_safe(key, value)
def __setitem__(self, key: str | tuple[str, ...], value: Any) -> None:
"""
Set attributes on the type object.
Multiple string keys can be used to set multiple attributes to the same value.
"""
keys = (key,) if isinstance(key, str) else key
for k in keys:
# Cache original implementation
base = self.base
if not in_cache(base, k):
if (attr := getattr(base, k, MISSING)) is not MISSING:
add_cache(base, k, attr)
# Check if this is a slots attr
if slot := get_slot(k):
# Allocate sub-struct if needed
self._try_alloc(slot)

with self.as_mutable():
self._pyobject.setattr_safe(k, value)

# <-- Begin Managed::Properties (structs::py_type.PyTypeObject) -->

Expand Down
11 changes: 11 additions & 0 deletions tests/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ def _foo_fn(self, x: int) -> str:
assert (10)._foo_fn(5) == "15"


def test_impl_cache():
@impl(int)
def _foo_fn(self, x: int) -> str:
return str(self + x)

impl(int)(_foo_fn)

# noinspection PyUnresolvedReferences
assert (10)._foo_fn(5) == "15"


def test_impl_new_property():
@impl(int)
@property
Expand Down

0 comments on commit 932462e

Please sign in to comment.