Debian Codegraph @

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

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