
    -PhNH                     T   d 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 dZdZdZd	Zd
ZdZd Zd Zd Zd Zd"dZd Zd Zd#dZ G d de          Z G d de          Z G d de          Z G d de          Zd Z	 	 	 	 d$dZ d Z!d  Z"e#d!k    r e"             dS dS )%a  
Import hooks; when installed with the install() function, these hooks
allow importing .pyx files as if they were Python modules.

If you want the hook installed every time you run Python
you can add it to your Python version by adding these lines to
sitecustomize.py (which you can create from scratch in site-packages
if it doesn't exist there or somewhere else on your python path)::

    import pyximport
    pyximport.install()

For instance on the Mac with a non-system Python 2.3, you could create
sitecustomize.py with only those two lines at
/usr/local/lib/python2.3/site-packages/sitecustomize.py .

A custom distutils.core.Extension instance and setup() args
(Distribution) for for the build can be defined by a <modulename>.pyxbld
file like:

# examplemod.pyxbld
def make_ext(modname, pyxfilename):
    from distutils.extension import Extension
    return Extension(name = modname,
                     sources=[pyxfilename, 'hello.c'],
                     include_dirs=['/myinclude'] )
def make_setup_args():
    return dict(script_args=["--compiler=mingw32"])

Extra dependencies can be defined by a <modulename>.pyxdep .
See README.

Since Cython 0.11, the :mod:`pyximport` module also has experimental
compilation support for normal Python modules.  This allows you to
automatically run Cython on every .pyx and .py module that Python
imports, including parts of the standard library and installed
packages.  Cython will still fail to compile a lot of Python modules,
in which case the import mechanism will fall back to loading the
Python source modules instead.  The .py import mechanism is installed
like this::

    pyximport.install(pyimport = True)

Running this module as a top-level script will run a test and then print
the documentation.
    N)MetaPathFinder)ExtensionFileLoaderSourceFileLoader)spec_from_file_location	pyximportz.pyz.pyxz.pyxdep.pyxbldFc                 2    |r| |z  } t          |            d S N)printmessageargss     S/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pyximport/pyximport.py_printr   B   s"     !D.	'NNNNN    c                 8    t           rt          | |           d S d S r
   )DEBUG_IMPORTr   r   s     r   _debugr   H   s*     w r   c                 &    t          | |           d S r
   )r   r   s     r   _infor   M   s    
7Dr   c                     dd l }ddlm} |j                            d|  |d|                     }|j                            |          }|j                            |           |S )Nr   )r   XXXXloader)importlib.utilimportlib.machineryr   utilr   module_from_specr   exec_module)	file_path	importlibr   specmodules        r   load_sourcer$   Q   s|    444444>11&)L\L\]cenLoLo1ppD^,,T22FKF###Mr   c                     t          | |          \  }}|sZt          |t                    s&|                    t	          j                              }ddlm}  || |g          }|	d|i|_        ||fS )Nr   )	Extension)namesourceslanguage_level)	handle_special_build
isinstancestrencodesysgetfilesystemencodingdistutils.extensionr&   cython_directives)modnamepyxfilenamer)   extension_mod
setup_argsr&   s         r   get_distutils_extensionr6   Z   s      4G[IIM* Q+s++ 	J &,,S-F-H-HIIK111111!	;-HHH%/?.PM+##r   c                    t           j                            |          d         t          z   d }i }t           j                                      rt                    }t          |dd           }|r" || |          }|r|j        sJ dz              t          |dd           }|r, |            }t          |t                    sJ dz              |s|sJ dz              fd|j        D             |_        ||fS )Nr   make_extz'make_ext in %s did not return Extensionmake_setup_argsz+make_setup_args in %s did not return a dictz'neither make_ext nor make_setup_args %sc                     g | ]?}t           j                            t           j                                      |          @S  )ospathjoindirname).0sourcespecial_builds     r   
<listcomp>z(handle_special_build.<locals>.<listcomp>   sG     2 2 2! w||BGOOM$B$BFKK 2 2 2r   )
r<   r=   splitext
PYXBLD_EXTexistsr$   getattrr(   r+   dict)r2   r3   extr5   modr8   r9   rB   s          @r   r*   r*   n   sZ   G$$[11!4zAM
CJ	w~~m$$ 2
 -((3z$// 	b(7K00Ca3;aa(QTa(aaaa!#'8>> 	:(**Jj.. : :1^+829 : : : 	:j 	: 	:#L+8$9 	: 	: 	:2 2 2 2%([2 2 2
?r   c                 :   dt                      v }t          j                            |           d         t          z   }t          j                            |          r;t          |          5 }|                                }d d d            n# 1 swxY w Y   d |D             }|g}|D ]f}t          j                            t          j        	                    |          |          }|
                    t          j        |                     g|rg t          d d <   |D ]y}ddlm}	  |	||           rct          d| |           t          j                            |          }
