o
    g                     @   s   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dZg g fddZ	d	d
 Z
dd Zdd Zg g g fddZdd Zd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             
   C   s   |t ||  }d}|std}|r|d7 }||7 }|d7 }t| ||}z	t| |||}	W n tyB }
 z	td| d|
d}
~
w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foo r!   Y/var/www/html/api-tag/env/lib/python3.10/site-packages/numpy/testing/_private/extbuild.pyr      s,   '
c           	      C   sR   |  dd }||  }|jdd t||}|tdg }t||| |g g d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cfiler!   r!   r"   r   T   s   
c                 C   sF   |d }| d}|t|  W d   |S 1 s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)sourcer2   filenamefr!   r!   r"   r,   u   s   
r,   c                 C   s   g }g }| D ]+\}}}d||f }d|v rd}nd}| d|||f  dj|||d}	| |	 qd|d	td||d
  }
|
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
    z%s_%sMETH_KEYWORDSz2(PyObject *self, PyObject *args, PyObject *kwargs)z (PyObject *self, PyObject *args)z{"%s", (PyCFunction)%s, %s},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flagsr>   r<   r=   	func_coder   r!   r!   r"   r      s$   
r   c                 C   s   dt | ||d }|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
    }
    )r0   r   r   )rD   )r0   r   r   r>   r!   r!   r"   r      s   	r   c                 C   s   t jdkrdg}dtjt jd g}nt jdr!g d}g }ng  }}	 t jdkr0|dg }t jdkrdd	D ],}|d
 |vrNtj|d
 rN||d
  |d |vrctj|d rc||d  q7|	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pathrC   base_prefix
startswithexistsrA   with_suffixget_so_suffixbuild)r3   r'   r   r(   r)   compile_extra
link_extrasr!   r!   r"   r/      s4   



r/   c                 C   s  | j d }tj|dd |jd }t| j d d7}	dd |D }
d	d |D }|	td
| d| jd  d|
 d| d| d| d| d W d   n1 sSw   Y  tj	dkrgt
jg d|d n	t
jg d|d t
jddg|d tt|| d | j |  dS )zuse meson to buildrY   Tr$   r#   zmeson.buildwtc                 S      g | ]}d | qS )z-Ir!   .0dr!   r!   r"   
<listcomp>       zbuild.<locals>.<listcomp>c                 S   r^   )z-Lr!   r_   r!   r!   r"   rb      rc   z;            project('foo', 'c')
            shared_module('z', 'z',
                c_args: z + z,
                link_args: z,
                link_with: z_,
                name_prefix: '',
                name_suffix: 'dummy',
            )
        NrJ   )mesonsetupz--buildtype=release--vsenv..)cwd)rd   re   rf   rg   rd   compilez.dummy)parentrR   makedirspartsr5   r6   textwrapdedentrP   rQ   
subprocess
check_callrenamer7   )r3   r'   rZ   r[   r   r(   r)   r   so_namefidincludes	link_dirsr!   r!   r"   rY      s>   





"rY   c                  C   s   t d} | s	J | S )N
EXT_SUFFIX)r-   r.   )retr!   r!   r"   rX      s   
rX   )__doc__rR   r   ro   rP   r-   rm   __all__r   r   r,   r   r   r/   rY   rX   r!   r!   r!   r"   <module>   s*    E
!
(
!