U
    ceU                     @   s  d Z ddlmZ ddlZejeeeeeeeeeeeeed zddlmZ W n e	k
rd   e
ZY nX ze W n ek
r   eZY nX 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ZddlZddlmZ ddlmZ d	Zd
jZedZej j!Z"de Z#e#$dZ%G dd deZ&dd Z'g Z(dd Z)dd Z*dd Z+dd Z,dd Z-dd Z.dd Z/d d! Z0dkd$d%Z1d&d' Z2d(d) Z3d*d+ Z4d,d- Z5e*d.d/ Z6e*d0d1 Z7e*d2d3 Z8d4d5 Z9e*d6d7 Z:ed8j;Z<e*e=e>d9d:efd;d<Z?d=d> Z@ed?jAZBdldAdBZCdCdD ZDdmdEdFZEdndGdHZFdIdJ ZGdKdL ZHdMdN ZIe*dOdP ZJe'dodRdSZKdTdU ZLdVdW ZMdpdXdYZNddZejOd#fd[d\ZPG d]d^ d^eZQd_d` ZRdadb ZSdcdd ZTdedf ZUdgdh ZVdidj ZWdS )qz@
Cython -- Things that don't belong anywhere else in particular
    )absolute_importN)
basestringossysreiocodecsglobshutiltempfilecython_version_function_caches_parse_file_version_match_file_encoding)r   wraps   )__version__)z__init__.pyz__init__.pycz__init__.pyxz__init__.pxdz__{0}_cachez^__(.+)_cache$z/* Generated by Cython %s */zus-asciic                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	"_TryFinallyGeneratorContextManagerz_
    Fast, bare minimum @contextmanager, only for try-finally, not for exception handling.
    c                 C   s
   || _ d S N)_gen)selfgen r   +lib/python3.8/site-packages/Cython/Utils.py__init__5   s    z+_TryFinallyGeneratorContextManager.__init__c                 C   s
   t | jS r   )nextr   r   r   r   r   	__enter__8   s    z,_TryFinallyGeneratorContextManager.__enter__c              	   C   s,   zt | j W n ttfk
r&   Y nX d S r   )r   r   StopIterationGeneratorExit)r   exc_typeZexc_valZexc_tbr   r   r   __exit__;   s    z+_TryFinallyGeneratorContextManager.__exit__N)__name__
__module____qualname____doc__r   r   r"   r   r   r   r   r   1   s   r   c                    s   t   fdd}|S )Nc                     s   t  | |S r   )r   )argskwargsgen_funcr   r   make_genC   s    z,try_finally_contextmanager.<locals>.make_genr   )r*   r+   r   r)   r   try_finally_contextmanagerB   s    r,   c                  C   s   t D ]} |   qd S r   )r   clear)cacher   r   r   clear_function_cachesL   s    r/   c                    s6   i  t   t t fdd}|_|S )Nc                     s(     | }|kr$|   } | < |S r   )get)r'   resr.   fZ
uncomputedr   r   wrapperV   s    z cached_function.<locals>.wrapper)r   appendobjectr   Zuncachedr3   r4   r   r2   r   cached_functionQ   s    
r8   c                 c   s4   t | D ]&}t|}|dk	r||dfV  qdS )zThe function iterates over the attributes of the object and,
    if it finds the name of the cache, it returns it and the corresponding method name.
    The method may not be present in the object.
    Nr   )dir_CACHE_NAME_PATTERNmatchgroup)objZ	attr_namer;   r   r   r   _find_cache_attributesa   s    
r>   c                 C   s*   t | D ]\}}t| |rt| | qdS )zbRemoves every cache found in the object,
    if a corresponding method exists for that cache.
    N)r>   hasattrdelattr)r=   
cache_namemethod_namer   r   r   clear_method_cachesl   s    
rC   c                    s   t j  fdd}|S )Nc                    sN   t |  d }|d kr$i }t|  | ||kr4|| S | f|  }||< |S r   )getattrsetattr)r   r'   r.   r1   rA   r3   r   r   r4   z   s    zcached_method.<locals>.wrapper)_build_cache_namer#   r7   r   rF   r   cached_methodw   s    