t          j        | |
|
f           |rt                              |           xd S d S )N_test_filesr   c                 6    g | ]}|                                 S r;   )strip)r@   depends     r   rC   z'handle_dependencies.<locals>.<listcomp>   s     888f6<<>>888r   )newerzRebuilding %s because of %s)globalsr<   r=   rD   
PYXDEP_EXTrF   open	readlinesr>   r?   extendglobrL   distutils.dep_utilrP   r   getmtimeutimeappend)r3   testing
dependfilefiddependsfilesrO   fullpathfilerP   filetimes              r   handle_dependenciesrc      s   wyy(G!!+..q1J>J 
w~~j!! -* 	&mmooG	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&88888  	. 	.Fw||BGOOJ$?$?$*, ,HLL8,,----  	 KN
  	- 	-D000000uT;'' -4k4HHH7++D11x&:;;; -&&t,,,7- -(	- 	-s   -BBBc           	      $   t           j                            |          sJ d|z              t          |           t	          | ||          \  }}t
          j        }t
          j                                        }|	                    |           |
                    d|          }ddlm}	 t          j                    }
d}|r1t          j        dk    r!t           j                            ||g          }t#          |          dk    rTt           j                            ||          }t           j                            ||          }t          j        |           	 |	                    ||||||t
          j                  }t          j        |
           n# t          j        |
           w xY wt           j                            ||          }t           j                            |          sJ d	|z              t           j                            t           j                            |          | d
z             }t1          j        |          }|D ]>}||k    r6	 t          j        |           # t4          $ r t7          d|           Y :w xY w?|S )NzPath does not exist: %sbuild_in_temp   )pyxbuild win32   )re   pyxbuild_dirr5   inplacereload_supportzCannot find: %sz_*zCouldn't remove %s)r<   r=   rF   rc   r6   pyxargsre   r5   copyupdatepoprh   rg   getcwdr.   platformcommonprefixlenrelpathchdir
pyx_to_dllrm   r>   r?   rV   removeIOErrorr   )r'   r3   rk   rl   r)   r4   r5   re   sargsrg   olddircommonso_pathjunkpath	junkstuffr=   s                   r   build_moduler      sl   7>>+&&OO(AK(OOOO$$$ 7k> Z ZM:)M##%%E	LLIIom<<MY[[FF C// %%|[&ABB
6{{Rgook6::w|V<<
%%k=4A3?16.55<5K & M M 	gll67++G7>>'""??$5$????w||BGOOG44d4i@@H	(##I 2 27??2	$ 2 2 2*D111112  Ns   &F F,I//JJc                   &    e Zd ZedddfdZddZdS )PyxImportMetaFinderNFc                 >    || _         || _        || _        || _        d S r
   )rk   rl   r)   	extensionselfr   rk   rl   r)   s        r   __init__zPyxImportMetaFinder.__init__   s$    (,"r   c                    |s!t          j                    gt          j        z   }d|v r|                    d          ^ }}n|}|D ]}t           j                            t           j                            ||                    rKt           j                            ||d| j        z             }t           j                            ||          g}n*t           j                            ||| j        z             }d }t           j                            |          st          ||t          || j        | j        | j                  |          c S d S )N.r   r   submodule_search_locations)r<   rr   r.   r=   splitisdirr>   r   rF   r   PyxImportLoaderrk   rl   r)   )	r   fullnamer=   targetparentsr'   entryfilenamesubmodule_locationss	            r   	find_speczPyxImportMetaFinder.find_spec   s?    	,IKK=38+D(??%^^C00NWddD 	@ 	@Ew}}RW\\%6677 +7<<tZ$.5PQQ')w||E4'@'@&A##7<<tdn/DEE&*#7>>(++ *(&x1BDLRVReff+>@ @ @ @ @ @
 tr   r
   )__name__
