<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division , absolute_import <EOL> from collections import defaultdict <EOL> import itertools <EOL> import sys <EOL> from types import ModuleType <EOL> from . import six , types <EOL> def transform_arg_name ( arg ) : <EOL> if isinstance ( arg , types . Record ) : <EOL> return "<STR_LIT>" % arg . _code <EOL> elif ( isinstance ( arg , types . Array ) and <EOL> isinstance ( arg . dtype , types . Record ) ) : <EOL> type_name = "<STR_LIT>" if arg . mutable else "<STR_LIT>" <EOL> return ( "<STR_LIT>" <EOL> % ( type_name , arg . dtype . _code , arg . ndim , arg . layout ) ) <EOL> else : <EOL> return str ( arg ) <EOL> def default_mangler ( name , argtypes ) : <EOL> codedargs = '<STR_LIT:.>' . join ( transform_arg_name ( a ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> for a in argtypes ) <EOL> return '<STR_LIT:.>' . join ( [ name , codedargs ] ) <EOL> _dynamic_modname = '<STR_LIT>' <EOL> _dynamic_module = ModuleType ( _dynamic_modname ) <EOL> _dynamic_module . __builtins__ = six . moves . builtins <EOL> class FunctionDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:args>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> _unique_ids = itertools . count ( <NUM_LIT:1> ) <EOL> def __init__ ( self , native , modname , qualname , unique_name , doc , <EOL> typemap , restype , calltypes , args , kws , mangler = None , <EOL> argtypes = None , inline = False ) : <EOL> self . native = native <EOL> self . modname = modname <EOL> self . qualname = qualname <EOL> self . unique_name = unique_name <EOL> self . doc = doc <EOL> self . typemap = typemap <EOL> self . calltypes = calltypes <EOL> self . args = args <EOL> self . kws = kws <EOL> self . restype = restype <EOL> if argtypes is not None : <EOL> assert isinstance ( argtypes , tuple ) , argtypes <EOL> self . argtypes = argtypes <EOL> else : <EOL> self . argtypes = tuple ( self . typemap [ '<STR_LIT>' + a ] for a in args ) <EOL> mangler = default_mangler if mangler is None else mangler <EOL> if self . modname : <EOL> self . mangled_name = mangler ( '<STR_LIT>' % ( self . modname , self . unique_name ) , <EOL> self . argtypes ) <EOL> else : <EOL> self . mangled_name = mangler ( self . unique_name , self . argtypes ) <EOL> self . inline = inline <EOL> def lookup_module ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . modname == _dynamic_modname : <EOL> return _dynamic_module <EOL> else : <EOL> return sys . modules [ self . modname ] <EOL> def lookup_function ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self . lookup_module ( ) , self . qualname ) <EOL> @ property <EOL> def llvm_func_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . mangled_name <EOL> @ property <EOL> def llvm_cpython_wrapper_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' + self . mangled_name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . unique_name ) <EOL> @ classmethod <EOL> def _get_function_info ( cls , interp ) : <EOL> """<STR_LIT>""" <EOL> func = interp . bytecode . func <EOL> qualname = interp . bytecode . func_qualname <EOL> modname = func . __module__ <EOL> doc = func . __doc__ or '<STR_LIT>' <EOL> args = tuple ( interp . arg_names ) <EOL> kws = ( ) <EOL> if modname is None : <EOL> modname = _dynamic_modname <EOL> unique_name = "<STR_LIT>" % ( qualname , next ( cls . _unique_ids ) ) <EOL> return qualname , unique_name , modname , doc , args , kws <EOL> @ classmethod <EOL> def _from_python_function ( cls , interp , typemap , restype , calltypes , <EOL> native , mangler = None , inline = False ) : <EOL> ( qualname , unique_name , modname , doc , args , kws , <EOL> ) = cls . _get_function_info ( interp ) <EOL> self = cls ( native , modname , qualname , unique_name , doc , <EOL> typemap , restype , calltypes , <EOL> args , kws , mangler = mangler , inline = inline ) <EOL> return self <EOL> class PythonFunctionDescriptor ( FunctionDescriptor ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> @ classmethod <EOL> def from_specialized_function ( cls , interp , typemap , restype , calltypes , <EOL> mangler , inline ) : <EOL> """<STR_LIT>""" <EOL> return cls . _from_python_function ( interp , typemap , restype , calltypes , <EOL> native = True , mangler = mangler , <EOL> inline = inline ) <EOL> @ classmethod <EOL> def from_object_mode_function ( cls , interp ) : <EOL> """<STR_LIT>""" <EOL> typemap = defaultdict ( lambda : types . pyobject ) <EOL> calltypes = typemap . copy ( ) <EOL> restype = types . pyobject <EOL> return cls . _from_python_function ( interp , typemap , restype , calltypes , <EOL> native = False ) <EOL> class ExternalFunctionDescriptor ( FunctionDescriptor ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( ) <EOL> def __init__ ( self , name , restype , argtypes ) : <EOL> args = [ "<STR_LIT>" % i for i in range ( len ( argtypes ) ) ] <EOL> super ( ExternalFunctionDescriptor , self ) . __init__ ( native = True , <EOL> modname = None , qualname = name , unique_name = name , doc = '<STR_LIT>' , <EOL> typemap = None , restype = restype , calltypes = None , <EOL> args = args , kws = None , mangler = lambda a , x : a , <EOL> argtypes = argtypes ) </s>
