???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3
u1‘WÃ  ã               @   sÎ   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlZddlm	Z	 ddl
mZ dd	l
mZ dd
l
mZ ddlmZ eeddƒZdd„ Zdd„ Zdd„ Zeƒ Zdd„ Zdd„ ZG dd„ deƒZdS )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>
é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN)Úpartial)Úfd_pair)ÚERROR_CHECKERS)Ú
SIGNATURES)Úload_ctypes_libraryÚ	O_CLOEXECi   c             C   s"   t ƒ }| j||ƒ |d |d fS )zèA ``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Úfds© r   ú/usr/lib/python3.6/pipe.pyÚ_pipe2_ctypes4   s    	r   c             C   sX   t jƒ }| t j@ dkr0x|D ]}t|t jƒ qW | t@ dkrTx|D ]}t|tƒ qBW |S )zæA ``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   Úfdr   r   r   Ú_pipe2_by_pipeB   s    

r   c              C   sN   t tdƒrtjS y$tdttƒ} t | dƒr2tt| ƒS tS  t	k
rH   tS X dS )z]Find the appropriate implementation for ``pipe2``.

Return a function implementing ``pipe2``.r   r   N)
Úhasattrr   r   r
   r	   r   r   r   r   ÚImportError)r   r   r   r   Ú_get_pipe2_implementationX   s    
r   c             C   s(   t j | t jdƒ}t j | t j||B ƒ dS )zwSet a flag on a file descriptor.

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

    r   N)ÚfcntlZF_GETFDZF_SETFD)r   Úflagr   r   r   r   r   k   s    r   c             C   s(   t j | t jdƒ}t j | t j||B ƒ dS )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   ZF_GETFLZF_SETFL)r   r   r   r   r   r   r   u   s    r   c               @   s,   e Zd ZdZedd„ ƒZdd„ Zdd„ ZdS )	ÚPipezïA 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             C   s   t tjtB ƒ\}}| ||ƒS )zLOpen and return a new :class:`Pipe`.

        The pipe uses non-blocking IO.)Ú_PIPE2r   r   r   )ÚclsÚsourceÚsinkr   r   r   ÚopenŠ   s    z	Pipe.openc             C   s$   t j|ddƒ| _t j|ddƒ| _dS )zÉCreate 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$   )ÚselfZ	source_fdZsink_fdr   r   r   Ú__init__’   s    zPipe.__init__c          
   C   s    z| j jƒ  W d| jjƒ  X dS )zCloses both sides of the pipe.N)r#   Úcloser$   )r)   r   r   r   r+   š   s    z
Pipe.closeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr%   r*   r+   r   r   r   r   r       s   	r    )r/   Z
__future__r   r   r   r   r   r   Ú	functoolsr   Zpyudev._ctypeslib.libcr   r   r	   Zpyudev._ctypeslib.utilsr
   Úgetattrr   r   r   r   r!   r   r   Úobjectr    r   r   r   r   Ú<module>    s&   

