o `@sRddlZddlZddlZddlmZGdddeZGdddZdaddZ dS) N)warnc@seZdZdZdS) NoFDErrorzQRaised by :class:`FileDescriptor` methods if it was already closed/converted N)__name__ __module__ __qualname____doc__rr-/usr/lib/python3/dist-packages/jeepney/fds.pyrsrc@seZdZdZdZdZdZddZddZd d Z d d Z d dZ ddZ ddZ ddZddZdddZddZeddgfddZdS) 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. _fdcCs ||_dSNr selffdrrr __init__s zFileDescriptor.__init__cCs4|j}|j|jkr d}n|j|jkrd}d|dS)Nclosed convertedz)r _CLOSED _CONVERTEDrdetailrrr __repr__s    zFileDescriptor.__repr__cCsB|j|jkrdS|j|jkrtd|j|j|_}t|dS)aClose 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 rrroscloserrrr r$s  zFileDescriptor.closecCs|Srrrrrr __enter__5szFileDescriptor.__enter__cCs |dSr)r)rexc_typeexc_valexc_tbrrr __exit__8s zFileDescriptor.__exit__cCs4|jdkrtd|jdtd|d|dSdS)NrzFileDescriptor (z") was neither closed nor converted) stacklevelsource)r rResourceWarningrrrrr __del__;s   zFileDescriptor.__del__cCs0|jdkr|j|jkr dnd}td|dS)Nrrrz"FileDescriptor object was already )r rrrrrr _checkCs zFileDescriptor._checkcCs||jS)zGet the integer file descriptor This does not change the state of the :class:`FileDescriptor` object, unlike the ``to_*`` methods. )r(r rrrr filenoHszFileDescriptor.filenocCs||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(rr rrrr to_raw_fdQs zFileDescriptor.to_raw_fdNcCs*|t|j|||||d}|j|_|S)aKConvert 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. ) bufferingencodingerrorsnewline)r(openr r)rmoder+r,r-r.frrr to_file_s zFileDescriptor.to_filecCs,ddlm}|||jd}|j|_|S)aCConvert 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))r3r(r r)rr3srrr to_socketrs  zFileDescriptor.to_socketreturncsdtd}|D]!\}}}|tjkr(|tjkr(||dt|t||jqfdd|DS)zMake a list of FileDescriptor from received file descriptors ancdata is a list of ancillary data tuples as returned by socket.recvmsg() iNcsg|]}|qSrr).0r7clsrr sz/FileDescriptor.from_ancdata..)arrayr3 SOL_SOCKET SCM_RIGHTS frombyteslenitemsize)r:ancdatafds cmsg_level cmsg_typedatarr9r from_ancdatas $zFileDescriptor.from_ancdata)r NNN)rrrr __slots__rrrrrrr"r'r(r)r*r2r5 classmethodrGrrrr r s$ r cCs*tdurd}tdj}t||atS)Nr7)_fds_buf_size_cacher<rAr3 CMSG_SPACE)maxfdsfd_sizerrr fds_buf_sizes  rO) r<rr3warningsr RuntimeErrorrr rKrOrrrr s