U
    {hS                    @   s  d dl Z e jeeeeeeeeeeeeeeeed d dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZmZ d dlmZmZ d dlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ dddddZdddddZddiZdZe dddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbdcdddedfdgdhdidjdkdldmdndodpdqdrdsdtdudvdwdxdydzd{d|d}d~dddddddddddddddddddddddddddddddddddddddddddddddddddddddgZ!d9dMdd/dldnd,dsdfdg
Z"e e e dZ#ddij$Z%e eddddj$Z&G dd dZ'dd Z(dZ)dd Z*e*Z)G dd dZ+G dd de+Z,G ddÄ de,Z-dddgdŜddǄZ.e.dddʍdd̄ Z/e. e-d͜ddτZ0e.ddddӍddՄ Z1e.dddddӍddڄ Z2e.dddʍddބ Z3e.ddddӍdd Z4e. dd Z5ddi fddZ6G dd de-Z7G dd de,Z8G dd dZ9G dd dZ:G dd dZ;e jeeeed e<dej=j>Z?e<dj>Z@e<djAZBe<djCZDG dd dZEG dd dZFG dd dZGdd ZHG dd dZIG d d dZJG dd dZKG dd dZLe jMe jNeOe jPeOdddZQe Rd	e jSeOe jPd
ddZTG dd dZUdS (      N)osreoperatortextwrapTemplateNamingOptionsStringEncodingUtilsSourceDescriptorStringIOTree
DebugFlagsdefaultdictclosingpartialwrapsr   )r   r   )r   contextmanager)r      )r   )r   )r   )r	      )r
   )r   )r   strrangeinput)unicode
basestringxrange	raw_inputz&PyLong_Typez&PyFloat_Typez&PyWrapperDescr_Type)Zpy_intZpy_longZpy_floatZwrapper_descriptorZPyTypeObjectZPyHeapTypeObject)      r   alpha   ArithmeticErrorAssertionErrorAttributeErrorBaseExceptionZBaseExceptionGroupBlockingIOErrorBrokenPipeErrorBufferErrorBytesWarningChildProcessErrorConnectionAbortedErrorConnectionErrorConnectionRefusedErrorConnectionResetErrorDeprecationWarningEOFErrorEllipsisZEncodingWarningEnvironmentError	ExceptionZExceptionGroupFalseFileExistsErrorFileNotFoundErrorFloatingPointErrorFutureWarningGeneratorExitIOErrorImportErrorImportWarningZ_IncompleteInputErrorIndentationError
IndexErrorInterruptedErrorIsADirectoryErrorKeyErrorKeyboardInterruptLookupErrorMemoryErrorModuleNotFoundError	NameErrorNoneNotADirectoryErrorNotImplementedNotImplementedErrorOSErrorOverflowErrorPendingDeprecationWarningPermissionErrorProcessLookupErrorZPythonFinalizationErrorRecursionErrorReferenceErrorResourceWarningRuntimeErrorRuntimeWarningStopAsyncIterationStopIterationSyntaxErrorSyntaxWarningSystemError
SystemExitTabErrorTimeoutErrorTrue	TypeErrorUnboundLocalErrorUnicodeDecodeErrorUnicodeEncodeErrorUnicodeErrorUnicodeTranslateErrorUnicodeWarningUserWarning
ValueErrorWarningZWindowsErrorZeroDivisionError__build_class__	__debug__
__import__absZaiterallZanextanyasciibinbool
breakpoint	bytearraybytescallablechrclassmethodcompilecomplex	copyrightcreditsdelattrdictdirdivmod	enumerateevalexecexitfilterfloatformat	frozensetgetattrglobalshasattrhashhelphexidint
isinstance
issubclassiterlenlicenselistlocalsmapmax
memoryviewminnextobjectoctopenordpowprintpropertyquitreprreversedroundsetsetattrslicesortedstaticmethodsumsupertupletypevarszip_)	Z	__cinit__Z__dealloc__Z__richcmp____next__	__await__	__aiter__	__anext__Z__getbuffer__Z__releasebuffer__inlineZCYTHON_INLINE)Zustringr   r   c                   @   sJ   e Zd ZdZdZdZdZdZddd	Zd
d Z	dd Z
dd Zdd ZdS )IncludeCodez]
    An include file and/or verbatim C code to be included in the
    generated sources.
    r   r   r   NTFc                 C   s   | j | _t|  j d7  _ i | _|rb|d dkrR|d dkrRd|| jd< d}nd|| jd< |rr|| j| j< |r| j| _n|r| j| _n| j| _d S )	Nr   r   <>z#include {}Fz#include "{}")	counterorderr   piecesr   INITIALlocationLATEEARLY)selfincludeZverbatimZlateinitial r   8/tmp/pip-unpacked-wheel-fhl22ezh/Cython/Compiler/Code.py__init__  s    

zIncludeCode.__init__c                 C   s<   ||kr0|| }t | j|j|_|j| j n| ||< dS )z
        Insert `self` in dict `d` with key `key`. If that key already
        exists, update the attributes of the existing value with `self`.
        N)r   r   r   update)r   dkeyotherr   r   r   dict_update.  s
    zIncludeCode.dict_updatec                 C   s   | j S N)r   r   r   r   r   sortkey:  s    zIncludeCode.sortkeyc                 C   s   | j dS )z
        Return the main piece of C code, corresponding to the include
        file. If there was no include file, return None.
        r   )r   getr   r   r   r   	mainpiece=  s    zIncludeCode.mainpiecec                 C   s$   t | jD ]}|| j|  q
d S r   )r   r   putln)r   codekr   r   r   writeD  s    zIncludeCode.write)NNTF)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r      s   
r   c                  C   s*   t jt jt jt} t j| dS )NZUtility)r   pathdirnameabspath__file__join)Z
Cython_dirr   r   r   get_utility_dirJ  s    r   c              
   C   sB   t jt | }ttj|dd}| W  5 Q R  S Q R X dS )zh
    Read all lines of the file at the provided path from a path relative
    to get_utility_dir().
    UTF-8)encodingN)r   r   r   r   r   r
   Zopen_source_file	readlines)r   filenamefr   r   r   read_utilities_from_utility_dir[  s    r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )AbstractUtilityCodeNc                 C   s   d S r   r   )r   outputr   r   r   put_codel  s    zAbstractUtilityCode.put_codec                 K   s   d S r   r   r   kwargsr   r   r   get_treeo  s    zAbstractUtilityCode.get_treec                 K   s   d S r   r   r   r   r   r   get_shared_library_scoper  s    z,AbstractUtilityCode.get_shared_library_scope)r   r   r   requiresr   r   r   r   r   r   r   r   h  s   r   c                   @   s   e Zd ZdZdZi ZedddZedd Zedd	 Z	ei fd
dZ
edddZedjfddZdd Zdd Zdd ZdddZdS )UtilityCodeBasea(  
    Support for loading utility code from a file.

    Code sections in the file can be specified as follows:

        ##### MyUtility.proto #####

        [proto declarations]

        ##### MyUtility.init #####

        [code run at module initialization]

        ##### MyUtility #####
        #@requires: MyOtherUtility
        #@substitute: naming

        [definitions]

        ##### MyUtility #####
        #@substitute: tempita

        [requires tempita substitution
         - context can't be specified here though so only
           tempita utility that requires no external context
           will benefit from this tag
         - only necessary when @required from non-tempita code]

    for prototypes and implementation respectively.  For non-python or
    -cython files backslashes should be used instead.  5 to 30 comment
    characters may be used on either side.

    If the @cname decorator is not used and this is a CythonUtilityCode,
    one should pass in the 'name' keyword argument to be used for name
    mangling of such entries.
    FNc                 C   s2  |d krd S d |}|rd|krd|d krzt|tt}W nH ttfk
r }	 z&td| d| d|dd|	 W 5 d }	~	X Y nX ||krtd	| d| d
|dd|}d| | }|dkr||d< n"|dkr||d< n|d }
||
|< |r.|d }
| D ]\}}|
	|t
 | qd S )N

substitutenamingz&Error parsing templated utility code '.z
' at line r   z: zFFound useless 'substitute: naming' declaration without replacements. (:)protor   implr   r   )r   r   r   r   r   r@   re   rR   items
setdefaultr   r   )clsutilitynamer   linesbegin_linenotagsr   Znew_codeeZall_tagsvaluesr   r   r   _add_utility  s4    


zUtilityCodeBase._add_utilityc              	   C   s  | j |}|r|S tj|\}}|dkrJd}ttdjd}t	j
}n(d}ttdjd}ttdjd}td	d
|i j}tdj}	t|}
tdd }g }tt}d  } }}d}t|
D ]\}}||}|rt|drN| |||||| |d }|d d = |  |d}|	|}|r@| \}}nd}|| }n$||d |d |d q|||| q|d krtd| |||||| t|}|| j |< |S )N)z.pyxz.pyz.pxdz.pxi#z^\s*#(?!\s*cython\s*:).* /z^\s*//.*|/\*[^*]*\*/z	\s+(\\?)$z\1zd^%(C)s{5,30}\s*(?P<name>(?:\w|\.)+)\s*%(C)s{5,30}|^%(C)s+@(?P<tag>\w+)\s*:\s*(?P<value>(?:\w|[.:])+)Cz?(.+)[.](proto(?:[.]\S+)?|impl|init|cleanup|module_state_decls)$c                   S   s
   d d i gS r   r   r   r   r   r   <lambda>      z:UtilityCodeBase.load_utilities_from_file.<locals>.<lambda>r   r   r   r   tagvaluezEmpty utility code file)_utility_cacher   r   r   splitextr   r   rw   subr   rstripmatchread_utilities_hookr   r   r   groupr  cleargroupsaddappendre   r|   )r   r   	utilitiesr   extcommentstrip_commentsr  Zmatch_specialZ
match_typeZ	all_linesr   r   r   r   r   r   linenolinemmtyper   r   r   load_utilities_from_file  s\    




z(UtilityCodeBase.load_utilities_from_filec           
         sr  d|kr| dd\}s t }|| \}}}|rd|krld|d krlt tsltj|f|S | | D ]\}}	||krq||dkrЈr fddt|	D }	n fddt|	D }	n@|dkr|	d	dh }	|	sq|n$|	sd
}	nt	|	dkrt
|	d }	|	||< q||d
k	r,||d< |d
k	r>||d< d|krP||d< d|krhrh|d<  f |S )z
        Load utility code from a file specified by from_file (relative to
        Cython/Utility) and name util_code_name.
        z::r   r   Ztempitar   c                    s   g | ]} j |fqS r   )load.0depr   	from_fileZorig_kwargsr   r   
<listcomp>  s   z(UtilityCodeBase.load.<locals>.<listcomp>c                    s   g | ]}  |qS r   )load_cachedr  )r   r#  r   r   r$    s   r   Nr   r   r   r   file)rsplitr"   r  r   TempitaUtilityCoder  copyr   r   r   r   )
r   util_code_namer#  r   r  r   r   r   r   r   r   r"  r   r    sN    





zUtilityCodeBase.loadc                 C   sB   ||| f}z
|| W S  t k
r(   Y nX | || }||< |S )z`
        Calls .load(), but using a per-type cache based on utility name and file name.
        )r@   r  )r   utility_code_namer#  Z_UtilityCodeBase__cacher   r   r   r   r   r%  4  s    

zUtilityCodeBase.load_cachedc                    sb   | j ||f|}|s,||j||jfS g g    fdd| dd fS )a  
        Load a utility code as a string. Returns (proto, implementation).

        If 'include_requires=True', concatenates all requirements before the actually
        requested utility code, separately for proto and impl part.

        In a lot of cases it may be better to use regular "load" and "CCodeWriter.put_code_here"
        since that is able to apply the code transformations to the code too.
        c                    sN   | j r| j D ]}| q| jr2| | j | jrJ | | j d S r   )r   r   r  format_coder   )Z	util_coder!  Zimplsprependprotosr   r   r.  S  s    

z/UtilityCodeBase.load_as_string.<locals>.prependr  )r  r,  r   r   r   )r   r*  r#  Zinclude_requiresr   utilr   r-  r   load_as_stringA  s    


	zUtilityCodeBase.load_as_stringz\n\n+c                 C   s   |r|d|  d }|S )z3
        Format a code section for output.
        r   z

strip)r   code_stringZreplace_empty_linesr   r   r   r,  _  s    zUtilityCodeBase.format_codec                 C   s   dt | j| jf S )Nz<%s(%s)>)r   r   r   r   r   r   r   __repr__g  s    zUtilityCodeBase.__repr__c                 K   s   d S r   r   r   r   r   r   r   j  s    zUtilityCodeBase.get_treec                 K   s   d S r   r   r   r   r   r   r   m  s    z(UtilityCodeBase.get_shared_library_scopec                 C   s   | S r   r   )r   Zmemodictr   r   r   __deepcopy__p  s    zUtilityCodeBase.__deepcopy__)N)F)N)r   r   r   r   Zis_cython_utilityr
  rv   r  r  r  r%  r1  r   rw   r  r,  r5  r   r   r6  r   r   r   r   r   v  s$   % 
=
5r   c                	   @   sl   e Zd ZdZdddddgZdd	d
Zdd Zdd Zdd ZdddZ	dde
dddZdd Zdd ZdS )UtilityCodeaF  
    Stores utility code to add during code generation.

    See GlobalState.put_utility_code.

    hashes/equals by instance

    proto           C prototypes
    impl            implementation code
    init            code to call on module initialization
    requires        utility code dependencies
    proto_block     the place in the resulting file where the prototype should
                    end up
    name            name of the utility code (or None)
    file            filename of the utility code file this utility was loaded
                    from (or None)
    r   r   initcleanupmodule_state_declsNutility_code_protoc
           
         s`   | _ | _| _| _| _| _i  _g  _| _| _	|	 _
