Ticket #11609: base.py

File base.py, 1.2 KB (added by rmkemker, 15 years ago)

django.contrib.gis.gdal.base

Line 
1from ctypes import c_void_p
2from types import NoneType
3from django.contrib.gis.gdal.error import GDALException
4
5class GDALBase(object):
6 """
7 Base object for GDAL objects that has a pointer access property
8 that controls access to the underlying C pointer.
9 """
10 # Initially the pointer is NULL.
11 _ptr = None
12
13 # Default allowed pointer type.
14 ptr_type = c_void_p
15
16 # Pointer access property.
17 def _get_ptr(self):
18 # Raise an exception if the pointer isn't valid don't
19 # want to be passing NULL pointers to routines --
20 # that's very bad.
21 if self._ptr: return self._ptr
22 else: raise GDALException('GDAL %s pointer no longer valid.' % self.__class__.__name__)
23
24 def _set_ptr(self, ptr):
25 # Only allow the pointer to be set with pointers of the
26 # compatible type or None (NULL).
27 if isinstance(ptr, int):
28 self._ptr = self.ptr_type(ptr)
29 elif isinstance(ptr, long):
30 self._ptr = self.ptr_type(int(ptr))
31 elif isinstance(ptr, (self.ptr_type, NoneType)):
32 self._ptr = ptr
33 else:
34 raise TypeError('Incompatible pointer type')
35
36 ptr = property(_get_ptr, _set_ptr)
37
Back to Top