K i:ddlZddlZddlZddlmZddlmZddlmZm Z ddl m Z m Z m Z mZmZddlmZmZmZmZddlmZddlmZdd Zd Zd Zd Zd ZdZdZdZ dZ!dZ"dZ#dZ$dZ%y)N)StringIO)Path)symbolsEq)autowrapbinary_functionCythonCodeWrapperUfuncifyCodeWrapper CodeWrapper)CCodeGen C99CodeGenCodeGenArgumentListError make_routine)raises)TmpFileManagerc pt}||||fi||j}|j|S)a5Wrapper for dump_fn. dump_fn writes its results to a stream object and this wrapper returns the contents of that stream as a string. This auxiliary function is used by many tests below. The header and the empty lines are not generator to facilitate the testing of the output. )rgetvalueclose)dump_fnroutinesprefixkwargsoutputsources i/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/utilities/tests/test_autowrap.py get_stringrs6ZF Hff// __ F LLN Mctd\}}}||z|z}td|}tt}t |j |g}d}||k(sJy)Nx,y,ztestzcdef extern from 'file.h': double test(double x, double y, double z) def test_c(double x, double y, double z): return test(x, y, z))rrr r rdump_pyx)xyzexprroutinecode_genrexpecteds r#test_cython_wrapper_scalar_functionr)#segGAq! E19D64(G ,H ))G9 5F #  X  rcddlm}td\}}}tt }t d||||z}t |j|g}d}||k(sJy)NrEqualityrr zcdef extern from 'file.h': void test(double x, double y, double *z) def test_c(double x, double y): cdef double z = 0 test(x, y, &z) return zsympy.core.relationalr,rr r rrr!r,r"r#r$r'r&rr(s rtest_cython_wrapper_outargr04sf.gGAq! .H68Aq1u#56G ))G9 5F   X  rcddlm}td\}}}tt }t d||||z|z}t |j|g}d}||k(sJy)Nrr+rr zcdef extern from 'file.h': void test(double x, double y, double *z) def test_c(double x, double y, double z): test(x, y, &z) return zr-r/s rtest_cython_wrapper_inoutargr2Gsj.gGAq! .H68Aq1uqy#9:G ))G9 5F   X  rc Dddlm}td\}}}td||||z}t t }ddt jiz}tj}tj|tjj|d}|j||t!|j#} | |k(sJt t d d gd gd d gdgddgdddii}ddt jiz}|j||t!|j#} | |k(sJddt jiz}d|_|j||t!|j#} | |k(sJtj&y)Nrr+rr afrom setuptools import setup from setuptools import Extension from Cython.Build import cythonize cy_opts = {'compiler_directives': {'language_level': '3'}} ext_mods = [Extension( 'wrapper_module_%(num)s', ['wrapper_module_%(num)s.pyx', 'wrapped_code_%(num)s.c'], include_dirs=[], library_dirs=[], libraries=[], extra_compile_args=['-std=c99'], extra_link_args=[] )] setup(ext_modules=cythonize(ext_mods, **cy_opts)) numzsetup.py) build_dirz/usr/local/includez/opt/booger/includez/user/local/libthelibnilibz -slow-mathz-lswampz -ltridentcompiler_directives boundscheckF) include_dirs library_dirs librariesextra_compile_argsextra_link_argscythonize_optionsa)from setuptools import setup from setuptools import Extension from Cython.Build import cythonize cy_opts = {'compiler_directives': {'boundscheck': False}} ext_mods = [Extension( 'wrapper_module_%(num)s', ['wrapper_module_%(num)s.pyx', 'wrapped_code_%(num)s.c'], include_dirs=['/usr/local/include', '/opt/booger/include'], library_dirs=['/user/local/lib'], libraries=['thelib', 'nilib'], extra_compile_args=['-slow-math', '-std=c99'], extra_link_args=['-lswamp', '-ltrident'] )] setup(ext_modules=cythonize(ext_mods, **cy_opts)) aNfrom setuptools import setup from setuptools import Extension from Cython.Build import cythonize cy_opts = {'compiler_directives': {'boundscheck': False}} import numpy as np ext_mods = [Extension( 'wrapper_module_%(num)s', ['wrapper_module_%(num)s.pyx', 'wrapped_code_%(num)s.c'], include_dirs=['/usr/local/include', '/opt/booger/include', np.get_include()], library_dirs=['/user/local/lib'], libraries=['thelib', 'nilib'], extra_compile_args=['-slow-math', '-std=c99'], extra_link_args=['-lswamp', '-ltrident'] )] setup(ext_modules=cythonize(ext_mods, **cy_opts)) T)r.r,rrr r r _module_countertempfilemkdtempr tmp_folderospathjoin_prepare_filesr read_text _need_numpycleanup) r,r"r#r$r&r'r(temp_dirsetup_file_path setup_texts r!test_cython_wrapper_compile_flagsrNXs.gGAq!68Aq1u#56G ,H k))*+H"!Hh'ggll8Z8O Gx8o&002J  !! ! /CEZ.[/@.A,4g+>5AN2;[1I4IM[`Ka3b #H k))*+H" Gx8o&002J  !! !  k))*!+H$ H Gx8o&002J  !! !rchddlm}ddlm}|d|d|d}}}|||fDcgc]}t |j c}\}}}||||z} t d| } tt} t| j| g} d} | j||| }| |k(sJycc}w) Nrr+)Dummyr"r#r$r zcdef extern from 'file.h': void test(double x_{x_id}, double y_{y_id}, double *z_{z_id}) def test_c(double x_{x_id}, double y_{y_id}): cdef double z_{z_id} = 0 test(x_{x_id}, y_{y_id}, &z_{z_id}) return z_{z_id})x_idy_idz_id) r.r,sympy.core.symbolrPstr dummy_indexrr r rr!format)r,rPr"r#r$drQrRrSr%r&r'rexpected_templater(s r$test_cython_wrapper_unique_dummyvarsrZs.'Cj%*eCj!qA561I>qAMM*>D$ Aq1u D64(G ,H ))G9 5F !''T4'HH X  ?sB/ctd\}}}t||zd}|t||zk(sJ|jdk(sJ|jdk(sJtt |||zd}|t||zk(sJ|jdk(sJ|jdk(sJtt |||z|zd}|t||z|zk(sJ|jdk(sJ|jdk(sJy)Nx y zdummybackendzx, ynamelessr$zx, y, z)rrrUargsreturnsr)r"r#r$fs rtest_autowrap_dummyrds gGAq! Q(A 3#a!e*   66V   99 "" "Aq1uw/A 3#a!e*   66V   99  Aq1uqy!73A 3#a!eai.  66Y   99  rctd\ttfdtt zdg}|t zk(sJ|j dk(sJ|jdk(sJttfdtt zzdg}|t zzk(sJ|j dk(sJ|jdk(sJtt zzdf}|t zzk(sJ|j dk(sJ|jdk(sJy) Nr\c<ttzdgSNr]r_rarrr"r#r$srz$test_autowrap_args..sXbAEl!.&rr]rhzy, xr$cDttzzdgSrgrirjsrrkz$test_autowrap_args..s&XbAEAI6F!Q.)rzy, x, z)rrrrrrUrarb)rcr"r#r$s @@@rtest_autowrap_argsrmsQgGAq! #&&'Aq1uwaVDAq    Cc"Q5A 3#a!e*   99S"'' "" "rctd\}}d}tj}tj} tj |t ||zd|}|t||zk(sJtj|tjsJ tj |tj|y#tj |tj|wxYw)Nroz./tmpr]rp) rrDgetcwdrArBchdirrrUrrrsshutilrmtree)r"r#rt saved_cwdtemp_cwdrcs r'test_autowrap_store_files_issue_gh12939r}s 5>DAq C I!H   QUGS 9sc!a%j   yybgg&&&  h  hs A$C ,C6ctd\}}td||zd}|jt||zk(sJy)Nrorcr]r^)rr_imp_rU)r"r#rcs rtest_binary_functionrs= 5>DAqQUG4A 779AE "" "rctd\}}}ttd}td||z|z}t |j |g}ddt jiz}||k(sJy)Nrufuncifyr af #include "Python.h" #include "math.h" #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" #include "numpy/halffloat.h" #include "file.h" static PyMethodDef wrapper_module_%(num)sMethods[] = { {NULL, NULL, 0, NULL} }; #ifdef NPY_1_19_API_VERSION static void test_ufunc(char **args, const npy_intp *dimensions, const npy_intp* steps, void* data) #else static void test_ufunc(char **args, npy_intp *dimensions, npy_intp* steps, void* data) #endif { npy_intp i; npy_intp n = dimensions[0]; char *in0 = args[0]; char *in1 = args[1]; char *in2 = args[2]; char *out0 = args[3]; npy_intp in0_step = steps[0]; npy_intp in1_step = steps[1]; npy_intp in2_step = steps[2]; npy_intp out0_step = steps[3]; for (i = 0; i < n; i++) { *((double *)out0) = test(*(double *)in0, *(double *)in1, *(double *)in2); in0 += in0_step; in1 += in1_step; in2 += in2_step; out0 += out0_step; } } PyUFuncGenericFunction test_funcs[1] = {&test_ufunc}; static char test_types[4] = {NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE}; static void *test_data[1] = {NULL}; #if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "wrapper_module_%(num)s", NULL, -1, wrapper_module_%(num)sMethods, NULL, NULL, NULL, NULL }; PyMODINIT_FUNC PyInit_wrapper_module_%(num)s(void) { PyObject *m, *d; PyObject *ufunc0; m = PyModule_Create(&moduledef); if (!m) { return NULL; } import_array(); import_umath(); d = PyModule_GetDict(m); ufunc0 = PyUFunc_FromFuncAndData(test_funcs, test_data, test_types, 1, 3, 1, PyUFunc_None, "wrapper_module_%(num)s", "Created in SymPy with Ufuncify", 0); PyDict_SetItemString(d, "test", ufunc0); Py_DECREF(ufunc0); return m; } #else PyMODINIT_FUNC initwrapper_module_%(num)s(void) { PyObject *m, *d; PyObject *ufunc0; m = Py_InitModule("wrapper_module_%(num)s", wrapper_module_%(num)sMethods); if (m == NULL) { return; } import_array(); import_umath(); d = PyModule_GetDict(m); ufunc0 = PyUFunc_FromFuncAndData(test_funcs, test_data, test_types, 1, 3, 1, PyUFunc_None, "wrapper_module_%(num)s", "Created in SymPy with Ufuncify", 0); PyDict_SetItemString(d, "test", ufunc0); Py_DECREF(ufunc0); } #endifr4)rr r rrdump_cr r@)r"r#r$ code_wrapperr&rr(s rtest_ufuncify_sourcer szgGAq!&z*'=>L61q519-G  ++gY 7FW nK// 0oW1Hp X  rc td\}}}|||f}||dzzd|dzzz}ttd}tt |Dcgc]0}t dj ||j|||2}}t|j|d}d d tjiz} || k(sJycc}w) Nr rzfunc{} multitest)funcnamea #include "Python.h" #include "math.h" #include "numpy/ndarraytypes.h" #include "numpy/ufuncobject.h" #include "numpy/halffloat.h" #include "file.h" static PyMethodDef wrapper_module_%(num)sMethods[] = { {NULL, NULL, 0, NULL} }; #ifdef NPY_1_19_API_VERSION static void multitest_ufunc(char **args, const npy_intp *dimensions, const npy_intp* steps, void* data) #else static void multitest_ufunc(char **args, npy_intp *dimensions, npy_intp* steps, void* data) #endif { npy_intp i; npy_intp n = dimensions[0]; char *in0 = args[0]; char *in1 = args[1]; char *in2 = args[2]; char *out0 = args[3]; char *out1 = args[4]; char *out2 = args[5]; npy_intp in0_step = steps[0]; npy_intp in1_step = steps[1]; npy_intp in2_step = steps[2]; npy_intp out0_step = steps[3]; npy_intp out1_step = steps[4]; npy_intp out2_step = steps[5]; for (i = 0; i < n; i++) { *((double *)out0) = func0(*(double *)in0, *(double *)in1, *(double *)in2); *((double *)out1) = func1(*(double *)in0, *(double *)in1, *(double *)in2); *((double *)out2) = func2(*(double *)in0, *(double *)in1, *(double *)in2); in0 += in0_step; in1 += in1_step; in2 += in2_step; out0 += out0_step; out1 += out1_step; out2 += out2_step; } } PyUFuncGenericFunction multitest_funcs[1] = {&multitest_ufunc}; static char multitest_types[6] = {NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE}; static void *multitest_data[1] = {NULL}; #if PY_VERSION_HEX >= 0x03000000 static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "wrapper_module_%(num)s", NULL, -1, wrapper_module_%(num)sMethods, NULL, NULL, NULL, NULL }; PyMODINIT_FUNC PyInit_wrapper_module_%(num)s(void) { PyObject *m, *d; PyObject *ufunc0; m = PyModule_Create(&moduledef); if (!m) { return NULL; } import_array(); import_umath(); d = PyModule_GetDict(m); ufunc0 = PyUFunc_FromFuncAndData(multitest_funcs, multitest_data, multitest_types, 1, 3, 3, PyUFunc_None, "wrapper_module_%(num)s", "Created in SymPy with Ufuncify", 0); PyDict_SetItemString(d, "multitest", ufunc0); Py_DECREF(ufunc0); return m; } #else PyMODINIT_FUNC initwrapper_module_%(num)s(void) { PyObject *m, *d; PyObject *ufunc0; m = Py_InitModule("wrapper_module_%(num)s", wrapper_module_%(num)sMethods); if (m == NULL) { return; } import_array(); import_umath(); d = PyModule_GetDict(m); ufunc0 = PyUFunc_FromFuncAndData(multitest_funcs, multitest_data, multitest_types, 1, 3, 3, PyUFunc_None, "wrapper_module_%(num)s", "Created in SymPy with Ufuncify", 0); PyDict_SetItemString(d, "multitest", ufunc0); Py_DECREF(ufunc0); } #endifr4) rr r rangelenrrWdiffrrr r@) r"r#r$ var_symbolsr%rirrr(s r test_ufuncify_source_multioutputrlsgGAq!a)K q!t8bAg D&z*'=>Lbghklwhxbyz]^ X__Q/;q>1JKXzHz  ++X LF_ ~K// 0_1H@ X  E{s5B<)file)&rDrAryiorpathlibr sympy.corerrsympy.utilities.autowraprrr r r sympy.utilities.codegenr r rrsympy.testing.pytestrsympy.testing.tmpfilesrrr)r0r2rNrZrdrmrur}rrrrrrs}  "AA(1 "&"Tl*&,   # ]@gr