Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib64/python3.5/site-packages/Cython/Compiler/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/site-packages/Cython/Compiler/__pycache__/Visitor.cpython-35.pyc



Z��Z�r�&@s�ddlmZmZddlZddlZddlmZddlmZddlmZddlm	Z	ddlm
Z
dd	lmZdd
lmZddl
Z
e
jde�ejddkr�eeeefZneeeeefZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Zdddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>ijZ d?d@dAdBd9dCd%dDijZ!GdEdF�dFe�Z"GdGdH�dHe�Z#dIdJ�Z$GdKdL�dLe�Z%dMdN�Z&dOdP�Z'GdQdR�dRe�Z(e)dSkr�ddl*Z*e*j+�dS)T�)�absolute_import�print_functionN�)�	TypeSlots)�Builtin)�Nodes)�	ExprNodes)�Errors)�
DebugFlags)�Future�
_PRINTABLE�cs�eZdZdZ�fdd�Zddd�Zdd�Zd	d
�Zej	dd��Z
d
d�Zej	dd��Zej	dd��Z
ddd�Zej	ejde�dd���Z�S)�TreeVisitora�
    Base class for writing visitors for a Cython tree, contains utilities for
    recursing such trees using visitors. Each node is
    expected to have a child_attrs iterable containing the names of attributes
    containing child nodes or lists of child nodes. Lists are not considered
    part of the tree structure (i.e. contained nodes are considered direct
    children of the parent node).

    visit_children visits each of the children of a given node (see the visit_children
    documentation). When recursing the tree using visit_children, an attribute
    access_path is maintained which gives information about the current location
    in the tree as a stack of tuples: (parent_node, attrname, index), representing
    the node, attribute and optional list index that was taken in each step in the path to
    the current node.

    Example:

    >>> class SampleNode(object):
    ...     child_attrs = ["head", "body"]
    ...     def __init__(self, value, head=None, body=None):
    ...         self.value = value
    ...         self.head = head
    ...         self.body = body
    ...     def __repr__(self): return "SampleNode(%s)" % self.value
    ...
    >>> tree = SampleNode(0, SampleNode(1), [SampleNode(2), SampleNode(3)])
    >>> class MyVisitor(TreeVisitor):
    ...     def visit_SampleNode(self, node):
    ...         print("in %s %s" % (node.value, self.access_path))
    ...         self.visitchildren(node)
    ...         print("out %s" % node.value)
    ...
    >>> MyVisitor().visit(tree)
    in 0 []
    in 1 [(SampleNode(0), 'head', None)]
    out 1
    in 2 [(SampleNode(0), 'body', 0)]
    out 2
    in 3 [(SampleNode(0), 'body', 1)]
    out 3
    out 0
    cs)tt|�j�i|_g|_dS)N)�superr�__init__�dispatch_table�access_path)�self)�	__class__��J/opt/alt/python35/lib64/python3.5/site-packages/Cython/Compiler/Visitor.pyrJs	zTreeVisitor.__init__rc
Cs�t|jpg�dddddg}g}t|dd�}|r�|d}|rzddl}|jj|j��}|jd||d|d	f�t|�}|j	�x�|D]�}	|	|kr�q�|	j
d
�s�|	jd
�r�q�yt||	�}
Wntk
rw�YnX|
dks�|
dkr8q�nCt
|
t�rZdt|
�}
n!t
|
t�soq�nt|
�}
|jd|	|
f�q�Wd
|jjdj|�fS)N�child_attrs�posZgil_messageZcpp_messageZsubexprsrz%s:%s:%sr��_z[...]/%dz%s = %sz%s(%s)z,
    )�listr�getattr�os.path�path�basename�get_description�append�dir�sort�
startswith�endswith�AttributeError�
isinstance�lenr�reprr�__name__�join)r�node�indent�ignored�valuesr�source�osZattribute_names�attr�valuerrr�	dump_nodeOs<
"


