E%+ | d Z ddlZddlZddlZddlZddgZddg dddZg g fdZd Zd
Z d Z
g g g fdZd
Zd Z
dS )zb
Build a c-extension module on-the-fly in tests.
See build_and_import_extensions for usage hints
Nbuild_and_import_extensioncompile_extension_module )prologue build_dirinclude_dirs more_initc ddl m} |t || z }d}|st j d }|r
|dz
}||z
}|dz
}t | || } t
| ||| }
n!# |$ r}t d| d |d }~ww xY wdd 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(u'abc')
>>> assert mod.test_bytes(b'abc')
r )CompileErrorz8PyObject *mod = PyModule_Create(&moduledef);
.z.#define INITERROR return NULL
z
return mod;zcould not compile in :N)distutils.errorsr
_make_methodspathlibPath_make_sourcer RuntimeErrorimportlib.utilutilspec_from_file_locationmodule_from_specloaderexec_module)modname functionsr r r r r bodyinit
source_stringmod_soe importlibspecfoos s/builddir/build/BUILD/cloudlinux-venv-1.0.10/venv/lib64/python3.11/site-packages/numpy/testing/_private/extbuild.pyr r s9 N .-----mIw777DD &L%% OD $55MH)Ym= = H H H?9???@@aGH >11'6BBD
.
)
)$
/
/CKC Js A( (B-BBc | d d }|| z }| d t || }|t j d gz }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 librarieslibrary_dirs)splitmkdir_convert_str_to_file sysconfigget_config_var
_c_compile) namebuilddirr r r* r+ r dirnamecfiles r$ r r P s * jjoob!GoGMM4M 88E9#;K#H#H"IIL
g/!Rb
c |dz }| d 5 }| t | ddd n# 1 swxY w Y |S )zHelper function to create a file ``source.c`` in `dirname` that contains
the string in `source`. Returns the file name
zsource.cwN)openwritestr)sourcer4 filenamefs r$ r. r. q s #H s q F Os #A
AAc
R g }g }| D ]b\ }}}|d|}d|v rd}nd}| d|d|d|d d |||
} | | cd | dt 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 signaturecode
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 )appendformatjoindict)r r
methods_tablecodesfuncnameflagsrD rB rC func_coder s r$ r r { s ME!* %&ww1 e##LII:I08)))UUUK M M M
FY)$FGG Y99U tyy//
A
A
AB BD Kr6 c 0 dt | || z }|S )zG Combines the code fragments into source code ready to be compiled
zn
#include