<s> import threading <EOL> from django . contrib . gis . geos . libgeos import ( <EOL> CONTEXT_PTR , error_h , lgeos , notice_h , <EOL> ) <EOL> class GEOSContextHandle ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . ptr = lgeos . initGEOS_r ( notice_h , error_h ) <EOL> def __del__ ( self ) : <EOL> if self . ptr and lgeos : <EOL> lgeos . finishGEOS_r ( self . ptr ) <EOL> class GEOSContext ( threading . local ) : <EOL> handle = None <EOL> thread_context = GEOSContext ( ) <EOL> class GEOSFunc ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func_name ) : <EOL> try : <EOL> self . cfunc = getattr ( lgeos , func_name + '<STR_LIT>' ) <EOL> self . threaded = True <EOL> self . thread_context = thread_context <EOL> except AttributeError : <EOL> self . cfunc = getattr ( lgeos , func_name ) <EOL> self . threaded = False <EOL> def __call__ ( self , * args ) : <EOL> if self . threaded : <EOL> if not self . thread_context . handle : <EOL> self . thread_context . handle = GEOSContextHandle ( ) <EOL> return self . cfunc ( self . thread_context . handle . ptr , * args ) <EOL> else : <EOL> return self . cfunc ( * args ) <EOL> def __str__ ( self ) : <EOL> return self . cfunc . __name__ <EOL> def _get_argtypes ( self ) : <EOL> return self . cfunc . argtypes <EOL> def _set_argtypes ( self , argtypes ) : <EOL> if self . threaded : <EOL> new_argtypes = [ CONTEXT_PTR ] <EOL> new_argtypes . extend ( argtypes ) <EOL> self . cfunc . argtypes = new_argtypes <EOL> else : <EOL> self . cfunc . argtypes = argtypes <EOL> argtypes = property ( _get_argtypes , _set_argtypes ) <EOL> def _get_restype ( self ) : <EOL> return self . cfunc . restype <EOL> def _set_restype ( self , restype ) : <EOL> self . cfunc . restype = restype <EOL> restype = property ( _get_restype , _set_restype ) <EOL> def _get_errcheck ( self ) : <EOL> return self . cfunc . errcheck <EOL> def _set_errcheck ( self , errcheck ) : <EOL> self . cfunc . errcheck = errcheck <EOL> errcheck = property ( _get_errcheck , _set_errcheck ) </s>