zTreeVisitor.dump_nodec
Cs�ddl}|}g}x�t|d�r�|j}|jjd�}t|tj�r�|j}|j	}|j
j|j�|j
f}	|j|||	f�|}|j}qW||fS)Nr�tb_framer)r�hasattrr5�f_locals�getr'r�Node�f_code�co_namerr�co_filename�f_linenor!�tb_next)
r�
stacktracer1�last_tracebackZnodes�framer,�code�method_namerrrr�_find_node_pathps			
zTreeVisitor._find_node_pathc
Cs7dg}x~|jD]s\}}}t||�}|dkrFd}n||}d|}|jd|jj|||j|�f�qW|jtj�d�\}}	|}
xK|	D]C\}}}|}
|jd|d|d||j|�f�q�Wt	j
t|
dd�|jjd	j|�||��dS)
N�z[%d]z%s.%s%s = %srzFile '%s', line %d, in %s: %srrr�
)rrr!rr*r4rD�sys�exc_infor	Z
CompilerCrashr+)
r�child�e�trace�parent�	attribute�indexr,r?Zcalled_nodesZ	last_noderCrrrr�_raise_compiler_error�s&		

		)z!TreeVisitor._raise_compiler_errorcCs�t|�}d}tj|�}d}x7|D]/}t|||jd�}|dk	r.|Sq.Wtt|�|�|jr�t|j�t|jddj�t|jddj�t	d||f��dS)Nzvisit_%srrz%Visitor %r does not accept object: %s���rP)
�type�inspect�getmrorr*�printrr�__dict__�RuntimeError)r�obj�cls�pattern�mro�handler_methodZmro_clsrrr�find_handler�s
	
zTreeVisitor.find_handlercCs
|j|�S)N)�_visit)rrWrrr�visit�szTreeVisitor.visitcCs�y\y|jt|�}Wn4tk
rP|j|�}||jt|�<YnX||�SWnotjk
rv�YnXtjk
r��YnAtk
r�}z!tj	r��|j
||�WYdd}~XnXdS)N)rrQ�KeyErrorr\r	�CompileErrorZ
AbortError�	Exceptionr
Zdebug_no_exception_interceptrO)rrWr[rJrrrr]�s
	zTreeVisitor._visitcCs9|jj|||f�|j|�}|jj�|S)N)rr!r]�pop)rrIrL�attrname�idx�resultrrr�_visitchild�s
zTreeVisitor._visitchildNcCs|j||�S)N)�_visitchildren)rrL�attrsrrr�
visitchildren�szTreeVisitor.visitchildrenrdcs��dkrdSi}x��jD]��|dk	rA�|krAq t���}|dk	r t|�tkr����fdd�t|�D�}n>�j|��d�}t|t�s�td��f��||�<q W|S)af
        Visits the children of the given parent. If parent is None, returns
        immediately (returning None).

        The return value is a dictionary giving the results for each
        child (mapping the attribute name to either the return value
        or a list of return values (in the case of multiple children
        in an attribute)).
        Ncs.g|]$\}}�j|��|��qSr)rf)�.0rd�x)r2rLrrr�
<listcomp>�s	z.TreeVisitor._visitchildren.<locals>.<listcomp>z!Cannot insert list here: %s in %r)rrrQr�	enumeraterfr'�AssertionError)rrLrhrerIZchildretvalr)r2rLrrrg�s(&zTreeVisitor._visitchildren)r*�
__module__�__qualname__�__doc__rr4rDrO�cython�finalr\r^r]rfri�locals�intrgrr)rrrs*!rc@s=eZdZdZddd�Zdd�Zdd�ZdS)	�VisitorTransforma�
    A tree transform is a base class for visitors that wants to do stream
    processing of the structure (rather than attributes etc.) of a tree.

    It implements __call__ to simply visit the argument node.

    It requires the visitor methods to return the nodes which should take
    the place of the visited node in the result tree (which can be the same
    or one or more replacement). Specifically, if the return value from
    a visitor method is:

    - [] or None; the visited node will be removed (set to None if an attribute and
    removed if in a list)
    - A single node; the visited node will be replaced by the returned node.
    - A list of nodes; the visited nodes will be replaced by all the nodes in the
    list. This will only work if the node was already a member of a list; if it
    was not, an exception will be raised. (Typically you want to ensure that you
    are within a StatListNode or similar before doing this.)
    NcCs�|j||�}x�|j�D]�\}}t|�tk	rPt|||�qg}xF|D]>}|dk	r]t|�tkr�||7}q]|j|�q]Wt|||�qW|S)N)rg�itemsrQr�setattrr!)rrLrhrer2ZnewnodeZnewlistrkrrrri�s

