E%+|dZddlZddlZddlZddlZddgZddgdddZggfdZd Zd Z d Z gggfd Z d Z dZ 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_dir include_dirs more_initcddlm}|t||z}d}|stjd}|r |dz }||z }|dz }t |||} t |||| } n!#|$r} td|d| d } ~ wwxYwdd 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_specloader exec_module)modname functionsrrrr 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.pyrrs9N.----- mIw77 7D D &L%%    OD $55MH) Y m== HHH?9???@@aGH > 1 1'6 B BD . ) )$ / /CKC   JsA((B-BBc|dd}||z }|dt||}|tjdgz}t |||z |ggS)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)splitmkdir_convert_str_to_file sysconfigget_config_var _c_compile) namebuilddirrrr*r+rdirnamecfiles r$rrPs*jjoob!GoG MM4M    8 8E9#;K#H#H"IIL  g/!Rb    c|dz }|d5}|t|dddn #1swxYwY|S)zHelper function to create a file ``source.c`` in `dirname` that contains the string in `source`. Returns the file name zsource.cwN)openwritestr)sourcer4filenamefs r$r.r.qs#H s  q F  Os#A  AAc Rg}g}|D]b\}}}|d|}d|vrd}nd}|d|d|d|dd ||| } || cd |d td || zz} | 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) rr methods_tablecodesfuncnameflagsrDrBrC func_coders r$rr{s M E!*  %&ww1 e # #LII:I08)))UUU K M M M FY)$F G G   Y 99U    tyy// A A A B BD Kr6c0dt|||z}|S)zG Combines the code fragments into source code ready to be compiled zn #include %(body)s PyMODINIT_FUNC PyInit_%(name)s(void) { %(init)s } )r2rr)rJ)r2rrrDs r$rrs0   4    D Kr6c  tjdkr2dg}dtjtjdzg}n+tjdrgd}d}ndx}} tjdkr|dgz}tjdkrd D]}|d z|vr:tj|d zr||d z|d z|vr:tj|d zr||d z| t}tj } t||||||||D]7\} } tj | | kr| tj | <8nQ#|D]7\} } tj | | kr| tj | <8wxYw|S) Nwin32z/we4013z /LIBPATH:libslinux)z-O0z-gz%-Werror=implicit-function-declarationz-fPICz/DEBUGdarwin)z/sw/z /opt/local/includelib)sysplatformospathrI base_prefix startswithexistsrG with_suffix get_so_suffixenvironcopybuilditemsget) r5r)rr*r+ compile_extra link_extras saved_environkeyvalues r$r1r1s |w" !BGLL&$I$IIJ   ) ) KKK  %))   |w8*,  |x( / /AI \11q9}552##A M2225y ,,E 1J1J,##AI...#// @@NJOO%%M ( > : )\ 3 3 3(--// ( (JCz~~c""e++"' 3 (---// ( (JCz~~c""e++"' 3 ( s F==AH cddlm}|dd}|dg} tj} tjj |tj g||} | fd| Dz } tj| n#tj| wxYw| | t|||| d S) z@cd into the directory where the cfile is, use distutils to buildr) new_compiler)forceverboser)r extra_preargsc>g|]}tj|z S)r;parent).0rr5s r$ zbuild..s(777aC q())777r6)r*rrr+N) numpy.distutils.ccompilerrm customizerZgetcwdchdirrucompiler;r2link_shared_object) r5r)rfrgrr*r+rmcompilerobjectsoldress ` r$rcrcs766666|!Q///H rG )++CHU\ __ %' 777737777    ^$$ !  #####s rs    ')C D)+d2>>>>>F"$"    B%%%P$46$$$$N###8r6