o
    ˷e(                     @   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 )zCompiler 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__compiler_flag).0fname r
   N/var/www/ideatree/venv/lib/python3.10/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   )hashlibsha1encode	hexdigestformat)codenumber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 )zParse 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filename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   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>   s    z)CachingCompiler.cache.<locals>.<listcomp>)r3   r!   lentime
splitlinesr   cacher   )r#   r2   r   r1   nameentryr
   r
   r   r:      s   


zCachingCompiler.cachec              	   c   sJ    | j  |@ }| j |B | _ zd V  W |  j | M  _ d S |  j | M  _ w r   r/   )r#   r(   turn_on_bitsr
   r
   r   extra_flags   s   &zCachingCompiler.extra_flags)r$   r%   )r   N)__name__
__module____qualname____doc__r   r,   r.   propertyr0   r3   r:   r   r>   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   r:   updater   )argsr
   r
   r   r       s   
r    )r   )rB   r   astr   r   	functoolsr   r   operatorr8   
contextlibr   reduceor_all_feature_names	PyCF_MASKr   r   r   r    r
   r
   r
   r   <module>   s$    
x