t fdd jD  _d S )Nc                 3   s   | ]}t  |d V  qd S r   )r   )r   partr   r   r   	<genexpr>  s     z'UtilityCode.__init__.<locals>.<genexpr>)r   r   r8  r9  r:  r   _cachespecialize_listproto_blockr   r&  r   
code_parts_parts_tuple)
r   r   r   r8  r9  r:  r   r@  r   r&  r   r   r   r     s    zUtilityCode.__init__c                 C   s
   t | jS r   )r   rB  r   r   r   r   __hash__  s    zUtilityCode.__hash__c                 C   sJ   | |krdS t | t | }}||k	r>t||s>t| |s>dS | j|jkS )NTF)r   r   rB  )r   r   	self_typeZ
other_typer   r   r   __eq__  s    zUtilityCode.__eq__c                 C   s   |dkrdS || S zY
        Format a string in this utility code with context. If None, do nothing.
        Nr   r   scontextr   r   r   none_or_sub  s    zUtilityCode.none_or_subc                    s   | j }|d k	r6|  d< |  d< d| d f }tt  }z| j| W S  tk
r   | jd krrd }n fdd| jD }t	| 
| j | 
| j | 
| j | 
| j | 
| j || j| }| j|< | j| | Y S X d S )Nr   	type_namez%s[%s]c                    s   g | ]}|  qS r   )
specialize)r   rdatar   r   r$    s     z*UtilityCode.specialize.<locals>.<listcomp>)r   Zempty_declaration_codeZspecialization_namer   r   r   r>  r@   r   r7  rJ  r   r   r8  r9  r:  r@  r?  r  )r   Z
pyrex_typerO  r   r   r   rH  r   rN  r   rL    s0    
zUtilityCode.specializeCCodeWriterGlobalState)writerr   	code_typec                 C   s   t | |}|sd S |dk}t| ||\}}|dkr6|nd}|d| j |rNdnd | d |r|s||| j d|  n
|| d S )N)r   r   r   r  z/* r   z */r   )r   process_utility_ccoder   r   put_or_includeput)r   rR  r   rS  r4  Zcan_be_reusedresult_is_module_specificZcode_type_namer   r   r   _put_code_section  s    
$zUtilityCode._put_code_sectionc                 C   s>   | j s
d S |d }| ||d |||j |  d S )Ninit_globalsr8  )r8  rX  r   error_goto_if_PyErr
module_pos)r   r   rR  r   r   r   _put_init_code_section  s    z"UtilityCode._put_init_code_sectionc                 C   s   | j r| j D ]}|| q| jr6| || j |d | jrN| |d |d | jrltjrl| |d |d | j	r| |d |d | j
r| | d S )Nr   utility_code_defr   cleanup_globalsr9  module_state_contentsr:  )r   use_utility_coder   rX  r@  r   r9  r   generate_cleanup_coder:  r8  r\  )r   r   
dependencyr   r   r   r     s    
zUtilityCode.put_code)	NNNNNNr;  NN)N)r   r   r   r   rA  r   rC  rE  rJ  rL  r   rX  r\  r   r   r   r   r   r7  u  s             
	

r7  F)regexis_module_specific_last_macro_processorc                    s     d  fdd}|S )z8Decorator to chain the code macro processors below.
    r   c                    s0   t  ttd fdd}|d< |S )N)utility_coder4  c                    s   d}d k	r| ||\}}r>D ]}||kr$ q>q$||fS d krT | ||}nt t ||}D ]$}||krltd| d| j dql|O }||fS )NFzLeft-over utility code macro 'z()' found in '')r   r  r   rR   r   )rf  r   r4  rW  macro)funcrd  last_processormacro_namesrc  r   r   process  s     z=add_macro_processor.<locals>.build_processor.<locals>.processr   )r   r7  r   )ri  rl  re  rd  rj  rk  rc  )ri  r   build_processor  s    z,add_macro_processor.<locals>.build_processorr   )rc  rd  re  rk  rn  r   rm  r   add_macro_processor  s    !ro  ZCSTRINGz)CSTRING\(\s*"""([^"]*(?:"[^"]+)*)"""\s*\))rc  c                 C   s*   | ddd}ddd | D S )zUReplace CSTRING('''xyz''') by a C compatible string, taking care of line breaks.
    r   "z\042r  c                 s   sB   | ]:}| d r| dr&d| dnd|dd  dV  qdS )\z\\rp  z\n"
Nr   "
)endswithr   r  r   r   r   r=  -  s   z!_wrap_c_string.<locals>.<genexpr>)r  replacer   
splitlines)r   matchobjcontentr   r   r   _wrap_c_string%  s    
ry  )rf  c                 C   s
   |  |S r   )r,  )rf  r   r   r   r   r   _format_impl_code2  s    rz  ZCALL_UNBOUND_METHODTzLCALL_UNBOUND_METHOD\(([a-zA-Z_]+),\s*"([^"]+)",\s*([^),\s]+)((?:,[^),]+)*)\))rd  rc  c                 C   sx   |  \}}}}d| }|r8dd |dd dD ng }t|dk s^tdt|d	d
| tj d||||S )zRReplace 'UNBOUND_METHOD(type, "name")' by a constant Python identifier cname.
    z&%sc                 S   s   g | ]}|  qS r   r2  )r   argr   r   r   r$  H  s     z*_inject_unbound_method.<locals>.<listcomp>r   N,r   z'CALL_UNBOUND_METHOD() does not support r   z call arguments->)r  splitr   r"   cached_unbound_method_call_coder   modulestateglobal_cname)r   rw  
type_cnamemethod_name	obj_cnameargsr   r   r   _inject_unbound_method7  s    $"
   r  ZPYIDENTZ	PYUNICODEzPY(IDENT|UNICODE)\("([^"]+)"\)c                 C   s0   |  \}}dtj| jt||dkdjf S )zDReplace 'PYIDENT("xyz")' by a constant Python identifier cname.
    %s->%sZIDENT
identifier)r  r   r  get_py_string_constr	   EncodedStringcname)r   rw  Zstr_typer   r   r   r   _inject_string_constantO  s     r  ZEMPTYzEMPTY\((bytes|unicode|tuple)\)c                 C   s$   | d}dtjttd| f S )zLReplace 'EMPTY(bytes|tuple|...)' by a constant Python identifier cname.
    r   r  Zempty_)r  r   r  r   )r   rw  rK  r   r   r   !_inject_empty_collection_constant^  s
    	
r  ZCGLOBALz(NAMED_)?CGLOBAL\(([^)]+)\)c                 C   s*   |  \}}|rtt|}tj d| S Nr}  )r  r   r   r  )r   rw  Zis_namedr   r   r   r   _inject_cglobalm  s    
r  c                 C   s   |S )z;Entry point for code processors, must be defined last.
    r   )rf  r   r4  r   r   r   rT  y  s    rT  c                 C   sr   | sdS |r| d| }|r&||d< z||  }W n4 t k
rf   ddlm} || |d }|| < Y nX ||S )z+Run tempita on string s with given context.Nr   Z__namer   r   r   )r@   ZTempitar   r   )rH  rI  r&  r   Z__cachetemplater   r   r   r   sub_tempita  s    r  c                       s:   e Zd Zd fdd	Zeddi fddZdd Z  ZS )	r(  Nc                    sb   |d kri }n|  }t||||}t||||}t||||}t j||f|||d| d S )N)r8  r   r&  )r)  r  r   r   )r   r   r   r   r8  r&  rI  r   	__class__r   r   r     s        zTempitaUtilityCode.__init__c                 C   sp   |rt t| nd }t|d k	s(t| |||f}z
|| W S  tk
rR   Y nX | j|||d }||< |S )N)rI  )r   r   r   r   r"   r@   r  )r   r+  r#  rI  Z_TempitaUtilityCode__cacheZcontext_keyr   r   r   r   r   r%    s    
zTempitaUtilityCode.load_cachedc                 C   s   |dkrdS t ||| j| jS rF  )r  r&  r   rG  r   r   r   rJ    s    zTempitaUtilityCode.none_or_sub)NNNNNN)r   r   r   r   rv   r%  rJ  __classcell__r   r   r  r   r(    s   r(  c                   @   s(   e Zd ZdZdZ dZdd Zdd ZdS )LazyUtilityCodez
    Utility code that calls a callback with the root code writer when
    available. Useful when you only have 'env' but not 'code'.
    z<lazy>Nc                 C   s
   || _ d S r   )callback)r   r  r   r   r   r     s    zLazyUtilityCode.__init__c                 C   s   |  |j}|| d S r   )r  
rootwriterr`  )r   globalstater   r   r   r   r     s    zLazyUtilityCode.put_code)r   r   r   r   r   r   r   r   r   r   r   r    s
   r  c                   @   s   e Zd Ze dfddZdd Zd2ddZd3d	d
Zd4ddZdd Z	dd Z
d5ddZdd Zdd Zdd Zdd Zdd Zd6d d!Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )7FunctionStateNc                 C   s   || _ || _|| _d | _d| _t | _|  | _| 	  d | _
d | _g | _d | _d | _d| _d| _g | _i | _i | _t | _d| _d | _g | _d| _d| _d| _d| _d S )Nr   FT)names_takenownerscopeerror_labellabel_counterr   labels_used	new_labelreturn_labelnew_error_labelcontinue_labelbreak_labelyield_labelsZexc_varsZcurrent_except	can_trace	gil_ownedtemps_allocated
temps_freetemps_used_typezombie_tempstemp_counterclosure_tempscollect_temps_stackshould_declare_error_indicatoruses_error_indicatorZerror_without_exceptionZneeds_refnanny)r   r  r  r  r   r   r   r     s4    
zFunctionState.__init__c                 C   sB   | j r>|  }|r>d| jjddd t|D f }t|d S )Nz-TEMPGUARD: Temps left over at end of '%s': %s, c                 S   s   g | ]\}}}d ||f qS )z%s [%s]r   )r   r   ctypeZ	is_pytempr   r   r   r$    s   z/FunctionState.validate_exit.<locals>.<listcomp>)r  temps_in_user  r   r   r   rR   )r   Z	leftoversmsgr   r   r   validate_exit  s    zFunctionState.validate_exitc                 C   s6   | j }|d | _ dtj|f }|d k	r2|d| 7 }|S )Nr   %s%dr   )r  r   Zlabel_prefix)r   r   nlabelr   r   r   r    s    
zFunctionState.new_labelyieldc                 C   s0   |  d| }t| jd |f}| j| |S )Nzresume_from_%sr   )r  r   r  r  )r   Z	expr_typer  Znum_and_labelr   r   r   new_yield_label  s    zFunctionState.new_yield_labelr  c                 C   s   | j }| |d | _ |S )Nerror)r  r  )r   prefixZold_err_lblr   r   r   r    s    zFunctionState.new_error_labelc                 C   s   | j | jfS r   r  r  r   r   r   r   get_loop_labels  s    zFunctionState.get_loop_labelsc                 C   s   |\| _ | _d S r   r  r   labelsr   r   r   set_loop_labels#  s    zFunctionState.set_loop_labelsc                 C   s.   |   }| | |d | |d f |S )Ncontinuebreak)r  r  r  )r   r  
old_labelsr   r   r   new_loop_labels'  s    zFunctionState.new_loop_labelsc                 C   s   | j | j| j| jfS r   r  r  r  r  r   r   r   r   get_all_labels.  s
    zFunctionState.get_all_labelsc                 C   s   |\| _ | _| _| _d S r   r  r  r   r   r   set_all_labels5  s    zFunctionState.set_all_labelsc                 C   sV   |   }g }t|ddddgD ](\}}|r<|| | q|| q| | |S )Nr  r  returnr  )r  r   r  r  r  )r   r  
new_labelsZ	old_labelr   r   r   r   all_new_labels;  s    
zFunctionState.all_new_labelsc                 C   s   | j | d S r   )r  r  r   lblr   r   r   	use_labelF  s    zFunctionState.use_labelc                 C   s
   || j kS r   )r  r  r   r   r   
label_usedI  s    zFunctionState.label_usedFTc                 C   sd  |j r|js|j}n^|jr(|js(|j}nJ|jrFddlm} ||}n,|j	rr|jsr| j
jd rr| j
tdd |js|d}| j||f}|r|dk	r|d r|d  }|d | nR|  jd7  _d	tj| jf }|| jkrqq| j||||f |s| j| ||f| j|< tjrD| j d
|||r:dndf  | j!r`| j!d ||f |S )a
  
        Allocates a temporary (which may create a new one or get a previously
        allocated and released one of the same type). Type is simply registered
        and handed back, but will usually be a PyrexType.

        If type.needs_refcounting, manage_ref comes into play. If manage_ref is set to
        True, the temp will be decref-ed on return statements and in exception
        handling clauses. Otherwise the caller has to deal with any reference
        counting of the variable.

        If not type.needs_refcounting, then manage_ref will be ignored, but it
        still has to be passed. It is recommended to pass False by convention
        if it is known that type will never be a reference counted type.

        static=True marks the temporary declaration with "static".
        This is only used when allocating backing store for a module-level
        C array literals.

        if reusable=False, the temp will not be reused after release.

        A C string referring to the variable is returned.
        r   )
