<s> from __future__ import absolute_import , print_function <EOL> import sys <EOL> import os <EOL> from . import ext_tools <EOL> from . import catalog <EOL> from . import common_info <EOL> from numpy . core . multiarray import _get_ndarray_c_version <EOL> ndarray_api_version = '<STR_LIT>' % ( _get_ndarray_c_version ( ) , ) <EOL> function_catalog = catalog . catalog ( ) <EOL> class inline_ext_function ( ext_tools . ext_function ) : <EOL> def function_declaration_code ( self ) : <EOL> code = '<STR_LIT>' <EOL> return code % self . name <EOL> def template_declaration_code ( self ) : <EOL> code = '<STR_LIT>' '<STR_LIT>' <EOL> return code % self . name <EOL> def parse_tuple_code ( self ) : <EOL> """<STR_LIT>""" <EOL> declare_return = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> py_objects = '<STR_LIT:U+002CU+0020>' . join ( self . arg_specs . py_pointers ( ) ) <EOL> if py_objects : <EOL> declare_py_objects = '<STR_LIT>' + py_objects + '<STR_LIT>' <EOL> else : <EOL> declare_py_objects = '<STR_LIT>' <EOL> py_vars = '<STR_LIT>' . join ( self . arg_specs . py_variables ( ) ) <EOL> if py_vars : <EOL> init_values = py_vars + '<STR_LIT>' <EOL> else : <EOL> init_values = '<STR_LIT>' <EOL> parse_tuple = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> return declare_return + declare_py_objects + init_values + parse_tuple <EOL> def arg_declaration_code ( self ) : <EOL> """<STR_LIT>""" <EOL> arg_strings = [ arg . declaration_code ( inline = <NUM_LIT:1> ) <EOL> for arg in self . arg_specs ] <EOL> return "<STR_LIT>" . join ( arg_strings ) <EOL> def arg_cleanup_code ( self ) : <EOL> """<STR_LIT>""" <EOL> arg_strings = [ arg . cleanup_code ( ) for arg in self . arg_specs ] <EOL> return "<STR_LIT>" . join ( arg_strings ) <EOL> def arg_local_dict_code ( self ) : <EOL> """<STR_LIT>""" <EOL> arg_strings = [ arg . local_dict_code ( ) for arg in self . arg_specs ] <EOL> return "<STR_LIT>" . join ( arg_strings ) <EOL> def function_code ( self ) : <EOL> from . ext_tools import indent <EOL> decl_code = indent ( self . arg_declaration_code ( ) , <NUM_LIT:4> ) <EOL> cleanup_code = indent ( self . arg_cleanup_code ( ) , <NUM_LIT:4> ) <EOL> function_code = indent ( self . code_block , <NUM_LIT:4> ) <EOL> try_code = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' + decl_code + '<STR_LIT>' + function_code + '<STR_LIT>' '<STR_LIT>' <EOL> catch_code = "<STR_LIT>" "<STR_LIT>" + "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" <EOL> return_code = "<STR_LIT>" + cleanup_code + "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" "<STR_LIT>" <EOL> all_code = self . function_declaration_code ( ) + indent ( self . parse_tuple_code ( ) , <NUM_LIT:4> ) + try_code + indent ( catch_code , <NUM_LIT:4> ) + return_code <EOL> return all_code <EOL> def python_function_definition_code ( self ) : <EOL> args = ( self . name , self . name ) <EOL> function_decls = '<STR_LIT>' % args <EOL> return function_decls <EOL> class inline_ext_module ( ext_tools . ext_module ) : <EOL> def __init__ ( self , name , compiler = '<STR_LIT>' ) : <EOL> ext_tools . ext_module . __init__ ( self , name , compiler ) <EOL> self . _build_information . append ( common_info . inline_info ( ) ) <EOL> function_cache = { } <EOL> def inline ( code , arg_names = [ ] , local_dict = None , global_dict = None , <EOL> force = <NUM_LIT:0> , <EOL> compiler = '<STR_LIT>' , <EOL> verbose = <NUM_LIT:0> , <EOL> support_code = None , <EOL> headers = [ ] , <EOL> customize = None , <EOL> type_converters = None , <EOL> auto_downcast = <NUM_LIT:1> , <EOL> newarr_converter = <NUM_LIT:0> , <EOL> ** kw ) : <EOL> """<STR_LIT>""" <EOL> global function_catalog <EOL> call_frame = sys . _getframe ( ) . f_back <EOL> if local_dict is None : <EOL> local_dict = call_frame . f_locals <EOL> if global_dict is None : <EOL> global_dict = call_frame . f_globals <EOL> if force : <EOL> module_dir = global_dict . get ( '<STR_LIT>' , None ) <EOL> func = compile_function ( code , arg_names , local_dict , <EOL> global_dict , module_dir , <EOL> compiler = compiler , <EOL> verbose = verbose , <EOL> support_code = support_code , <EOL> headers = headers , <EOL> customize = customize , <EOL> type_converters = type_converters , <EOL> auto_downcast = auto_downcast , <EOL> ** kw ) <EOL> function_catalog . add_function ( code , func , module_dir ) <EOL> results = attempt_function_call ( code , local_dict , global_dict ) <EOL> else : <EOL> try : <EOL> results = apply ( function_cache [ code ] , ( local_dict , global_dict ) ) <EOL> return results <EOL> except TypeError as msg : <EOL> msg = str ( msg ) . strip ( ) <EOL> if msg [ : <NUM_LIT:16> ] == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> raise TypeError ( msg ) <EOL> except NameError as msg : <EOL> msg = str ( msg ) . strip ( ) <EOL> if msg [ : <NUM_LIT:16> ] == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> raise NameError ( msg ) <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> results = attempt_function_call ( code , local_dict , global_dict ) <EOL> except ValueError : <EOL> module_dir = global_dict . get ( '<STR_LIT>' , None ) <EOL> func = compile_function ( code , arg_names , local_dict , <EOL> global_dict , module_dir , <EOL> compiler = compiler , <EOL> verbose = verbose , <EOL> support_code = support_code , <EOL> headers = headers , <EOL> customize = customize , <EOL> type_converters = type_converters , <EOL> auto_downcast = auto_downcast , <EOL> ** kw ) <EOL> function_catalog . add_function ( code , func , module_dir ) <EOL> results = attempt_function_call ( code , local_dict , global_dict ) <EOL> return results <EOL> def attempt_function_call ( code , local_dict , global_dict ) : <EOL> global function_catalog <EOL> try : <EOL> results = apply ( function_cache [ code ] , ( local_dict , global_dict ) ) <EOL> return results <EOL> except TypeError as msg : <EOL> msg = str ( msg ) . strip ( ) <EOL> if msg [ : <NUM_LIT:16> ] == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> raise TypeError ( msg ) <EOL> except NameError as msg : <EOL> msg = str ( msg ) . strip ( ) <EOL> if msg [ : <NUM_LIT:16> ] == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> raise NameError ( msg ) <EOL> except KeyError : <EOL> pass <EOL> function_list = function_catalog . get_functions_fast ( code ) <EOL> for func in function_list : <EOL> try : <EOL> results = apply ( func , ( local_dict , global_dict ) ) <EOL> function_catalog . fast_cache ( code , func ) <EOL> function_cache [ code ] = func <EOL> return results <EOL> except TypeError as msg : <EOL> msg = str ( msg ) <EOL> if msg [ : <NUM_LIT:16> ] == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> raise TypeError ( msg ) <EOL> except NameError as msg : <EOL> msg = str ( msg ) . strip ( ) <EOL> if msg [ : <NUM_LIT:16> ] == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> raise NameError ( msg ) <EOL> module_dir = global_dict . get ( '<STR_LIT>' , None ) <EOL> function_list = function_catalog . get_functions ( code , module_dir ) <EOL> for func in function_list : <EOL> try : <EOL> results = apply ( func , ( local_dict , global_dict ) ) <EOL> function_catalog . fast_cache ( code , func ) <EOL> function_cache [ code ] = func <EOL> return results <EOL> except : <EOL> pass <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def inline_function_code ( code , arg_names , local_dict = None , <EOL> global_dict = None , auto_downcast = <NUM_LIT:1> , <EOL> type_converters = None , compiler = '<STR_LIT>' ) : <EOL> call_frame = sys . _getframe ( ) . f_back <EOL> if local_dict is None : <EOL> local_dict = call_frame . f_locals <EOL> if global_dict is None : <EOL> global_dict = call_frame . f_globals <EOL> ext_func = inline_ext_function ( '<STR_LIT>' , code , arg_names , <EOL> local_dict , global_dict , auto_downcast , <EOL> type_converters = type_converters ) <EOL> from . import build_tools <EOL> compiler = build_tools . choose_compiler ( compiler ) <EOL> ext_func . set_compiler ( compiler ) <EOL> return ext_func . function_code ( ) <EOL> def compile_function ( code , arg_names , local_dict , global_dict , <EOL> module_dir , <EOL> compiler = '<STR_LIT>' , <EOL> verbose = <NUM_LIT:1> , <EOL> support_code = None , <EOL> headers = [ ] , <EOL> customize = None , <EOL> type_converters = None , <EOL> auto_downcast = <NUM_LIT:1> , <EOL> ** kw ) : <EOL> code = ndarray_api_version + '<STR_LIT:\n>' + code <EOL> module_path = function_catalog . unique_module_name ( code , module_dir ) <EOL> storage_dir , module_name = os . path . split ( module_path ) <EOL> mod = inline_ext_module ( module_name , compiler ) <EOL> ext_func = inline_ext_function ( '<STR_LIT>' , code , arg_names , <EOL> local_dict , global_dict , auto_downcast , <EOL> type_converters = type_converters ) <EOL> mod . add_function ( ext_func ) <EOL> if customize : <EOL> mod . customize = customize <EOL> if support_code : <EOL> mod . customize . add_support_code ( support_code ) <EOL> for header in headers : <EOL> mod . customize . add_header ( header ) <EOL> if verbose > <NUM_LIT:0> : <EOL> print ( '<STR_LIT>' ) <EOL> mod . compile ( location = storage_dir , compiler = compiler , <EOL> verbose = verbose , ** kw ) <EOL> try : <EOL> sys . path . insert ( <NUM_LIT:0> , storage_dir ) <EOL> exec ( '<STR_LIT>' + module_name ) <EOL> func = eval ( module_name + '<STR_LIT>' ) <EOL> finally : <EOL> del sys . path [ <NUM_LIT:0> ] <EOL> return func </s>
