
    uhh$                         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ddddZ	 ddZ	d	 Z
d
 Zd Z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                r   |g }|t          ||           z   }d}|st          j        d          }|r
|dz  }||z  }|dz  }t          | ||          }t	          | |||          }	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')
    Nz
    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;r   )_make_methodspathlibPath_make_sourcer   importlib.utilutilspec_from_file_locationmodule_from_specloaderexec_module)modname	functionsr   r   r   r	   bodyinitsource_stringmod_so	importlibspecfoos                V/var/www/histauto/venv/lib/python3.11/site-packages/numpy/testing/_private/extbuild.pyr   r      s    N mIw777DD  &L%%	   		OD $55M%L-9 9F>11'6BBD
.
)
)$
/
/CKC   J    c                     |                      d          d         }|| z  }|                    d           t          ||          }|pg }|pg }|pg }t          |||z  |||          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)outputfilenamer   	librarieslibrary_dirs)splitmkdir_convert_str_to_file
_c_compile)	namebuilddirr   r   r&   r'   r   dirnamecfiles	            r   r   r   R   s    * jjoob!GoGMM4M    88E%2LRI%2Lg/!Y!
 
 
 
r    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)sourcer.   filenamefs       r   r*   r*   v   s     #H	s		 q	F              Os   #A

AAc           
          g }g }| D ]U\  }}}| d| }d|v rd}nd}|                     d|d|d|d           d	| | d
| d}	|                     |	           Vd                    |          dd                    |          |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* z
        {
        z
        }
        
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join)r   r   methods_tablecodesfuncnameflagscode	cfuncname	signature	func_coder   s              r   r   r      s    ME!*    %++++	e##LII:I08)))UUUK	M 	M 	M#%.  
  	 	Y99U  ii..7
C
CD DD Kr    c                     d| ||d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
    }
    )r,   r   r    )r,   r   r   rC   s       r   r   r      s(    	 dD D Kr    c           	          g }t           j        dk    rdg}|                    d           n&t           j                            d          rg d}ng }t	          | ||||||          S )Nwin32z/we4013z/DEBUGlinux)z-O0z-gz%-Werror=implicit-function-declarationz-fPIC)sysplatformr=   
startswithbuild)r/   r%   r   r&   r'   
link_extracompile_extras          r   r+   r+      s    J
|w"(####		 	 	)	) K K K ~zi/ / /r    c                    | j         dz  }t          j        |d           t          | j         dz  d          5 }d |D             }	|                    t          j        d|j        d          d	| j        d          d
| d|	 d| d                     ddd           n# 1 swxY w Y   | j         dz  }
t          |
d          5 }|                    t          j        dt          j	         d                     ddd           n# 1 swxY w Y   t          j
        dk    rt          j        g d|           n0t          j        dddddt          j        |
           g|           |j        d         t                      z   }t          j        ddg|           t          j        t!          ||z            | j         |z             | j         |z  S )zuse meson to buildrO   Tr#   zmeson.buildwtc                     g | ]}d |z   S )z-LrH   ).0ds     r   
<listcomp>zbuild.<locals>.<listcomp>   s    444!TAX444r    z            project('foo', 'c')
            py = import('python').find_installation(pure: false)
            py.extension_module(
                'r"   z',
                'z',
                c_args: z,
                link_args: z',
                include_directories: z,
            )
        Nz.mesonpy-native-file.iniz-            [binaries]
            python = 'z
'
        rJ   )mesonsetupz--buildtype=release--vsenv..)cwdrX   rY   rZ   r[   z--native-file=compile)parentosmakedirsr2   r3   textwrapdedentpartsrL   
executablerM   
subprocess
check_callfspathget_so_suffixrenamer4   )r/   r%   rQ   rP   r   r&   r'   r   fid	link_dirsnative_file_nameso_names               r   rO   rO      s    w&IK	D))))	el]*D	1	1 S44|444			(/ 
# !&r*	
# 
#
 +b/
# 
# '
# 
# &
# 
# '3
# 
# 
# 
 
 
	 
	 
	               |&@@		%	% 		(/ #~# # #   	 	 	              
 |w 0 0 0 #,	  	  	  	  	  	w#%Sbi@P6Q6Q%S%SU"+	  	  	  	 
 "2&8G7I.I>>>>Ic)g%&&w(>???<'!!s$   ABB#&B#6DD
D
c                  6    t          j        d          } | sJ | S )N
EXT_SUFFIX)	sysconfigget_config_var)rets    r   rh   rh      s     

"<
0
0CJJJJr    )NN)__doc__r_   r   re   rL   rp   ra   __all__r   r   r*   r   r   r+   rO   rh   rH   r    r   <module>ru      s     
			      



     ')C
D )+dR> > > > >F 59!
 !
 !
 !
H  % % %P  $/ / /$(" (" ("V    r    