You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

errcheck.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. """
  2. Error checking functions for GEOS ctypes prototype functions.
  3. """
  4. from ctypes import c_void_p, string_at
  5. from django.contrib.gis.geos.error import GEOSException
  6. from django.contrib.gis.geos.libgeos import GEOSFuncFactory
  7. # Getting the `free` routine used to free the memory allocated for
  8. # string pointers returned by GEOS.
  9. free = GEOSFuncFactory('GEOSFree')
  10. free.argtypes = [c_void_p]
  11. def last_arg_byref(args):
  12. "Return the last C argument's value by reference."
  13. return args[-1]._obj.value
  14. def check_dbl(result, func, cargs):
  15. "Check the status code and returns the double value passed in by reference."
  16. # Checking the status code
  17. if result != 1:
  18. return None
  19. # Double passed in by reference, return its value.
  20. return last_arg_byref(cargs)
  21. def check_geom(result, func, cargs):
  22. "Error checking on routines that return Geometries."
  23. if not result:
  24. raise GEOSException('Error encountered checking Geometry returned from GEOS C function "%s".' % func.__name__)
  25. return result
  26. def check_minus_one(result, func, cargs):
  27. "Error checking on routines that should not return -1."
  28. if result == -1:
  29. raise GEOSException('Error encountered in GEOS C function "%s".' % func.__name__)
  30. else:
  31. return result
  32. def check_predicate(result, func, cargs):
  33. "Error checking for unary/binary predicate functions."
  34. if result == 1:
  35. return True
  36. elif result == 0:
  37. return False
  38. else:
  39. raise GEOSException('Error encountered on GEOS C predicate function "%s".' % func.__name__)
  40. def check_sized_string(result, func, cargs):
  41. """
  42. Error checking for routines that return explicitly sized strings.
  43. This frees the memory allocated by GEOS at the result pointer.
  44. """
  45. if not result:
  46. raise GEOSException('Invalid string pointer returned by GEOS C function "%s"' % func.__name__)
  47. # A c_size_t object is passed in by reference for the second
  48. # argument on these routines, and its needed to determine the
  49. # correct size.
  50. s = string_at(result, last_arg_byref(cargs))
  51. # Freeing the memory allocated within GEOS
  52. free(result)
  53. return s
  54. def check_string(result, func, cargs):
  55. """
  56. Error checking for routines that return strings.
  57. This frees the memory allocated by GEOS at the result pointer.
  58. """
  59. if not result:
  60. raise GEOSException('Error encountered checking string return value in GEOS C function "%s".' % func.__name__)
  61. # Getting the string value at the pointer address.
  62. s = string_at(result)
  63. # Freeing the memory allocated within GEOS
  64. free(result)
  65. return s