Ticket #9747: geos_explicit_char_restype.diff

File geos_explicit_char_restype.diff, 1.9 KB (added by jbronn, 7 years ago)

Uses a subclass of c_char_p for the response type.

  • django/contrib/gis/geos/prototypes/geom.py

    1 from ctypes import c_char_p, c_int, c_size_t, c_uint, POINTER
     1from ctypes import c_char_p, c_int, c_size_t, c_ubyte, c_uint, POINTER
    22from django.contrib.gis.geos.libgeos import lgeos, CS_PTR, GEOM_PTR
    33from django.contrib.gis.geos.prototypes.errcheck import \
    44    check_geom, check_minus_one, check_sized_string, check_string, check_zero
     6# This is the return type used by binary output (WKB, HEX) routines.
     7c_uchar_p = POINTER(c_ubyte)
     9# We create a simple subclass of c_char_p here because when the response
     10# type is set to c_char_p, you get a _Python_ string and there's no way
     11# to access the string's address inside the error checking function.
     12# In other words, you can't free the memory allocated inside GEOS.  Previously,
     13# the return type would just be omitted and the integer address would be
     14# used -- but this allows us to be specific in the function definition and
     15# keeps the reference so it may be free'd.
     16class geos_char_p(c_char_p):
     17    pass
    619### ctypes generation functions ###
    720def bin_constructor(func):
    821    "Generates a prototype for binary construction (HEX, WKB) GEOS routines."
    1629    "Generates a prototype for the routines that return a a sized string."
    1730    func.argtypes = [GEOM_PTR, POINTER(c_size_t)]
    1831    func.errcheck = check_sized_string
     32    func.restype = c_uchar_p
    1933    return func
    2135def geom_output(func, argtypes):
    4458    # We do _not_ specify an argument type because we want just an
    4559    # address returned from the function.
    4660    func.argtypes = [GEOM_PTR]
     61    func.restype = geos_char_p
    4762    func.errcheck = check_string
    4863    return func
Back to Top