K i$dZddlZddlZddlZddlZddlZddlZddgZddddddZ ddZ d Z d Z d Z d Z d ZdZy)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_initcb|g}|t||z}d}|stjd}|r |dz }||z }|dz }t|||}t ||||} ddl} | j j|| } | j j| } | jj| | 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_methodspathlibPath _make_sourcerimportlib.utilutilspec_from_file_locationmodule_from_specloader exec_module) modname functionsrrrr bodyinit source_stringmod_so importlibspecfoos e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/numpy/testing/_private/extbuild.pyrrsN mIw7 7D D LL%    OD $5M %L-9F >> 1 1'6 BD .. ) )$ /CKKC Jc|jdd}||z }|jdt||}|xsg}|xsg}|xsg}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 Texist_ok)outputfilenamer libraries library_dirs)splitmkdir_convert_str_to_file _c_compile) namebuilddirrrr&r'rdirnamecfiles rrrRsu*jjob!GoG MM4M  8E%2LRI%2L  g/!Y!  r c|dz }|jd5}|jt|ddd|S#1swY|SxYw)zHelper function to create a file ``source.c`` in `dirname` that contains the string in `source`. Returns the file name zsource.cwN)openwritestr)sourcer.filenamefs rr*r*vsF#H s q F  O Os <Ac g}g}|D]M\}}}|d|}d|vrd}nd}|jd|d|d|dd ||d |d } |j| Od j|d d j||dzz} | 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)appendjoin) rr methods_tablecodesfuncnameflagscode cfuncname signature func_coders rr r s M E!* %iq + e #LI:I08)U K M#YK0    Y 99U  ii .7 C D DD Kr cd|||dz}|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 } )r,rr)r,rrrCs rrrs# dD  D Kr c g}tjdk(rdg}|jdn&tjjdrgd}ng}t |||||||S)Nwin32z/we4013z/DEBUGlinux)z-O0z-gz%-Werror=implicit-function-declarationz-fPIC)sysplatformr= startswithbuild)r/r%rr&r' link_extra compile_extras rr+r+sgJ ||w" (#   )K   ~zi //r c|jdz }tj|dt|jdz d5}|D cgc]} d| z } } |j t j d|jdd |jdd |d | d |d ddd|jdz } t| d5}|j t j dtjddddtjdk(rtjgd|n2tjdddddtj| g||jdtz} tjddg|tjt!|| z |j| z |j| z Scc} w#1swY9xYw#1swYxYw)zuse meson to buildrOTr#z meson.buildwtz-Lz 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)mesonsetupz--buildtype=release--vsenv..)cwdrTrUrVrWz--native-file=compile)parentosmakedirsr2r3textwrapdedentpartsrL executablerM subprocess check_callfspath get_so_suffixrenamer4) r/r%rQrPrr&r'rfidd link_dirsnative_file_nameso_names rrOrOs w&IKK D) ell]*D 1 S'34!TAX4 4 (//'!&&r*+,++b/"#&(%;'&&2^4  #   ||&@@  % (//'~~&' #     ||w0#, w#~bii@P6Q5R%SU"+ ""2&8G7I.I>IIc)g%& w(>? <<' !!C5  s+G GAG7G%GG"%G.c8tjd}|sJ|S)N EXT_SUFFIX) sysconfigget_config_var)rets rrdrds  " "< 0C J3 Jr )NN)__doc__r[r rarLrmr]__all__rrr*r rr+rOrdrHr rrrsf    ')C D)+dR>F59! H%P$/$("Vr