PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://code.google.com/p/mango-py/
Python | 95 lines | 57 code | 9 blank | 29 comment | 12 complexity | c409a1ac9a93856901dcf96b73aff7ef MD5 | raw file
 1"""
 2 Error checking functions for GEOS ctypes prototype functions.
 3"""
 4import os
 5from ctypes import c_void_p, string_at, CDLL
 6from django.contrib.gis.geos.error import GEOSException
 7from django.contrib.gis.geos.libgeos import GEOS_VERSION
 8from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc
 9
10# Getting the `free` routine used to free the memory allocated for
11# string pointers returned by GEOS.
12if GEOS_VERSION >= (3, 1, 1):
13    # In versions 3.1.1 and above, `GEOSFree` was added to the C API
14    # because `free` isn't always available on all platforms.
15    free = GEOSFunc('GEOSFree')
16    free.argtypes = [c_void_p]
17    free.restype = None
18else:
19    # Getting the `free` routine from the C library of the platform.
20    if os.name == 'nt':
21        # On NT, use the MS C library.
22        libc = CDLL('msvcrt')
23    else:
24        # On POSIX platforms C library is obtained by passing None into `CDLL`.
25        libc = CDLL(None)
26    free = libc.free
27
28### ctypes error checking routines ###
29def last_arg_byref(args):
30    "Returns the last C argument's value by reference."
31    return args[-1]._obj.value
32
33def check_dbl(result, func, cargs):
34    "Checks the status code and returns the double value passed in by reference."
35    # Checking the status code
36    if result != 1: return None
37    # Double passed in by reference, return its value.
38    return last_arg_byref(cargs)
39
40def check_geom(result, func, cargs):
41    "Error checking on routines that return Geometries."
42    if not result:
43        raise GEOSException('Error encountered checking Geometry returned from GEOS C function "%s".' % func.__name__)
44    return result
45
46def check_minus_one(result, func, cargs):
47    "Error checking on routines that should not return -1."
48    if result == -1:
49        raise GEOSException('Error encountered in GEOS C function "%s".' % func.__name__)
50    else:
51        return result
52
53def check_predicate(result, func, cargs):
54    "Error checking for unary/binary predicate functions."
55    val = ord(result) # getting the ordinal from the character
56    if val == 1: return True
57    elif val == 0: return False
58    else:
59        raise GEOSException('Error encountered on GEOS C predicate function "%s".' % func.__name__)
60
61def check_sized_string(result, func, cargs):
62    """
63    Error checking for routines that return explicitly sized strings.
64
65    This frees the memory allocated by GEOS at the result pointer.
66    """
67    if not result:
68        raise GEOSException('Invalid string pointer returned by GEOS C function "%s"' % func.__name__)
69    # A c_size_t object is passed in by reference for the second
70    # argument on these routines, and its needed to determine the
71    # correct size.
72    s = string_at(result, last_arg_byref(cargs))
73    # Freeing the memory allocated within GEOS
74    free(result)
75    return s
76
77def check_string(result, func, cargs):
78    """
79    Error checking for routines that return strings.
80
81    This frees the memory allocated by GEOS at the result pointer.
82    """
83    if not result: raise GEOSException('Error encountered checking string return value in GEOS C function "%s".' % func.__name__)
84    # Getting the string value at the pointer address.
85    s = string_at(result)
86    # Freeing the memory allocated within GEOS
87    free(result)
88    return s
89
90def check_zero(result, func, cargs):
91    "Error checking on routines that should not return 0."
92    if result == 0:
93        raise GEOSException('Error encountered in GEOS C function "%s".' % func.__name__)
94    else:
95        return result