PyrexTypes
cpp_localsZOptionalLocalsCppSupport.cppFNr   r  z/* %s allocated (%s)%s */r  	 - zombier   )"Zis_cv_qualifiedZis_referenceZcv_base_typeis_fake_referenceZref_base_typeZis_cfunctionr  r  Z
c_ptr_typeis_cpp_classr  
directivesr`  r7  r%  needs_refcountingr  r   popremover  r   codewriter_temp_prefixr  r  r  r  r  r  r   debug_temp_code_commentsr  r   r  )r   r   
manage_refstaticZreusabler  freelistresultr   r   r   allocate_tempN  s8    
  zFunctionState.allocate_tempc                 C   s   | j | \}}| j||f}|dkr>g t f}|| j||f< ||d krVtd| || jkrn|d | |d | tj	r| j
d||| jkrdndf  dS )zv
        Releases a temporary so that it can be reused by other code needing
        a temp of the same type.
        Nr   zTemp %s freed twice!r   z/* %s released %s*/r  r  )r  r  r   r   rR   r  r  r  r   r  r  r   )r   r   r   r  r  r   r   r   release_temp  s    

 zFunctionState.release_tempc                 C   sV   g }| j D ]F\}}}}| j||f}|dks:||d kr
||||oJ|jf q
|S )zxReturn a list of (cname,type,manage_ref) tuples of temp names and their type
        that are currently in use.
        Nr   )r  r  r   r  r  )r   usedr   r   r  r  r  r   r   r   r    s    zFunctionState.temps_in_usec                 C   s   dd |   D S )zReturn a list of (cname,type) tuples of temp names and their type
        that are currently in use. This includes only temps
        with a reference counted type which owns its reference.
        c                 S   s$   g | ]\}}}|r|j r||fqS r   )r  )r   r   r   r  r   r   r   r$    s    z9FunctionState.temps_holding_reference.<locals>.<listcomp>)r  r   r   r   r   temps_holding_reference  s    z%FunctionState.temps_holding_referencec                 C   s   dd | j D S )zRReturn a list of (cname, type) tuples of refcount-managed Python objects.
        c                 S   s    g | ]\}}}}|r||fqS r   r   )r   r  r   r  r  r   r   r   r$    s   
z3FunctionState.all_managed_temps.<locals>.<listcomp>)r  r   r   r   r   all_managed_temps  s    zFunctionState.all_managed_tempsc                 C   s   t dd | j D S )zReturn a list of (cname, type) tuples of refcount-managed Python
        objects that are not currently in use.  This is used by
        try-except and try-finally blocks to clean up temps in the
        error case.
        c                 S   s.   g | ]&\\}}}|r|d  D ]}||fqqS )r   r   )r   r   r  r  r  r   r   r   r$    s
   
 
 z8FunctionState.all_free_managed_temps.<locals>.<listcomp>)r   r  r   r   r   r   r   all_free_managed_temps  s    z$FunctionState.all_free_managed_tempsc                 C   s   | j t  dS )zJ
        Useful to find out which temps were used in a code block
        N)r  r  r   r   r   r   r   start_collecting_temps  s    z$FunctionState.start_collecting_tempsc                 C   s
   | j  S r   )r  r  r   r   r   r   stop_collecting_temps  s    z#FunctionState.stop_collecting_tempsc                 C   s   t || _d S r   )ClosureTempAllocatorr  r   r  r   r   r   init_closure_temps  s    z FunctionState.init_closure_temps)N)r  )r  )r  )FT)r   r   r   r   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    s,   +




:	r  c                   @   s   e Zd ZdZdddZdS )NumConstzGlobal info about a Python number constant held by GlobalState.

    cname       string
    value       string
    py_type     string     int, long, float
    value_code  string     evaluation code if different from value
    Nc                 C   s    || _ || _|| _|p|| _d S r   )r  r	  py_type
value_code)r   r  r	  r  r  r   r   r   r     s    zNumConst.__init__)Nr   r   r   r   r   r   r   r   r   r    s   r  c                   @   s   e Zd ZdZdd ZdS )PyObjectConstz>Global info about a generic constant held by GlobalState.
    c                 C   s   || _ || _d S r   )r  r   r   r  r   r   r   r   r     s    zPyObjectConst.__init__Nr  r   r   r   r   r    s   r  )possible_unicode_identifierpossible_bytes_identifierreplace_identifierfind_alphanumsz(?![0-9])\w+$s   (?![0-9])\w+$z[^a-zA-Z0-9_]+z([a-zA-Z0-9]+)c                   @   s"   e Zd ZdZdd ZdddZdS )StringConstz?Global info about a C string constant held by GlobalState.
    c                 C   s"   || _ || _t|| _d | _d S r   )r  textr	   Zescape_byte_stringescaped_value
py_strings)r   r  r  byte_stringr   r   r   r     s    zStringConst.__init__Nc           
      C   s,  | j }|s|d kr d  }}d}n,d}| }|dkr>d }d }ndt|}|rVd}n2|d krt|trvtt|}qtt|}nd}|||f}| j	d kri | _	n"z| j	| W S  t
k
r   Y nX |rtjd ntj |rdnd |rd| nd d| jttjd   }t||||}	|	| j	|< |	S )	NTF)utf8zutf-8rn   Zusasciizus-asciir  r   ubr   )r  lowerr   r  r   rs   rp   r  r  r  r@   r   interned_prefixesZpy_const_prefixr  r   const_prefixPyStringConst)
r   r   r  r  Zencoding_key
is_unicodeinternr   Zpystring_cname	py_stringr   r   r   r    s:    


H
zStringConst.get_py_string_const)N)r   r   r   r   r   r  r   r   r   r   r    s   r  c                   @   s"   e Zd ZdZdddZdd ZdS )	r  zDGlobal info about a Python string constant held by GlobalState.
    Fc                 C   s   || _ || _|| _|| _d S r   )r  r   r  r  )r   r  r   r  r  r   r   r   r   8  s    zPyStringConst.__init__c                 C   s   | j |j k S r   r  r   r   r   r   r   __lt__>  s    zPyStringConst.__lt__N)FF)r   r   r   r   r   r  r   r   r   r   r  0  s   
r  c                %   @   s  e Zd Zi Zddddddddd	d
dddddddddddddddddddddd d!d"d#d$d%g%Zddddd%gZd{d'd(Zd)d* Zd+d, Zd-d. Z	d/d0 Z
d1d2 Zd3d4 Zd|d5d6Zd}d8d9Zd:d; Zd~d<d=Zd>d? Zd@dA ZdBdC ZddDdEZdFdG ZdHdI ZdJdK ZddLdMZdNdO ZdPdQ ZdRdS ZddUdVZedWdXdYZdZd[ Zd\d] Z d^d_ Z!d`da Z"dbdc Z#ddedfZ$dgdh Z%didj Z&dkdl Z'dmdn Z(dodp Z)dqdr Z*dsdt Z+dudv Z,dwdx Z-dydz Z.d&S )rQ  Zh_codefilename_tableZutility_code_proto_before_typesZnumeric_typedefsZcomplex_type_declarationsZtype_declarationsr;  Zmodule_declarationsZtypeinfoZbefore_global_var
global_varstring_declsdeclsZlate_includesmodule_stater_  Zmodule_state_endconstant_name_definesmodule_state_clearmodule_state_traverseZmodule_codeZmodule_exttypesinitfunc_declarationsZinit_modulepystring_tablecached_builtinscached_constantsinit_constantsinit_codeobjectsrY  r^  cleanup_moduleZmain_methodutility_code_pragmasr]  utility_code_pragmas_endendNc                 C   s   i | _ g | _i | _t | _i | _d| _|| _|| _i | _	|| _
i | _i | _i | _i | _g | _i | _g | _i | _i | _t | _||  || _d S )NF)r  filename_listinput_file_contentsr   utility_codesdeclared_cnamesZin_utility_code_generationcode_configcommon_utility_include_dirpartsmodule_nodeconst_cnames_usedstring_const_indexdedup_const_indexpyunicode_ptr_const_indexcodeobject_constantsnum_const_indexarg_default_constantsconst_array_counterscached_cmethodsinitialised_constantsset_global_stater  )r   rR  r!  r  r  r   r   r   r     s,    
zGlobalState.__init__c                 C   s  | j }t| jD ]0\}}|  }| j|< |dkr|d|  qtjsR| jd= n:| jd }|dt	j
 dt	j d |dt	j d | jd	 }|jd
t	j
 dt	j ddd |dt	j d |td | jd }|d | jd }|dt	j
 dt	j d |dt	j d tjs:| jd= n:| jd }|dt	j
 dt	j d |dt	j d | jd }|d |d | jd }| jjr|d |d |d d S )Nr   z/* #### Code section: %s ### */r  zint __Pyx_InitCachedBuiltins( *r   CYTHON_UNUSED_VAR();r  zint __Pyx_InitCachedConstants(T)refnannyZ__Pyx_InitCachedConstantsrY  zint __Pyx_InitGlobals(void)r  zint __Pyx_InitConstants(r^  zvoid __Pyx_CleanupGlobals(r;  r  z)/* --- Runtime support code (head) --- */r]  z
#line 1 "cython_utility"
z"/* --- Runtime support code --- */)r  r   code_layoutinsertion_pointr   r   r   cache_builtinsstart_initcfuncr   modulestatetype_cnamemodulestatevalue_cnameput_setup_refcount_contextr	   r  ra  r  emit_linenumsr   )r   r  ir<  wr   r   r   r   initialize_main_c_code  sR    














z"GlobalState.initialize_main_c_codec                 C   s$   | j }| jD ]}| | j|< qd S r   )r  h_code_layoutr2  r   )r   r  r<  r   r   r   initialize_main_h_code  s    
z"GlobalState.initialize_main_h_codec                 C   s   |    | jd }tdd}|||j |d | jd }tdd}|||j |d | jd }td	d}|||j |d d S )
Nr]  ZTypeConversionszTypeConversion.cr  r  ZUtilityCodePragmasModuleSetupCode.cr  ZUtilityCodePragmasEnd)	close_global_declsr   r(  r%  rV  r,  r   r   r7  )r   r   r0  r   r   r   finalize_main_c_code  s    




z GlobalState.finalize_main_c_codec                 C   s
   | j | S r   )r   )r   r   r   r   r   __getitem__  s    zGlobalState.__getitem__c                 C   sL  |    tjrV| jd }|d ||jrD||j |d |d |  | jd }|	  |d ||jr||j |	  |d |d |  dD ]L}| j| }|d ||jr||j |d |d |  qtj