zVisitorTransform.visitchildrencCs|j|�|S)N)ri)rr,rrr�recurse_to_childrens
z$VisitorTransform.recurse_to_childrencCs
|j|�S)N)r])r�rootrrr�__call__szVisitorTransform.__call__)r*rorprqriryr{rrrrrv�srvcsReZdZdZ�fdd�Z�fdd�Zdd�Zdd	�Z�S)
�CythonTransformz�
    Certain common conventions and utilities for Cython transforms.

     - Sets up the context of the pipeline in self.context
     - Tracks directives in effect in self.current_directives
    cs tt|�j�||_dS)N)rr|r�context)rr})rrrrszCythonTransform.__init__csDddlm}t||j�r.|j|_tt|�j|�S)Nr)�
ModuleNode)rEr~r'�
directives�current_directivesrr|r{)rr,r~)rrrr{szCythonTransform.__call__cCs/|j}|j|_|j|�||_|S)N)r�rri)rr,�oldrrr�visit_CompilerDirectivesNode s
	
	z,CythonTransform.visit_CompilerDirectivesNodecCs|j|�|S)N)ri)rr,rrr�
visit_Node's
zCythonTransform.visit_Node)r*rorprqrr{r�r�rr)rrr|s
r|c@sXeZdZdd�Zdd�Zdd�Zdd�Zd	d
�Zdd�Zd
S)�ScopeTrackingTransformcCs#d|_||_|j|�|S)N�module)�
scope_type�
scope_noderi)rr,rrr�visit_ModuleNode0s		
z'ScopeTrackingTransform.visit_ModuleNodecCsG|j|jf}||_||_|j|�|\|_|_|S)N)r�r�ri)rr,r��prevrrr�visit_scope6s		
z"ScopeTrackingTransform.visit_scopecCs|j|d�S)N�cclass)r�)rr,rrr�visit_CClassDefNode>sz*ScopeTrackingTransform.visit_CClassDefNodecCs|j|d�S)NZpyclass)r�)rr,rrr�visit_PyClassDefNodeAsz+ScopeTrackingTransform.visit_PyClassDefNodecCs|j|d�S)N�function)r�)rr,rrr�visit_FuncDefNodeDsz(ScopeTrackingTransform.visit_FuncDefNodecCs|j|d�S)N�struct)r�)rr,rrr�visit_CStructOrUnionDefNodeGsz2ScopeTrackingTransform.visit_CStructOrUnionDefNodeN)	r*rorpr�r�r�r�r�r�rrrrr�+sr�cs�eZdZdZ�fdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Z�S)�EnvTransformz@
    This transformation keeps a stack of the environments.
    cs2g|_|j||j�tt|�j|�S)N)�	env_stack�enter_scope�scoperr�r{)rrz)rrrr{Os	zEnvTransform.__call__cCs|jddS)NrrP)r�)rrrr�current_envTszEnvTransform.current_envcCs|jddS)NrrrP)r�)rrrr�current_scope_nodeWszEnvTransform.current_scope_nodecCs|j�j�S)N)r��global_scope)rrrrr�ZszEnvTransform.global_scopecCs|jj||f�dS)N)r�r!)rr,r�rrrr�]szEnvTransform.enter_scopecCs|jj�dS)N)r�rb)rrrr�
