a
    bR                     @   s  d Z ddlmZ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
Z
ddlZddlZe Zejd dk Zze W n ey   ddlmZ Y n0 zddlZW n ey   ddlZY n0 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 m!Z!m"Z" zddl#m$Z$ W n  ey:   ddl%m$Z$ Y n0 ddl&m'Z' ddl(m)Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddgg dddgdgddZ1e1d e1d< erdd Z2ndd Z2e!G dd  d e Z3e j4e'e3j5j 6d!d"e'e3j.j e'e3j7j d#Z dS )$uA  
=====================
Cython related magics
=====================

Magic command interface for interactive work with Cython

.. note::

  The ``Cython`` package needs to be installed separately. It
  can be obtained using ``easy_install`` or ``pip``.

Usage
=====

To enable the magics below, execute ``%load_ext cython``.

``%%cython``

{CYTHON_DOC}

``%%cython_inline``

{CYTHON_INLINE_DOC}

``%%cython_pyximport``

{CYTHON_PYXIMPORT_DOC}

Author:
* Brian Granger

Code moved from IPython and adapted by:
* Martín Gaitán

Parts of this code were taken from Cython.inline.
    )absolute_importprint_functionN   )reload)Distribution	Extension)	build_ext)display)magic_arguments)Magicsmagics_class
cell_magic)get_ipython_cache_dir)dedent   )__version__)CompileError   )cython_inline)	cythonizez-fprofile-generate-fprofile-dir={TEMPDIR})z-fprofile-usez-fprofile-correctionr   )genusez	-prof-genz	-prof-use)gcciccr   Zmingw32c                 C   s   t | tr| S | tS N)
isinstancebytesencodeIO_ENCODINGname r"   i/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/Cython/Build/IpythonMagic.py	encode_fsj   s    r$   c                 C   s   | S r   r"   r    r"   r"   r#   r$   m   s    c                       s  e Zd Z fddZdd Zedd Zedd Ze ej	d	d
ddddej	ddddddej	dddddddej	dddddddej	ddddddej	dd d!g d"dej	d#d!g d$dej	d%d&d!g d'dej	d(d)d*d+ej	d,d-d.d!g d/d0ej	d1d2d!g d3dej	d4d5d!g d6dej	d7d8ddd9d:ej	d;d<d=d>d?d:ed@dA Z
dBdC ZdRdDdEZdSdFdGZdHdI ZedJdK ZdLdM ZddddefdNdOZedPdQ Z  ZS )TCythonMagicsc                    s&   t t| | i | _i | _d| _d S )NF)superr%   __init___reloads_code_cache_pyximport_installed)selfshell	__class__r"   r#   r'   t   s    zCythonMagics.__init__c              	   C   sp   |j }d|v r|d }ndd |D }|D ]@}z| j||| i W q* tyh   d| }t|Y q*0 q*d S )N__all__c                 S   s   g | ]}| d s|qS )_)
startswith).0kr"   r"   r#   
<listcomp>       z,CythonMagics._import_all.<locals>.<listcomp>z%'module' object has no attribute '%s')__dict__r,   pushKeyErrorAttributeError)r+   moduleZmdictkeysr3   msgr"   r"   r#   _import_allz   s    
zCythonMagics._import_allc                 C   s   | j j}| j j}t|||dS )a  Compile and run a Cython code cell using Cython.inline.

        This magic simply passes the body of the cell to Cython.inline
        and returns the result. If the variables `a` and `b` are defined
        in the user's namespace, here is a simple example that returns
        their sum::

            %%cython_inline
            return a+b

        For most purposes, we recommend the usage of the `%%cython` magic.
        )localsglobals)r,   Zuser_global_nsZuser_nsr   )r+   linecellZlocsZglobsr"   r"   r#   r      s    zCythonMagics.cython_inlinec                 C   s   |  }|std|d }tj|ddd}|| W d   n1 sL0    Y  dtjvsf| js|ddl}|	  d	| _|| j