rH   c                 C   s   t j| \}}|| S r   )r   pathsplitext)rI   Znewsufbase_r   r   r   replace_suffix   s    rM   c                 C   s&   t j| rt |  tj| dddS )Nwz
ISO-8859-1)encoding)r   rI   existsunlinkr   openrI   r   r   r   open_new_file   s    
rT   c                 C   sf   t | dddsd S zt| }W n tk
r2   Y n0X |d |  |rbt| |j|jd f d S )NTF)allow_failedif_not_foundzN#error Do not use this file, it is the result of a failed Cython compilation.
r   )	is_cython_generated_filerT   EnvironmentErrorwritecloser   utimest_atimest_mtime)rI   str3   r   r   r   castrate_file   s    r_   FTc              	   C   s~   d}d }t j| rVz(t| d}|t|}W 5 Q R X W n ttfk
rT   Y nX |d krb|S |dp||rx||kp|| S )NsM   #error Do not use this file, it is the result of a failed Cython compilation.rbs   /* Generated by Cython )	r   rI   rP   rR   readlenOSErrorIOError
startswith)rI   rU   rV   Zfailure_markerfile_contentr3   r   r   r   rW      s    

rW   c              	   C   s`   d}t j| rRz(t| d}|tt}W 5 Q R X W n ttfk
rP   Y nX |o^|	tS )N    r`   )
r   rI   rP   rR   ra   rb   GENERATED_BY_MARKER_BYTESrc   rd   re   )rI   rf   r3   r   r   r   file_generated_by_this_cython   s    ri   c                 C   s   t | }||kS r   )modification_time)rI   timeZftimer   r   r   file_newer_than   s    rl   c                 C   s6   zt |  W n" tk
r0   t j| s, Y nX d S r   )r   makedirsrc   rI   isdirrS   r   r   r   safe_makedirs   s
    ro   c                 C   s^   t j|t j| }zt|}W n tk
r>   t| Y nX t| |sNdS t	| | dS )z
    Copy file sourcefile to directory destdir (creating it if needed),
    preserving metadata. If the destination file exists and is not
    older than the source file, the copying is skipped.
    N)
r   rI   joinbasenamerj   rc   ro   rl   r
   Zcopy2)Z
sourcefileZdestdirZdestfileZdesttimer   r   r   copy_file_to_dir_if_newer   s    
rr   c                 C   s0   t j| }| |kr|S t|r(t|S |S d S r   )r   rI   dirnameis_package_dirfind_root_package_dir)Z	file_pathr9   r   r   r   ru      s    ru   c                 C   s4   d}|D ]"}t j| |} t| }|rd}q| |fS )NTF)r   rI   rp   contains_init)dir_pathZpackage_names	namespacers   Zhas_initr   r   r   check_package_dir   s    ry   c                 C   s*   t D ] }tj| |}t|r dS qd S Nr   )PACKAGE_FILESr   rI   rp   path_exists)rw   filenamerI   r   r   r   rv     s    rv   c                 C   s   t | rdS d S rz   )rv   )rw   r   r   r   rt     s    rt   c                 C   s   t j| rdS znt}t|dd }|r|t j| }||r||t|d d  }z|| W W dS  t	k
rz   Y W dS X W n t
k
r   Y nX dS )NTarchiver   F)r   rI   rP   
__loader__rD   normpathre   rb   get_datard   	NameError)rI   loaderZarchive_pathr   arcnamer   r   r   r|     s"    

r|   z .*[.]cython-([0-9]+)[.][^./\\]+$z^([0-9]+)[.]([0-9]+).*z\1\2c           
      C   s   |r|dd dkst tj| |}t|d | }|| }tj|sPd}d|f}|D ]@}t|}|r\t|d }	|d |	  k r|kr\n q\|	|f}q\|d S )a?  
    Search a directory for versioned pxd files, e.g. "lib.cython-30.pxd" for a Cython 3.0+ version.

    @param directory: the directory to search
    @param filename: the filename without suffix
    @param suffix: the filename extension including the dot, e.g. ".pxd"
    @return: the file path if found, or None
    Nr   .z	.cython-*r   )AssertionErrorr   rI   rp   r	   rP   r   int)
Z	directoryr}   suffixZ_current_versionZpath_prefixZmatching_filesrI   Z
best_matchZversionsZint_versionr   r   r   find_versioned_file1  s    
r   c                 C   sJ   t | trFz&t }|d kr$t }| |} W n tk
rD   Y nX | S r   )
isinstancebytesr   getfilesystemencodinggetdefaultencodingdecodeUnicodeDecodeError)r}   Zfilename_encodingr   r   r   decode_filenameQ  s    
r   s   (\w*coding)[:=]\s*([-\w.]+)UTF-8c                 C   s   d}d}t |dk r8| d}||7 }|d}|sq8qt|d }|rf|ddkrf|d	d
S t |dkrt|d }|r|d	d
S |S )Nr   rg      i     
r   r   s   c_string_encoding   z	iso8859-1)rb   ra   splitr   r<   r   )r3   defaultlinesstartdatamr   r   r   detect_opened_file_encodingb  s     