exit_scope`szEnvTransform.exit_scopecCs.|j||j�|j|�|j�|S)N)r�Zlocal_scoperir�)rr,rrrr�cs

zEnvTransform.visit_FuncDefNodecCs|j|�|S)N)ri)rr,rrr�visit_GeneratorBodyDefNodeis
z'EnvTransform.visit_GeneratorBodyDefNodecCs.|j||j�|j|�|j�|S)N)r�r�rir�)rr,rrr�visit_ClassDefNodems

zEnvTransform.visit_ClassDefNodecCs.|j||j�|j|�|j�|S)N)r�r�rir�)rr,rrrr�ss

z(EnvTransform.visit_CStructOrUnionDefNodecCsG|jr6|j||j�|j|�|j�n
|j|�|S)N)Z
expr_scoper�rir�)rr,rrr�visit_ScopedExprNodeys	


z!EnvTransform.visit_ScopedExprNodecCsv|jredd�|jD�}|j||�|j||j�j�|j|d�|j�n
|j|�|S)NcSs"g|]}|dkr|�qS)�defaultr)rjr2rrrrl�s	z3EnvTransform.visit_CArgDeclNode.<locals>.<listcomp>r�)r�)r�rrir�r�Zouter_scoper�)rr,rhrrr�visit_CArgDeclNode�s	

zEnvTransform.visit_CArgDeclNode)r*rorprqr{r�r�r�r�r�r�r�r�r�r�r�rr)rrr�Ks	r�csLeZdZdZ�fdd�Zdd�Zdd�Zdd	�Z�S)
�NodeRefCleanupMixinas
    Clean up references to nodes that were replaced.

    NOTE: this implementation assumes that the replacement is
    done first, before hitting any further references during
    normal tree traversal.  This needs to be arranged by calling
    "self.visitchildren()" at a proper place in the transform
    and by ordering the "child_attrs" of nodes appropriately.
    cs#tt|�j|�i|_dS)N)rr�r�
_replacements)r�args)rrrr�szNodeRefCleanupMixin.__init__cCsA|j}||jkr%|j|�|jj||�|_|S)N)�argr�rir8)rr,r�rrr�visit_CloneNode�s
	
z#NodeRefCleanupMixin.visit_CloneNodecCsb|j}|dks$||jkr:|j|�|j}|dk	r^|jj||�|_|S)N)Z
expressionr�rir8)rr,�exprrrr�visit_ResultRefNode�s	
	z'NodeRefCleanupMixin.visit_ResultRefNodecCs||j|<|S)N)r�)rr,�replacementrrr�replace�s
zNodeRefCleanupMixin.replace)r*rorprqrr�r�r�rr)rrr��s
		r��<�__lt__z<=�__le__z==�__eq__z!=�__ne__z>=�__ge__�>�__gt__�+�__add__�&�__and__�/�__div__z//�__floordiv__z<<�
__lshift__�%�__mod__�*�__mul__�|�__or__z**�__pow__z>>�
__rshift__�-�__sub__�^�__xor__�in�__contains__�not�__not__�~�__inv__�__neg__�__pos__c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�MethodDispatcherTransforma�
    Base class for transformations that want to intercept on specific
    builtin functions or methods of builtin types, including special
    methods triggered by Python operators.  Must run after declaration
    analysis when entries were assigned.

    Naming pattern for handler methods is as follows:

    * builtin functions: _handle_(general|simple|any)_function_NAME

    * builtin methods: _handle_(general|simple|any)_method_TYPENAME_METHODNAME
    cCs�|j|�|j}|jjs&|S|j}t|tj�sE|S|j}|rkt|tj	�rk|S|j
}|j||||�S)N)rir�rQ�is_pyobjectZpositional_argsr'r�	TupleNode�keyword_argsZDictNoder��_dispatch_to_handler)rr,r��	arg_tupler�r�rrr�visit_GeneralCallNode�s
				z/MethodDispatcherTransform.visit_GeneralCallNodecCsl|j|�|j}|jjrM|j}t|tj�sA|S|j}n	|j}|j	|||d�S)N)
rir�rQr�r�r'rr�r�r�)rr,r�r�r�rrr�visit_SimpleCallNode�s
			z.MethodDispatcherTransform.visit_SimpleCallNodecCs'|jr|j|�|S|j|�S)N)Zcascaderi�_visit_binop_node)rr,rrr�visit_PrimaryCmpNode�s	
z.MethodDispatcherTransform.visit_PrimaryCmpNodecCs
|j|�S)N)r�)rr,rrr�visit_BinopNodesz)MethodDispatcherTransform.visit_BinopNodec	Cs�|j|�t|j�}|r�|j|j}}|dkrQ||}}n3|dkr�tj|j�j�j	j
kr�d}|j}|jr�|j
}nd}|j|dd||d||gd�}|S)Nr�r��__truediv__�objectF)ri�'find_special_method_for_binary_operator�operator�operand1�operand2r�divisionr�r�r}Zfuture_directivesrQ�is_builtin_type�name�_dispatch_to_method_handler)rr,�special_method_namer�r��obj_type�	type_namerrrr�s"
!		z+MethodDispatcherTransform._visit_binop_nodec	Csz|j|�t|j�}|rv|j}|j}|jrI|j}nd}|j|dd||d|gd�}|S)Nr�F)ri�&find_special_method_for_unary_operatorr��operandrQr�r�r�)rr,r�r�r�r�rrr�visit_UnopNodes
			z(MethodDispatcherTransform.visit_UnopNodecCsT|rdpd}t|d||fd�}|dkrPt|d|d�}|S)NZgeneralZsimplez
_handle_%s_%sz_handle_any_%s)r)rZ
match_nameZ
has_kwargsZ	call_type�handlerrrr�
_find_handler*s
z'MethodDispatcherTransform._find_handlercCs�|jd}|j}|jrH|jsAt|jj�dkr�|SnH|jr�|jjr�|jjs�t|jjj�dkr�|Sn|S|j||||�S)Nrr)	�cf_state�rhs�is_name�entryr(Zcf_assignments�is_attributerWr�)rr,r��arg_list�kwargsZ
assignmentr3rrr�_delegate_to_assigned_value1s
		"(z5MethodDispatcherTransform._delegate_to_assigned_valuec
Cs�|jrq|js|S|j}|jpI||j�j�j|j�k}|s|jr}|jjr}|j	||||�S|r|j
r|jr|jjj
r|jj|djkr|j|jddddd|jjjd|d|d|d	|�S|S|jd
|j|�}|dkrE|j||j|||�S|r^|||||�S||||�Sn|jr�|j}|jjr�|j}	nS|jr�|jr�|jj}|s�|jr�|S|j}	|dd�}n|S|	j}
d}|
j
rX|
tjkrL|	jrL|rL|djjrL|	j}d}	d}q^|
j}nd
}|j||	||||||�S|SdS)Nr�self_arg�is_unbound_methodTr�r,r�r�r�zfunction_%srFr�)r�r��
is_builtinr�Z
builtin_scopeZlookup_herer�r�Z	is_singler�Z
is_cmethodr�Zparent_typer�rQr�r��_handle_functionr�rMr�rWrZas_variablerZ	type_type)
rr,r�r�r�r�r�Zfunction_handler�	attr_namer�r�r�r�rrrr�As`				$'					.		z.MethodDispatcherTransform._dispatch_to_handlerc	Cs�|jd||f|�}	|	dkr�|tjksC|dkrY|jd||�}	|	dkr�|j|||||||�S|dk	r�|gt|�}|r�|	|||||�}
n|	||||�}
|
S)Nzmethod_%s_%s�__new__zslot%s)r�rZmethod_name_to_slot�_handle_methodr)rr�r�r�r�r,r�r�r�Zmethod_handlerrerrrr��s&
z5MethodDispatcherTransform._dispatch_to_method_handlercCs|S)zFallback handlerr)rr,Z
function_namer�r�r�rrrr��sz*MethodDispatcherTransform._handle_functioncCs|S)zFallback handlerr)rr,r�r�r�r�r�r�rrrr��sz(MethodDispatcherTransform._handle_methodN)r*rorprqr�r�r�r�r�r�r�r�r�r�r�r�rrrrr��sAr�cs@eZdZdZ�fdd�Zdd�Zdd�Z�S)�RecursiveNodeReplacerzY
    Recursively replace all occurrences of a node in a subtree by
    another node.
    cs*tt|�j�|||_|_dS)N)rr�r�	orig_node�new_node)rr�r�)rrrr�szRecursiveNodeReplacer.__init__cCs8||jkr|jS|j|jkr4|j|_|S)N)r�r�r�)rr,rrrr��s