v r| j
| }nt| tj| }|| j
|< | | dS )
a)  Compile and import a Cython code cell using pyximport.

        The contents of the cell are written to a `.pyx` file in the current
        working directory, which is then imported using `pyximport`. This
        magic requires a module name to be passed::

            %%cython_pyximport modulename
            def f(x):
                return 2.0*x

        The compiled module is then imported and all of its symbols are
        injected into the user's namespace. For most purposes, we recommend
        the usage of the `%%cython` magic.
        zmodule name must be given.pyxwutf-8encodingN	pyximportr   T)strip
ValueErrorioopenwritesysmodulesr*   rG   installr(   
__import__r=   )r+   r@   rA   module_namefnamefrG   r:   r"   r"   r#   cython_pyximport   s     (


zCythonMagics.cython_pyximportz-az
--annotate
store_trueFz/Produce a colorized HTML version of the source.)actiondefaulthelpz-+z--cplusz Output a C++ rather than C file.z-3language_levelstore_constr   NzSelect Python 3 syntax.)destrV   constrW   rX   z-2r   zSelect Python 2 syntax.z-fz--forcezWForce the compilation of a new module, even if the source has been previously compiled.z-cz--compile-argsappendzoExtra flags to pass to compiler via the `extra_compile_args` Extension flag (can be specified  multiple times).z--link-argszjExtra flags to pass to linker via the `extra_link_args` Extension flag (can be specified  multiple times).z-lz--libzNAdd a library to link the extension against (can be specified multiple times).z-nz--namez%Specify a name for the Cython module.)rX   z-Llibrary_dirsdirzPAdd a path to the list of library directories (can be specified multiple times).)r[   metavarrV   rW   rX   z-Iz	--includezPAdd a path to the list of include directories (can be specified multiple times).z-Sz--srczFAdd a path to the list of src files (can be specified multiple times).z--pgopgozEnable profile guided optimisation in the C compiler. Compiles the cell twice and executes it in between to generate a runtime profile.)r[   rV   rW   rX   z	--verbosequietstore_falseTz_Print debug information like generated .c/.cpp file location and exact gcc/g++ command invoked.c              
   C   s\  t | j|}|dr|n|d }tjt d}||tj	tj
tf}tj|s\t| |jrj|d7 }|jr~|t f7 }|jrt|j}ndtt|d  }tj||d }tj||| j }	tj|	}
|jp|
 }|jrtj|sd}d}|rj| j|||||jd	}|du r2dS t|d
ksDJ |d }|| j|< |jrj| || z$| j|||jrdnd|jd W n t j!j"y   Y dS 0 t#$||	}| %| |jrXz<t&j'|dd}|( }W d   n1 s0    Y  W nB t)yF } z(t*dtj+d t*|tj+d W Y d}~nd}~0 0 t,-| .|S dS )al  Compile and import everything from a Cython code cell.

        The contents of the cell are written to a `.pyx` file in the
        directory `IPYTHONDIR/cython` using a filename with the hash of the
        code. This file is then cythonized and compiled. The resulting module
        is imported and all of its symbols are injected into the user's
        namespace. The usage is similar to that of `%%cython_pyximport` but
        you don't have to pass a module name::

            %%cython
            def f(x):
                return 2.0*x

        To compile OpenMP codes, pass the required  `--compile-args`
        and `--link-args`.  For example with gcc::

            %%cython --compile-args=-fopenmp --link-args=-fopenmp
            ...

        To enable profile guided optimisation, pass the ``--pgo`` option.
        Note that the cell itself needs to take care of establishing a suitable
        profile when executed. This can be done by implementing the functions to
        optimise, and then calling them directly in the same cell on some realistic
        training data like this::

            %%cython --pgo
            def critical_function(data):
                for item in data:
                    ...

            # execute function several times to build profile
            from somewhere import some_typical_data
            for _ in range(100):
                critical_function(some_typical_data)

        In Python 3.5 and later, you can distinguish between the profile and
        non-profile runs as follows::

            if "_pgo_" in __name__:
                ...  # execute critical code here
        
cython)ra   Z_cython_magic_rD   z.htmlTN)rb   r   r   r   )pgo_step_namerb   rE   zICython completed successfully but the annotated source could not be read.file)/r
   Zparse_argstringre   endswithospathjoinr   rM   version_info
