← Back
Editing: ctypeslib.cpython-311.pyc
� �;7f}E � � � d Z g d�ZddlZddlmZmZmZmZm Z ddl mZmZ ddl Z n # e$ r dZ Y nw xY we �d� ZeZeZeZddlmZ eZn#ddlmc mZ ej � � Z[e j Zd� Zd � Zg d �Zd� Z G d� d e� � Z G d� de � � Z!i Z"dd�Z#e �(d� Z$d� Z% e%� � Z&d� Z'd� Z(d� Z)d� Z*d� Z+dd�Zd� ZdS dS )a7 ============================ ``ctypes`` Utility Functions ============================ See Also -------- load_library : Load a C library. ndpointer : Array restype/argtype with verification. as_ctypes : Create a ctypes array from an ndarray. as_array : Create an ndarray from a ctypes array. References ---------- .. [1] "SciPy Cookbook: ctypes", https://scipy-cookbook.readthedocs.io/items/Ctypes.html Examples -------- Load the C library: >>> _lib = np.ctypeslib.load_library('libmystuff', '.') #doctest: +SKIP Our result type, an ndarray that must be of type double, be 1-dimensional and is C-contiguous in memory: >>> array_1d_double = np.ctypeslib.ndpointer( ... dtype=np.double, ... ndim=1, flags='CONTIGUOUS') #doctest: +SKIP Our C-function typically takes an array and updates its values in-place. For example:: void foo_func(double* x, int length) { int i; for (i = 0; i < length; i++) { x[i] = i*i; } } We wrap it using: >>> _lib.foo_func.restype = None #doctest: +SKIP >>> _lib.foo_func.argtypes = [array_1d_double, c_int] #doctest: +SKIP Then, we're ready to call ``foo_func``: >>> out = np.empty(15, dtype=np.double) >>> _lib.foo_func(out, len(out)) #doctest: +SKIP )�load_library� ndpointer�c_intp� as_ctypes�as_array�as_ctypes_type� N)�integer�ndarray�dtype�asarray� frombuffer)� _flagdict�flagsobjc � � t d� � �)z� Dummy object that raises an ImportError if ctypes is not available. Raises ------ ImportError If ctypes is not available. zctypes is not available.)�ImportError)�args�kwdss �1/usr/lib/python3/dist-packages/numpy/ctypeslib.py�_dummyr C s � � �4�5�5�5� )�intpc � � t j dk rddl}|� dd�� � t j | � � } t j |� � }t j � | � � d }|s�ddlm } |� � }| |z g} |d � � � }||k s|� d| |z � � ddl}d|� d� � �d |� d� � �d�} |� d| | z � � n# t t f$ r Y nw xY w| g}t j � |� � }t j � |� � s t j � |� � } n|} |D ]e}t j � | |� � }t j � |� � r$ t j | c S # t* $ r � w xY w�ft+ d� � �)a It is possible to load a library using >>> lib = ctypes.cdll[<full_path_name>] # doctest: +SKIP But there are cross-platform considerations, such as library file extensions, plus the fact Windows will just load the first library it finds with that name. NumPy supplies the load_library function as a convenience. .. versionchanged:: 1.20.0 Allow libname and loader_path to take any :term:`python:path-like object`. Parameters ---------- libname : path-like Name of the library, which can have 'lib' as a prefix, but without an extension. loader_path : path-like Where the library can be found. Returns ------- ctypes.cdll[libpath] : library object A ctypes library object Raises ------ OSError If there is no library with the expected extension, or the library is defective and cannot be loaded. z1.0.1r NzAAll features of ctypes interface may not work with ctypes < 1.0.1� )� stacklevel� )�get_shared_lib_extensionT)� is_python_ext�.�SOABI�-� MULTIARCHz.sozno file with expected extension)�ctypes�__version__�warnings�warn�os�fsdecode�path�splitext�numpy.distutils.misc_utilr �insert� sysconfig�get_config_var�KeyErrorr �abspath�isdir�dirname�join�exists�cdll�OSError) �libname�loader_pathr$ �extr �so_ext�libname_ext�so_ext2r, �so_ext3�libdir�ln�libpaths r r r Z sK � �B ���'�'��O�O�O��M�M� 0�<=� � ?� ?� ?� �+�g�&�&���k�+�.�.���g���w�'�'��*��� $� K�J�J�J�J�J�-�-�/�/�F�"�V�+�,�K� /�.�T�B�B�B�G��f�$�$��"�"�1�g��&7�8�8�8� � � � � � �)2�)A�)A�'�)J�)J�)J�)J�)2�)A�)A�+�)N�)N�)N�)N�P���"�"�1�g��&7�8�8�8�8���k�*� � � ��� ���� #�)�K��g�o�o�k�2�2���w�}�}�[�)�)� !��W�_�_�[�1�1�F�F� �F�� � �B��g�l�l�6�2�.�.�G��w�~�~�g�&�&� ��!�;�w�/�/�/�/��� � � ������ � �7�8�8�8s �=AD � D�D�G�G(c �4 � d}| D ]}|t | z }�|S �Nr )r )�flaglist�num�vals r �_num_fromflagsrE � s+ � � �C�� � ���y��~�����Jr )�C_CONTIGUOUS�F_CONTIGUOUS�ALIGNED� WRITEABLE�OWNDATA�WRITEBACKIFCOPYc �l � g }t D ])}t | }| |z r|� |� � �*|S �N)� _flagnamesr �append)rC �res�key�values r �_flags_fromnumrS � s@ � � �C�� � ���#����%�K� ��J�J�s�O�O�O���Jr c �$ � e Zd Zed� � � ZdS )�_ndptrc � � t |t � � st d� � �| j �'|j | j k rt d| j z � � �| j �'|j | j k rt d| j z � � �| j �4|j | j k r$t dt | j � � z � � �| j �A|j j | j z | j k r$t dt | j � � z � � �|j S )Nzargument must be an ndarrayzarray must have data type %szarray must have %d dimension(s)zarray must have shape %szarray must have flags %s)� isinstancer � TypeError�_dtype_r �_ndim_�ndim�_shape_�shape�str�_flags_�flagsrC rS r"