r$| jd }|d |  tj
rH| jd }|d |  d S )	Nr  	return 0;
return -1;}r  )rY  r  r^  r  )generate_const_declarationsr   r3  r   r   r  r  	put_labelexit_cfunc_scopeput_finish_refcount_contextra  )r   r:  r<  r   r   r   r?    sD    
















zGlobalState.close_global_declsc                 C   s   | d  d|j  d S )Nr	  zstatic PyObject *%s;)r   r  r   entryr   r   r   put_pyobject_decl-  s    zGlobalState.put_pyobject_declc                 C   s,   |d k	r"|| j krd S | j | | jd S )Nr  )r+  r  r   r   targetr   r   r   get_cached_constants_writer2  s
    
z'GlobalState.get_cached_constants_writerFc                 C   sD   |rdp
d}z| j ||f }W n  tk
r>   | ||}Y nX |S )Nlongr   r'  r@   new_num_const)r   	str_valuelongnessr  cr   r   r   get_int_const:  s    zGlobalState.get_int_constc                 C   s:   z| j |df }W n" tk
r4   | |d|}Y nX |S )Nr   rP  )r   rR  r  rT  r   r   r   get_float_constB  s
    zGlobalState.get_float_constc                 C   s@   |d k	r | j |}|d k	r |S | |}|d k	r<|| j |< |S r   )r$  r   new_array_const_cname)r   r  	dedup_keyconstr   r   r   get_py_constI  s    

zGlobalState.get_py_constc                 C   s$   |  d}t||}| j| |S Nr  )new_const_cnamer  r(  r  )r   r   r  rT  r   r   r   get_argument_default_constS  s    

z&GlobalState.get_argument_default_constc                 C   sL   |j r| }n| }z| j| }W n  tk
rF   | ||}Y nX |S r   )r  Z
utf8encodeZ
byteencoder#  r@   new_string_const)r   r  r  rT  r   r   r   get_string_const\  s    
zGlobalState.get_string_constc                 C   sD   |j s
tz| j| }W n& tk
r>   |   }| j|< Y nX |S r   )r  r"   r%  r@   r\  )r   r  rT  r   r   r   get_pyunicode_ptr_consth  s    
z#GlobalState.get_pyunicode_ptr_constc                 C   s   |  |}||j|}|S r   )r_  r  r   )r   r  r  Zc_stringr  r   r   r   r  q  s    
zGlobalState.get_py_string_constc                 C   s,   t | j}tj d| d}| j| |S )N[])r   r&  r   codeobjtab_cnamer  )r   nodeidxr   r   r   r   get_py_codeobj_constw  s    
z GlobalState.get_py_codeobj_constc                 C   s   | j |ddS NTr  r  r   r  r   r   r   get_interned_identifier}  s    z#GlobalState.get_interned_identifierc                 C   s$   |  |}t|||}|| j|< |S r   )new_string_const_cnamer  r#  )r   r  r  r  rT  r   r   r   r^    s    

zGlobalState.new_string_constc                 C   s,   |  ||}t||||}|| j||f< |S r   )new_num_const_cnamer  r'  )r   r	  r  r  r  rT  r   r   r   rQ    s    zGlobalState.new_num_constc                 C   s   | dd}| j|dS )NASCIIignore)r	  )decoder\  )r   Zbytes_valuer	  r   r   r   rk    s    z"GlobalState.new_string_const_cnamec                 C   sP   | j }|jddd }}||krD|| d  }||< |jd|d}qd||< |S )Nr  )sepr   r   r   )r"  r   )r   Z
format_strr  r  r	  r   r   r   r   unique_const_cname  s    zGlobalState.unique_const_cnamec                 C   s   |dkr|d7 }d}t j| }|dddddd}t|d	krr| |d
 |d d  d |dd   }nd||f }|S )NrO  Lr   r   r   +-Zneg_*   zlarge{counter}_   Z_xxx_i%s%s)r   r  ru  r   rq  )r   r	  r  r  r  r   r   r   rl    s    
"zGlobalState.new_num_const_cnamer  c                 C   sH   t d|d d d}| |d }|r6tj| }ntj}d||f S )Nr       z{sep}{counter}rw  )r  r3  rq  r   r  r   )r   r  r	  Zname_suffixr   r   r   r\    s    zGlobalState.new_const_cname)r  c                 C   s2   | j |d}|d | j |< tj | d| dS )Nr   r   ra  rb  )r)  r   r   pyrex_prefix)r   r  countr   r   r   rW    s    z!GlobalState.new_array_const_cnamec              	   C   sN   ||f}z| j | }W n2 tk
rH   | dd||f  }| j |< Y nX |S )NZumethodz%s_%s)r*  r@   r\  )r   r  r  r   r  r   r   r   get_cached_unbound_method  s     
z%GlobalState.get_cached_unbound_methodc           	      C   sJ   dt | }| t|d | ||}|g| }d|||d|f S )NzCallUnboundCMethod%dObjectHandling.cz__Pyx_%s(&%s%s, %s)r  )r   r`  r7  r%  r{  r   )	r   Zmodulestate_cnamer  r  r  Z
arg_cnamesr+  Zcache_cnamer  r   r   r   r    s    
z+GlobalState.cached_unbound_method_call_codec                 C   sV   |j rR|jrR| |j|rR| | |j}|tkr:t| }| |jt	
||j d S r   )
is_builtinZis_constshould_declarer  rK  r   renamed_py2_builtins_mapput_cached_builtin_initposr	   r  )r   rJ  r   r   r   r   add_cached_builtin_decl  s    
 z#GlobalState.add_cached_builtin_declc              	   C   sN   | j d }|| |j}| tdd |d|||||f  d S )Nr  ZGetBuiltinNamer|  z*%s = __Pyx_GetBuiltinName(%s); if (!%s) %s)	r    name_in_main_c_code_module_staterj  r  r`  r7  r%  r   
error_goto)r   r  r   r  r:  Zcname_in_modulestater   r   r   r    s    


z#GlobalState.put_cached_builtin_initc                 C   s,   |    |   |   |   |   d S r   )generate_cached_methods_declsgenerate_object_constant_declsgenerate_codeobject_constantsgenerate_string_constantsgenerate_num_constantsr   r   r   r   rE    s
    z'GlobalState.generate_const_declarationsTc                 C   sv   |dk rdnd}|rdnd}| j d }|d| d| d	| d
| d	 | j d }|d| d| d| d d S )Ni   r   Z
Py_ssize_tZPy_VISITZ__Pyx_VISIT_CONSTr  zfor (z i=0; i<z	; ++i) { z(traverse_module_state->z[i]); }r  z&; ++i) { Py_CLEAR(clear_module_state->)r   r   )r   struct_attr_cnamerz  may_have_refcyclesZcounter_typeZ
visit_callrR  r   r   r   )_generate_module_array_traverse_and_clear  s    
$
z5GlobalState._generate_module_array_traverse_and_clearc           
      C   s>  dd | j D }|  |D ]x\}}}| jd d|j|  |jjsLq| jd jd| |jddd	 |jjr||d
7 }| jd d| d qt	| j
 D ]\}}tj | }| jd d| d| d | j||dd t|}|d k	r|tjkr| jd }	|	d| d |	d|	| d  qd S )Nc                 S   s   g | ]}t |j|j|fqS r   r   r  r   rT  r   r   r   r$    s   z>GlobalState.generate_object_constant_decls.<locals>.<listcomp>r  z%s;r  zclear_module_state->TF)clear_before_decrefnannyz	->memviewr  z Py_VISIT(traverse_module_state->r/  
PyObject *ra  ];r  r^  zfor (size_t i=0; i<z; ++i) z{ Py_CLEAR(%s); }z[i])r(  sortr   r   r   declaration_coder  put_xdecref_clearis_memoryviewslicer   r)  r   r   ry  r  cleanup_level_for_type_prefixr   ra  rV  r  )
r   constsr   r  rT  r  rz  r  Zcleanup_levelZpart_writerr   r   r   r    s@    



z*GlobalState.generate_object_constant_declsc           	      C   s   | j s
d S | jd }| jd }g }t| j  D ]p\\}}}|| | t|j}|	d|  |	d|
||f  |	|
| d|
| d q0tjr| jd }|D ]}|	d|
| d	 qd S )
Nr  r  z__Pyx_CachedCFunction %s;z%s.type = (PyObject*)%s;z.method_name = &;r^  z	Py_CLEAR(z	.method);)r*  r   r   r   r  rj  r	   r  r  r   r  r   ra  )	r   declr8  cnamesr  r  r  Zmethod_name_cnamer9  r   r   r   r  "  s,    


 
z)GlobalState.generate_cached_methods_declsc              
   C   s  dd | j  D }|  g }d}t }dd }| jd }|D ]\}}}	t|	j}
|jd| d|
 d	d
d |	j	d k	r@t
|	j|krt
|	j}|	j	 D ]*}||| ||	jt
|j|f qq@t| j D ]`\}	}t|	\}}|r|d |d||f  |r|d |d||f  |d q|sFd S |  | jd }|d |tdt|t
|d  t
|}| jd dtj d| d | jtj|dd t|}|jt
d |rd|ks|d dkst|dd t|D }|dtjd|f  |d tj  t|D ]^\}\}}}||| }|j}| jd! d"|jtj|f  |d#|||||j|jf  q:|d$ |d% | t !d&d' | jd( }|d)tj|"tjtj|#| j$f  d S )*Nc                 S   s   g | ]}t |j|j|fqS r   r  r  r   r   r   r$  <  s     z9GlobalState.generate_string_constants.<locals>.<listcomp>r   c                 S   s*   | j r"| j dkr"d| j   dS dS d S )N)rm  USASCIIzUS-ASCIIUTF8r   rp  0)r   r  )r  r   r   r   normalise_encoding_nameB  s    zFGlobalState.generate_string_constants.<locals>.normalise_encoding_namer
  zstatic const char z[] = "z";T)safez#ifdef Py_UNICODE_WIDEz static Py_UNICODE %s[] = { %s };#else#endifr  r  a          typedef struct {
            const char *s;
        #if %(max_length)d <= 65535
            const unsigned short n;
        #elif %(max_length)d / 2 < INT_MAX
            const unsigned int n;
        #elif %(max_length)d / 2 < LONG_MAX
            const unsigned long n;
        #else
            const Py_ssize_t n;
        #endif
        #if %(num_encodings)d <= 31
            const unsigned int encoding : 5;
        #elif %(num_encodings)d <= 255
            const unsigned char encoding;
        #elif %(num_encodings)d <= 65535
            const unsigned short encoding;
        #else
            const Py_ssize_t encoding;
        #endif
            const unsigned int is_unicode : 1;
            const unsigned int intern : 1;
        } __Pyx_StringTabEntry;
        )
max_lengthZnum_encodingsr  r  ra  r  Fr  )r   r  c                 S   s   i | ]\}}||qS r   r   )r   r9  r   r   r   r   
<dictcomp>  s      z9GlobalState.generate_string_constants.<locals>.<dictcomp>z(static const char * const %s[] = { %s };r  z*static const __Pyx_StringTabEntry %s[] = {r  z#define %s %s[%s]z6{%s, sizeof(%s), %d, %d, %d}, /* PyObject cname: %s */z{0, 0, 0, 0, 0}z};ZInitStringszStringTools.cr  z*if (__Pyx_InitStrings(%s, %s, %s) < 0) %s;)%r#  r   r  r   r   r	   Zsplit_string_literalr  r   r  r   r  r  r  r   r%  r   Zencode_pyunicode_stringrV  r   dedentr|   r   Zstringtab_cnamer  r"   r   Zstringtab_encodings_cnamer   r  r  r`  r7  r%  r  r  r[  )r   Zc_constsr  Zlongest_pystring	encodingsr  Zdecls_writerr   r  rT  Zcliteralr  Zutf16_arrayZutf32_arrayr:  Zpy_string_countencodings_mapr  Zc_cnameZencodings_indexr  r  r   r   r   r  ;  s    







 &




z%GlobalState.generate_string_constantsc                 C   s$  | j d }dtj dtj d}| jsd|| |dtj d |d |  |d d S d	}d
}d
}d
}d
}d
}d
}	| jD ]n}
|
j}|j	st
|t|j|j }t
||j}t
||j}t
|t|
j}t
||jd
 }t
