Skip to content

Commit

Permalink
i think im done
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroIntensity committed Oct 15, 2022
1 parent 79fb7ab commit 1189bac
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 46 deletions.
27 changes: 0 additions & 27 deletions .github/workflows/lint.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Why would you ever need this

- [Documentation](https://pointerspy.netlify.app/)
- [Documentation](https://pointers.zintensity.dev/)
- [Source](https://github.com/ZeroIntensity/pointers.py)
- [PyPI](https://pypi.org/project/pointers.py)

Expand Down
6 changes: 3 additions & 3 deletions docs/using_pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def my_function(a: str, b: str, c: Pointer): # must be type hinted as Pointer t
my_function('a', 'b', 'c')
```

This will be fine for most people, but it hsa removes type safety on the target function. If you don't care about type safety in your code, then don't worry about this, but if you do, then there are alternatives.
This will be fine for most people, but it removes type safety on the target function. If you don't care about type safety in your code, then don't worry about this, but if you do, then there are alternatives.

The first alternative is `decay_annotated`, which decays parameters hinted as `Annotated[T, Pointer]` to a pointer.

Expand Down Expand Up @@ -110,7 +110,7 @@ def my_function(a: str, b: str, c: Annotated[str, Pointer]):
print(a, b, ~c) # type checker error!
```

The solution is to use `decay_wrapped`, which takes the caller function as a paremeter:
The solution is to use `decay_wrapped`, which takes a fake function as a parameter:

```py
from pointers import decay_wrapped, Pointer
Expand All @@ -126,7 +126,7 @@ def my_function(a: str, b: str, c: Pointer[str]):
my_function('a', 'b', 'c') # works just fine, type checker things c takes a string
```

It can be broken pretty easily though:
If the wrapper doesn't match, things won't work properly:

```py
from pointers import decay_wrapped, Pointer
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers = [
dependencies = [
"typing_extensions",
]
version = "2.2.0-beta"
version = "2.2.0"

[project.urls]
Documentation = "https://pointers.zintensity.dev"
Expand Down
3 changes: 1 addition & 2 deletions src/mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ static PyObject* handle(PyObject* self, PyObject* args) {

PyErr_Format(
PyExc_RuntimeError,
"%s occured during execution of %S",
val == 1 ? "segment violation" : "python aborted",
"segment violation occured during execution of %S",
name
);

Expand Down
4 changes: 2 additions & 2 deletions src/pointers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
TypedCPointer, VoidPointer, array, cast, to_c_ptr, to_struct_ptr, to_voidp
)
from .calloc import AllocatedArrayPointer, calloc
from .constants import NULL, Nullable, handle, raw_type, struct_cast
from .custom_binding import binding, binds
from .decay import decay, decay_annotated, decay_wrapped
from .exceptions import (
Expand All @@ -28,6 +27,7 @@
from .object_pointer import Pointer, to_ptr
from .std_structs import DivT, Lconv, LDivT, Tm
from .structure import Struct, StructPointer
from .util import NULL, Nullable, handle, raw_type, struct_cast

__version__ = "2.2.0-beta"
__version__ = "2.2.0"
__license__ = "MIT"
2 changes: 1 addition & 1 deletion src/pointers/base_pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from _pointers import add_ref, remove_ref

from ._utils import deref, force_set_attr, move_to_mem
from .constants import NULL, Nullable, handle
from .exceptions import DereferenceError, FreedMemoryError, NullPointerError
from .util import NULL, Nullable, handle

__all__ = (
"BasePointer",
Expand Down
11 changes: 8 additions & 3 deletions src/pointers/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
from .exceptions import InvalidBindingParameter
from .std_structs import STRUCT_MAP, DivT, Lconv, LDivT, Tm
from .structure import StructPointer
from .util import handle

if TYPE_CHECKING:
from .structure import Struct

T = TypeVar("T")

PointerLike = Union[TypedCPointer[T], VoidPointer, None]
StringLike = Union[str, bytes, VoidPointer, TypedCPointer[bytes]]
PointerLike = Optional[Union[TypedCPointer[T], VoidPointer]]
StringLike = Optional[Union[str, bytes, VoidPointer, TypedCPointer[bytes]]]
Format = Union[StringLike, PointerLike]
TypedPtr = Optional[PointerLike[T]]
PyCFuncPtrType = type(ctypes.CFUNCTYPE(None))
Expand Down Expand Up @@ -330,6 +331,7 @@ def wrapper(*args):
return _CFuncTransport(wrapper, fn)


@handle
def binding_base(
fn: "ctypes._NamedFuncPointer",
*args,
Expand Down Expand Up @@ -380,7 +382,7 @@ def binding_base(


def make_string(data: StringLike) -> Union[bytes, ctypes.c_char_p]:
if type(data) not in {VoidPointer, str, bytes, TypedCPointer}:
if (type(data) not in {VoidPointer, str, bytes, TypedCPointer}) and data:
raise InvalidBindingParameter(
f"expected a string-like object, got {repr(data)}" # noqa
)
Expand All @@ -404,6 +406,9 @@ def make_string(data: StringLike) -> Union[bytes, ctypes.c_char_p]:
if isinstance(data, str):
return data.encode()

if not data:
data = ctypes.c_char_p(None) # type: ignore

assert isinstance(data, ctypes.c_char_p), f"{data} is not a char*"
return data

Expand Down
2 changes: 1 addition & 1 deletion src/pointers/c_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from ._utils import get_mapped, map_type
from .base_pointers import BaseCPointer, IterDereferencable, Typed
from .constants import handle
from .util import handle

if TYPE_CHECKING:
from .structure import Struct, StructPointer
Expand Down
2 changes: 1 addition & 1 deletion src/pointers/calloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from ._cstd import c_calloc, c_free
from .base_pointers import BaseAllocatedPointer
from .constants import handle
from .exceptions import AllocationError
from .util import handle

__all__ = ("AllocatedArrayPointer", "calloc")

Expand Down
2 changes: 1 addition & 1 deletion src/pointers/malloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from ._cstd import c_free, c_malloc, c_realloc
from .base_pointers import BaseAllocatedPointer, IterDereferencable
from .constants import handle
from .exceptions import AllocationError, InvalidSizeError
from .util import handle

__all__ = ("AllocatedPointer", "malloc", "free", "realloc")

Expand Down
2 changes: 1 addition & 1 deletion src/pointers/object_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from _pointers import add_ref, remove_ref, set_ref

from .base_pointers import NULL, BaseObjectPointer, BasePointer, Nullable
from .constants import handle
from .exceptions import InvalidSizeError
from .util import handle

T = TypeVar("T")

Expand Down
2 changes: 1 addition & 1 deletion src/pointers/std_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
PyVarObject
)
from .c_pointer import TypedCPointer, VoidPointer
from .constants import raw_type
from .structure import Struct, StructPointer
from .util import raw_type

__all__ = (
"Tm",
Expand Down
2 changes: 1 addition & 1 deletion src/pointers/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from ._utils import attempt_decode, get_mapped, get_py
from .base_pointers import BaseCPointer
from .c_pointer import TypedCPointer, VoidPointer
from .constants import RawType, handle
from .object_pointer import Pointer
from .util import RawType, handle

T = TypeVar("T", bound="Struct")

Expand Down
File renamed without changes.

0 comments on commit 1189bac

Please sign in to comment.