Skip to content

Commit

Permalink
Simplify ptrdiff getter, move tasks out of actual preamble
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Jan 27, 2024
1 parent 01e6c95 commit 683aadb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
21 changes: 4 additions & 17 deletions src/ctypesgen/printer_python/preamble.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
# TODO
# - add c_ptrdiff_t only on as-needed basis
# - Avoid ctypes glob import (pollutes namespace)

import ctypes
from ctypes import * # noqa: F401, F403

def _get_ptrdiff_t():

int_types = (ctypes.c_int16, ctypes.c_int32)
int_types = [ctypes.c_int16, ctypes.c_int32]
# Some builds of ctypes do not provide c_int64. Assumably, this means the platform doesn't have 64-bit pointers.
if hasattr(ctypes, "c_int64"):
# Some builds of ctypes apparently do not have ctypes.c_int64
# defined; it's a pretty good bet that these builds do not
# have 64-bit pointers.
int_types += (ctypes.c_int64,)

c_ptrdiff_t = None
for t in int_types:
if ctypes.sizeof(t) == ctypes.sizeof(ctypes.c_size_t):
c_ptrdiff_t = t

return c_ptrdiff_t
int_types.append(ctypes.c_int64)
return next((t for t in int_types if sizeof(t) == sizeof(c_size_t)), None)

c_ptrdiff_t = _get_ptrdiff_t()
4 changes: 3 additions & 1 deletion src/ctypesgen/printer_python/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
DEFAULTHEADER_PATH = THIS_DIR/"defaultheader.py"
LIBRARYLOADER_PATH = CTYPESGEN_DIR/"libraryloader.py"


# Concerning newlines handling, please read docs/dev_comments.md

def ParagraphCtxFactory(file):
Expand Down Expand Up @@ -107,6 +106,9 @@ def print_info(self, argv):


def print_templates(self, opts, outpath):
# TODO(preamble)
# - Perspectively, we'd like to add individual templates only on as-needed basis.
# - The ctypes * import is somewhat unfortunate for the namespace. On the other hand, prefixing would make the output bigger and less readable.
if opts.embed_preamble:
self._embed_file(PREAMBLE_PATH, "preamble")
if opts.library:
Expand Down

0 comments on commit 683aadb

Please sign in to comment.