|	t|j}	q|	d }|td|  d|  d|  d|  d|  d|  d|  d | tdd || |d |d | jD ]}
|
|d q|d |d |d |d |d |  |d t| j}| j d dtj d| d | jtj|d d! d S )"Nr  zint __Pyx_CreateCodeObjects(r-  r   r.  r/  rB  rD  i  r   /   z=        typedef struct {
            unsigned int argcount : z.;
            unsigned int num_posonly_args : z-;
            unsigned int num_kwonly_args : z%;
            unsigned int nlocals : z#;
            unsigned int flags : z(;
            unsigned int first_line : z/;
            unsigned int line_table_length : z;;
        } __Pyx_PyCode_New_function_description;
        Z
NewCodeObjr>  z)PyObject* tuple_dedup_map = PyDict_New();z*if (unlikely(!tuple_dedup_map)) return -1;badzPy_DECREF(tuple_dedup_map);zbad:rC  r  r  ra  r  Fr  )r   r   r5  r6  r&  r4  r   rG  def_nodeZis_generator_expressionr   r   r  Znum_kwonly_argsZnum_posonly_argsvarnamesr  Znode_positionsrV  r   r  
bit_lengthr`  r7  r%  Zgenerate_codeobjrc  r  )r   r:  Zinit_functionZ	max_flagsZmax_func_argsZmax_kwonly_argsZmax_posonly_argsZmax_varsZmax_lineZmax_positionsrd  r  Zmax_linetable_lenZcode_object_countr   r   r   r    st    
















 z)GlobalState.generate_codeobject_constantsc              
   C   s   dd | j  D }|  | jd }|D ]\}}}}}}|j}| jd d|  | jd d|  | jd d	|  |d
krd}	n:|dkrd}	n,t|rd}	nt|	ddkrd}	nd}	|
|}
|d|
|	| ||
| jf  q*d S )Nc                 S   s2   g | ]*}|j |jd  dkt|j|j|j|fqS )r   rt  )r  r	  r   r  r  r   r   r   r$    s   z6GlobalState.generate_num_constants.<locals>.<listcomp>r  r  zPyObject *%s;r  z!Py_CLEAR(clear_module_state->%s);r  z-__Pyx_VISIT_CONST(traverse_module_state->%s);r   zPyFloat_FromDouble(%s)rO  zPyLong_FromString("%s", 0, 0)rt     zPyLong_FromLong(%sL)zPyLong_FromLong(%s)z%s = %s; %s)r'  r   r  r   r  r   r
   Zlong_literalr   lstripr  error_goto_if_nullr[  )r   r  r  r  r   r	  r  rT  r  functionZ
init_cnamer   r   r   r    s:    




 z"GlobalState.generate_num_constantsc                 C   sR   || j kr@| j | }t|jt|jks,t|j|jks<tdS || j |< dS d S )NFT)r  r   r   r"   r8  )r   r  rJ  r   r   r   r   r~    s    


zGlobalState.should_declarec                 C   sP   |  }z| j| }W n4 tk
rJ   t| j}| j| || j|< Y nX |S r   )Zget_filenametable_entryr  r@   r   r  r  )r   source_descrJ  indexr   r   r   lookup_filename*  s    
zGlobalState.lookup_filenamec              	   C   st   z| j | W S  tk
r    Y nX |jddd}zdd |D }W 5 t|drV|  X |sf|d || j |< |S )Nrm  rn  )r   error_handlingclosec                 S   s(   g | ] }d |  dddd qS )z * z*/z.*[inserted by cython to avoid comment closer]/z/*z-/[inserted by cython to avoid comment start]*)r  ru  rt  r   r   r   r$  <  s   
  z7GlobalState.commented_file_contents.<locals>.<listcomp>r  )r  r@   	get_linesr   r  r  )r   r  Zsource_fileFr   r   r   commented_file_contents4  s"    


 

z#GlobalState.commented_file_contentsc                 C   s(   |r$|| j kr$| j | ||  dS )a  
        Adds code to the C file. utility_code should
        a) implement __eq__/__hash__ for the purpose of knowing whether the same
           code has already been included
        b) implement put_code, which takes a globalstate instance

        See UtilityCode.
        N)r  r  r   )r   rf  r   r   r   r`  M  s    	zGlobalState.use_utility_codec                 C   s4   |d krd S |j r| |j  |jr0| |j d S r   )rf  r`  Zutility_code_definitionrI  r   r   r   use_entry_utility_codeZ  s    z"GlobalState.use_entry_utility_code)N)N)F)N)N)N)r  r  )T)/r   r   r   r  r1  r<  r   r;  r=  r@  rA  r?  rK  rN  rU  rV  rZ  r]  r_  r`  r  rf  rj  r^  rQ  rk  rq  rl  r\  r   rW  r{  r  r  r  rE  r  r  r  r  r  r  r~  r  r  r`  r  r   r   r   r   rQ  B  s   *
4*



		

	
		


*zG 
rQ  c                    s2   | j t  fdd}fdd}t||S )Nc                    s
    | j S r   )	funcstater   )attribute_ofr   r   r   f  s    z!funccontext_property.<locals>.getc                    s   t | j | d S r   )r   r  )r   r	  r  r   r   r   h  s    z!funccontext_property.<locals>.set)r   r   
attrgetterr   )ri  r   r   r   )r  r   r   funccontext_propertyc  s
    
r  c                   @   s   e Zd ZdddZdS )CCodeConfigTc                 C   s   || _ || _|| _d S r   )emit_code_commentsr8  c_line_in_traceback)r   r8  r  r  r   r   r   r   r  s    zCCodeConfig.__init__N)TTT)r   r   r   r   r   r   r   r   r  m  s   r  c                   @   s
  e Zd ZdZejd dd6ddZdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zed'd( Zed)d* Zd7d,d-Zd8d.d/Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Z d:d; Z!d<d= Z"d>d? Z#d@dA Z$dBdC Z%dDdE Z&dFdG Z'd9dHdIZ(d:dJdKZ)dLdM Z*dNdO Z+d;dPdQZ,dRdS Z-dTdU Z.d<dVdWZ/dXdY Z0dZd[ Z1d\d] Z2d^d_ Z3d=d`daZ4dbdc Z5e6ddde Z7e6dfdg Z8dhdi Z9djdk Z:d>dmdnZ;d?dodpZ<dqdr Z=dsdt Z>dudv Z?dwdx Z@dydz ZAd{d| ZBeCd}d~dZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLd@ddZMdd ZNdd ZOdd ZPdd ZQeRfddZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdAddZZdBddZ[dCddZ\dDddZ]dEddZ^dFddZ_dd Z`dd Zadd Zbdd Zcdd Zddd Zedd ZfddÄ Zgddń ZhddǄ ZiddɄ Zjdd˄ Zkdd̈́ Zlddτ Zmddф Znddӄ ZodGddքZpdd؄ Zqddڄ ZrdHdd܄ZsdIdd߄ZtdJddZudd Zvdd ZwdKddZxdLddZydd Zzdd Z{dMddZ|dNddZ}dd Z~dd ZdOddZdPddZdQddZdd Zdd Zd d Zdd Zdd Zdd ZdRdd	ZdSd
dZdTddZdUddZdd Zdd ZdVddZdWddZdXddZdYddZdd Zdd ZdZd d!Zd"d# Zd$d% Zd[d&d'Zd(d) Zd\d*d+ZeCd,d-fd.d/Zd0d1 Zd2d3 Zd4d5 ZdS (]  rP  a  
    Utility class to output C code.

    When creating an insertion point one must care about the state that is
    kept:
    - formatting state (level, bol) is cloned and used in insertion points
      as well
    - labels, temps, exc_vars: One must construct a scope in which these can
      exist by calling enter_cfunc_scope/exit_cfunc_scope (these are for
      sanity checking and forward compatibility). Created insertion points
      looses this scope and cannot access it.
    - marker: Not copied to insertion point
    - filename_table, filename_list, input_file_contents: All codewriters
      coming from the same root share the same instances simultaneously.
    create_fromNFc                 C   s   |d krt  }|| _d | _d | _g | _d | _d | _d | _d| _d| _	d| _
|d k	r| |j |j| _|r|j| _|j
| _
|j	| _	|j| _|j| _d S )Nr   r   )r   bufferlast_poslast_marked_posZpyclass_stackr  r  r  levelZ
call_levelbolr,  )r   r  r  copy_formattingr   r   r   r     s*     zCCodeWriter.__init__c                 C   s   t |||}|S r   rP  )r   r  r  r  r  r   r   r   
create_new  s    zCCodeWriter.create_newc                 C   s    | j d kst|| _ |j| _d S r   )r  r"   r  )r   Zglobal_stater   r   r   r,    s    zCCodeWriter.set_global_statec                 C   s   | j | d S r   )r  copyto)r   r   r   r   r   r    s    zCCodeWriter.copytoc                 C   s
   | j  S r   )r  getvaluer   r   r   r   r    s    zCCodeWriter.getvaluec                 C   s"   d|kr|  | n
| | d S Nr   )_write_lines_write_to_bufferr   rH  r   r   r   r     s    zCCodeWriter.writec                 C   s@   | j r| j d d nd}| jj|g|d  | | d S )Nr   Nr   r   )r  r  markersextendrz  r  )r   rH  Zfilename_liner   r   r   r    s    zCCodeWriter._write_linesc                 C   s   | j | d S r   )r  r   r  r   r   r   r    s    zCCodeWriter._write_to_bufferc                 C   s   | j | | j dd}|S )NT)r  r  r  )r  r  r2  r  r   r   r   r2    s    zCCodeWriter.insertion_pointc                 C   s
   t | dS )z
        Creates a new CCodeWriter connected to the same global state, which
        can later be inserted using insert.
        r  r  r   r   r   r   
new_writer  s    zCCodeWriter.new_writerc                 C   s"   |j | j kst| j|j dS )z
        Inserts the contents of another code writer (created with
        the same global state) in the current location.

        It is ok to write to the inserted writer also after insertion.
        N)r  r"   r  insert)r   rR  r   r   r   r    s    zCCodeWriter.insertc                 C   s   d S r   r   r   r   r   r   r    s    zCCodeWriter.label_counterc                 C   s   d S r   r   r   r   r   r   r    s    zCCodeWriter.return_labelc                 C   s   d S r   r   r   r   r   r   r    s    zCCodeWriter.error_labelc                 C   s   d S r   r   r   r   r   r   r    s    zCCodeWriter.labels_usedc                 C   s   d S r   r   r   r   r   r   r    s    zCCodeWriter.continue_labelc                 C   s   d S r   r   r   r   r   r   r    s    zCCodeWriter.break_labelc                 C   s   d S r   r   r   r   r   r   return_from_error_cleanup_label  s    z+CCodeWriter.return_from_error_cleanup_labelc                 C   s   d S r   r   r   r   r   r   r    s    zCCodeWriter.yield_labelsTc                 c   sj   t ||D ]Z\}}| |sq
|r0| | d}|dk	rF| j||d | | ||fV  | | q
dS )a  
        Helper for generating multiple label interceptor code blocks.

        @param new_labels: the new labels that should be intercepted
        @param orig_labels: the original labels that we should dispatch to after the interception
        @param skip_to_label: a label to skip to before starting the code blocks
        @param pos: the node position to mark for each interceptor block
        @param trace: add a trace line for the pos marker or not
        N)trace)r   r  put_gotomark_posrF  )r   r  Zorig_labelsZskip_to_labelr  r  r  Z
orig_labelr   r   r   label_interceptor  s    




zCCodeWriter.label_interceptorc                 C   s   | j |S r   )r  r  r   r   r   r   r   r  	  r  zCCodeWriter.new_labelc                 G   s   | j j| S r   )r  r  r   r  r   r   r   r  	  r  zCCodeWriter.new_error_labelc                 G   s   | j j| S r   )r  r  r  r   r   r   r  	  r  zCCodeWriter.new_yield_labelc                 C   s
   | j  S r   )r  r  r   r   r   r   r  	  r  zCCodeWriter.get_loop_labelsc                 C   s   | j |S r   )r  r  r  r   r   r   r  	  r  zCCodeWriter.set_loop_labelsc                 G   s   | j j| S r   )r  r  r  r   r   r   r  	  r  zCCodeWriter.new_loop_labelsc                 C   s
   | j  S r   )r  r  r   r   r   r   r  	  r  zCCodeWriter.get_all_labelsc                 C   s   | j |S r   )r  r  r  r   r   r   r  	  r  zCCodeWriter.set_all_labelsc                 C   s
   | j  S r   )r  r  r   r   r   r   r  	  r  zCCodeWriter.all_new_labelsc                 C   s   | j |S r   )r  r  r  r   r   r   r  	  r  zCCodeWriter.use_labelc                 C   s   | j |S r   )r  r  r  r   r   r   r  	  r  zCCodeWriter.label_usedc                 C   s   t | |d| _d S )N)r  )r  r  r  r   r   r   enter_cfunc_scope"	  s    zCCodeWriter.enter_cfunc_scopec                 C   s"   | j d krd S | j   d | _ d S r   )r  r  r   r   r   r   rG  %	  s    

zCCodeWriter.exit_cfunc_scopec                 C   sT   | j jd }|d| d | | | d | d| d |rP|   dS )z
        Init code helper function to start a cfunc scope and generate
        the prototype and function header ("static SIG {") of the function.
        r  zstatic CYTHON_SMALL_CODE ; /*proto*/r  static  {N)r  r   r   r  put_declare_refcount_context)r   	signaturer  r0  r   r   r   r   r4  +	  s    

zCCodeWriter.start_initcfuncc           	      C   sl   | |}d|| d| d}|rB| jd |ddd  |rP| | | d | |d  d S )	Nr  (r   r  CYTHON_UNUSED r  r  r  )Zmangle_internalr  r  r   ru  r  )	r   Zclass_scopereturn_typeZc_slot_nameZargs_signatureZneeds_funcstateZneeds_prototypeZslotfunc_cnameZdeclarationr   r   r   start_slotfunc8	  s    


zCCodeWriter.start_slotfuncc                 C   s   |  | j||jS r   )name_in_module_stater  rU  r  )r   rR  rS  r   r   r   
get_py_intF	  s    zCCodeWriter.get_py_intc                 C   s   |  | j||jS r   )r  r  rV  r  )r   rR  r  r   r   r   get_py_floatK	  s    zCCodeWriter.get_py_floatc                 C   s   |  | j||S r   )r  r  rZ  )r   r  rX  r   r   r   rZ  P	  s    zCCodeWriter.get_py_constc                 C   s   | j |jS r   )r  r_  r  ri  r   r   r   r_  U	  s    zCCodeWriter.get_string_constc                 C   s   | j |S r   )r  r`  ri  r   r   r   r`  X	  s    z#CCodeWriter.get_pyunicode_ptr_constc                 C   s   | j ||j}| |S r   )r  r  r  r  )r   r  r  r  r   r   r   r  [	  s
     zCCodeWriter.get_py_string_constc                 C   s   |  | j|S r   )r  r  rf  )r   rd  r   r   r   rf  `	  s    z CCodeWriter.get_py_codeobj_constc                 C   s   |  | j|jS r   )r  r  r]  r  r   r   r   r   r   r]  c	  s    z&CCodeWriter.get_argument_default_constc                 C   s
   |  |S r   rh  ri  r   r   r   r  f	  s    zCCodeWriter.internc                 C   s   | j |ddS rg  rh  ri  r   r   r   intern_identifieri	  s    zCCodeWriter.intern_identifierc                 C   s   | j |S r   )r  rN  rL  r   r   r   rN  l	  s    z'CCodeWriter.get_cached_constants_writerc                 C   s$   | j jd kr| |S | j j|S r   )r  r  r  r  )r   r  r   r   r   r  o	  s    
