PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/gdal/prototypes/errcheck.py

https://code.google.com/p/mango-py/
Python | 127 lines | 71 code | 14 blank | 42 comment | 16 complexity | 64b0a2a98d8309ecfec9ecbbef2d4a50 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. This module houses the error-checking routines used by the GDAL
  3. ctypes prototypes.
  4. """
  5. from ctypes import c_void_p, string_at
  6. from django.contrib.gis.gdal.error import check_err, OGRException, SRSException
  7. from django.contrib.gis.gdal.libgdal import lgdal
  8. # Helper routines for retrieving pointers and/or values from
  9. # arguments passed in by reference.
  10. def arg_byref(args, offset=-1):
  11. "Returns the pointer argument's by-refernece value."
  12. return args[offset]._obj.value
  13. def ptr_byref(args, offset=-1):
  14. "Returns the pointer argument passed in by-reference."
  15. return args[offset]._obj
  16. def check_bool(result, func, cargs):
  17. "Returns the boolean evaluation of the value."
  18. if bool(result): return True
  19. else: return False
  20. ### String checking Routines ###
  21. def check_const_string(result, func, cargs, offset=None):
  22. """
  23. Similar functionality to `check_string`, but does not free the pointer.
  24. """
  25. if offset:
  26. check_err(result)
  27. ptr = ptr_byref(cargs, offset)
  28. return ptr.value
  29. else:
  30. return result
  31. def check_string(result, func, cargs, offset=-1, str_result=False):
  32. """
  33. Checks the string output returned from the given function, and frees
  34. the string pointer allocated by OGR. The `str_result` keyword
  35. may be used when the result is the string pointer, otherwise
  36. the OGR error code is assumed. The `offset` keyword may be used
  37. to extract the string pointer passed in by-reference at the given
  38. slice offset in the function arguments.
  39. """
  40. if str_result:
  41. # For routines that return a string.
  42. ptr = result
  43. if not ptr: s = None
  44. else: s = string_at(result)
  45. else:
  46. # Error-code return specified.
  47. check_err(result)
  48. ptr = ptr_byref(cargs, offset)
  49. # Getting the string value
  50. s = ptr.value
  51. # Correctly freeing the allocated memory beind GDAL pointer
  52. # w/the VSIFree routine.
  53. if ptr: lgdal.VSIFree(ptr)
  54. return s
  55. ### DataSource, Layer error-checking ###
  56. ### Envelope checking ###
  57. def check_envelope(result, func, cargs, offset=-1):
  58. "Checks a function that returns an OGR Envelope by reference."
  59. env = ptr_byref(cargs, offset)
  60. return env
  61. ### Geometry error-checking routines ###
  62. def check_geom(result, func, cargs):
  63. "Checks a function that returns a geometry."
  64. # OGR_G_Clone may return an integer, even though the
  65. # restype is set to c_void_p
  66. if isinstance(result, (int, long)):
  67. result = c_void_p(result)
  68. if not result:
  69. raise OGRException('Invalid geometry pointer returned from "%s".' % func.__name__)
  70. return result
  71. def check_geom_offset(result, func, cargs, offset=-1):
  72. "Chcks the geometry at the given offset in the C parameter list."
  73. check_err(result)
  74. geom = ptr_byref(cargs, offset=offset)
  75. return check_geom(geom, func, cargs)
  76. ### Spatial Reference error-checking routines ###
  77. def check_srs(result, func, cargs):
  78. if isinstance(result, (int, long)):
  79. result = c_void_p(result)
  80. if not result:
  81. raise SRSException('Invalid spatial reference pointer returned from "%s".' % func.__name__)
  82. return result
  83. ### Other error-checking routines ###
  84. def check_arg_errcode(result, func, cargs):
  85. """
  86. The error code is returned in the last argument, by reference.
  87. Check its value with `check_err` before returning the result.
  88. """
  89. check_err(arg_byref(cargs))
  90. return result
  91. def check_errcode(result, func, cargs):
  92. """
  93. Check the error code returned (c_int).
  94. """
  95. check_err(result)
  96. return
  97. def check_pointer(result, func, cargs):
  98. "Makes sure the result pointer is valid."
  99. if isinstance(result, (int, long)):
  100. result = c_void_p(result)
  101. if bool(result):
  102. return result
  103. else:
  104. raise OGRException('Invalid pointer returned from "%s"' % func.__name__)
  105. def check_str_arg(result, func, cargs):
  106. """
  107. This is for the OSRGet[Angular|Linear]Units functions, which
  108. require that the returned string pointer not be freed. This
  109. returns both the double and tring values.
  110. """
  111. dbl = result
  112. ptr = cargs[-1]._obj
  113. return dbl, ptr.value