
    9*bL                         d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
  eedd      Zd Zd	 Zd
 Z e       Zd Zd Z G d d      Zy)a#  
    pyudev._os.pipe
    ===============

    Fallback implementations for pipe.

    1. pipe2 from python os module
    2. pipe2 from libc
    3. pipe from python os module

    The Pipe class wraps the chosen implementation.

    .. moduleauthor:: Sebastian Wiesner  <lunaryorn@gmail.com>
    N)partial)ERROR_CHECKERSFD_PAIR
SIGNATURES)load_ctypes_library	O_CLOEXECi   c                 N    t               }| j                  ||       |d   |d   fS )zA ``pipe2`` implementation using ``pipe2`` from ctypes.

    ``libc`` is a :class:`ctypes.CDLL` object for libc.  ``flags`` is an
    integer providing the flags to ``pipe2``.

    Return a pair of file descriptors ``(r, w)``.

    r      )r   pipe2)libcflagsfdss      1/usr/lib/python3/dist-packages/pyudev/_os/pipe.py_pipe2_ctypesr   -   s+     )CJJsEq63q6>    c                     t        j                         }| t         j                  z  dk7  r!|D ]  }t        |t         j                          | t        z  dk7  r|D ]  }t        |t                |S )zA ``pipe2`` implementation using :func:`os.pipe`.

    ``flags`` is an integer providing the flags to ``pipe2``.

    .. warning::

       This implementation is not atomic!

    Return a pair of file descriptors ``(r, w)``.

    r   )ospipe
O_NONBLOCKset_fd_status_flagr   set_fd_flag)r   r   file_descriptors      r   _pipe2_by_piper   ;   sh     '')Cr}}!" 	?O>	?yA" 	4O3	4Jr   c                      t        t        d      rt        j                  S 	 t        dt        t
              } t        | d      rt        t        |       S t        S # t        $ r	 t        cY S w xY w)zf
    Find the appropriate implementation for ``pipe2``.

    Return a function implementing ``pipe2``.r   r   )
hasattrr   r   r   r   r   r   r   r   ImportError)r   s    r   _get_pipe2_implementationr   Q   sa    
 r7xx"6:~F,3D',BGM4(	
HV	
  s   0A A A+*A+c                     t        j                   | t         j                  d      }t        j                   | t         j                  ||z         y)zwSet a flag on a file descriptor.

    ``fd`` is the file descriptor or file object, ``flag`` the flag as integer.

    r   N)fcntlF_GETFDF_SETFDfdflagr   s      r   r   r   d   2     KKEMM1-E	KKEMM54<0r   c                     t        j                   | t         j                  d      }t        j                   | t         j                  ||z         y)z~Set a status flag on a file descriptor.

    ``fd`` is the file descriptor or file object, ``flag`` the flag as integer.

    r   N)r   F_GETFLF_SETFLr"   s      r   r   r   n   r%   r   c                   ,    e Zd ZdZed        Zd Zd Zy)PipezA unix pipe.

    A pipe object provides two file objects: :attr:`source` is a readable file
    object, and :attr:`sink` a writeable.  Bytes written to :attr:`sink` appear
    at :attr:`source`.

    Open a pipe with :meth:`open()`.

    c                 Z    t        t        j                  t        z        \  }} | ||      S )zLOpen and return a new :class:`Pipe`.

        The pipe uses non-blocking IO.)_PIPE2r   r   r   )clssourcesinks      r   openz	Pipe.open   s(    
 bmmi7864  r   c                 t    t        j                  |dd      | _        t        j                  |dd      | _        y)zCreate a new pipe object from the given file descriptors.

        ``source_fd`` is a file descriptor for the readable side of the pipe,
        ``sink_fd`` is a file descriptor for the writeable side.rbr   wbN)r   fdopenr.   r/   )self	source_fdsink_fds      r   __init__zPipe.__init__   s,    
 ii	43IIgtQ/	r   c                     	 | j                   j                          | j                  j                          y# | j                  j                          w xY w)zCloses both sides of the pipe.N)r.   closer/   )r5   s    r   r:   z
Pipe.close   s4    	KKIIOODIIOOs	   7 AN)__name__
__module____qualname____doc__classmethodr0   r8   r:    r   r   r*   r*   x   s%     ! !0r   r*   )r>   r   r   	functoolsr   pyudev._ctypeslib.libcr   r   r   pyudev._ctypeslib.utilsr   getattrr   r   r   r   r,   r   r   r*   r@   r   r   <module>rE      s^   "   	  G F 7 BY/	,  
#	$11   r   