Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/gis/tests/lib_gis_ctypes_void_pointer_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Tests of the ctypes preamble shared by the grass.lib modules

POINTER(None) in the generated modules is ctypes.c_void_p itself, so the
preamble must not replace its from_param: that would change every void pointer
argument in the process, including those of other packages.
"""

import ctypes
import sys

import pytest

import grass.lib.gis # noqa: F401 # the import is what is being tested


def test_void_pointer_converts_integers():
"""After importing grass.lib, c_void_p still converts an integer address"""
assert not isinstance(ctypes.c_void_p.from_param(2**40), int)


@pytest.mark.skipif(sys.platform != "win32", reason="C long is 32 bits on Windows")
def test_void_pointer_argument_takes_a_64_bit_address():
"""A void pointer argument declared after the import takes any address"""
memmove = ctypes.CDLL("msvcrt").memmove
memmove.argtypes = (ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t)
memmove.restype = ctypes.c_void_p
source = ctypes.create_string_buffer(b"grass")
target = ctypes.create_string_buffer(len(source))
memmove(ctypes.addressof(target), ctypes.addressof(source), len(source))
assert target.value == b"grass"
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ def _copy_preamble_loader_files(self, path):
pointer = """def POINTER(obj):
p = ctypes.POINTER(obj)

# POINTER(None) is ctypes.c_void_p itself, which the whole process shares,
# and its own from_param already turns None into NULL
if p is ctypes.c_void_p:
return p

# Convert None to a real NULL pointer to work around bugs
# in how ctypes handles None on 64-bit platforms
if not isinstance(p.from_param, classmethod):
Expand Down
Loading