a
8g+^® ã @ sÎ d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlZddlm Z ddl
mZ dd l
mZ dd
l
m
Z
ddlmZ eedd
ƒZdd„ Zdd„ Zdd„ Zeƒ Zdd„ Zdd„ ZG dd„ deƒZdS )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
é )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN)Úpartial)ÚERROR_CHECKERS)ÚFD_PAIR)Ú
SIGNATURES)Úload_ctypes_libraryÚ O_CLOEXECi c C s" t ƒ }| ||¡ |d |d fS )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 ú3/usr/lib/python3.9/site-packages/pyudev/_os/pipe.pyÚ
_pipe2_ctypes2 s r c C sP t ¡ }| t j@ dkr,|D ]}t|t jƒ q| t@ dkrL|D ]}t|tƒ q<|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_pipe@ s r c C sR t tdƒrtjS z&tdttƒ} t | dƒr2tt| ƒntW S t yL t Y S 0 dS )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_implementationV s
ÿÿr c C s( t | t jd¡}t | t j||B ¡ dS )zwSet a flag on a file descriptor.
``fd`` is the file descriptor or file object, ``flag`` the flag as integer.
r N)ÚfcntlZF_GETFDZF_SETFD©r Úflagr r r r r h s r c C s( t | t jd¡}t | t j||B ¡ dS )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 ZF_GETFLZF_SETFLr r r r r r s r c @ s, e Zd ZdZedd„ ƒZdd„ Zdd„ ZdS ) Ú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 tjtB ƒ\}}| ||ƒ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 |dd¡| _t |dd¡| _dS )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_fdZsink_fdr r r Ú__init__ s z
Pipe.__init__c C s* z| j ¡ W | j ¡ n| j ¡ 0 dS )zCloses both sides of the pipe.N)r$ Úcloser% )r* r r r r, — s z
Pipe.closeN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodr&