§
    :Fca5Q  ã                   óž  — d 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 ddlZ eed	d
¦  «        Z	 ddlZn# e$ r dZY nw xY wdgZddgZej        Zej        Zeej        gZereg d¢z  Zd„ Zd„ Zd„ Zd„ Zd„ Z  e!ed¦  «        �rqej"        Z#d„ Z$d„ Z" e!ed¦  «        r6ej%        Z&d„ Z'e'Z%e (                    d¦  «         e (                    d¦  «          e!ed¦  «        s e!ed¦  «        �rddl)Z)ej*        Z+ej,        Z-d„ Z.i Z/d„ Z0d2d„Z1d„ Z*d3d!„Z2ddd e$fd"„Z3e (                    d#¦  «         e (                    d$¦  «         dev rddd e'fd%„Z4e (                    d&¦  «         e
j5        sjd'„ Z"dev rd(„ Z%e (                    d)¦  «          e!ed*¦  «        r>ej6        Z7ej8        Z9d+„ Z6d,„ Z8e (                    d*¦  «         e (                    d-¦  «         n5d.„ Z"dev rd/„ Z%e (                    d)¦  «         ne :                    d¦  «          ee e;¦   «         eez   d0¬1¦  «        Z< e= e>eez   ¦  «        ¦  «        Z?dS )4aQ  
Low-level operating system functions from :mod:`os`.

Cooperative I/O
===============

This module provides cooperative versions of :func:`os.read` and
:func:`os.write`. These functions are *not* monkey-patched; you
must explicitly call them or monkey patch them yourself.

POSIX functions
---------------

On POSIX, non-blocking IO is available.

- :func:`nb_read`
- :func:`nb_write`
- :func:`make_nonblocking`

All Platforms
-------------

On non-POSIX platforms (e.g., Windows), non-blocking IO is not
available. On those platforms (and on POSIX), cooperative IO can
be done with the threadpool.

- :func:`tp_read`
- :func:`tp_write`

Child Processes
===============

The functions :func:`fork` and (on POSIX) :func:`forkpty` and :func:`waitpid` can be used
to manage child processes.

.. warning::

   Forking a process that uses greenlets does not eliminate all non-running
   greenlets. Any that were scheduled in the hub of the forking thread in the parent
   remain scheduled in the child; compare this to how normal threads operate. (This behaviour
   may change is a subsequent major release.)
é    )Úabsolute_importN)Ú_get_hub_noargs)Úreinit)Úconfig)ÚPY3)Úcopy_globalsÚEAGAINé   ÚforkÚtp_readÚtp_write)Úmake_nonblockingÚnb_readÚnb_writec                 óÜ   — t          j         | t           j        d¦  «        }t          |t          j        z  ¦  «        s/t          j         | t           j        |t          j        z  ¦  «         dS dS )z™Put the file descriptor *fd* into non-blocking mode if
        possible.

        :return: A boolean value that evaluates to True if successful.
        r   TN)ÚfcntlÚF_GETFLÚboolÚosÚ
O_NONBLOCKÚF_SETFL)ÚfdÚflagss     ú+/usr/lib/python3/dist-packages/gevent/os.pyr   r   L   s\   € õ ”˜B¥¤¨qÑ1Ô1ˆÝ�E�BœMÑ)Ñ*Ô*ð 	ÝŒK˜�EœM¨5µ2´=Ñ+@ÑAÔAÐAØ�4ð	ð 	ó    c                 óª  — d}d}	 	 	 t          | |¦  «        }||�|                     ¦   «          d}d}S S # t          $ r3}|j        t          vr‚ t
          st          j        ¦   «          Y d}~nd}~ww xY w|€)t          ¦   «         }|j	         
                    | d¦  «        }|                     |¦  «         Œ¯# |�|                     ¦   «          d}d}w xY w)a  
        Read up to *n* bytes from file descriptor *fd*. Return a
        byte string containing the bytes read, which may be shorter than
        *n*. If end-of-file is reached, an empty string is returned.

        The descriptor must be in non-blocking mode.
        Né   )Ú_readÚcloseÚOSErrorÚerrnoÚignored_errorsr   ÚsysÚ	exc_clearÚget_hubÚloopÚioÚwait)r   ÚnÚhubÚeventÚresultÚes         r   r   r   W   sý   € ð ˆØˆð	ð ð(Ý" 2 q™\œ\�FØ!ð Ð Ø—’‘”�Ø�Ø��ð !øõ ð (ð (ð (Ø”w¥nÐ4Ð4ØÝð (Ýœ™œ˜øøøøøøøøøð	(øøøð
 �;Ý!™)œ)�CØœHŸKšK¨¨AÑ.Ô.�EØ—’˜‘”�ð øð Ð Ø—’‘”�Ø�Ø��
�
�
�
ó,   †B6 ˆ5 µ
A2¿)A-Á(B6 Á-A2Á2AB6 Â6Cc                 óª  — d}d}	 	 	 t          | |¦  «        }||�|                     ¦   «          d}d}S S # t          $ r3}|j        t          vr‚ t
          st          j        ¦   «          Y d}~nd}~ww xY w|€)t          ¦   «         }|j	         
                    | d¦  «        }|                     |¦  «         Œ¯# |�|                     ¦   «          d}d}w xY w)zò
        Write some number of bytes from buffer *buf* to file
        descriptor *fd*. Return the number of bytes written, which may
        be less than the length of *buf*.

        The file descriptor must be in non-blocking mode.
        Nr   é   )Ú_writer   r    r!   r"   r   r#   r$   r%   r&   r'   r(   )r   Úbufr*   r+   r,   r-   s         r   r   r   v   sý   € ð ˆØˆð	ð ð(Ý# B¨™_œ_�FØ!ð Ð Ø—’‘”�Ø�Ø��ð !øõ ð (ð (ð (Ø”w¥nÐ4Ð4ØÝð (Ýœ™œ˜øøøøøøøøøð	(øøøð
 �;Ý!™)œ)�CØœHŸKšK¨¨AÑ.Ô.�EØ—’˜‘”�ð øð Ð Ø—’‘”�Ø�Ø��
�
�
�
r.   c                 ó^   — t          ¦   «         j                             t          | |f¦  «        S )zÊRead up to *n* bytes from file descriptor *fd*. Return a string
    containing the bytes read. If end-of-file is reached, an empty string
    is returned.

    Reading is done using the threadpool.
    )r%   Ú
threadpoolÚapplyr   )r   r)   s     r   r   r   •   s%   € õ ‰9Œ9Ô×%Ò%¥e¨b°!¨WÑ5Ô5Ð5r   c                 ó^   — t          ¦   «         j                             t          | |f¦  «        S )zŽWrite bytes from buffer *buf* to file descriptor *fd*. Return the
    number of bytes written.

    Writing is done using the threadpool.
    )r%   r4   r5   r1   )r   r2   s     r   r   r   Ÿ   s%   € õ ‰9Œ9Ô×%Ò%¥f¨r°3¨iÑ8Ô8Ð8r   c                  óB   — t          ¦   «         } | st          ¦   «          | S )a÷  
        Forks the process using :func:`os.fork` and prepares the
        child process to continue using gevent before returning.

        .. note::

            The PID returned by this function may not be waitable with
            either the original :func:`os.waitpid` or this module's
            :func:`waitpid` and it may not generate SIGCHLD signals if
            libev child watchers are or ever have been in use. For
            example, the :mod:`gevent.subprocess` module uses libev
            child watchers (which parts of gevent use libev child
            watchers is subject to change at any time). Most
            applications should use :func:`fork_and_watch`, which is
            monkey-patched as the default replacement for
            :func:`os.fork` and implements the ``fork`` function of
            this module by default, unless the environment variable
            ``GEVENT_NOWAITPID`` is defined before this module is
            imported.

        .. versionadded:: 1.1b2
        )Ú	_raw_forkr   )r,   s    r   Úfork_geventr9   ­   s#   € õ. ‘”ˆØð 	Ý‰HŒHˆHØˆr   c                  ó   — t          ¦   «         S )zL
        A wrapper for :func:`fork_gevent` for non-POSIX platforms.
        ©r9   © r   r   r   r   É   s   € õ ‰}Œ}Ðr   Úforkptyc                  óL   — t          ¦   «         \  } }| st          ¦   «          | |fS )a¶  
            Forks the process using :func:`os.forkpty` and prepares the
            child process to continue using gevent before returning.

            Returns a tuple (pid, master_fd). The `master_fd` is *not* put into
            non-blocking mode.

            Availability: Some Unix systems.

            .. seealso:: This function has the same limitations as :func:`fork_gevent`.

            .. versionadded:: 1.1b5
            )Ú_raw_forkptyr   )ÚpidÚ	master_fds     r   Úforkpty_geventrB   Ò   s,   € õ *™^œ^‰NˆC�Øð Ý‘”�Ø˜	�>Ð!r   rB   ÚWNOWAITÚWNOHANGc                  ó   — d S ©Nr<   r<   r   r   ú<lambda>rG   ò   s   €  € r   c                 ó6  — |                       ¦   «          	 | j        | j        t          j        ¦   «         ft          | j        <   |r || ¦  «         t          ¦   «          t          ¦   «          |                      ¦   «          d S # |                      ¦   «          w xY wrF   )Ústopr@   ÚrstatusÚtimeÚ_watched_childrenÚ_on_child_hookÚ_reap_childrenr   )ÚwatcherÚcallbacks     r   Ú	_on_childrQ   ÷   s‹   € ð �LŠL‰NŒNˆNð	 Ø29´+¸w¼ÕPTÔPYÑP[ÔP[Ð1\Õ! '¤+Ñ.Øð &Ø�H˜WÑ%Ô%Ð%åÑ Ô Ð åÑ Ô Ð à—’‘”���ø�—’‘”��øøøs   –AB ÂBé<   c                 óœ   ‡— t          j         ¦   «         }|| z
  Šˆfd„t                               ¦   «         D ¦   «         }|D ]
}t          |= Œd S )Nc                 ó\   •— g | ](\  }}t          |t          ¦  «        r|d          ‰k     ¯&|‘Œ)S )r0   )Ú
isinstanceÚtuple)Ú.0r@   ÚvalÚoldest_alloweds      €r   ú
<listcomp>z"_reap_children.<locals>.<listcomp>  sJ   ø€ ð ð ð Ù ˜˜Så˜c¥5Ñ)Ô)ðð /2°!¬f°~Ò.EÐ.Eð à.EÐ.EÐ.Er   )rK   rL   Úitems)ÚtimeoutÚnowÚdeadr@   rY   s       @r   rN   rN     ss   ø€ õ  ”)‘+”+ˆCØ  7™]ˆNðð ð ð å$×*Ò*Ñ,Ô,ðñ ô ˆDð
 ð +ð +�Ý% cÐ*Ð*ð+ð +r   c                 ó’  — | dk    rÚ| dk    rNd}t                                ¦   «         D ]2\  }}t          |t          ¦  «        r|�|d         |k     r