z%RecursiveNodeReplacer.visit_CloneNodecCs+|j|�||jkr#|jS|SdS)N)rir�r�)rr,rrrr��s
z RecursiveNodeReplacer.visit_Node)r*rorprqrr�r�rr)rrr��sr�cCst||�}||�dS)N)r�)�tree�old_noder�Z
replace_inrrr�recursively_replace_node�sr�cs4eZdZdZ�fdd�Zdd�Z�S)�
NodeFinderz2
    Find out if a node appears in a subtree.
    cs)tt|�j�||_d|_dS)NF)rr�rr,�found)rr,)rrrr�s	zNodeFinder.__init__cCs;|jrn+||jkr'd|_n|j|d�dS)NT)r�r,rg)rr,rrrr��s
	zNodeFinder.visit_Node)r*rorprqrr�rr)rrr��sr�cCs t|�}|j|�|jS)N)r�r^r�)r�r,�finderrrr�
tree_contains�s
rcCsE|\}}}|dkr.t|||�n|t||�|<dS)zkReplaces a node. ptr is of the form used on the access path stack
    (parent, attrname, listidx|None)
    N)rxr)�ptrr3rLrcZlistidxrrr�replace_node�src@seZdZdZdddd�Zdd�Zdd�Zdd	d
�Zdd�Zd
d�Z	dd�Z
dd�ZdS)�	PrintTreez�Prints a representation of the tree to standard output.
    Subclass and override repr_of to provide more information
    about nodes. NcCsYtj|�d|_|dk	s.|dk	rL|p7d|p@df|_n	d|_dS)NrErr�i@)rr�_indent�_line_range)r�start�endrrrr�s

	zPrintTree.__init__cCs|jd7_dS)Nz  )r)rrrrr-�szPrintTree.indentcCs|jdd�|_dS)Nr���)r)rrrr�unindent�szPrintTree.unindentcCstd|�|j|�|S)NzParse tree dump at phase '%s')rTr^)rr�Zphaserrrr{�s
zPrintTree.__call__cCs2|j|�|j�|j|�|j�|S)N)�_print_noder-rir)rr,rrrr��s




