
    G/Ph                         d 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gZddg dddZg g fdZ	d	 Z
d
 Zd Zg g g fdZd Zd ZdS )zb
Build a c-extension module on-the-fly in tests.
See build_and_import_extensions for usage hints

    Nbuild_and_import_extensioncompile_extension_module )prologue	build_dirinclude_dirs	more_initc                   |t          ||           z   }d}|st          j        d          }|r
|dz  }||z  }|dz  }t          | ||          }	 t	          | |||          }	n&# t
          $ r}
t          d| d          |
d}
~
ww xY wddl}|j        	                    | |	          }|j        
                    |          }|j                            |           |S )	a  
    Build and imports a c-extension module `modname` from a list of function
    fragments `functions`.


    Parameters
    ----------
    functions : list of fragments
        Each fragment is a sequence of func_name, calling convention, snippet.
    prologue : string
        Code to precede the rest, usually extra ``#include`` or ``#define``
        macros.
    build_dir : pathlib.Path
        Where to build the module, usually a temporary directory
    include_dirs : list
        Extra directories to find include files when compiling
    more_init : string
        Code to appear in the module PyMODINIT_FUNC

    Returns
    -------
    out: module
        The module will have been loaded and is ready for use

    Examples
    --------
    >>> functions = [("test_bytes", "METH_O", """
        if ( !PyBytesCheck(args)) {
            Py_RETURN_FALSE;
        }
        Py_RETURN_TRUE;
    """)]
    >>> mod = build_and_import_extension("testme", functions)
    >>> assert not mod.test_bytes('abc')
    >>> assert mod.test_bytes(b'abc')
    z
    PyObject *mod = PyModule_Create(&moduledef);
    #ifdef Py_GIL_DISABLED
    PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
    #endif
           .z.#define INITERROR return NULL
                z
return mod;zcould not compile in :Nr   )_make_methodspathlibPath_make_sourcer   	ExceptionRuntimeErrorimportlib.utilutilspec_from_file_locationmodule_from_specloaderexec_module)modname	functionsr   r   r   r	   bodyinitsource_stringmod_soe	importlibspecfoos                 _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/numpy/testing/_private/extbuild.pyr   r      s)   N mIw777DD  &L%%	   		OD $55MH)Ym= = H H H?9???@@aGH >11'6BBD
.
)
)$
/
/CKC   Js   A" "
B,B  Bc                     |                      d          d         }|| z  }|                    d           t          ||          }|t          j        d          gz   }t          |||z  |g g           S )aH  
    Build an extension module and return the filename of the resulting
    native code file.

    Parameters
    ----------
    name : string
        name of the module, possibly including dots if it is a module inside a
        package.
    builddir : pathlib.Path
        Where to build the module, usually a temporary directory
    include_dirs : list
        Extra directories to find include files when compiling
    libraries : list
        Libraries to link into the extension module
    library_dirs: list
        Where to find the libraries, ``-L`` passed to the linker
    r   Texist_ok	INCLUDEPY)outputfilenamer   	librarieslibrary_dirs)splitmkdir_convert_str_to_file	sysconfigget_config_var
_c_compile)	namebuilddirr   r   r*   r+   r   dirnamecfiles	            r#   r   r   T   s    * jjoob!GoGMM4M    88E9#;K#H#H"IILg/!Rb
 
 
 
    c                     |dz  }|                     d          5 }|                    t          |                      ddd           n# 1 swxY w Y   |S )zHelper function to create a file ``source.c`` in `dirname` that contains
    the string in `source`. Returns the file name
    zsource.cwN)openwritestr)sourcer4   filenamefs       r#   r.   r.   u   s     #H	s		 q	F              Os   #A

AAc           
      R   g }g }| D ]b\  }}}|d|}d|v rd}nd}|                     d|d|d|d           d	                    |||
          }	|                     |	           cd                    |          dt          d                    |          |          z  z   }
|
S )z Turns the name, signature, code in functions into complete functions
    and lists them in a methods_table. Then turns the methods_table into a
    ``PyMethodDef`` structure and returns the resulting code fragment ready
    for compilation
    _METH_KEYWORDSz2(PyObject *self, PyObject *args, PyObject *kwargs)z (PyObject *self, PyObject *args)z{"z", (PyCFunction)z, z},z^
        static PyObject* {cfuncname}{signature}
        {{
        {code}
        }}
        )	cfuncname	signaturecode