|} |d         }Œ3| dk    r€| dk    rj|dk    rdt	          ¦   «         }|j                             dd¦  «        5 }|                     |¦  «         |j        |j	        fcddd¦  «         S # 1 swxY w Y   t          | |¦  «        S | t           v rÏ|t          z  s t          t           |          t          ¦  «        r6t           |          }t          |t          ¦  «        rt           | = |dd…         S dS t           |          }|j                             | d¦  «        5 }t	          ¦   «                              |¦  «         ddd¦  «         n# 1 swxY w Y   |j        |j	        fS t          | |¦  «        S )a  
            Wait for a child process to finish.

            If the child process was spawned using
            :func:`fork_and_watch`, then this function behaves
            cooperatively. If not, it *may* have race conditions; see
            :func:`fork_gevent` for more information.

            The arguments are as for the underlying
            :func:`os.waitpid`. Some combinations of *options* may not
            be supported cooperatively (as of 1.1 that includes
            WUNTRACED). Using a *pid* of 0 to request waiting on only processes
            from the current process group is not cooperative. A *pid* of -1
            to wait for any child is non-blocking, but may or may not
            require a trip around the event loop, depending on whether any children
            have already terminated but not been waited on.

            Availability: POSIX.

            .. versionadded:: 1.1b1
            .. versionchanged:: 1.2a1
               More cases are handled in a cooperative manner.
            r   éÿÿÿÿNr0   F)r   r   )rL   r[   rU   rV   r%   r&   Úchildr(   ÚrpidrJ   Ú_waitpidÚ_WNOHANG)	r@   ÚoptionsÚfinished_atÚkÚvr*   rO   r,   Únew_watchers	            r   Úwaitpidrj      sc  € ðB �aŠxˆxà˜"’9�9ð
 #'�KÝ 1× 7Ò 7Ñ 9Ô 9ð /ð /™˜˜1å *¨1­eÑ 4Ô 4ð/à%0Ð%8¸A¸a¼DÀ;Ò<NÐ<Nà"#˜CØ*+¨A¬$˜Køà˜!’8�8ð ˜b’y�y W°¢\ \Ý%™iœi˜Ø œXŸ^š^¨A¨uÑ5Ô5ð A¸ØŸHšH WÑ-Ô-Ð-Ø#*¤<°´Ð#@ðAð Að Að Añ Aô Að Að Að Að Að Að Aøøøð Að Að Að Aõ $ C¨Ñ1Ô1Ð1àÕ'Ð'Ð'ð �XÑ%ð 
"­Õ4EÀcÔ4JÍEÑ)RÔ)Rð 
"õ /¨sÔ3�FÝ! &­%Ñ0Ô0ð *õ .¨cÐ2Ø% b q bœzÐ)à!˜6õ ,¨CÔ0�ð ”\×'Ò'¨¨UÑ3Ô3ð 0°{Ý‘I”I—N’N ;Ñ/Ô/Ð/ð0ð 0ð 0ñ 0ô 0ð 0ð 0ð 0ð 0ð 0ð 0øøøð 0ð 0ð 0ð 0ð ”| W¤_Ð4Ð4õ ˜C Ñ)Ô)Ð)s$   Â#CÃC
ÃC
Å2"F Æ F$Æ'F$Fc                 óª   — |pt          ¦   «         j        }|                     | |¬¦  «        }|t          | <   |                     t
          ||¦  «         d S )N)Úref)r%   r&   ra   rL   ÚstartrQ   )r@   rP   r&   rl   rO   s        r   Ú_watch_childrn   �  sN   € ØÐ)�7™9œ9œ>ˆDØ—j’j ¨#�jÑ.Ô.ˆGØ%,Õ˜cÑ"Ø�MŠM�) W¨hÑ7Ô7Ð7Ð7Ð7r   c                 óB   —  |¦   «         }|rt          || ||¦  «         |S )a¿  
            Fork a child process and start a child watcher for it in the parent process.

            This call cooperates with :func:`waitpid` to enable cooperatively waiting
            for children to finish. When monkey-patching, these functions are patched in as
            :func:`os.fork` and :func:`os.waitpid`, respectively.

            In the child process, this function calls :func:`gevent.hub.reinit` before returning.

            Availability: POSIX.

            :keyword callback: If given, a callable that will be called with the child watcher
                when the child finishes.
            :keyword loop: The loop to start the watcher in. Defaults to the
                loop of the current hub.
            :keyword fork: The fork function. Defaults to :func:`the one defined in this
                module <gevent.os.fork_gevent>` (which automatically calls :func:`gevent.hub.reinit`).
                Pass the builtin :func:`os.fork` function if you do not need to
                initialize gevent in the child process.

            .. versionadded:: 1.1b1
            .. seealso::
                :func:`gevent.monkey.get_original` To access the builtin :func:`os.fork`.
            )rn   )rP   r&   rl   r   r@   s        r   Úfork_and_watchrp   –  s0   € ð2 �$‘&”&ˆCØð 7å˜S (¨D°#Ñ6Ô6Ð6ØˆJr   rp   r9   c                 óJ   ‡‡— g Šˆˆfd„}t          | |||¦  «         ‰d         S )z¾
                Like :func:`fork_and_watch`, except using :func:`forkpty_gevent`.

                Availability: Some Unix systems.

                .. versionadded:: 1.1b5
                c                  óR   •—  ‰¦   «         } ‰                      | ¦  «         | d         S )Nr   )Úappend)Ú
