o
    US`c                     @   sx   d dl mZ d dlZddlmZ ddlmZ d dlZh dZh dZ	G dd	 d	eZ
	
						dddZdd ZdS )    )partialN   )AsyncResource)async_wraps>   rawmodenamebufferclosederrorsfilenoisattyclosefdencodinggetvaluenewlinesreadableseekablewritable	getbufferline_buffering>   peekreadseektellflushread1writereadallreadintoreadlinetruncate	readinto1	readlines
writelinesc                       s\   e Zd ZdZdd Zedd Zdd Z fdd	Zd
d Z	dd Z
dd Zdd Z  ZS )AsyncIOWrapperaI  A generic :class:`~io.IOBase` wrapper that implements the :term:`asynchronous
    file object` interface. Wrapped methods that could block are executed in
    :meth:`trio.to_thread.run_sync`.

    All properties and methods defined in in :mod:`~io` are exposed by this
    wrapper, if they exist in the wrapped file object.

    c                 C   s
   || _ d S N_wrapped)selffile r+   8/usr/local/lib/python3.10/dist-packages/trio/_file_io.py__init__=   s   
zAsyncIOWrapper.__init__c                 C   s   | j S )z.object: A reference to the wrapped file objectr'   r)   r+   r+   r,   wrapped@   s   zAsyncIOWrapper.wrappedc                    s^   |t v r
t| j|S |tv r+t| j| t| j| jj| fdd}t| || |S t|)Nc                     s*   t  g| R i |}tj|I d H S r&   )r   trio	to_threadrun_sync)argskwargsfuncmethr+   r,   wrapperL   s   z+AsyncIOWrapper.__getattr__.<locals>.wrapper)_FILE_SYNC_ATTRSgetattrr(   _FILE_ASYNC_METHODSr   	__class__setattrAttributeError)r)   r   r8   r+   r6   r,   __getattr__F   s   zAsyncIOWrapper.__getattr__c                    sB   t t  }| fddtD  | fddtD  |S )Nc                 3        | ]}t  j|r|V  qd S r&   hasattrr/   .0ar.   r+   r,   	<genexpr>Y       z)AsyncIOWrapper.__dir__.<locals>.<genexpr>c                 3   r@   r&   rA   rC   r.   r+   r,   rF   Z   rG   )setsuper__dir__updater9   r;   )r)   attrsr<   r.   r,   rJ   W   s   zAsyncIOWrapper.__dir__c                 C   s   | S r&   r+   r.   r+   r+   r,   	__aiter__]   s   zAsyncIOWrapper.__aiter__c                    s   |   I d H }|r|S tr&   )r    StopAsyncIteration)r)   liner+   r+   r,   	__anext__`   s
   zAsyncIOWrapper.__anext__c                    s    t j| jjI dH }t|S )zLike :meth:`io.BufferedIOBase.detach`, but async.

        This also re-wraps the result in a new :term:`asynchronous file object`
        wrapper.

        N)r0   r1   r2   r(   detach	wrap_file)r)   r   r+   r+   r,   rR   g   s   zAsyncIOWrapper.detachc                    sX   t jdd t j| jjI dH  W d   n1 sw   Y  t j I dH  dS )zLike :meth:`io.IOBase.close`, but async.

        This is also shielded from cancellation; if a cancellation scope is
        cancelled, the wrapped file object will still be safely closed.

        T)shieldN)r0   ZCancelScoper1   r2   r(   closeZlowlevelZcheckpoint_if_cancelledr.   r+   r+   r,   acloser   s
   	zAsyncIOWrapper.aclose)__name__
__module____qualname____doc__r-   propertyr/   r?   rJ   rN   rQ   rR   rV   __classcell__r+   r+   rM   r,   r%   3   s    	
r%   rTc           	         s.   t tjtj| |||||||	I dH }|S )a+  Asynchronous version of :func:`io.open`.

    Returns:
        An :term:`asynchronous file object`

    Example::

        async with await trio.open_file(filename) as f:
            async for line in f:
                pass

        assert f.closed

    See also:
      :func:`trio.Path.open`

    N)rS   r0   r1   r2   ioopen)	r*   r   	bufferingr   r   newliner   opener_filer+   r+   r,   	open_file   s   re   c                    s:    fdd}|dr|ds|dst d t S )a[  This wraps any file object in a wrapper that provides an asynchronous
    file object interface.

    Args:
        file: a :term:`file object`

    Returns:
        An :term:`asynchronous file object` that wraps ``file``

    Example::

        async_file = trio.wrap_file(StringIO('asdf'))

        assert await async_file.read() == 'asdf'

    c                    s   t  | ott | S r&   )rB   callabler:   )attrr*   r+   r,   has   s   zwrap_file.<locals>.hasrU   r   r   zK{} does not implement required duck-file methods: close and (read or write))	TypeErrorformatr%   )r*   ri   r+   rh   r,   rS      s   rS   )r]   r^   NNNTN)	functoolsr   r_   abcr   Z_utilr   r0   r9   r;   r%   re   rS   r+   r+   r+   r,   <module>   s"    P
#