r   c                 C   s   |  ddkr| d dS )z
    Read past a BOM at the beginning of a source file.
    This could be added to the scanner, but it's *substantially* easier
    to keep it at this level.
    r   u   ﻿r   N)ra   seek)r3   r   r   r   skip_bomy  s    r   c                 C   s   d }zL|d kr>t | d}t|}|d t j|||d}nt j| ||d}W n^ tk
r   tj| rn z"t	}| 
|jrt|| ||}W n ttfk
r   Y nX Y nX |d krt| t| |S )Nr`   r   rO   errors)r   rR   r   r   TextIOWrapperrc   r   rI   rP   r   re   r~   open_source_from_loaderr   AttributeErrorFileNotFoundErrorr   )source_filenamerO   error_handlingstreamr3   r   r   r   r   open_source_file  s4    
  r   c                 C   sB   t j|}|t| jd d  }| |}tjt|||dS )Nr   r   )	r   rI   r   rb   r~   r   r   r   BytesIO)r   r   rO   r   Znrmpathr   r   r   r   r   r     s    
r   c                 C   s   d}| d d dkr$d}| dd  } t | dk r<t| d} n| d dkr| d }|dkrtt| } t| dd  d	} q|d
krt| dd  d} q|dkrt| dd  d} qt| d} n
t| d} |r|  S | S )NFr   -Tr   r   0ZxX   ZoO   ZbB)rb   r   strip_py2_long_suffix)valueZis_negZliteral_typer   r   r   str_to_number  s$    
r   c                 C   s   | d dkr| dd S | S )z~
    Python 2 likes to append 'L' to stringified numbers
    which in then can't process when converting them to numbers.
    r   ZlLNr   )Z	value_strr   r   r   r     s    r   c                 C   s,   t | trt| } d|   ko$dk n   S )Ni   l        )r   r   r   )r   r   r   r   long_literal  s    
r   c                  C   sz   dt jkrt jd S d} t jdkrFtjdkr:t jd} nt jd} | rdt j| rdt j	| dS t jt j	dd	S )
z
    Return the base directory containing Cython's caches.

    Priority:

    1. CYTHON_CACHE_DIR
    2. (OS X): ~/Library/Caches/Cython
       (posix not OS X): XDG_CACHE_HOME/cython if XDG_CACHE_HOME defined
    3. ~/.cython

    ZCYTHON_CACHE_DIRNposixdarwinz~/Library/CachesZXDG_CACHE_HOMEcython~z.cython)
r   environnamer   platformrI   
expanduserr0   rn   rp   )parentr   r   r   get_cython_cache_dir  s    



r   r   c              	   #   s   t | }zdtjddNdgffdd	t  |   fdd}|V  t ||    W 5 Q R X W 5 t | X d S )Nza+b)moderg   c                    s$    j s d   | d< | d S )Nr   )closedr   ra   )Z_output)	temp_filer   r   read_output  s    
z captured_fd.<locals>.read_outputc                     s    }  r|   S | S r   )r   )result)rO   r   r   r   
get_output  s    zcaptured_fd.<locals>.get_output)r   duprZ   r   ZTemporaryFiledup2fileno)r   rO   Zorig_streamr   r   )rO   r   r   r   captured_fd  s    
r   c                  C   sN   t  g} t jt jt jt jfD ]*}t|dd }|d k	r|| kr| | q| S )NrO   )r   r   stdoutstdin
__stdout__	__stdin__rD   r5   )Z
candidatesr   rO   r   r   r   get_encoding_candidates  s    
r   c              	   C   sL   |   }|sd S t D ]*}z||W   S  tk
r>   Y qX q|dS )Nzlatin-1)stripr   r   r   )capturedZcaptured_bytesrO   r   r   r   prepare_captured  s    
r   c                 C   s(   t | } | r$|r|| ||  d S r   )r   rY   )r   outputZheader_liner   r   r   print_captured(  s
    
r   r   c                 C   sb   |r| | |  z
|j}W n tk
r8   |}Y nX | |  |rR| | |r^|  d S r   )rY   flushbufferr   )sZheader_textendfiler   outr   r   r   print_bytes0  s    




r   c                   @   s:   e Zd ZdddZdd Zdd Zdd	 Zd
d ZeZdS )
OrderedSetr   c                 C   s   g | _ t | _| | d S r   )_listset_setupdate)r   elementsr   r   r   r   @  s    zOrderedSet.__init__c                 C   s
   t | jS r   )iterr   r   r   r   r   __iter__E  s    zOrderedSet.__iter__c                 C   s   |D ]}|  | qd S r   )add)r   r   er   r   r   r   H  s    zOrderedSet.updatec                 C   s&   || j kr"| j| | j | d S r   )r   r   r5   r   )r   r   r   r   r   r   L  s    
zOrderedSet.addc                 C   s
   t | jS r   )boolr   r   r   r   r   __bool__Q  s    zOrderedSet.__bool__N)r   )	r#   r$   r%   r   r   r   r   r   Z__nonzero__r   r   r   r   r   ?  s   
r   c                    s    fdd}|S )z6Class decorator for creating a class with a metaclass.c                    sh   | j  }|d}|d k	r@t|tr,|g}|D ]}|| q0|dd  |dd   | j| j|S )N	__slots____dict____weakref__)r   copyr0   r   strpopr#   	__bases__)clsZ	orig_varsslotsZ	slots_var	metaclassr   r   r4   [  s    


zadd_metaclass.<locals>.wrapperr   )r   r4   r   r   r   add_metaclassY  s    r   c                 C   s   | dks|  drtdd S )Nr   zcython.z;cython is a special module, cannot be used as a module name)re   
ValueError)Zfull_module_namer   r   r   $raise_error_if_module_name_forbiddeni  s    r   c                 C   s   g }d}t d| D ]V}|dkrFdddd| }|ddg dd	 }q|d
krT qlq|dkr|t| q|dgd	  dd }|d	  |7  < d}|D ]}|d> | }qd| S )z
    Parse and translate public version identifier like '4.3a1' into the readable hex representation '0x040300A1' (like PY_VERSION_HEX).

    SEE: https://peps.python.org/pep-0440/#public-version-identifiers
       z(\D+))abZrc         r   Nr   )z.devz.prez.postr      r   z0x%08X)r   r   r5   r   )Zversion_stringZdigitsZrelease_statusZsegment
hexversiondigitr   r   r   build_hex_versiono  s     r   c           
   	   C   s   t j|}t  }|t js*|t j7 }g }|D ]Z}||rvzt j||}W q tk
rr   t j	|}Y qX nt j	|}|
| q2t j| |d }|d|d 7 }t| d d}	|	| W 5 Q R X d S )Nz: \
  z \
  
z.deprN   )r   rI   rs   getcwdendswithsepre   relpathr   abspathr5   rp   rR   rY   )
targetsourceZdependenciesZsrc_base_dircwdpathsfnamenewpathZdeplineZoutfiler   r   r   write_depfile  s"    

r  c                   C   sR   t dt  tj s"tjtjkr&d S tdtdkr>d S tjdt  d S )NzCython version %sr   r   zCython version %s
)	printr   r   stderrisattyr   r   fstatrY   r   r   r   r   print_version  s    r  c                 C   s   |   d}d}d|ks"d|krF|d|kr2dndd\}}t|}d|krv|d}|d| ||d d  }nt|}||7 }|d| d|t|   d d|   ||d  d}|dkr|S dS )	z
    Generate a 'normalised', simple digits string representation of a float value
    to allow string comparisons.  Examples: '.123', '123.456', '123.'
    r   r   Er   r   r   Nz.0)lowerlstripr   r   indexrb   rstrip)Z	float_strZ	str_valueZexpZnum_int_digitsr   r   r   r   normalise_float_repr  s.    


r  )FT)r   )NN)NN)r   N)N)Xr&   Z
__future__r   r   Zdeclarer6   listZ__builtin__r   ImportErrorr   r   r   rc   r   r   r   r   r   r	   r
   r   	functoolsr    r   r   r{   formatrG   compiler:   rI   getmtimerj   ZGENERATED_BY_MARKERencoderh   r   r,   r   r/   r8   r>   rC   rH   rM   rT   r_   rW   ri   rl   ro   rr   ru   ry   rv   rt   r|   findallr   r   subr   r   searchr   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   <module>   s            













!   




