ó
¢áSc           @` s¶  d  Z  d d l m Z m Z m Z d d d d d d d	 g Z d d
 l Z d d
 l Z d d l m	 Z	 m
 Z
 m Z m Z m Z d d l m Z m Z y d d
 l Z Wn e k
 r¼ d
 Z n Xe d
 k rd „  Z e Z e Z e Z e Z d d l m Z e Z nE d d
 l j j  Z! e! j" ƒ  Z [! e j# Z d „  Z e e d d ƒ Z d „  Z$ d d d d d d g Z% d „  Z& d e f d „  ƒ  YZ' i  Z( d
 d
 d
 d
 d „ Z) e d
 k	 r²e Z* i  Z+ d „  Z, e* j- e* j. e* j/ e* j0 e* j1 f d f e* j2 e* j3 e* j4 e* j5 e* j6 f d f e* j7 e* j8 f d f g Z9 xE e9 D]= \ Z: Z; x. e: D]& Z< e, e< d e; e* j= e< ƒ f ƒ qGWq4We> e* j/ d  ƒ Z? d! „  Z@ d" „  ZA d
 d# „ Z d$ „  Z n  d
 S(%   s%  
============================
``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", http://www.scipy.org/Cookbook/Ctypes

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

i    (   t   divisiont   absolute_importt   print_functiont   load_libraryt	   ndpointert   testt   ctypes_load_libraryt   c_intpt	   as_ctypest   as_arrayN(   t   integert   ndarrayt   dtypet	   deprecatet   array(   t	   _flagdictt   flagsobjc          O` s   t  d ƒ ‚ d S(   s±   
        Dummy object that raises an ImportError if ctypes is not available.

        Raises
        ------
        ImportError
            If ctypes is not available.

        s   ctypes is not available.N(   t   ImportError(   t   argst   kwds(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt   _dummyC   s    
(   t   intpc         C` s[  t  j d k  r+ d d  l } | j d ƒ n  t j j |  ƒ d } | s¢ d d l m } | ƒ  } |  | g } | d t	 ƒ } | | k s« | j
 d |  | ƒ q« n	 |  g } t j j | ƒ } t j j | ƒ sä t j j | ƒ } n | } x^ | D]V }	 t j j | |	 ƒ }
 t j j |
 ƒ rñ y t  j |
 SWqGt k
 rC‚  qGXqñ qñ Wt d ƒ ‚ d  S(   Ns   1.0.1i    sA   All features of ctypes interface may not work with ctypes < 1.0.1i   (   t   get_shared_lib_extensiont   is_python_exts   no file with expected extension(   t   ctypest   __version__t   warningst   warnt   ost   patht   splitextt   numpy.distutils.misc_utilR   t   Truet   insertt   abspatht   isdirt   dirnamet   joint   existst   cdllt   OSError(   t   libnamet   loader_pathR   t   extR   t   so_extt   libname_extt   so_ext2t   libdirt   lnt   libpath(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyR   [   s0    		c         C` s)   d } x |  D] } | t  | 7} q W| S(   Ni    (   R   (   t   flaglistt   numt   val(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt   _num_fromflags…   s    t   C_CONTIGUOUSt   F_CONTIGUOUSt   ALIGNEDt	   WRITEABLEt   OWNDATAt   UPDATEIFCOPYc         C` s?   g  } x2 t  D]* } t | } |  | @r | j | ƒ q q W| S(   N(   t
   _flagnamesR   t   append(   R3   t   rest   keyt   value(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt   _flags_fromnum�   s    

t   _ndptrc           B` s/   e  Z d  „  Z e d „  ƒ Z e d „  ƒ Z RS(   c         C` s
   t  |  ƒ S(   sª   This method is called when this class is used as the .restype
        asttribute for a shared-library function.   It constructs a numpy
        array from a void pointer.(   R   (   t   self(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt   _check_retval_˜   s    c         C` sU   i |  j  j d 6|  d 6d  d 6|  j d 6d d 6|  j  j d d d	 6|  j t f d
 6S(   Nt   descrt   __reft   stridest   shapei   t   versioni    i   t   typestrt   data(   t   _dtype_RE   t   Nonet   _shape_R@   t   False(   RC   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt   __array_interface__ž   s    
c         C` s  t  | t ƒ s t d ƒ ‚ n  |  j d  k	 rU | j |  j k rU t d |  j ƒ ‚ n  |  j d  k	 rŒ | j |  j k rŒ t d |  j ƒ ‚ n  |  j d  k	 rÉ | j	 |  j k rÉ t d t
 |  j ƒ ƒ ‚ n  |  j d  k	 r| j j |  j @|  j k rt d t |  j ƒ ƒ ‚ n  | j S(   Ns   argument must be an ndarrays   array must have data type %ss   array must have %d dimension(s)s   array must have shape %ss   array must have flags %s(   t
   isinstanceR   t	   TypeErrorRL   RM   R   t   _ndim_t   ndimRN   RH   t   strt   _flags_t   flagsR3   RA   R   (   t   clst   obj(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt
   from_param©   s     (   t   __name__t
   __module__RD   t   propertyRP   t   classmethodRZ   (    (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyRB   –   s   	c   	      C` sq  |  d k	 r t |  ƒ }  n  d } | d k	 rü t | t ƒ rN | j d ƒ } nQ t | t t f ƒ rx | } t | ƒ } n' t | t ƒ rŸ | j	 } t | ƒ } n  | d k rü y) g  | D] } | j
 ƒ  j ƒ  ^ qµ } Wn t d ƒ ‚ n Xt | ƒ } qü n  y t |  | | | f SWn t k
 r'n X|  d k r=d } n' |  j r[t t |  ƒ ƒ } n	 |  j } | d k	 r�| d | 7} n  | d k	 ry# g  | D] } t | ƒ ^ q—} Wn) t k
 rÛt | ƒ g } | f } n Xt | ƒ } | d d j | ƒ 7} n  | d k	 r(| d d j | ƒ 7} n g  } t d | t f i |  d 6| d	 6| d
 6| d 6ƒ } | t |  <| S(   sC  
    Array-checking restype/argtypes.

    An ndpointer instance is used to describe an ndarray in restypes
    and argtypes specifications.  This approach is more flexible than
    using, for example, ``POINTER(c_double)``, since several restrictions
    can be specified, which are verified upon calling the ctypes function.
    These include data type, number of dimensions, shape and flags.  If a
    given array does not satisfy the specified restrictions,
    a ``TypeError`` is raised.

    Parameters
    ----------
    dtype : data-type, optional
        Array data-type.
    ndim : int, optional
        Number of array dimensions.
    shape : tuple of ints, optional
        Array shape.
    flags : str or tuple of str
        Array flags; may be one or more of:

          - C_CONTIGUOUS / C / CONTIGUOUS
          - F_CONTIGUOUS / F / FORTRAN
          - OWNDATA / O
          - WRITEABLE / W
          - ALIGNED / A
          - UPDATEIFCOPY / U

    Returns
    -------
    klass : ndpointer type object
        A type object, which is an ``_ndtpr`` instance containing
        dtype, ndim, shape and flags information.

    Raises
    ------
    TypeError
        If a given array does not satisfy the specified restrictions.

    Examples
    --------
    >>> clib.somefunc.argtypes = [np.ctypeslib.ndpointer(dtype=np.float64,
    ...                                                  ndim=1,
    ...                                                  flags='C_CONTIGUOUS')]
    ... #doctest: +SKIP
    >>> clib.somefunc(np.array([1, 2, 3], dtype=np.float64))
    ... #doctest: +SKIP

    t   ,s   invalid flags specificationt   anys   _%ddt   _t   xs   ndpointer_%sRL   RN   RS   RV   N(   RM   t   _dtypeRQ   RU   t   splitt   intR
   RA   R   R3   t   stript   upperRR   R5   t   _pointer_type_cachet   KeyErrort   namest   idt   tupleR%   t   typeRB   (	   R   RT   RH   RW   R3   Rb   t   namet   strshapet   klass(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyR   À   s^    4	)			#

c         ` s^   y |  j  Wn t k
 r n Xd St | ƒ j ‰  |  t ˆ  <‡  f d †  } t | ƒ |  _  d S(   s„   Given a ctypes simple type, construct and attach an
        __array_interface__ property to it if it does not yet have one.
        Nc         ` sM   i d ˆ  f g d 6|  d 6d  d 6d
 d 6d d 6ˆ  d 6t j |  ƒ t f d	 6S(   Nt    RE   RF   RG   RH   i   RI   RJ   RK   (    (   RM   t   ctt	   addressofRO   (   RC   (   RJ   (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyRP   :  s    (   RP   t   AttributeErrorRc   RU   t
   _typecodesR]   (   t   simple_typeR   RP   (    (   RJ   sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt   prep_simple/  s      

t   it   ut   fs   %c%di   c         ` sµ   y |  j  Wn t k
 r n Xd Sg  ‰ |  } x/ t | ƒ t k r` ˆ j | j ƒ | j } q2 Wt ˆ ƒ ‰ | ƒ  j  } | d ‰  | d ‰ ‡  ‡ ‡ f d †  } t | ƒ |  _  d S(   sƒ   Given a ctypes array type, construct and attach an
        __array_interface__ property to it if it does not yet have one.
        NRE   RJ   c         ` sD   i ˆ  d 6|  d 6d  d 6ˆ d 6d d 6ˆ d 6t j |  ƒ t f d 6S(	   NRE   RF   RG   RH   i   RI   RJ   RK   (   RM   Rr   Rs   RO   (   RC   (   RE   RH   RJ   (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyRP   h  s    
(	   RP   Rt   Rm   t   _ARRAY_TYPER=   t   _length_t   _type_Rl   R]   (   t
   array_typet   obt   aiRP   (    (   RE   RH   RJ   sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt
   prep_arrayV  s       


c         C` s   y |  j  Wn t k
 r n Xd S|  j } t t | ƒ ƒ } i d d 6| j d 6t j | ƒ t f d 6| d 6} | |  _  d S(   s�   Given a ctypes pointer object, construct and
        attach an __array_interface__ property to it if it does not
        yet have one.
        Ni   RI   RJ   RK   RH   (	   RP   Rt   t   contentsRc   Rm   RU   Rr   Rs   RO   (   t   pointer_objRH   R‚   R   t   inter(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt   prep_pointert  s      	


c         C` sd   t  |  ƒ } y | j Wn: t k
 rS t |  d ƒ rF t |  | ƒ qT t | ƒ n Xt |  d t ƒS(   s  Create a numpy array from a ctypes array or a ctypes POINTER.
        The numpy array shares the memory with the ctypes object.

        The size parameter must be given if converting from a ctypes POINTER.
        The size parameter is ignored if converting from a ctypes array
        R‚   t   copy(   Rm   RP   Rt   t   hasattrR…   R�   R   RO   (   RY   RH   t   tp(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyR	   Š  s     c         C` s¼   |  j  } | d r" t d ƒ ‚ n  | d d k rA t d ƒ ‚ n  | d \ } } | rf t d ƒ ‚ n  t | d } x) | d	 d
 d
 d … D] } | | } qŒ W| j | ƒ } | | _ | S(   s‚   Create and return a ctypes object from a numpy array.  Actually
        anything that exposes the __array_interface__ is accepted.RG   s   strided arrays not supportedRI   i   s,   only __array_interface__ version 3 supportedRK   s   readonly arrays unsupportedRJ   RH   Niÿÿÿÿ(   RP   RR   Ru   t   from_addresst   __keep(   RY   R€   t   addrt   readonlyRˆ   t   dimt   result(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyR   š  s    	
	(B   t   __doc__t
   __future__R    R   R   t   __all__t   sysR   t   numpyR
   R   R   Rc   R   R   t   numpy.core.multiarrayR   R   R   R   RM   R   R   R   R   R	   R   R   t   objectt   _ndptr_baset   numpy.core._internalt   coret	   _internalt   nict   _getintp_ctypet   c_void_pR5   R<   RA   RB   Rh   R   Rr   Ru   Rw   t   c_bytet   c_shortt   c_intt   c_longt
   c_longlongt   c_ubytet   c_ushortt   c_uintt   c_ulongt   c_ulonglongt   c_floatt   c_doublet   simple_typest   typest   codeRˆ   t   sizeofRm   R{   R�   R…   (    (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/numpy/ctypeslib.pyt   <module>3   s^   (
				'					)f	''(		