%PDF- %PDF-
| Direktori : /lib64/python3.9/site-packages/numpy/testing/_private/__pycache__/ |
| Current File : //lib64/python3.9/site-packages/numpy/testing/_private/__pycache__/extbuild.cpython-39.pyc |
a
z[yc� � @ s� d Z ddlZddlZddlZddlZddgZddg dd�dd�Zg g fdd�Zd d
� Zdd� Z d
d� Z
g g g fdd�Zdd� Zdd� Z
dS )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� ddl m} |t|| � }d}|s,t�d�}|r@|d7 }||7 }|d7 }t| ||�} zt| ||| �}
W n6 |y� } ztd|� d��|�W Y d }~n
d }~0 0 dd 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)Zdistutils.errorsr �
_make_methods�pathlib�Path�_make_sourcer �RuntimeError�importlib.util�util�spec_from_file_location�module_from_spec�loader�exec_module)�modname� functionsr r r r r �body�init�
source_stringZmod_so�e� importlib�specZfoo� r �E/usr/lib64/python3.9/site-packages/numpy/testing/_private/extbuild.pyr s* '
�(c C sR | � d�d }|| }|jdd� 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_okZ INCLUDEPY)�outputfilenamer � libraries�library_dirs)�split�mkdir�_convert_str_to_file� sysconfig�get_config_var�
_c_compile) �nameZbuilddirr r r$ r% r �dirname�cfiler r r r P s
�c C sD |d }|� d��}|�t| �� W d � n1 s60 Y |S )zHelper function to create a file ``source.c`` in `dirname` that contains
the string in `source`. Returns the file name
zsource.c�wN)�open�write�str)�sourcer- �filename�fr r r r( q s ,r( c C s� g }g }| D ]V\}}}d||f }d|v r0d}nd}|� d|||f � dj|||d�} |� | � qd�|�d td�|�|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_%sZ
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 Z
methods_tableZcodes�funcname�flagsr8 r6 r7 Z func_coder r r r r { s$ ��
�
r c C s dt | ||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
}
)r, r r )r>