__module____qualname__PYX_EXTr   r   r;   r   r   r   r      sF        !(tU[_ # # # #     r   r   c                   &    e Zd ZedddfdZddZdS )PyImportMetaFinderNFc                 ~    || _         || _        || _        || _        i | _        g d| _        ddg| _        d| _        d S )N)Cythonrg   zpyximport.pyxbuild	distutilscythonzCython.z
distutils.F)rk   rl   r)   r   uncompilable_modulesblocked_modulesblocked_packagesfoundr   s        r   r   zPyImportMetaFinder.__init__  sT    (,"$&! 7  7  7!*L 9


r   c                    | j         rd S t          j        v rd S t          fd| j        D                       rd S | j        v rd S | j                                       }|s!t          j                    gt          j	        z   }	 |D ]'}t          j	        
                    t          j	                            ||                    rKt          j	                            ||d| j        z             }t          j	                            ||          g}n*t          j	                            ||| j        z             }d }t          j	                            |          sd| _         t          |t          || j        | j        | j                  |          c | j                                         S 	 | j                                         n# | j                                         w xY wd S )Nc                 :    g | ]}                     |          S r;   )
startswith)r@   pkgr   s     r   rC   z0PyImportMetaFinder.find_spec.<locals>.<listcomp>  s'    JJJS##C((JJJr   r   Tr   )r   r.   modulesanyr   r   rZ   r<   rr   r=   r   r>   r   rF   r   r   rk   rl   r)   rq   )r   r   r=   r   r'   r   r   r   s    `      r   r   zPyImportMetaFinder.find_spec  s   : 	4s{""4JJJJD4IJJJKK 	4t+++4##H--- 	,IKK=38+D	' D D7==eT!:!:;; /!w||E4dn9TUUH+-7<<t+D+D*E''!w||E4$.3HIIH*.'w~~h// !
.h*8T5FVZVijj/BD D D D D
  $$&&&&#D"  $$&&&&D $$&&&&ts   DG G)r
   )r   r   r   PY_EXTr   r   r;   r   r   r   r      sF        !'dEZ^ 	 	 	 	# # # # # #r   r   c                   2     e Zd Z fdZ fdZ fdZ xZS )r   c                     t           j                            t           j                            |                    d         }t	                                          ||           || _        || _        || _        d S )Nr   )	r<   r=   rD   basenamesuperr   _pyxbuild_dir_inplace_language_level)r   r   rk   rl   r)   module_name	__class__s         r   r   zPyxImportLoader.__init__5  sd    g&&rw'7'7'A'ABB1Eh///)-r   c                 z   	 t          |j        |j        | j        | j        | j                  }|| _        ||_        t                                          |          S # t          $ rP}t          d|z             t          j        r|j                            t                    rt          j                            |j        |j        t%          |j        |j                            }t          j                            |          }|j        |j        |j        dz   |j        dz   fv sJ |j        |j        f            |cY d }~S t+          j                    d         }dd l}t1          d|j        d	 |j        t+          j                    d d                    }|                    |          d }~ww xY w)
N)r3   rk   rl   r)   #Failed to load extension module: %rr   co   r   zBuilding module z	 failed: )r   r'   originr   r   r   r=   r   create_module	Exceptionr   rn    load_py_module_on_import_failureendswithr   r!   r   r   r   r   __file__r.   exc_info	tracebackImportErrorformat_exception_onlywith_traceback)	r   r"   r~   failure_excrJ   tbr   excr   s	           r   r   zPyxImportLoader.create_module<  s   	-"49$+TXTf+/=I]_ _ _GDI!DK77((... 	- 	- 	-8;FGGG7 -DK<P<PQW<X<X - ~==diEUVZV_aealEmEm > o on55d;;|T[35FVYHY'ZZZZ]`]ikokv\wZZZ





