diff --git a/django/contrib/gis/geos/libgeos.py b/django/contrib/gis/geos/libgeos.py
index feb225cf8c..7a5b11bdd0 100644
|
a
|
b
|
def error_h(fmt, lst):
|
| 97 | 97 | err_msg = fmt % lst |
| 98 | 98 | except TypeError: |
| 99 | 99 | err_msg = fmt |
| 100 | | logger.error("GEOS_ERROR: %s\n", err_msg) |
| | 100 | from django.contrib.gis.geos.prototypes.threadsafe import thread_context |
| | 101 | thread_context.last_error = err_msg |
| 101 | 102 | |
| 102 | 103 | |
| 103 | 104 | error_h = ERRORFUNC(error_h) |
diff --git a/django/contrib/gis/geos/prototypes/errcheck.py b/django/contrib/gis/geos/prototypes/errcheck.py
index 044bf8bc5c..18839983a9 100644
|
a
|
b
|
from ctypes import c_void_p, string_at
|
| 6 | 6 | |
| 7 | 7 | from django.contrib.gis.geos.error import GEOSException |
| 8 | 8 | from django.contrib.gis.geos.libgeos import GEOSFuncFactory |
| | 9 | from django.contrib.gis.geos.prototypes.threadsafe import thread_context |
| 9 | 10 | |
| 10 | 11 | # Getting the `free` routine used to free the memory allocated for |
| 11 | 12 | # string pointers returned by GEOS. |
| … |
… |
def check_dbl(result, func, cargs):
|
| 24 | 25 | """ |
| 25 | 26 | # Checking the status code |
| 26 | 27 | if result != 1: |
| | 28 | thread_context.last_error = None |
| 27 | 29 | return None |
| 28 | 30 | # Double passed in by reference, return its value. |
| | 31 | thread_context.last_error = None |
| 29 | 32 | return last_arg_byref(cargs) |
| 30 | 33 | |
| 31 | 34 | |
| 32 | 35 | def check_geom(result, func, cargs): |
| 33 | 36 | "Error checking on routines that return Geometries." |
| 34 | 37 | if not result: |
| 35 | | raise GEOSException( |
| 36 | | 'Error encountered checking Geometry returned from GEOS C function "%s".' |
| 37 | | % func.__name__ |
| | 38 | geos_msg = thread_context.last_error |
| | 39 | thread_context.last_error = None |
| | 40 | |
| | 41 | error_msg = ( |
| | 42 | f'Error encountered checking Geometry returned from GEOS C function ' |
| | 43 | f'"{func.__name__}".' |
| 38 | 44 | ) |
| | 45 | if geos_msg: |
| | 46 | error_msg = f"{error_msg} {geos_msg}" |
| | 47 | raise GEOSException(error_msg) |
| | 48 | thread_context.last_error = None |
| 39 | 49 | return result |
| 40 | 50 | |
| 41 | 51 | |
| 42 | 52 | def check_minus_one(result, func, cargs): |
| 43 | 53 | "Error checking on routines that should not return -1." |
| 44 | 54 | if result == -1: |
| 45 | | raise GEOSException( |
| 46 | | 'Error encountered in GEOS C function "%s".' % func.__name__ |
| 47 | | ) |
| | 55 | geos_msg = thread_context.last_error |
| | 56 | thread_context.last_error = None |
| | 57 | |
| | 58 | error_msg = f'Error encountered in GEOS C function "{func.__name__}".' |
| | 59 | if geos_msg: |
| | 60 | error_msg = f"{error_msg} {geos_msg}" |
| | 61 | raise GEOSException(error_msg) |
| 48 | 62 | else: |
| | 63 | thread_context.last_error = None |
| 49 | 64 | return result |
| 50 | 65 | |
| 51 | 66 | |
| 52 | 67 | def check_predicate(result, func, cargs): |
| 53 | 68 | "Error checking for unary/binary predicate functions." |
| 54 | 69 | if result == 1: |
| | 70 | thread_context.last_error = None |
| 55 | 71 | return True |
| 56 | 72 | elif result == 0: |
| | 73 | thread_context.last_error = None |
| 57 | 74 | return False |
| 58 | 75 | else: |
| 59 | | raise GEOSException( |
| 60 | | 'Error encountered on GEOS C predicate function "%s".' % func.__name__ |
| 61 | | ) |
| | 76 | geos_msg = thread_context.last_error |
| | 77 | thread_context.last_error = None |
| | 78 | |
| | 79 | error_msg = f'Error encountered on GEOS C predicate function "{func.__name__}".' |
| | 80 | if geos_msg: |
| | 81 | error_msg = f"{error_msg} {geos_msg}" |
| | 82 | raise GEOSException(error_msg) |
| 62 | 83 | |
| 63 | 84 | |
| 64 | 85 | def check_sized_string(result, func, cargs): |
| 65 | 86 | """ |
| 66 | 87 | Error checking for routines that return explicitly sized strings. |
| 67 | 88 | |
| 68 | 89 | This frees the memory allocated by GEOS at the result pointer. |
| 69 | 90 | """ |
| 70 | 91 | if not result: |
| 71 | | raise GEOSException( |
| 72 | | 'Invalid string pointer returned by GEOS C function "%s"' % func.__name__ |
| 73 | | ) |
| | 92 | geos_msg = thread_context.last_error |
| | 93 | thread_context.last_error = None |
| | 94 | |
| | 95 | error_msg = f'Invalid string pointer returned by GEOS C function "{func.__name__}"' |
| | 96 | if geos_msg: |
| | 97 | error_msg = f"{error_msg} {geos_msg}" |
| | 98 | raise GEOSException(error_msg) |
| 74 | 99 | # A c_size_t object is passed in by reference for the second |
| 75 | 100 | # argument on these routines, and its needed to determine the |
| 76 | 101 | # correct size. |
| 77 | 102 | s = string_at(result, last_arg_byref(cargs)) |
| 78 | 103 | # Freeing the memory allocated within GEOS |
| 79 | 104 | free(result) |
| | 105 | thread_context.last_error = None |
| 80 | 106 | return s |
| 81 | 107 | |
| 82 | 108 | |
| … |
… |
def check_string(result, func, cargs):
|
| 87 | 113 | This frees the memory allocated by GEOS at the result pointer. |
| 88 | 114 | """ |
| 89 | 115 | if not result: |
| 90 | | raise GEOSException( |
| 91 | | 'Error encountered checking string return value in GEOS C function "%s".' |
| 92 | | % func.__name__ |
| | 116 | geos_msg = thread_context.last_error |
| | 117 | thread_context.last_error = None |
| | 118 | |
| | 119 | error_msg = ( |
| | 120 | f'Error encountered checking string return value in GEOS C function ' |
| | 121 | f'"{func.__name__}".' |
| 93 | 122 | ) |
| | 123 | if geos_msg: |
| | 124 | error_msg = f"{error_msg} {geos_msg}" |
| | 125 | raise GEOSException(error_msg) |
| 94 | 126 | # Getting the string value at the pointer address. |
| 95 | 127 | s = string_at(result) |
| 96 | 128 | # Freeing the memory allocated within GEOS |
| 97 | 129 | free(result) |
| | 130 | thread_context.last_error = None |
| 98 | 131 | return s |
diff --git a/django/contrib/gis/geos/prototypes/threadsafe.py b/django/contrib/gis/geos/prototypes/threadsafe.py
index d4f7ffb8ac..eedfc3f32a 100644
|
a
|
b
|
class GEOSContextHandle(GEOSBase):
|
| 20 | 20 | # to hold a reference to GEOSContextHandle for this thread. |
| 21 | 21 | class GEOSContext(threading.local): |
| 22 | 22 | handle = None |
| | 23 | last_error = None |
| 23 | 24 | |
| 24 | 25 | |
| 25 | 26 | thread_context = GEOSContext() |
diff --git a/tests/gis_tests/data/geometries.json b/tests/gis_tests/data/geometries.json
index 6856ac793a..a66ab14843 100644
|
a
|
b
|
|
| 13 | 13 | "errors": [ |
| 14 | 14 | {"wkt": "GEOMETR##!@#%#............a32515", "bad": true, "hex": false, "msg": "String input unrecognized as WKT EWKT, and HEXEWKB."}, |
| 15 | 15 | {"wkt": "Foo.Bar", "bad": true, "hex": false, "msg": "String input unrecognized as WKT EWKT, and HEXEWKB."}, |
| 16 | | {"wkt": "POINT (5, 23)", "bad": true, "hex": false, "msg": "Error encountered checking Geometry returned from GEOS C function \"GEOSWKTReader_read_r\"."}, |
| | 16 | {"wkt": "POINT (5, 23)", "bad": true, "hex": false, "msg": "Error encountered checking Geometry returned from GEOS C function \"GEOSWKTReader_read_r\". ParseException: Expected number but encountered ','"}, |
| 17 | 17 | {"wkt": "AAABBBDDDAAD##@#1113511111-098111111111111111533333333333333", "bad": true, "hex": true, "msg": "String input unrecognized as WKT EWKT, and HEXEWKB."}, |
| 18 | | {"wkt": "FFFFFFFFFFFFFFFFF1355555555555555555565111", "bad": true, "hex": true, "msg": "Error encountered checking Geometry returned from GEOS C function \"GEOSWKBReader_readHEX_r\"."}, |
| | 18 | {"wkt": "FFFFFFFFFFFFFFFFF1355555555555555555565111", "bad": true, "hex": true, "msg": "Error encountered checking Geometry returned from GEOS C function \"GEOSWKBReader_readHEX_r\". ParseException: Unknown WKB type 535"}, |
| 19 | 19 | {"wkt": "", "bad": true, "hex": false, "msg": "String input unrecognized as WKT EWKT, and HEXEWKB."} |
| 20 | 20 | ], |
| 21 | 21 | "wkt_out": [ |