executablecython_versionexistsmakedirsra   forcetimer!   strhashlibmd5r   	hexdigestso_extisfileannotate
_cythonizerb   lenr)   _profile_pgo_wrapper_build_extension	distutilserrorsr   impload_dynamicr=   rJ   rK   readIOErrorprintstderrr	   ZHTMLclean_annotated_html)r+   r@   rA   argscodelib_dirkeyrQ   Z	html_filemodule_pathZhave_moduleZneed_cythonize	extension
extensionsr:   rS   Zannotated_htmler"   r"   r#   re      s`    m




,$zCythonMagics.cythonc              	   C   s   t  |}|j}d| }tj||d }tj|ddd*}|t	d||d  W d   n1 sj0    Y  |j
|g |_
||_| j||d	d
 tj||| j }t|| dS )a  
        Generate a .c file for a separate extension module that calls the
        module init function of the original module.  This makes sure that the
        PGO profiler sees the correct .o file of the final module, but it still
        allows us to import the module under a different name for profiling,
        before recompiling it into the PGO optimised module.  Overwriting and
        reimporting the same shared library is not portable.
        Z_pgo_z.crC   rD   rE   a<  
            #include "Python.h"
            #if PY_MAJOR_VERSION < 3
            extern PyMODINIT_FUNC init%(module_name)s(void);
            PyMODINIT_FUNC init%(pgo_module_name)s(void); /*proto*/
            PyMODINIT_FUNC init%(pgo_module_name)s(void) {
                PyObject *sys_modules;
                init%(module_name)s();  if (PyErr_Occurred()) return;
                sys_modules = PyImport_GetModuleDict();  /* borrowed, no exception, "never" fails */
                if (sys_modules) {
                    PyObject *module = PyDict_GetItemString(sys_modules, "%(module_name)s");  if (!module) return;
                    PyDict_SetItemString(sys_modules, "%(pgo_module_name)s", module);
                    Py_DECREF(module);
                }
            }
            #else
            extern PyMODINIT_FUNC PyInit_%(module_name)s(void);
            PyMODINIT_FUNC PyInit_%(pgo_module_name)s(void); /*proto*/
            PyMODINIT_FUNC PyInit_%(pgo_module_name)s(void) {
                return PyInit_%(module_name)s();
            }
            #endif
            )rQ   pgo_module_nameNr   )rf   )copyr!   rj   rk   rl   rJ   rK   rL   textwrapr   sourcesr~   rx   r   r   )r+   r   r   rQ   r   Zpgo_wrapper_c_filerS   Zso_module_pathr"   r"   r#   r}   q  s    	

&z!CythonMagics._profile_pgo_wrapperc              
   C   s4  t j||d }t|}|j}ttt|j}d|v rNdd l	}	|
