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.
 
 
 
 

39 lines
1.3 KiB

  1. from ctypes import c_void_p
  2. from django.contrib.gis.geos.error import GEOSException
  3. class GEOSBase(object):
  4. """
  5. Base object for GEOS objects that has a pointer access property
  6. that controls access to the underlying C pointer.
  7. """
  8. # Initially the pointer is NULL.
  9. _ptr = None
  10. # Default allowed pointer type.
  11. ptr_type = c_void_p
  12. # Pointer access property.
  13. def _get_ptr(self):
  14. # Raise an exception if the pointer isn't valid don't
  15. # want to be passing NULL pointers to routines --
  16. # that's very bad.
  17. if self._ptr:
  18. return self._ptr
  19. else:
  20. raise GEOSException('NULL GEOS %s pointer encountered.' % self.__class__.__name__)
  21. def _set_ptr(self, ptr):
  22. # Only allow the pointer to be set with pointers of the
  23. # compatible type or None (NULL).
  24. if ptr is None or isinstance(ptr, self.ptr_type):
  25. self._ptr = ptr
  26. else:
  27. raise TypeError('Incompatible pointer type')
  28. # Property for controlling access to the GEOS object pointers. Using
  29. # this raises an exception when the pointer is NULL, thus preventing
  30. # the C library from attempting to access an invalid memory location.
  31. ptr = property(_get_ptr, _set_ptr)