<s> from __future__ import absolute_import <EOL> import itertools <EOL> from time import time <EOL> from . import Errors <EOL> from . import DebugFlags <EOL> from . import Options <EOL> from . Visitor import CythonTransform <EOL> from . Errors import CompileError , InternalError , AbortError <EOL> from . import Naming <EOL> def dumptree ( t ) : <EOL> print ( t . dump ( ) ) <EOL> return t <EOL> def abort_on_errors ( node ) : <EOL> if Errors . num_errors != <NUM_LIT:0> : <EOL> raise AbortError ( "<STR_LIT>" ) <EOL> return node <EOL> def parse_stage_factory ( context ) : <EOL> def parse ( compsrc ) : <EOL> source_desc = compsrc . source_desc <EOL> full_module_name = compsrc . full_module_name <EOL> initial_pos = ( source_desc , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> saved_cimport_from_pyx , Options . cimport_from_pyx = Options . cimport_from_pyx , False <EOL> scope = context . find_module ( full_module_name , pos = initial_pos , need_pxd = <NUM_LIT:0> ) <EOL> Options . cimport_from_pyx = saved_cimport_from_pyx <EOL> tree = context . parse ( source_desc , scope , pxd = <NUM_LIT:0> , full_module_name = full_module_name ) <EOL> tree . compilation_source = compsrc <EOL> tree . scope = scope <EOL> tree . is_pxd = False <EOL> return tree <EOL> return parse <EOL> def parse_pxd_stage_factory ( context , scope , module_name ) : <EOL> def parse ( source_desc ) : <EOL> tree = context . parse ( source_desc , scope , pxd = True , <EOL> full_module_name = module_name ) <EOL> tree . scope = scope <EOL> tree . is_pxd = True <EOL> return tree <EOL> return parse <EOL> def generate_pyx_code_stage_factory ( options , result ) : <EOL> def generate_pyx_code_stage ( module_node ) : <EOL> module_node . process_implementation ( options , result ) <EOL> result . compilation_source = module_node . compilation_source <EOL> return result <EOL> return generate_pyx_code_stage <EOL> def inject_pxd_code_stage_factory ( context ) : <EOL> def inject_pxd_code_stage ( module_node ) : <EOL> for name , ( statlistnode , scope ) in context . pxds . items ( ) : <EOL> module_node . merge_in ( statlistnode , scope ) <EOL> return module_node <EOL> return inject_pxd_code_stage <EOL> def use_utility_code_definitions ( scope , target , seen = None ) : <EOL> if seen is None : <EOL> seen = set ( ) <EOL> for entry in scope . entries . values ( ) : <EOL> if entry in seen : <EOL> continue <EOL> seen . add ( entry ) <EOL> if entry . used and entry . utility_code_definition : <EOL> target . use_utility_code ( entry . utility_code_definition ) <EOL> for required_utility in entry . utility_code_definition . requires : <EOL> target . use_utility_code ( required_utility ) <EOL> elif entry . as_module : <EOL> use_utility_code_definitions ( entry . as_module , target , seen ) <EOL> def sort_utility_codes ( utilcodes ) : <EOL> ranks = { } <EOL> def get_rank ( utilcode ) : <EOL> if utilcode not in ranks : <EOL> ranks [ utilcode ] = <NUM_LIT:0> <EOL> original_order = len ( ranks ) <EOL> ranks [ utilcode ] = <NUM_LIT:1> + min ( [ get_rank ( dep ) for dep in utilcode . requires or ( ) ] or [ - <NUM_LIT:1> ] ) + original_order * <NUM_LIT> <EOL> return ranks [ utilcode ] <EOL> for utilcode in utilcodes : <EOL> get_rank ( utilcode ) <EOL> return [ utilcode for utilcode , _ in sorted ( ranks . items ( ) , key = lambda kv : kv [ <NUM_LIT:1> ] ) ] <EOL> def normalize_deps ( utilcodes ) : <EOL> deps = { } <EOL> for utilcode in utilcodes : <EOL> deps [ utilcode ] = utilcode <EOL> def unify_dep ( dep ) : <EOL> if dep in deps : <EOL> return deps [ dep ] <EOL> else : <EOL> deps [ dep ] = dep <EOL> return dep <EOL> for utilcode in utilcodes : <EOL> utilcode . requires = [ unify_dep ( dep ) for dep in utilcode . requires or ( ) ] <EOL> def inject_utility_code_stage_factory ( context ) : <EOL> def inject_utility_code_stage ( module_node ) : <EOL> module_node . prepare_utility_code ( ) <EOL> use_utility_code_definitions ( context . cython_scope , module_node . scope ) <EOL> module_node . scope . utility_code_list = sort_utility_codes ( module_node . scope . utility_code_list ) <EOL> normalize_deps ( module_node . scope . utility_code_list ) <EOL> added = [ ] <EOL> for utilcode in module_node . scope . utility_code_list : <EOL> if utilcode in added : <EOL> continue <EOL> added . append ( utilcode ) <EOL> if utilcode . requires : <EOL> for dep in utilcode . requires : <EOL> if dep not in added and dep not in module_node . scope . utility_code_list : <EOL> module_node . scope . utility_code_list . append ( dep ) <EOL> tree = utilcode . get_tree ( cython_scope = context . cython_scope ) <EOL> if tree : <EOL> module_node . merge_in ( tree . body , tree . scope , merge_scope = True ) <EOL> return module_node <EOL> return inject_utility_code_stage <EOL> class UseUtilityCodeDefinitions ( CythonTransform ) : <EOL> def __call__ ( self , node ) : <EOL> self . scope = node . scope <EOL> return super ( UseUtilityCodeDefinitions , self ) . __call__ ( node ) <EOL> def process_entry ( self , entry ) : <EOL> if entry : <EOL> for utility_code in ( entry . utility_code , entry . utility_code_definition ) : <EOL> if utility_code : <EOL> self . scope . use_utility_code ( utility_code ) <EOL> def visit_AttributeNode ( self , node ) : <EOL> self . process_entry ( node . entry ) <EOL> return node <EOL> def visit_NameNode ( self , node ) : <EOL> self . process_entry ( node . entry ) <EOL> self . process_entry ( node . type_entry ) <EOL> return node <EOL> def create_pipeline ( context , mode , exclude_classes = ( ) ) : <EOL> assert mode in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> from . Visitor import PrintTree <EOL> from . ParseTreeTransforms import WithTransform , NormalizeTree , PostParse , PxdPostParse <EOL> from . ParseTreeTransforms import ForwardDeclareTypes , AnalyseDeclarationsTransform <EOL> from . ParseTreeTransforms import AnalyseExpressionsTransform , FindInvalidUseOfFusedTypes <EOL> from . ParseTreeTransforms import CreateClosureClasses , MarkClosureVisitor , DecoratorTransform <EOL> from . ParseTreeTransforms import InterpretCompilerDirectives , TransformBuiltinMethods <EOL> from . ParseTreeTransforms import ExpandInplaceOperators , ParallelRangeTransform <EOL> from . ParseTreeTransforms import CalculateQualifiedNamesTransform <EOL> from . TypeInference import MarkParallelAssignments , MarkOverflowingArithmetic <EOL> from . ParseTreeTransforms import AdjustDefByDirectives , AlignFunctionDefinitions <EOL> from . ParseTreeTransforms import RemoveUnreachableCode , GilCheck <EOL> from . FlowControl import ControlFlowAnalysis <EOL> from . AnalysedTreeTransforms import AutoTestDictTransform <EOL> from . AutoDocTransforms import EmbedSignature <EOL> from . Optimize import FlattenInListTransform , SwitchTransform , IterationTransform <EOL> from . Optimize import EarlyReplaceBuiltinCalls , OptimizeBuiltinCalls <EOL> from . Optimize import InlineDefNodeCalls <EOL> from . Optimize import ConstantFolding , FinalOptimizePhase <EOL> from . Optimize import DropRefcountingTransform <EOL> from . Optimize import ConsolidateOverflowCheck <EOL> from . Buffer import IntroduceBufferAuxiliaryVars <EOL> from . ModuleNode import check_c_declarations , check_c_declarations_pxd <EOL> if mode == '<STR_LIT>' : <EOL> _check_c_declarations = check_c_declarations_pxd <EOL> _specific_post_parse = PxdPostParse ( context ) <EOL> else : <EOL> _check_c_declarations = check_c_declarations <EOL> _specific_post_parse = None <EOL> if mode == '<STR_LIT>' : <EOL> _align_function_definitions = AlignFunctionDefinitions ( context ) <EOL> else : <EOL> _align_function_definitions = None <EOL> stages = [ <EOL> NormalizeTree ( context ) , <EOL> PostParse ( context ) , <EOL> _specific_post_parse , <EOL> InterpretCompilerDirectives ( context , context . compiler_directives ) , <EOL> ParallelRangeTransform ( context ) , <EOL> AdjustDefByDirectives ( context ) , <EOL> WithTransform ( context ) , <EOL> MarkClosureVisitor ( context ) , <EOL> _align_function_definitions , <EOL> RemoveUnreachableCode ( context ) , <EOL> ConstantFolding ( ) , <EOL> FlattenInListTransform ( ) , <EOL> DecoratorTransform ( context ) , <EOL> ForwardDeclareTypes ( context ) , <EOL> AnalyseDeclarationsTransform ( context ) , <EOL> AutoTestDictTransform ( context ) , <EOL> EmbedSignature ( context ) , <EOL> EarlyReplaceBuiltinCalls ( context ) , <EOL> TransformBuiltinMethods ( context ) , <EOL> MarkParallelAssignments ( context ) , <EOL> ControlFlowAnalysis ( context ) , <EOL> RemoveUnreachableCode ( context ) , <EOL> MarkOverflowingArithmetic ( context ) , <EOL> IntroduceBufferAuxiliaryVars ( context ) , <EOL> _check_c_declarations , <EOL> InlineDefNodeCalls ( context ) , <EOL> AnalyseExpressionsTransform ( context ) , <EOL> FindInvalidUseOfFusedTypes ( context ) , <EOL> ExpandInplaceOperators ( context ) , <EOL> IterationTransform ( context ) , <EOL> SwitchTransform ( context ) , <EOL> OptimizeBuiltinCalls ( context ) , <EOL> CreateClosureClasses ( context ) , <EOL> CalculateQualifiedNamesTransform ( context ) , <EOL> ConsolidateOverflowCheck ( context ) , <EOL> DropRefcountingTransform ( ) , <EOL> FinalOptimizePhase ( context ) , <EOL> GilCheck ( ) , <EOL> UseUtilityCodeDefinitions ( context ) , <EOL> ] <EOL> filtered_stages = [ ] <EOL> for s in stages : <EOL> if s . __class__ not in exclude_classes : <EOL> filtered_stages . append ( s ) <EOL> return filtered_stages <EOL> def create_pyx_pipeline ( context , options , result , py = False , exclude_classes = ( ) ) : <EOL> if py : <EOL> mode = '<STR_LIT>' <EOL> else : <EOL> mode = '<STR_LIT>' <EOL> test_support = [ ] <EOL> if options . evaluate_tree_assertions : <EOL> from . . TestUtils import TreeAssertVisitor <EOL> test_support . append ( TreeAssertVisitor ( ) ) <EOL> if options . gdb_debug : <EOL> from . . Debugger import DebugWriter <EOL> from . ParseTreeTransforms import DebugTransform <EOL> context . gdb_debug_outputwriter = DebugWriter . CythonDebugWriter ( <EOL> options . output_dir ) <EOL> debug_transform = [ DebugTransform ( context , options , result ) ] <EOL> else : <EOL> debug_transform = [ ] <EOL> return list ( itertools . chain ( <EOL> [ parse_stage_factory ( context ) ] , <EOL> create_pipeline ( context , mode , exclude_classes = exclude_classes ) , <EOL> test_support , <EOL> [ inject_pxd_code_stage_factory ( context ) , <EOL> inject_utility_code_stage_factory ( context ) , <EOL> abort_on_errors ] , <EOL> debug_transform , <EOL> [ generate_pyx_code_stage_factory ( options , result ) ] ) ) <EOL> def create_pxd_pipeline ( context , scope , module_name ) : <EOL> from . CodeGeneration import ExtractPxdCode <EOL> return [ <EOL> parse_pxd_stage_factory ( context , scope , module_name ) <EOL> ] + create_pipeline ( context , '<STR_LIT>' ) + [ <EOL> ExtractPxdCode ( ) <EOL> ] <EOL> def create_py_pipeline ( context , options , result ) : <EOL> return create_pyx_pipeline ( context , options , result , py = True ) <EOL> def create_pyx_as_pxd_pipeline ( context , result ) : <EOL> from . ParseTreeTransforms import AlignFunctionDefinitions , MarkClosureVisitor , WithTransform , AnalyseDeclarationsTransform <EOL> from . Optimize import ConstantFolding , FlattenInListTransform <EOL> from . Nodes import StatListNode <EOL> pipeline = [ ] <EOL> pyx_pipeline = create_pyx_pipeline ( context , context . options , result , <EOL> exclude_classes = [ <EOL> AlignFunctionDefinitions , <EOL> MarkClosureVisitor , <EOL> ConstantFolding , <EOL> FlattenInListTransform , <EOL> WithTransform <EOL> ] ) <EOL> for stage in pyx_pipeline : <EOL> pipeline . append ( stage ) <EOL> if isinstance ( stage , AnalyseDeclarationsTransform ) : <EOL> break <EOL> def fake_pxd ( root ) : <EOL> for entry in root . scope . entries . values ( ) : <EOL> if not entry . in_cinclude : <EOL> entry . defined_in_pxd = <NUM_LIT:1> <EOL> if entry . name == entry . cname and entry . visibility != '<STR_LIT>' : <EOL> entry . cname = entry . scope . mangle ( Naming . func_prefix , entry . name ) <EOL> return StatListNode ( root . pos , stats = [ ] ) , root . scope <EOL> pipeline . append ( fake_pxd ) <EOL> return pipeline <EOL> def insert_into_pipeline ( pipeline , transform , before = None , after = None ) : <EOL> """<STR_LIT>""" <EOL> assert before or after <EOL> cls = before or after <EOL> for i , t in enumerate ( pipeline ) : <EOL> if isinstance ( t , cls ) : <EOL> break <EOL> if after : <EOL> i += <NUM_LIT:1> <EOL> return pipeline [ : i ] + [ transform ] + pipeline [ i : ] <EOL> def run_pipeline ( pipeline , source , printtree = True ) : <EOL> from . Visitor import PrintTree <EOL> error = None <EOL> data = source <EOL> try : <EOL> try : <EOL> for phase in pipeline : <EOL> if phase is not None : <EOL> if DebugFlags . debug_verbose_pipeline : <EOL> t = time ( ) <EOL> print ( "<STR_LIT>" % phase ) <EOL> if not printtree and isinstance ( phase , PrintTree ) : <EOL> continue <EOL> data = phase ( data ) <EOL> if DebugFlags . debug_verbose_pipeline : <EOL> print ( "<STR_LIT>" % ( time ( ) - t ) ) <EOL> except CompileError as err : <EOL> Errors . report_error ( err ) <EOL> error = err <EOL> except InternalError as err : <EOL> if Errors . num_errors == <NUM_LIT:0> : <EOL> raise <EOL> error = err <EOL> except AbortError as err : <EOL> error = err <EOL> return ( error , data ) </s>