pid_and_fdr=   r,   s    €€r   Ú_forkz forkpty_and_watch.<locals>._forkÃ  s*   ø€ Ø!( ¡¤�JØ—M’M *Ñ-Ô-Ð-Ø% aœ=Ð(r   r   ©rp   )rP   r&   rl   r=   ru   r,   s      ` @r   Úforkpty_and_watchrw   ¹  sI   øø€ ð �ð)ð )ð )ð )ð )ð )õ ˜x¨¨s°EÑ:Ô:Ð:Ø˜a”yÐ r   rw   c                  ó   — t          | i |¤ŽS )a×  
                Forks a child process and starts a child watcher for it in the
                parent process so that ``waitpid`` and SIGCHLD work as expected.

                This implementation of ``fork`` is a wrapper for :func:`fork_and_watch`
                when the environment variable ``GEVENT_NOWAITPID`` is *not* defined.
                This is the default and should be used by most applications.

                .. versionchanged:: 1.1b2
                rv   ©ÚargsÚkwargss     r   r   r   Ð  s   € õ & tÐ6¨vÐ6Ð6Ð6r   c                  ó   — t          | i |¤ŽS )a˜  
                    Like :func:`fork`, but using :func:`forkpty_gevent`.

                    This implementation of ``forkpty`` is a wrapper for :func:`forkpty_and_watch`
                    when the environment variable ``GEVENT_NOWAITPID`` is *not* defined.
                    This is the default and should be used by most applications.

                    .. versionadded:: 1.1b5
                    )rw   ry   s     r   r=   r=   ß  s   € õ -¨dÐ=°fÐ=Ð=Ð=r   rj   Úposix_spawnc                  ó<   — t          | i |¤Ž}t          |¦  «         |S rF   )Ú_raw_posix_spawnrn   ©rz   r{   r@   s      r   r}   r}   ñ  s'   € Ý*¨DÐ;°FÐ;Ð;�CÝ  Ñ%Ô%Ð%Ø�Jr   c                  ó<   — t          | i |¤Ž}t          |¦  «         |S rF   )Ú_raw_posix_spawnprn   r€   s      r   Úposix_spawnprƒ   ö  s'   € Ý+¨TÐ<°VÐ<Ð<�CÝ  Ñ%Ô%Ð%Ø�Jr   rƒ   c                  ó   — t          ¦   «         S )a›  
                Forks a child process, initializes gevent in the child,
                but *does not* prepare the parent to wait for the child or receive SIGCHLD.

                This implementation of ``fork`` is a wrapper for :func:`fork_gevent`
                when the environment variable ``GEVENT_NOWAITPID`` *is* defined.
                This is not recommended for most applications.
                r;   r<   r   r   r   r   þ  s   € õ #‘}”}Ð$r   c                  ó   — t          ¦   «         S )a~  
                    Like :func:`fork`, but using :func:`os.forkpty`

                    This implementation of ``forkpty`` is a wrapper for :func:`forkpty_gevent`
                    when the environment variable ``GEVENT_NOWAITPID`` *is* defined.
                    This is not recommended for most applications.

                    .. versionadded:: 1.1b5
                    )rB   r<   r   r   r=   r=   
  s   € õ *Ñ+Ô+Ð+r   r<   )Únames_to_ignoreÚdunder_names_to_keep)rR   )NNF)@Ú__doc__Ú
__future__r   r   r#   Ú
gevent.hubr   r%   r   Úgevent._configr   Úgevent._compatr   Úgevent._utilr   r!   Úgetattrr	   r   ÚImportErrorÚ__implements__Ú__extensions__Úreadr   Úwriter1   ÚEINTRr"   r   r   r   r   r   Úhasattrr   r8   r9   r=   r?   rB   rs   rK   rj   rc   rD   rd   rM   rL   rQ   rN   rn   rp   rw   Údisable_watch_childrenr}   r   rƒ   r‚   ÚremoveÚglobalsÚ__imports__ÚlistÚsetÚ__all__r<   r   r   ú<module>r�      s™  ðð)ð )ðV 'Ð &Ð &Ð &Ð &Ð &à 	€	€	€	Ø 
€
€
€
Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %Ø €€€à	ˆ�˜ "Ñ	%Ô	%€ðØ€L€L€L€LøØð ð ð Ø€E€E€Eðøøøð �€Ø˜ZÐ(€à
Œ€Ø	Œ€ð ˜%œ+Ð&€ð 	ð JàÐAÐAÐAÑA€Nð	ð 	ð 	ðð ð ð>ð ð ð>6ð 6ð 6ð9ð 9ð 9ð €7ˆ2ˆvÑÔñ p"ð ”€Iðð ð ð8ð ð ð €wˆr�9ÑÔð 0Ø”zˆð	"ð 	"ð 	"ð& !ˆà×Ò˜iÑ(Ô(Ð(Ø×ÒÐ.Ñ/Ô/Ð/à€wˆr�9ÑÔð k-  ¨¨YÑ!7Ô!7ñ k-àˆˆˆà”:ˆØ”:ˆð &˜ˆð Ðð	 ð 	 ð 	 ð	+ð 	+ð 	+ð 	+ð4n	*ð n	*ð n	*ð`	8ð 	8ð 	8ð 	8ð %)¨t¸À[ð 	ð 	ð 	ð 	ð> 	×ÒÐ.Ñ/Ô/Ð/Ø×Ò˜mÑ,Ô,Ð,à˜Ð&Ð&Ø+/°dÀÈ~ð !ð !ð !ð !ð" ×!Ò!Ð"5Ñ6Ô6Ð6ð Ô,ð H	-ð7ð 7ð 7ð ˜NÐ*Ð*ð>ð >ð >ð ×!Ò! )Ñ,Ô,Ð,àˆw�r˜=Ñ)Ô)ð 6Ø#%¤>Ð Ø$&¤OÐ!ðð ð ð
ð ð ð
 ×%Ò% mÑ4Ô4Ð4Ø×%Ò% nÑ5Ô5Ð5øð	%ð 	%ð 	%ð ˜NÐ*Ð*ð
,ð 
,ð 
,ð ×!Ò! )Ñ,Ô,Ð,øð ×Ò˜&Ñ!Ô!Ð!ð ˆl˜2˜w˜w™yœyØ+9¸NÑ+JØ02ð4ñ 4ô 4€ð ˆ$ˆsˆs�> NÑ2Ñ3Ô3Ñ
4Ô
4€€€s   ÁA ÁAÁA