zPrintTree.visit_NodecCs�|j|�|j�|jd}|jdks]|jd|koX|jdknr�td|jd|j|j�f�|j�|j|j�|j	�|j	�|S)Nrrz
%s- %s: %sr�)
rr-rrrTr�repr_ofr�rir)rr,�linerrrr�s


9&


zPrintTree.visit_CloneNodecCs�|jd}|jdksF|jd|koA|jdknr�t|j�dkrdd}n;|jd\}}}|dk	r�d||f}n|}td|j||j|�f�dS)Nrrz(root)z%s[%d]z
%s- %s: %srP)rrr(rrTrr
)rr,rr�rLr2rdrrrrs
9	zPrintTree._print_nodecCs0|dkrdS|jj}t|tj�rQ|dt|j�|jf7}n�t|tj	�rw|d|j7}n�t|tj
�r�|j}|dt|�7}n|jr(|j}|dj�}d|kr�|j
d�d}d|kr	|j
d�d}|d	||d|d
f7}|SdS)
Nz(none)z(type=%s, name="%s")z(name="%s")z	(type=%s)rr�r�\z(pos=(%s:%s:%s))rrPrP)rr*r'rZNameNoder)rQr�rZDefNodeZExprNoderr �split)rr,re�trrrrrr
s&#			zPrintTree.repr_of)r*rorprqrr-rr{r�r�rr
rrrrr�s	
r�__main__),�
__future__rrrGrRrErrrrr	r
rrr�declare�tuple�version_info�bytes�strru�floatr�unicode�longr�rrvr|r�r�r�r8r�r�r�r�r�r�rrrr*ZdoctestZtestmodrrrr�<module>sj�, D$�R