\^^A&    !kIII>y>rPQr@RSS#U V V((,,,	-s&   AA F:*CF5F:A-F55F:c                    	 t                                          |          S # t          $ rV}dd l}t	          d|z             t          d|j        d |j        t          j	                    d d                    d }~ww xY w)Nr   r   zExecuting module z failed r   )
r   r   r   r   r   r   r   r   r.   r   )r   r#   r   r   r   s       r   r   zPyxImportLoader.exec_moduleR  s    	\77&&v... 	\ 	\ 	\8;FGGG+OOO%DY%DclnnUWVWUWFX%Y%Y[ \ \ \	\s    $ 
BAA??B)r   r   r   r   r   r   __classcell__)r   s   @r   r   r   3  ss        . . . . .- - - - -,\ \ \ \ \ \ \ \ \r   r   c                       e Zd ZdZdZi ZdS )PyxArgsTN)r   r   r   	build_dirre   r5   r;   r   r   r   r   ]  s        IMJJJr   r   c                      d} d}t           j        D ]1}t          |t                    rt          |t                    rd} /d}2| |fS )NFT)r.   	meta_pathr+   r   r   )has_py_importerhas_pyx_importerimporters      r   _have_importersr   c  s^    OM ( (h 344 	(($677 ("&#' ,,,r   Tc	                 f   |i }|s=t           j                            t           j                            d          d          }t	                      a|t
          _        |t
          _        |pi                                 t
          _	        |t
          _
        |t
          _        t                      \  }	}
d\  }}|r@|	s>t          |||          }ddl}ddl}ddl}t"          j                            d|           | r3|
s1t)          |||          }t"          j                            |           ||fS )a5	   Main entry point for pyxinstall.

    Call this to install the ``.pyx`` import hook in
    your meta-path for a single Python process.  If you want it to be
    installed whenever you use Python, add it to your ``sitecustomize``
    (as described above).

    :param pyximport: If set to False, does not try to import ``.pyx`` files.

    :param pyimport: You can pass ``pyimport=True`` to also
        install the ``.py`` import hook
        in your meta-path.  Note, however, that it is rather experimental,
        will not work at all for some ``.py`` files and packages, and will
        heavily slow down your imports due to search and compilation.
        Use at your own risk.

    :param build_dir: By default, compiled modules will end up in a ``.pyxbld``
        directory in the user's home directory.  Passing a different path
        as ``build_dir`` will override this.

    :param build_in_temp: If ``False``, will produce the C files locally. Working
        with complex dependencies and debugging becomes more easy. This
        can principally interfere with existing files of the same name.

    :param setup_args: Dict of arguments for Distribution.
        See ``distutils.core.setup()``.

    :param reload_support: Enables support for dynamic
        ``reload(my_module)``, e.g. after a change in the Cython code.
        Additional files ``<so_path>.reloadNN`` may arise on that account, when
        the previously loaded module file cannot be overwritten.

    :param load_py_module_on_import_failure: If the compilation of a ``.py``
        file succeeds, but the subsequent import fails for some reason,
        retry the import with the normal ``.py`` module instead of the
        compiled module.  Note that this may lead to unpredictable results
        for modules that change the system state during their import, as
        the second import will rerun these modifications in whatever state
        the system was left after the import of the compiled module
        failed.

    :param inplace: Install the compiled module
        (``.so`` for Linux and Mac / ``.pyd`` for Windows)
        next to the source file.

    :param language_level: The source language level to use: 2 or 3.
        The default is to use the language level of the current Python
        runtime for .py files and Py2 for ``.pyx`` files.
    N~r   )NN)rk   rl   r)   r   )r<   r=   r>   
expanduserr   rn   r   re   ro   r5   rm   r   r   r   Cython.Compiler.MainCython.Compiler.PipelineCython.Compiler.Optimizer.   r   insertr   rZ   )r   pyimportr   re   r5   rm   r   rl   r)   r   r   py_importerpyx_importerr   s                 r   installr   p  sX   j 
 EGLL!3!3C!8!8)DD	 iiG!G)G$*0022G+G/OG,(7(9(9%O% *K - -(i8FH H H 	XWWWWWWWWWWWQ,,, +) +*	7:HJ J J\***$$r   c                     	 t           j                            |            n# t          $ r Y nw xY w	 t           j                            |           dS # t          $ r Y dS w xY w)z#
    Uninstall an import hook.
    N)r.   r   ry   
ValueError)r   r   s     r   	uninstallr     s    [))))   \*****   s   " 
//A 
A"!A"c                      dd l } t          | _        t          |           D ]@}t	          | |          }	 t          |dt                     *# t          t          f$ r Y =w xY wt          |            d S )Nr   r   )	__main__mod_namer   dirrG   setattrAttributeError	TypeErrorhelp)r   r'   items      r   	show_docsr     s    OOO HH  x&&	D,1111	* 	 	 	D	NNNNNs   A

AAr   r
   )NFN)	TFNTNFFFN)$__doc__rV   r!   r<   r.   importlib.abcr   r   r   r   r   r   r   r   r   rR   rE   r   r   r   r   r$   r6   r*   rc   r   r   r   r   objectr   r   r   r   r   r   r;   r   r   <module>r      sE  - -^      				 



 ( ( ( ( ( ( E E E E E E E E 2 2 2 2 2 2	


    
    $ $ $ $(  4$- $- $-N* * * *^    .   D0 0 0 0 0 0 0 0f&\ &\ &\ &\ &\) &\ &\ &\T    f   
- 
- 
- KO,1<AQ% Q% Q% Q%h  "	 	 	 zIKKKKK r   