Skip to content

Commit

Permalink
Merge pull request #59 from ionite34/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ionite34 committed Feb 20, 2023
2 parents 3ee3420 + 2a46f65 commit bda745a
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 150 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ repos:
hooks:
- id: black

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.245"
hooks:
- id: flake8
additional_dependencies: [flake8-builtins, flake8-print]
- id: ruff
files: ^src/
args:
- --ignore=W503,E203,F842,A003,F403,F405
- --max-line-length=120
- --ignore=F842,A003,F403,F405
- --line-length=120
- --fix
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,59 @@ print("meaning of life") # 42
print("meaning of life" == 42) # True
```

## CPython Struct bindings and API methods
- Easily make calls to CPython stable ABI (`ctypes.pythonapi`) as bound methods on `PyObject` instances.
```python
from einspect.structs import PyDictObject

d = {"a": (1, 2), "b": (3, 4)}

res = PyDictObject(d).GetItem("a")

if res:
print(res.contents.NewRef())
```
> Equivalent to the following with ctypes:
```python
from ctypes import pythonapi, py_object, c_void_p, cast

d = {"a": (1, 2), "b": (3, 4)}

PyDict_GetItem = pythonapi["PyDict_GetItem"]
# Can't use auto cast py_object for restype,
# since missing keys return NULL and causes segmentation fault with no set error
PyDict_GetItem.restype = c_void_p
PyDict_GetItem.argtypes = [py_object, py_object]

res = PyDict_GetItem(d, "a")
res = cast(res, py_object)

Py_NewRef = pythonapi["Py_NewRef"]
Py_NewRef.restype = py_object
Py_NewRef.argtypes = [py_object]

try:
print(Py_NewRef(res.value))
except ValueError:
pass
```

- Create new instances of PyObject structs with field values, from existing objects, or from address.
```python
from einspect.structs import PyLongObject, PyTypeObject

x = PyLongObject(
ob_refcnt=1,
ob_type=PyTypeObject(int).as_ref(),
ob_size=1,
ob_item=[15],
).into_object()

print(x) # 15
print(x == 15) # True
print(x is 15) # False
```

<!-- end intro -->

## Fully typed interface
Expand Down
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.11"
release = "v0.5.12"


# -- General configuration ---------------------------------------------------
Expand Down
143 changes: 51 additions & 92 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "einspect"
version = "0.5.11"
version = "0.5.12"
packages = [{ include = "einspect", from = "src" }]
description = "Extended Inspect - view and modify memory structs of runtime objects."
authors = ["ionite34 <dev@ionite.io>"]
Expand Down Expand Up @@ -47,9 +47,8 @@ pytest-cov = "^4.0.0"
pytest-xdist = "^3.1.0"
mypy = "^0.991"
pre-commit = "^2.20.0"
flake8 = { version = "^6.0.0", python = ">= 3.8.1" }
flake8-print = { version = "^5.0.0", python = ">= 3.8.1" }
black = "^23.1.0"
ruff = "^0.0.247"

[tool.poetry.group.tools.dependencies]
rich = "^12.6.0"
Expand All @@ -73,6 +72,11 @@ sphinx-copybutton = "^0.5.1"
furo = "^2022.12.7"
sphinx-autodoc-typehints = "^1.21.8"

[tool.ruff]
ignore = ["F842", "A003", "F403", "F405"]
line-length = 120
fixable = ["F"]

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
Expand Down
Loading

0 comments on commit bda745a

Please sign in to comment.