z CCodeWriter.name_in_module_statec                 C   s   t j d|  S r  )r   r6  r  r   r   r   r  y	  s    z,CCodeWriter.name_in_main_c_code_module_statec                 C   s   t j d|  S r  )r   r  r  r   r   r   name_in_slot_module_state	  s    z%CCodeWriter.name_in_slot_module_statec                 C   s   |j r|jS | |jS d S r   )Zis_py_class_scopeZnamespace_cnamer  r  r   r   r   namespace_cname_in_module_state	  s    z+CCodeWriter.namespace_cname_in_module_statec                 C   s   |j r| |jS |jS d S r   )is_extension_typer  Ztypeptr_cnamer  r   r   r   typeptr_cname_in_module_state	  s    z)CCodeWriter.typeptr_cname_in_module_stater  c                 C   s|   | j r| jr|   | jjrJ| jrJ| j\}}}| d| d|  d |rh|r^| | n
| 	| | d d| _d S )Nz
#line z "rr  r   r   )
r  r  emit_markerr  r8  r  r  get_escaped_descriptionput_saferV  )r   r   r  r  r  r   r   r   r   r   	  s    

zCCodeWriter.putlnc                 C   s>   |d krd S | j r0| j d d |d d kr0d S ||f| _d S )Nr   )r  r  r   r  r  r   r   r   r  	  s
     zCCodeWriter.mark_posc                 C   sR   | j \}}|| _d | _ | d | jjr@|   | | | |rN| | d S r  )r  r  r  r  r  indent_build_markerwrite_trace_liner  r   r   r   r  	  s    

zCCodeWriter.emit_markerc                 C   sf   | j rb| j jrb| jjd rb|   | d|d dd| |dd| j j dd| | d	 d S )N	linetracez__Pyx_TraceLine(r   r   r|  z)
)	r  r  r  r  r  r  pos_to_offsetr  r  r   r  r   r   r   r  	  s
    8zCCodeWriter.write_trace_linec                 C   s   |\}}}t |tst| j|}|td|d | }|d  d7  < ||||d  7 }d|}d|  d|d	d| d
S )Nr   r   r   z             # <<<<<<<<<<<<<<r   r   z/* "z":r   z
*/
)r   r   r"   r  r  r   r   r  )r   r  r  r  colcontentsr   r   r   r   r   r  	  s    

zCCodeWriter._build_markerc                 C   s   |  | d| _d S r  )r   r  r   r   r   r   r   r  	  s    
zCCodeWriter.put_safec              	   C   s  | j j}|rt|dkrt|d }| d| d}tj	||}tj
|s| dt  }d}z`z4t|}	|	| W 5 Q R X t|| d}W n& ttfk
r   tj
|sȂ Y nX W 5 |stj
|rt| X |dd	}
d
|
 d}| | d S )Ni   r  r   z.hz.tmpFTrq  r  z
#include "rr  )r  r  r   hashlibsha256encode	hexdigestr   r   r   existsgetpidunlinkr
   open_new_filer   shutilmover4   rM   ru  rV  )r   r   r   Zinclude_dirr   Zinclude_filer   Ztmp_pathdoner   Zc_pathr   r   r   rU  	  s*    zCCodeWriter.put_or_includec                 C   s   d}d|kr| d}nd}d|krp|| d8 }|dk rJ|  j|7  _n&|dkrp|d dkrpd}|  jd8  _| jr~|   | | d| _|dkr|  j|7  _n|r|  jd7  _d S )NF{r   rD  Tr   )rz  r  r  r  r   )r   r   Z
fix_indentdlr   r   r   rV  	  s&    
zCCodeWriter.put)r   c                 C   sR   |j rt|j|| | jd || j |jrNtjrN|| jd | jd d S )Nr   r^  r9  )	r   r"   r   rX  r  r\  r9  r   ra  )r   r   r   r   r   put_code_here	  s      zCCodeWriter.put_code_herec                 C   s   |  j d7  _ d S Nr   r  r   r   r   r   increase_indent
  s    zCCodeWriter.increase_indentc                 C   s   |  j d8  _ d S r  r  r   r   r   r   decrease_indent

  s    zCCodeWriter.decrease_indentc                 C   s   |  d |   d S )Nr	  )r   r  r   r   r   r   begin_block
  s    
zCCodeWriter.begin_blockc                 C   s   |    | d d S )NrD  )r  r   r   r   r   r   	end_block
  s    zCCodeWriter.end_blockc                 C   s   |  d| j  d S )Nz  )r  r  r   r   r   r   r  
  s    zCCodeWriter.indentc                 C   s   dt |d d d  S )Nz0x%02X%02X%02X%02X)r   r   r   r   r  )r   )r   Z	pyversionr   r   r   get_py_version_hex
  s    zCCodeWriter.get_py_version_hexc                 C   s   || j jkr| d|  d S )Nz%s:;)r  r  r   r  r   r   r   rF  
  s    zCCodeWriter.put_labelc                 C   s   | j | | d|  d S )Ngoto %s;)r  r  r   r  r   r   r   r  
  s    zCCodeWriter.put_gotoc                 C   s   |j dkr|s|jsd S |j dkr,|js,d S |js<| d |rN| d|  |jrn| |jj|j|d n| |jj	|j|d |j
d k	r| d|j|j
  n|jjr| d | d | jj| d S )Nprivater  %s )dll_linkagez = %sz = NULLr  )Z
visibilityZdefined_in_pxdr  Zcf_usedrV  Zis_cpp_optionalr   cpp_optional_declaration_coder  r  r8  r  literal_codeis_pyobjectr   r  r  r  )r   rJ  Zstorage_classr  Z
definitionr   r   r   put_var_declaration#
  s.    

 

 


zCCodeWriter.put_var_declarationc                 C   s   |j D ]\}}}}|jr6|js6|jjd r6||}n
||}|jrV| d|  q|j	rx| d||
|jf  q| d|rdpd|f  q|jr| jjrd}nd}| d|tjf  | d	|tjf  | d|tjf  d S )
Nr  z
%s = NULL;z%s = %s;z%s%s;r  r  r  z%sint %s = 0;z%sconst char *%s = NULL;)r  r  r  r  r  r  r  r  r   r  r  default_valuer  r  r  r   lineno_cnamefilename_cnameclineno_cname)r   Zfunc_contextr   r   r  r  r  Zunusedr   r   r   put_temp_declarations=
  s     
z!CCodeWriter.put_temp_declarationsc                 C   s   |  tj |  d d S r[  )r   r
   ZGENERATED_BY_MARKERr   r   r   r   put_generated_byT
  s    zCCodeWriter.put_generated_byc                 C   s    |  d|  |  d|  d S )Nz
#ifndef %sz
#define %sr   )r   Zguardr   r   r   put_h_guardX
  s    zCCodeWriter.put_h_guardc                 C   s   t jrd| S |S d S )Nzunlikely(%s))r   Zgcc_branch_hintsr   condr   r   r   unlikely\
  s    zCCodeWriter.unlikelyc                    s$   |sdS dd  fdd|D  S )Nr  r   c                    s   g | ]} ||qS r   r   )r   r  mapperr   r   r$  e
  s     z8CCodeWriter.build_function_modifiers.<locals>.<listcomp>)r   )r   	modifiersr(  r   r'  r   build_function_modifiersb
  s    z$CCodeWriter.build_function_modifiersc                 C   s2   |j }|js|j  r|j jr(d|j S |jS d S )Nz(PyObject *))r   Zis_self_argZis_completer  r  )r   rJ  r   r   r   r   entry_as_pyobjecti
  s    
zCCodeWriter.entry_as_pyobjectc                 C   s   ddl m}m} ||||S )Nr   py_object_typetypecast)r  r-  r.  )r   r  r   r-  r.  r   r   r   as_pyobjectq
  s    zCCodeWriter.as_pyobjectc                 C   s   | | | d S r   )Zgenerate_gotrefr  r   r   r   
put_gotrefu
  s    zCCodeWriter.put_gotrefc                 C   s   | | | d S r   )Zgenerate_giverefr  r   r   r   put_giverefx
  s    zCCodeWriter.put_giverefc                 C   s   | | | d S r   )Zgenerate_xgiverefr  r   r   r   put_xgiveref{
  s    zCCodeWriter.put_xgiverefc                 C   s   | | | d S r   )Zgenerate_xgotrefr  r   r   r   put_xgotref~
  s    zCCodeWriter.put_xgotrefc                 C   s   |j | ||d d S N)r  )Zgenerate_increfr   r  r   r  r   r   r   
put_incref
  s    zCCodeWriter.put_increfc                 C   s   |j | ||d d S r4  )Zgenerate_xincrefr5  r   r   r   put_xincref
  s    zCCodeWriter.put_xincrefc                 C   s   |j | |||d d S N)r  have_gil)Zgenerate_decrefr   r  r   r  r9  r   r   r   
put_decref
  s    zCCodeWriter.put_decrefc                 C   s   |j | |||d d S r8  )Zgenerate_xdecrefr:  r   r   r   put_xdecref
  s    zCCodeWriter.put_xdecrefc                 C   s   |j | ||||d d S N)r  r  r9  )Zgenerate_decref_clearr   r  r   r  r  r9  r   r   r   put_decref_clear
  s    
 zCCodeWriter.put_decref_clearc                 C   s   |j | ||||d d S r=  )Zgenerate_xdecref_clearr>  r   r   r   r  
  s    
 zCCodeWriter.put_xdecref_clearc                 C   s   | | || d S r   )Zgenerate_decref_setr   r  r   	rhs_cnamer   r   r   put_decref_set
  s    zCCodeWriter.put_decref_setc                 C   s   | | || d S r   )Zgenerate_xdecref_setr@  r   r   r   put_xdecref_set
  s    zCCodeWriter.put_xdecref_setc                 C   s   |j | ||d d S N)r9  )Zgenerate_incref_memoryviewslice)r   Zslice_cnamer   r9  r   r   r   put_incref_memoryviewslice
  s    z&CCodeWriter.put_incref_memoryviewslicec                 C   s   | j |j|j|d d S rD  )rE  r  r   )r   rJ  r9  r   r   r   put_var_incref_memoryviewslice
  s    z*CCodeWriter.put_var_incref_memoryviewslicec                 C   s   |  |j|j d S r   )r0  r  r   rI  r   r   r   put_var_gotref
  s    zCCodeWriter.put_var_gotrefc                 C   s   |  |j|j d S r   )r1  r  r   rI  r   r   r   put_var_giveref
  s    zCCodeWriter.put_var_giverefc                 C   s   |  |j|j d S r   )r3  r  r   rI  r   r   r   put_var_xgotref
  s    zCCodeWriter.put_var_xgotrefc                 C   s   |  |j|j d S r   )r2  r  r   rI  r   r   r   put_var_xgiveref
  s    zCCodeWriter.put_var_xgiverefc                 K   s   | j |j|jf| d S r   )r6  r  r   r   rJ  kwdsr   r   r   put_var_incref
  s    zCCodeWriter.put_var_increfc                 K   s   | j |j|jf| d S r   )r7  r  r   rK  r   r   r   put_var_xincref
  s    zCCodeWriter.put_var_xincrefc                 K   s   | j |j|jf| d S r   )r;  r  r   rK  r   r   r   put_var_decref
  s    zCCodeWriter.put_var_decrefc                 K   s   | j |j|jf| d S r   )r<  r  r   rK  r   r   r   put_var_xdecref
  s    zCCodeWriter.put_var_xdecrefc                 K   s"   | j |j|jfd|ji| d S Nr  )r?  r  r   
in_closurerK  r   r   r   put_var_decref_clear
  s    z CCodeWriter.put_var_decref_clearc                 K   s   | j |j|j|f| d S r   )rB  r  r   r   rJ  rA  rL  r   r   r   put_var_decref_set
  s    zCCodeWriter.put_var_decref_setc                 K   s   | j |j|j|f| d S r   )rC  r  r   rT  r   r   r   put_var_xdecref_set
  s    zCCodeWriter.put_var_xdecref_setc                 K   s"   | j |j|jfd|ji| d S rQ  )r  r  r   rR  rK  r   r   r   put_var_xdecref_clear
  s    z!CCodeWriter.put_var_xdecref_clearr   c                 C   s4   |D ]*}|r|j r|jr$| | q| | qd S r   )r  Zxdecref_cleanuprP  rO  )r   entriesZ	used_onlyrJ  r   r   r   put_var_decrefs
  s
    
zCCodeWriter.put_var_decrefsc                 C   s   |D ]}|  | qd S r   )rP  r   rX  rJ  r   r   r   put_var_xdecrefs
  s    zCCodeWriter.put_var_xdecrefsc                 C   s   |D ]}|  | qd S r   )rW  rZ  r   r   r   put_var_xdecrefs_clear
  s    z"CCodeWriter.put_var_xdecrefs_clearc                 C   sJ   ddl m}m} |||d}|r4| d||f  n| d||f  d S )Nr   r,  Py_Nonez%s = %s; __Pyx_INCREF(Py_None);z%s = %s; Py_INCREF(Py_None);)r  r-  r.  r   )r   r  r   r  r-  r.  Zpy_noner   r   r   put_init_to_py_none
  s
    zCCodeWriter.put_init_to_py_none%sc                 C   s.   ||j  }| ||j| |jr*| d d S )Nr]  )r  r^  r   rR  r1  )r   rJ  r  r  r   r   r   r   put_init_var_to_py_none
  s    
z#CCodeWriter.put_init_var_to_py_nonec                 C   s2  d}|j s|jdkr\ddlm} |jtkr\||jr<d}n |jdkrT| jjd sTn|r\d S |j	 }|snd S |j r||j
g7 }|r||n|j}|j }	|	dkrd	|	|f }|j }
|r|| jj|j}| }|r| | | d
|
|d||jr|jnd|f  |r.|r.| d d S )NF__getattribute__r   )	TypeSlotsT__getattr__Zfast_getattrZPyCFunctionz(void(*)(void))(%s)%sz{%s, (PyCFunction)%s, %s, %s}%s|r  r  )
is_specialr   r  rb  special_py_methodsZis_binop_number_slotr  r  r  method_flagsZmethod_coexistput_pymethoddef_wrapper
func_cnameZmethod_function_typeas_c_string_literalZget_slot_tableZget_slot_by_method_nameZpreprocessor_guard_coder   r   docZ	doc_cname)r   rJ  ZtermZ
allow_skipZwrapper_code_writerZis_number_slotrb  rg  Zfunc_ptrcastZ
entry_nameZslotZpreproc_guardr   r   r   put_pymethoddef
  sH    




zCCodeWriter.put_pymethoddefc                 C   s   |j }|jr|j pg }ddlm} ||krtj| }| d|  d|j  }|j	dkr~| d|  | d | d n| d	|  | d
 |S )Nr   )method_noargszBstatic PyObject *%s(PyObject *self, CYTHON_UNUSED PyObject *arg) {z%s(self)r   zPyObject *res = %s;zFif (!res && !PyErr_Occurred()) { PyErr_SetNone(PyExc_StopIteration); }zreturn res;z
return %s;rD  )
ri  re  r  rg  rb  rn  r   Zmethod_wrapper_prefixr   r   )r   rJ  ri  rg  rn  Z	func_callr   r   r   rh    s    




z#CCodeWriter.put_pymethoddef_wrapperc                 C   s:   | j jd r"| j tdd n| j tdd d S )NZfast_gilZFastGilr>  Z	NoFastGil)r  r  r`  r7  r%  r   r   r   r   use_fast_gil_utility_code0  s    z%CCodeWriter.use_fast_gil_utility_codec                 C   sn   | j jd dkr*ddlm} || jdd | j tdd |   |s\d	}|r\| 	d
 | 
