Code

Ticket #13488: 13488.1.diff

File 13488.1.diff, 1.9 KB (added by jbronn, 3 years ago)

Create reference to thread_context in GEOSFunc instances.

Line 
1diff -r 7f798c38beea django/contrib/gis/geos/prototypes/threadsafe.py
2--- a/django/contrib/gis/geos/prototypes/threadsafe.py  Wed Dec 22 17:43:30 2010 +0000
3+++ b/django/contrib/gis/geos/prototypes/threadsafe.py  Wed Dec 22 14:37:04 2010 -0600
4@@ -20,18 +20,6 @@
5 
6 thread_context = GEOSContext()
7 
8-def call_geos_threaded(cfunc, args):
9-    """
10-    This module-level routine calls the specified GEOS C thread-safe
11-    function with the context for this current thread.
12-    """
13-    # If a context handle does not exist for this thread, initialize one.
14-    if not thread_context.handle:
15-        thread_context.handle = GEOSContextHandle()
16-    # Call the threaded GEOS routine with pointer of the context handle
17-    # as the first argument.
18-    return cfunc(thread_context.handle.ptr, *args)
19-
20 class GEOSFunc(object):
21     """
22     Class that serves as a wrapper for GEOS C Functions, and will
23@@ -43,6 +31,9 @@
24             # take an additional context handle parameter.
25             self.cfunc = getattr(lgeos, func_name + '_r')
26             self.threaded = True
27+            # Create a reference here to thread_context so it's not
28+            # garbage-collected before an attempt to call this object.
29+            self.thread_context = thread_context
30         except AttributeError:
31             # Otherwise, use usual function.
32             self.cfunc = getattr(lgeos, func_name)
33@@ -50,7 +41,12 @@
34 
35     def __call__(self, *args):
36         if self.threaded:
37-            return call_geos_threaded(self.cfunc, args)
38+            # If a context handle does not exist for this thread, initialize one.
39+            if not self.thread_context.handle:
40+                self.thread_context.handle = GEOSContextHandle()
41+            # Call the threaded GEOS routine with pointer of the context handle
42+            # as the first argument.
43+            return self.cfunc(self.thread_context.handle.ptr, *args)
44         else:
45             return self.cfunc(*args)
46