File: //lib/python3/dist-packages/jeepney/__pycache__/fds.cpython-310.pyc
o
���`� � @ sR d dl Z d dlZd dlZd dlmZ G dd� de�ZG dd� d�Zdadd� Z dS ) � N)�warnc @ s e Zd ZdZdS )� NoFDErrorzQRaised by :class:`FileDescriptor` methods if it was already closed/converted
N)�__name__�
__module__�__qualname__�__doc__� r r �-/usr/lib/python3/dist-packages/jeepney/fds.pyr s r c @ s� e Zd ZdZdZdZdZdd� Zdd� Zd d
� Z dd� Z
d
d� Zdd� Zdd� Z
dd� Zdd� Zddd�Zdd� Zedd gfdd��ZdS ) �FileDescriptora+ A file descriptor received in a D-Bus message
This wrapper helps ensure that the file descriptor is closed exactly once.
If you don't explicitly convert or close the FileDescriptor object, it will
close its file descriptor when it goes out of scope, and emit a
ResourceWarning.
��_fd������c C s
|| _ d S �Nr ��self�fdr r r �__init__ s
zFileDescriptor.__init__c C s4 | j }| j | jkrd}n| j | jkrd}d|� d�S )N�closed� convertedz<FileDescriptor (z)>)r �_CLOSED�
_CONVERTED�r �detailr r r �__repr__ s zFileDescriptor.__repr__c C sB | j | jkrdS | j | jkrtd��| j| j | _ }t�|� dS )a Close the file descriptor
This can safely be called multiple times, but will raise RuntimeError
if called after converting it with one of the ``to_*`` methods.
This object can also be used in a ``with`` block, to close it on
leaving the block.
z.Can't close FileDescriptor after converting itN)r r r r �os�closer r r r r $ s zFileDescriptor.closec C s | S r r �r r r r � __enter__5 s zFileDescriptor.__enter__c C s | � � d S r )r )r �exc_type�exc_val�exc_tbr r r �__exit__8 s zFileDescriptor.__exit__c C s4 | j dkrtd| j � d�td| d� | �� d S d S )Nr zFileDescriptor (z") was neither closed nor converted� )�
stacklevel�source)r r �ResourceWarningr r r r r �__del__; s
��zFileDescriptor.__del__c C s0 | j dk r| j | jkr
dnd}td|� ���d S )Nr r r z"FileDescriptor object was already )r r r r r r r �_checkC s
�zFileDescriptor._checkc C s | � � | jS )z�Get the integer file descriptor
This does not change the state of the :class:`FileDescriptor` object,
unlike the ``to_*`` methods.
)r( r r r r r �filenoH s zFileDescriptor.filenoc C s | � � | j| j| _}|S )a, Convert to the low-level integer file descriptor::
raw_fd = fd.to_raw_fd()
os.write(raw_fd, b'xyz')
os.close(raw_fd)
The :class:`FileDescriptor` can't be used after calling this. The caller
is responsible for closing the file descriptor.
)r( r r r r r r � to_raw_fdQ s
zFileDescriptor.to_raw_fdNc C s* | � � t| j|||||d�}| j| _|S )aK Convert to a Python file object::
with fd.to_file('w') as f:
f.write('xyz')
The arguments are the same as for the builtin :func:`open` function.
The :class:`FileDescriptor` can't be used after calling this. Closing
the file object will also close the file descriptor.
)� buffering�encoding�errors�newline)r( �openr r )r �moder+ r, r- r. �fr r r �to_file_ s �zFileDescriptor.to_filec C s, ddl m } | �� || jd�}| j| _|S )aC Convert to a socket object
This returns a standard library :func:`socket.socket` object::
with fd.to_socket() as sock:
b = sock.sendall(b'xyz')
The wrapper object can't be used after calling this. Closing the socket
object will also close the file descriptor.
r )�socket)r) )r3 r( r r )r r3 �sr r r � to_socketr s
zFileDescriptor.to_socket�returnc sd t � d�}|D ]!\}}}|tjkr(|tjkr(|�|dt|�t|�|j � � q� fdd�|D �S )z�Make a list of FileDescriptor from received file descriptors
ancdata is a list of ancillary data tuples as returned by socket.recvmsg()
�iNc s g | ]}� |��qS r r )�.0r7 ��clsr r �
<listcomp>� s z/FileDescriptor.from_ancdata.<locals>.<listcomp>)�arrayr3 �
SOL_SOCKET�
SCM_RIGHTS� frombytes�len�itemsize)r: �ancdata�fds�
cmsg_level� cmsg_type�datar r9 r �from_ancdata� s
$�zFileDescriptor.from_ancdata)r
NNN)r r r r � __slots__r r r r r r r"