|	  tj|ddd}
|
| W d    n1 s~0    Y  t||g| ||j|j|j|j|jrdndd	}z`t||jd
d}|jd ur|jdv sJ |j|d< ntjd dkrd|d< t|gfi |W S  ty.   Y d S 0 d S )NrB   numpyr   rC   rD   rE   zc++c)r!   r   include_dirsr^   extra_compile_argsextra_link_args	librarieslanguageT)rb   rz   rr   )r   r   rY   r   )rj   rk   rl   r$   includelistmaprt   srcr   r]   Zget_includerJ   rK   rL   r   r^   Zcompile_argsZ	link_argslibZcplusdictrz   rY   rM   rm   r   r   )r+   rQ   r   r   r   rb   Zpyx_fileZc_include_dirsZc_src_filesr   rS   r   optsr"   r"   r#   r{     sB    (

zCythonMagics._cythonizec              	   C   sn   | j ||||d}d }z8|s,tjtjj}|  W |sj|d urjtj| n|sh|d urhtj| 0 d S )N)r   temp_dirrf   )_get_build_extensionr   logset_thresholdDEBUGrun)r+   r   r   r   rf   rb   build_extensionZold_thresholdr"   r"   r#   r~     s    
zCythonMagics._build_extensionc           
         s  |j j}|dkr|j j}|snfd|v s2d|d v r8d}nLd|v sLd|d v rRd}n2d|v sfd|d v rld}nd|v sd|d v rd}t|}g }|r||v r fdd|| D }|jD ].}	||	j|	jf |	j| |	_|	j| |	_qnt	d	||f t
jd
 |S )Nunixclangr   r   r   zg++c                    s   g | ]}|j  d qS ))ZTEMPDIR)format)r2   rS   r   r"   r#   r4     r5   z/CythonMagics._add_pgo_flags.<locals>.<listcomp>z6No PGO %s configuration known for C compiler type '%s'rg   )compilercompiler_typecompiler_so
PGO_CONFIGgetr   r]   r   r   r   rM   r   )
r+   r   Z	step_namer   r   Zcompiler_cmdconfigZ
orig_flagsflagsr   r"   r   r#   _add_pgo_flags  s2    

zCythonMagics._add_pgo_flagsc                 C   s6   z| j W S  ty0   |  d| _ | j  Y S 0 dS )z*The extension suffix for compiled modules. N)Z_so_extr9   r   Zget_ext_filenamer+   r"   r"   r#   rx     s
    zCythonMagics.so_extc                 C   s0   zddl m} W n ty"   Y n
0 |  dS )zzclear distutils mkpath cache

        prevents distutils from skipping re-creation of dirs that have been removed
        r   )_path_createdN)distutils.dir_utilr   ImportErrorclear)r+   r   r"   r"   r#   _clear_distutils_mkpath_cache  s
    z*CythonMagics._clear_distutils_mkpath_cachec           	         s   |    t }| }z|d W n ty6   Y n0 || sJ|| j rr|G  fddd|}||}|  rt|_	|rt|}||_
|d ur|g|_|S )Nz	setup.cfgc                       s   e Zd Z fddZdS )z5CythonMagics._get_build_extension.<locals>._build_extc                    s    |   |  d S r   )build_extensionsr   Zadd_pgo_flagsZbase_build_extrf   r   r"   r#   r     s    zFCythonMagics._get_build_extension.<locals>._build_ext.build_extensionsN)__name__
__module____qualname__r   r"   r   r"   r#   
_build_ext  s   r   )r   r   find_config_filesremoverI   parse_config_filesr   finalize_optionsr$   
build_temp	build_libr   )	r+   r   r   r   rf   r   distconfig_filesr   r"   r   r#   r     s2    
z!CythonMagics._get_build_extensionc                    s*   t d d fdd|  D } | S )zClean up the annotated HTML source.

        Strips the link to the generated C or C++ file, which we do not
        present to the user.
        z&<p>Raw output: <a href="(.*)">(.*)</a>rd   c                 3   s   | ]}  |s|V  qd S r   )match)r2   lrr"   r#   	<genexpr>+  r5   z4CythonMagics.clean_annotated_html.<locals>.<genexpr>)recompilerl   
splitlines)htmlr"   r   r#   r   #  s    
z!CythonMagics.clean_annotated_html)T)NNT)r   r   r   r'   r=   r   r   rT   r
   Zargumentre   r}   r{   r~   r   propertyrx   r   r   r   staticmethodr   __classcell__r"   r"   r-   r#   r%   q   s   

&


(m/
$


"r%   z-+, --cplusz--cplus    )Z
CYTHON_DOCZCYTHON_INLINE_DOCZCYTHON_PYXIMPORT_DOC)8__doc__
__future__r   r   r   rJ   rj   r   rM   rs   r   Zdistutils.logr   r   getfilesystemencodingr   rm   ZIS_PY2r   	NameErrorru   r   rv   distutils.corer   r   Zdistutils.command.build_extr   ZIPython.corer	   r
   ZIPython.core.magicr   r   r   ZIPython.pathsr   ZIPython.utils.pathZIPython.utils.textr   ZShadowr   ro   ZCompiler.Errorsr   ZInliner   ZDependenciesr   r   r$   r%   r   re   replacerT   r"   r"   r"   r#   <module>   sr   -
   ?