d|  dS )a  
        Acquire the GIL. The generated code is safe even when no PyThreadState
        has been allocated for this thread (for threads not initialized by
        using the Python API). Additionally, the code generated by this method
        may be called recursively.
        Zsubinterpreters_compatiblenor   )warningzTAcquiring the GIL is currently very unlikely to work correctly with subinterpreters.r   ForceInitThreadsr>  __pyx_gilstate_savezPyGILState_STATE z%s = __Pyx_PyGILState_Ensure();N)r  r  ZErrorsrq  r  r`  r7  r%  ro  rV  r   )r   declare_gilstatevariablerq  r   r   r   put_ensure_gil6  s     

zCCodeWriter.put_ensure_gilc                 C   s"   |    |sd}| d|  dS )zD
        Releases the GIL, corresponds to `put_ensure_gil`.
        rs  z__Pyx_PyGILState_Release(%s);N)ro  r   )r   ru  r   r   r   put_release_ensured_gilM  s    z#CCodeWriter.put_release_ensured_gilc                 C   s,   |  d |  dtj d |  d d S )N-#if CYTHON_COMPILING_IN_CPYTHON_FREETHREADINGzPyMutex_Lock(&r/  r  r   r   Zparallel_freethreading_mutexr   r   r   r   put_acquire_freethreading_lockV  s    
z*CCodeWriter.put_acquire_freethreading_lockc                 C   s,   |  d |  dtj d |  d d S )Nrx  zPyMutex_Unlock(&r/  r  ry  r   r   r   r   put_release_freethreading_lock[  s    
z*CCodeWriter.put_release_freethreading_lockc                 C   sj   |    | d |r$| d|  |r2| d | d |rf| d | d |   | d dS )	z
        Acquire the GIL. The thread's thread state must have been initialized
        by a previous `put_release_gil`
        z__Pyx_FastGIL_Forget();z_save = %s;zif (_save) {ZPy_BLOCK_THREADSrD  ##if CYTHON_COMPILING_IN_LIMITED_APIr  N)ro  r   rw  r   ru  Zunknown_gil_stater   r   r   put_acquire_gil`  s    




zCCodeWriter.put_acquire_gilc                 C   s   |    | d | d |rZ| d |   | d | d | d | d | d |rr| d	 |r| d
|  | d dS )z2Release the GIL, corresponds to `put_acquire_gil`.zPyThreadState *_save;z_save = NULL;r|  r  zif (PyGILState_Check())r  r	  ZPy_UNBLOCK_THREADSrD  z%s = _save;z__Pyx_FastGIL_Remember();N)ro  r   rv  r}  r   r   r   put_release_gilr  s     








zCCodeWriter.put_release_gilc                 C   s   |  d d S )Nz%PyGILState_STATE __pyx_gilstate_save;r!  r   r   r   r   rt    s    zCCodeWriter.declare_gilstatec                 C   s   |  d|| |f S )Nzif (%s < 0) %s)r   r  )r   r  r	  r   r   r   put_error_if_neg  s    zCCodeWriter.put_error_if_negc              	   C   s   |rdnd}|j rd}n:|jjr,|jr,d}n&|jjrN|jrN|jsN|jjrNd}nd}| j	t
| | d |s|j|j}| d|d	| | |j | |f  d S )
NZNogilr  ZRaiseClosureNameErrorZRaiseCppGlobalNameErrorZRaiseCppAttributeErrorZRaiseUnboundLocalErrorr|  z!if (unlikely(!%s)) { %s(%s); %s }Z__Pyx_)Zfrom_closurer   r  Z
is_cglobalZis_variableZ	is_memberr  Zis_c_class_scoper  r`  r7  r%  Zcheck_for_null_coder  r   r   rj  r  )r   r  rJ  Zin_nogil_contextZunbound_check_codeZ	nogil_tagri  r   r   r   put_error_if_unbound  s&    z CCodeWriter.put_error_if_unboundc                 C   s.   d| j _|rd| j _d| |d |d f S )NTz__PYX_MARK_ERR_POS(%s, %s)r   r   )r  r  r  r  )r   r  r  r   r   r   set_error_info  s    zCCodeWriter.set_error_infoc                 C   sT   | j j}| j | |d kr$d| S d| j _|r8d| j _d| |d |d |f S )Nr  Tz__PYX_ERR(%s, %s, %s)r   r   )r  r  r  r  r  r  )r   r  r  r  r   r   r   r    s    zCCodeWriter.error_gotoc                 C   s   d|  || |f S )Nz
if (%s) %s)r%  r  )r   r$  r  r   r   r   error_goto_if  s    zCCodeWriter.error_goto_ifc                 C   s   |  d| |S )Nz!%sr  r   r  r  r   r   r   r    s    zCCodeWriter.error_goto_if_nullc                 C   s   |  d| |S )Nz(%s < 0)r  r  r   r   r   error_goto_if_neg  s    zCCodeWriter.error_goto_if_negc                 C   s   |  d|S )NzPyErr_Occurred()r  r  r   r   r   rZ    s    zCCodeWriter.error_goto_if_PyErrc                 C   s   | j |S r   )r  r  )r   r   r   r   r   r    s    zCCodeWriter.lookup_filenamec                 C   s   |  d d S )NZ__Pyx_RefNannyDeclarationsr!  r   r   r   r   r    s    z(CCodeWriter.put_declare_refcount_contextc                 C   s>   |  }|r | jtdd | d||r0dp2df  d S )Nrr  r>  z#__Pyx_RefNannySetupContext(%s, %d);r   r   )rj  r  r`  r7  r%  r   )r   r   Zacquire_gilr   r   r   r7    s    
z&CCodeWriter.put_setup_refcount_contextc                 C   s   |  |rdnd d S )Nz"__Pyx_RefNannyFinishContextNogil()z__Pyx_RefNannyFinishContext();r!  r   nogilr   r   r   rH    s    z'CCodeWriter.put_finish_refcount_contextc                 C   s<   |  }||rtjndtjtjf}d| j_| d|  dS )z
        Build a Python traceback for propagating exceptions.

        qualified_name should be the qualified name of the function.
        r   Tz#__Pyx_AddTraceback(%s, %s, %s, %s);N)rj  r   r  r  r  r  r  r   )r   qualified_nameZinclude_clineformat_tupler   r   r   put_add_traceback  s    zCCodeWriter.put_add_tracebackc                 C   sL   |t jt jt j| jjd |f}d| j_| d|  | j	t
