a
    ÂóFb(  ã                   @   s–   d Z ddlZddlm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 e ejdd„ ejD ƒ¡Zddd„ZG d	d
„ d
ejƒZdd„ ZdS )zéCompiler tools with improved interactive support.

Provides compilation machinery similar to codeop, but with caching support so
we can provide interactive tracebacks.

Authors
-------
* Robert Kern
* Fernando Perez
* Thomas Kluyver
é    N)ÚPyCF_ONLY_AST)Úcontextmanagerc                 c   s   | ]}t t|ƒjV  qd S ©N)ÚgetattrÚ
__future__Zcompiler_flag)Ú.0Úfname© r	   ú6lib/python3.9/site-packages/IPython/core/compilerop.pyÚ	<genexpr>/   s   ÿr   c                 C   s(   t  |  d¡¡ ¡ }d ||dd… ¡S )zf Compute a (probably) unique name for code for caching.

    This now expects code to be unicode.
    zutf-8z<ipython-input-{0}-{1}>Né   )ÚhashlibZsha1ÚencodeZ	hexdigestÚformat)ÚcodeÚnumberZhash_digestr	   r	   r
   Ú	code_name6   s    r   c                   @   sT   e Zd ZdZdd„ Zddd„Zdd	„ Zed
d„ ƒZdd„ Z	ddd„Z
edd„ ƒZdS )ÚCachingCompilerzFA compiler that caches code compiled from interactive statements.
    c                 C   s>   t j | ¡ ttdƒsi t_ttdƒs.tjt_tt_i | _	d S )NÚ_ipython_cacheÚ_checkcache_ori)
ÚcodeopÚCompileÚ__init__ÚhasattrÚ	linecacher   Ú
checkcacher   Úcheck_linecache_ipythonÚ_filename_map©Úselfr	   r	   r
   r   I   s    

zCachingCompiler.__init__ú	<unknown>Úexecc                 C   s   t |||| jtB dƒS )zÅParse code to an AST with the current compiler flags active.

        Arguments are exactly the same as ast.parse (in the standard library),
        and are passed to the built-in compile function.é   )ÚcompileÚflagsr   )r   ÚsourceÚfilenameZsymbolr	   r	   r
   Ú	ast_parsed   s    zCachingCompiler.ast_parsec                 C   s   t j| _dS )z&Reset compiler flags to default state.N)r   ZPyCF_DONT_IMPLY_DEDENTr$   r   r	   r	   r
   Úreset_compiler_flagsk   s    z$CachingCompiler.reset_compiler_flagsc                 C   s   | j S )z;Flags currently active in the compilation process.
        ©r$   r   r	   r	   r
   Úcompiler_flagsq   s    zCachingCompiler.compiler_flagsc                 C   s
   t ||ƒS )a¹  Compute filename given the code, and the cell number.

        Parameters
        ----------
        raw_code : str
            The raw cell code.
        transformed_code : str
            The executable Python source code to cache and compile.
        number : int
            A number which forms part of the code's name. Used for the execution
            counter.

        Returns
        -------
        The computed filename.
        )r   )r   Úraw_codeÚtransformed_coder   r	   r	   r
   Úget_code_namew   s    zCachingCompiler.get_code_namer   Nc                 C   s^   |du r|}|   |||¡}|| j|< t|ƒt ¡ dd„ | ¡ D ƒ|f}|tj|< |tj|< |S )ah  Make a name for a block of code, and cache the code.

        Parameters
        ----------
        transformed_code : str
            The executable Python source code to cache and compile.
        number : int
            A number which forms part of the code's name. Used for the execution
            counter.
        raw_code : str
            The raw code before transformation, if None, set to `transformed_code`.

        Returns
        -------
        The name of the cached code (as a string). Pass this as the filename
        argument to compilation, so that tracebacks are correctly hooked up.
        Nc                 S   s   g | ]}|d  ‘qS )Ú
r	   )r   Úliner	   r	   r
   Ú
<listcomp>§   ó    z)CachingCompiler.cache.<locals>.<listcomp>)r-   r   ÚlenÚtimeÚ
splitlinesr   Úcacher   )r   r,   r   r+   ÚnameÚentryr	   r	   r
   r5   Š   s    
ü

zCachingCompiler.cachec              	   c   sJ   | j  |@ }| j |B | _ zd V  W |  j | M  _ n|  j | M  _ 0 d S r   r)   )r   r$   Zturn_on_bitsr	   r	   r
   Úextra_flags®   s
    zCachingCompiler.extra_flags)r    r!   )r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r(   Úpropertyr*   r-   r5   r   r8   r	   r	   r	   r
   r   E   s   


$r   c                  G   s   t j| Ž  t j t j¡ dS )zECall linecache.checkcache() safely protecting our cached values.
    N)r   r   r5   Úupdater   )Úargsr	   r	   r
   r   ½   s    
r   )r   )r<   r   Úastr   r   Ú	functoolsr   r   Úoperatorr3   Ú
contextlibr   ÚreduceÚor_Zall_feature_namesZ	PyCF_MASKr   r   r   r   r	   r	   r	   r
   Ú<module>   s"   ÿÿ
x