-
Notifications
You must be signed in to change notification settings - Fork 93
Add 'bool' to ctypes type map, needed for C23 #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks, I've also pulled this into pypdfium2-ctypesgen. |
Great! (I don't dare make any explicit promises, due to time restraints, but I haven't given up on bringing back your work here!) |
|
Yes, I'd really appreciate my branch eventually being merged here! But before that, I figured there would be a few features that I might have to add back (binding to multiple DLLs in the same file, and mixed calling convention (cdecl/stdcall)). |
|
Perhaps the major break, that I noticed, was the drop of the "String" wrapper (part of the preamble). |
|
Hmm, I can see the issue with existing public APIs, but I've been opposed to implicit string encoding/decoding, and the big wrapper classes in particular. Technically, the expected encoding is specific to each C API, so one should really review the documentation in question and encode explicitly on the caller side IMHO. Also, an important note is that not all Perhaps best to make a new major release and mandate that all callers encode/decode their strings explicitly. |
|
That said, maybe we could add an opt-in, leaner template based on I once made a draft here: pypdfium2-team#1 (comment) |
|
Re-reading the draft, I think the |
|
How about something like this (disclaimer: untested): import sys
import ctypes
import functools
if sys.version_info < (3, 8):
# NOTE alternatively, we could write our own cached property backport with python's descriptor protocol
def cached_property(func):
return property( functools.lru_cache(maxsize=1)(func) )
else:
cached_property = functools.cached_property
DEFAULT_ENCODING = "utf-8"
class _wraps_c_char_p:
def __init__(self, ptr):
self.ptr = ptr
@cached_property
def raw(self):
return self.ptr.value
@cached_property
def decoded(self):
if self.raw is None:
raise RuntimeError("Null pointer cannot be decoded")
return self.raw.decode(DEFAULT_ENCODING)
def __str__(self):
return self.decoded
def __getattr__(self, attr):
return getattr(self.decoded, attr)
def __eq__(self, other):
if type(self) is type(other):
return self is other or self.raw == other.raw
elif isinstance(other, str):
return self.decoded == other
else:
return self.raw == other
class String (ctypes.c_char_p):
@classmethod
def _check_retval_(cls, result):
return _wraps_c_char_p(result)
@classmethod
def from_param(cls, obj):
if isinstance(obj, str):
obj = obj.encode(DEFAULT_ENCODING)
return super().from_param(obj)Update: fixes, eq method added |
|
OK, I've basically implemented this in the fork/branch: pypdfium2-team@3bcd9e4 Testing/feedback is appreciated! |
Add
boolto ctypes type map. This change is needed for C23 support, where_Boolis not necessarily defined by default.See e.g., GCC:
GCC 15 now defaults to C23.
Closes #173