a6  
    static PyMethodDef methods[] = {
    %(methods)s
    { NULL }
    };
    static struct PyModuleDef moduledef = {
        PyModuleDef_HEAD_INIT,
        "%(modname)s",  /* m_name */
        NULL,           /* m_doc */
        -1,             /* m_size */
        methods,        /* m_methods */
    };
    )methodsr   )appendformatjoindict)r   r   methods_tablecodesfuncnameflagsrD   rB   rC   	func_coder   s              r#   r   r      s     ME!*    %&ww1	e##LII:I08)))UUUK	M 	M 	M
 FY)$FGG 	 	Y99U  tyy//
A
A
AB BD Kr6   c                 0    dt          | ||          z  }|S )zG Combines the code fragments into source code ready to be compiled
    zn
    #include <Python.h>

    %(body)s

    PyMODINIT_FUNC
    PyInit_%(name)s(void) {
    %(init)s
    }
    )r2   r   r   )rJ   )r2   r   r   rD   s       r#   r   r      s0    	 4  D Kr6   c           	         t           j        dk    r2dg}dt          j                            t           j        d          z   g}n+t           j                            d          rg d}g }ng x}}	 t           j        dk    r|dgz   }t           j        dk    rd	D ]}|d
z   |vr:t          j                            |d
z             r|                    |d
z              |dz   |vr:t          j                            |dz             r|                    |dz              |	                    t                                }t          | ||||||           |S )Nwin32z/we4013z	/LIBPATH:libslinux)z-O0z-gz%-Werror=implicit-function-declarationz-fPICz/DEBUGdarwin)z/sw/z/opt/local/includelib)sysplatformospathrI   base_prefix
startswithexistsrG   with_suffixget_so_suffixbuild)r5   r)   r   r*   r+   compile_extra
link_extrass           r#   r1   r1      sn   
|w"!BGLL&$I$IIJ

		 	 	)	) K K K

%''

|w8*,

|x( 	/ 	/AI\11q9}55 2##A	M2225y,,E	1J1J,##AI...#//@@N	~zi/ / / r6   c                    | j         dz  }t          j        |d           |j        d         }t	          | j         dz  d          5 }	d |D             }
d |D             }|	                    t          j        d	| d
| j        d          d|
 d| d| d| d| d                     ddd           n# 1 swxY w Y   t          j	        dk    rt          j        g d|           nt          j        g d|           t          j        ddg|           t          j        t          ||z            dz   | j         |z             dS )zuse meson to buildra   Tr&   r%   zmeson.buildwtc                     g | ]}d |z   S )z-I .0ds     r#   
<listcomp>zbuild.<locals>.<listcomp>   s    333D1H333r6   c                     g | ]}d |z   S )z-Lrh   ri   s     r#   rl   zbuild.<locals>.<listcomp>   s    444!TAX444r6   z;            project('foo', 'c')
            shared_module('z', 'z',
                c_args: z + z,
                link_args: z,
                link_with: z_,
                name_prefix: '',
                name_suffix: 'dummy',
            )
        NrR   )mesonsetupz--buildtype=release--vsenv..)cwd)rn   ro   rp   rq   rn   compilez.dummy)parentrZ   makedirspartsr9   r:   textwrapdedentrX   rY   
subprocess
check_callrenamer;   )r5   r)   rb   rc   r   r*   r+   r   so_namefidincludes	link_dirss               r#   ra   ra      s    w&IK	D))))"2&G	el]*D	1	1 S33l33344|444			(/ 	##	# 	#).R	# 	# "	# 	# '4	# 	# &		# 	# +5		# 	#
 &	# 	# 	# 	 	 		 		 		               |w 0 0 0 #,	  	  	  	  	  	AAA"+	  	  	  	  7I.I>>>>Ic)g%&&15<'3IJJJJJs   A!B33B7:B7c                  6    t          j        d          } | sJ | S )N
EXT_SUFFIX)r/   r0   )rets    r#   r`   r`      s     

"<
0
0CJJJJr6   )__doc__rZ   r   ry   rX   r/   rw   __all__r   r   r.   r   r   r1   ra   r`   rh   r6   r#   <module>r      s    
			      



     ')C
D )+d2@ @ @ @ @J "$"
 
 
 
B  % % %P  $ 46   >K K KB    r6   