dd dS )z
        Generate code to print a Python warning for an unraisable exception.

        qualified_name should be the qualified name of the function.
        Zunraisable_tracebacksTz0__Pyx_WriteUnraisable("%s", %s, %s, %s, %d, %d);ZWriteUnraisableExceptionzExceptions.cN)r   r  r  r  r  r  r  r  r   r`  r7  r%  )r   r  r  r  r   r   r   put_unraisable  s    

zCCodeWriter.put_unraisablec                 C   s   | j jd p| j jd S )NZprofiler  )r  r  r   r   r   r   
is_tracing  s    zCCodeWriter.is_tracingc                 C   s0   | j j}|r||jkr|j}q|r,|j| S dS )zk
        Calculate a fake 'instruction offset' from a node position as 31 bit int (32 bit signed).
        r   )r  r  Znode_positions_to_offsetZparent_scope)r   r  r  r   r   r   r    s    zCCodeWriter.pos_to_offsetc                 C   s   |  |rdnd d S )NZ__Pyx_TraceDeclarationsGenZ__Pyx_TraceDeclarationsFuncr!  )r   is_generatorr   r   r   put_trace_declarations  s    z"CCodeWriter.put_trace_declarationsc                 C   s   |r|  d|  d S )Nz__Pyx_TraceFrameInit(%s)r!  )r   codeobjr   r   r   put_trace_frame_init  s    z CCodeWriter.put_trace_frame_initc                 C   s~   |rdnd}|  | d|  dtj d| |d  d|d  d| |d	d|d	d|rbtjnd
 d| | d d S )NZ__Pyx_TraceStartGenZ__Pyx_TraceStartFuncr  r  ra  r   ], r   r   r  r/  )r   rj  r   filetable_cnamer  r  Zskip_dispatch_cnamer  )r   r   r  r  r  Zis_cpdef_func
trace_funcr   r   r   put_trace_start  s    fzCCodeWriter.put_trace_startc                 C   s   |  dt|dd d S )Nz__Pyx_PyMonitoring_ExitScope(r   r/  )r   rp   r  r   r   r   put_trace_exit"  s    zCCodeWriter.put_trace_exitc              	   C   s2   |  |}| d| d| | d| d d S )Nz__Pyx_TraceYield(r  r/  r  r   r  )r   retvalue_cnamer  r  r   r   r   put_trace_yield%  s    
zCCodeWriter.put_trace_yieldc                 C   s   | j j}|jr|jjd n|d }|j }| |d }| |}| d| dt	j
 d| d| d| | d| d d S )Nr   r   z__Pyx_TraceResumeGen(r  ra  r  r/  )r  r  Zscope_classr  r   rj  r  r  r   r   r  r  )r   r  r  
first_liner   Zfilename_indexr  r   r   r   put_trace_resume)  s    

0zCCodeWriter.put_trace_resumec              	   C   s4   |  d| | dt|ddt|dd d S )N__Pyx_TraceException(r  r   r/  r   r  rp   )r   r  reraiseZfreshr   r   r   put_trace_exception:  s    zCCodeWriter.put_trace_exceptionc                 C   s   |  dtj d d S )Nr  z, 0, 0);)r   r   r  r   r   r   r   put_trace_exception_propagating=  s    z+CCodeWriter.put_trace_exception_propagatingc                 C   s   |  d| | d d S )Nz__Pyx_TraceExceptionHandled(r/  )r   r  r  r   r   r   put_trace_exception_handled@  s    z'CCodeWriter.put_trace_exception_handledc                 C   s(   |  d| | dt|dd d S )Nz__Pyx_TraceExceptionUnwind(r  r   r/  r  )r   r  r  r   r   r   put_trace_unwindC  s    zCCodeWriter.put_trace_unwindc              	   C   s2   |  |}| d| d| | d| d d S )Nz__Pyx_TraceStopIteration(r  r/  r  )r   r  r	  r  r   r   r   put_trace_stopiterationF  s    
z#CCodeWriter.put_trace_stopiterationc                 C   s   d}d}|d krnF|j r$||}n4|jr0d}n(|jr<d}n|jrTd}d|j }nd}| |}| | d| | d| | dt|dd| d d S )	Nr  Z__Pyx_TraceReturnValuer]  Z__Pyx_TraceReturnCValuer  r  r   r/  )	r  r/  Zis_void	is_stringZto_py_functionr  r   r  rp   )r   r  r  r  r  Z	extra_argr  r  r   r   r   put_trace_returnJ  s     
zCCodeWriter.put_trace_returnZDefaultPlacementNewr  c                 C   s"   | j | | d| d d S )Nz$__Pyx_default_placement_construct(&(z));)r  r`  r   )r   rM  Z_utility_coder   r   r   put_cpp_placement_newb  s    z!CCodeWriter.put_cpp_placement_newc                 C   s"   |  d |  | |  d d S )Nz#ifdef _OPENMPz#endif /* _OPENMP */r!  )r   stringr   r   r   putln_openmpg  s    

zCCodeWriter.putln_openmpc                 C   sD   |  d|  |  d |  d |  d |  d |  d dS )zl
        Redefine the macros likely() and unlikely to no-ops, depending on
        condition 'cond'
        #if %s    #undef likely    #undef unlikelyz    #define likely(x)   (x)z    #define unlikely(x) (x)r  Nr!  r#  r   r   r   undef_builtin_expectl  s    



z CCodeWriter.undef_builtin_expectc                 C   sD   |  d|  |  d |  d |  d |  d |  d d S )Nr  r  r  z2    #define likely(x)   __builtin_expect(!!(x), 1)z2    #define unlikely(x) __builtin_expect(!!(x), 0)r  r!  r#  r   r   r   redef_builtin_expectx  s    



z CCodeWriter.redef_builtin_expect)NNF)NNT)N)NF)TF)N)N)N)r  F)T)r  NT)T)T)TT)TT)FTT)FTT)r   )T)r_  T)TN)TN)N)NT)NT)FN)F)T)F)F)T)F)F)N)FFF)F)FF)F)NF)r   r   r   r   cythonr   r   r  r,  r  r  r   r  r  r2  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rG  r4  r  r  r  rZ  r_  r`  r  rf  r]  r  r  rN  r  r   r  r  r  r  r   r  r  r  r  r  rU  rV  r7  r  r  r  r  r  r  r  rF  r  r  r  r   r"  r%  modifier_output_mapperr*  r+  r/  r0  r1  r2  r3  r6  r7  r;  r<  r?  r  rB  rC  rE  rF  rG  rH  rI  rJ  rM  rN  rO  rP  rS  rU  rV  rW  rY  r[  r\  r^  r`  rm  rh  ro  rv  rw  rz  r{  r~  r  rt  r  r  r  r  r  r  r  rZ  r  r  r7  rH  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   rP  x  sJ  
	











   
1		rP  c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
PyrexCodeWriterc                 C   s   t || _d| _d S r  )r
   r  r   r  )r   Zoutfile_namer   r   r   r     s    zPyrexCodeWriter.__init__c                 C   s   | j dd| j |f  d S )Nz%s%s
r&  )r   r   r  r  r   r   r   r     s    zPyrexCodeWriter.putlnc                 C   s   |  j d7  _ d S r  r  r   r   r   r   r    s    zPyrexCodeWriter.indentc                 C   s   |  j d8  _ d S r  r  r   r   r   r   r    s    zPyrexCodeWriter.dedentN)r   r   r   r   r   r  r  r   r   r   r   r    s   r  c                   @   s~   e Zd ZdZdddZddd	Zd d
dZedd Zdd Z	dd Z
d!ddZd"ddZdd Zdd Zdd Zdd ZdS )#PyxCodeWriterz7
    Can be used for writing out some Cython code.
    Nr   rn   c                 C   s.   |pt  | _|| _|| _|| _|| _i | _d S r   )r   r  r  original_levelrI  r   _insertion_points)r   r  indent_levelrI  r   r   r   r   r     s    zPyxCodeWriter.__init__r   c                 C   s   |  j |7  _ dS )NTr  r   levelsr   r   r   r    s    zPyxCodeWriter.indentc                 C   s   |  j |8  _ d S r   r  r  r   r   r   r    s    zPyxCodeWriter.dedentc                 c   s$   |  | |   dV  |   dS )zf
        with pyx_code.indenter("for i in range(10):"):
            pyx_code.putln("print i")
        N)r   r  r  )r   r  r   r   r   indenter  s    
zPyxCodeWriter.indenterc                 C   s
   | j  S r   )r  emptyr   r   r   r   r    s    zPyxCodeWriter.emptyc                 C   s$   | j  }t|tr || j}|S r   )r  r  r   rs   ro  r   )r   r  r   r   r   r    s    

zPyxCodeWriter.getvaluec                 C   sR   |d kr| j d k	r| j }|d k	r*t||}| j|rH| jd  | dnd d S )Nz    r   )rI  r  r  r   r  )r   r  rI  r   r   r   r     s    

zPyxCodeWriter.putlnc                 C   sJ   |d kr| j d k	r| j }|d k	r*t||}t|| jd }| j| d S )Nr  )rI  r  _indent_chunkr  r  r   )r   chunkrI  r   r   r   	put_chunk  s    

zPyxCodeWriter.put_chunkc                 C   s   t | | j | j| jS r   )r   r  r2  r  rI  r   r   r   r   r2    s    zPyxCodeWriter.insertion_pointc                 C   s   | j   | j| _d S r   )r  resetr  r  r   r   r   r   r    s    
zPyxCodeWriter.resetc                 C   s   |   | j|< d S r   )r2  r  r  r   r   r   named_insertion_point  s    z#PyxCodeWriter.named_insertion_pointc                 C   s
   | j | S r   )r  r  r   r   r   rA    s    zPyxCodeWriter.__getitem__)Nr   Nrn   )r   )r   )N)N)r   r   r   r   r   r  r  r   r  r  r  r   r  r2  r  r  rA  r   r   r   r   r    s   





	

r  )r  indentation_lengthr  c                    s   d| kst | jdd}|s | S |d d}|r<||d< n|d= |sJdS t| d }t|D ]6\}}t|}|d t|krd||< q^||k r^|}q^|t| krd}||k rd||    fd	d
|D }n"||kr|| fdd
|D }d|S )zONormalise leading space to the intended indentation and strip empty lines.
    	T)keependsr   r&  r   r   r   c                    s    g | ]}|d kr | nd qS )r   r   rt  )
add_indentr   r   r$    s   z!_indent_chunk.<locals>.<listcomp>c                    s$   g | ]}|d kr| d nd qS )r   Nr   rt  )startr   r   r$    s   r  )r"   rv  r  r   r   _count_indentationr   )r  r  r   	last_lineZmin_indentationr9  r  Zline_indentationr   )r  r  r   r    s<    



r  r   )rH  r  c                 C   s&   d}t | D ]\}}|dkr q"q|S )Nr   r&  )r   )rH  r9  chr   r   r   r    s
    r  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )r  c                 C   s   || _ i | _i | _d| _d S r  )klassr  r  temps_count)r   r  r   r   r   r     s    zClosureTempAllocator.__init__c                 C   s&   | j  D ]\}}t|| j|< q
d S r   )r  r   r   r  )r   r   r  r   r   r   r  !  s    zClosureTempAllocator.resetc                 C   s   || j kr g | j |< g | j|< n| j| r:| j| dS dtj| jf }| jjd |||dd | j | | |  jd7  _|S )Nr   r  T)r  r   r  r   Zis_cdefr   )	r  r  r  r   r  r  r  Zdeclare_varr  )r   r   r  r   r   r   r  %  s    


z"ClosureTempAllocator.allocate_tempN)r   r   r   r   r  r  r   r   r   r   r    s   r  )Vr  Zdeclarer   r  r   r   r   r  r   r  r   	functoolsr   r   
contextlibr   r   collectionsr   r  r   r   r   r	   r
   ZScanningr   r   r  Zctypedef_builtins_mapZbasicsize_builtins_mapZKNOWN_PYTHON_BUILTINS_VERSIONr   ZKNOWN_PYTHON_BUILTINSZuncachable_builtinsrf  r   r  r  r   r   r  r   r   r   r7  ro  ry  rz  r  r  r  r  rT  r  r(  r  r  r  r  rw   Ur  r  r  r  r  findallr  r  r  rQ  r  r  rP  r  r  finalZccallr   r   r  Z	exceptvalZcfuncr  r  r   r   r   r   <module>   st             # 
L
   )
	
 


	

"   ?      '
        K/
	