o
    U˟iKf                     @   s  d Z ddlm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mZ ddlZeed
dZzddlZW n eyK   dZY nw dgZddgZejZejZejZejZeejgZ dZ!ermdd Z"ndd Z"eri Z#edg7 Zdd Zeg d7 Zdd Z$dd Z%dd Z&dd Z'dd Z(e)edr\ej*Z+dd Z,d d Z*e)ed!rej-Z.d"d# Z/e/Z-e0d! e0d# e)ed$se)ed%r[ddl1Z1ej2Z3ej4Z5d&d' Z6i Z7d(d) Z8d@d+d,Z9d-d. Z2dAd0d1Z:ddd/e,fd2d3Z;e0d3 e0d d!ev rddd/e/fd4d5Z<e0d5 ej=sId6d Z*d!ev r%d7d! Z-e0d. e)ed8rHej>Z?ej@ZAd9d8 Z>d:d; Z@e0d8 e0d; nd<d Z*d!ev rVd=d! Z-e0d. neBd eeeC ee d>d?ZDeEeFee ZGdS )BaQ  
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)S_ISREG)_get_hub_noargs)reinit)Event)config)copy_globalsEAGAIN   forktp_readtp_writeTc                 C   s   t  | t jS N)fcntlF_GETFDfd r   Q/var/www/apps/myagent/mysuperagent/venv/lib/python3.10/site-packages/gevent/os.py_check_fd_validQ   s   r   c                 C      d S r   r   r   r   r   r   r   V   s   closec                    s   t |  t| }t|jrtrt| S t }|j}| tv rJt	| ddurE|
 }|| W d   n1 s;w   Y  d ttj||  |  t t| <  fdd} ||  dS )a  
        Close a file descriptor.

        This function cooperates with gevent to avoid crashing
        the process if you (accidentally) call it while you're
        still performing IO on the file descriptor; for example, if you have it
        registered with a ``Selector`` implementation, which documents
        that you *must* unregister FDs before closing them.

        If the *fd* refers to a regular file, this cooperation is *not*
        used. This is because trying to use gevent to poll on regular
        files doesn't work and shouldn't be done. We assume that everyone
        is following the rules.

        .. caution::
           This function is not intended for use on Windows.

        .. versionadded:: 25.8.1
        NgMbP?c              
      s   z:zt |  W n	 ty   Y nw W   t| d        d S W   t| d        d S   t| d        w r   )_closeOSErrorset_closing_fd_to_eventpopstopr   r   checkeventr   r   cb   s&   
zclose.<locals>.cb)r   _fstatr   st_mode_NO_DEFER_REG_FILEr   get_hubloopr   r   idlewaitr   errnoEBADF
closing_fdr   r   start)r   statshubr&   watcherr!   r   r   r   r   `   s(   #



)make_nonblockingnb_readnb_writec                 C   s<   t  | t jd}t|tj@ st  | t j|tjB  dS dS )zPut the file descriptor *fd* into non-blocking mode if
        possible.

        :return: A boolean value that evaluates to True if successful.
        r   TN)r   F_GETFLboolos
O_NONBLOCKF_SETFL)r   flagsr   r   r   r0      s
   r0   c              
   C   s   d}d}zD	 zt | |}|W W |dur|  d}d}S S  ty4 } z|jtvr* W Y d}~nd}~ww |du rCt }|j| d}|| q|durU|  d}d}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   )	_readr   r   r)   ignored_errorsr%   r&   ior(   )r   nr.   r    resulter   r   r   r1      6   
	

r1   c              
   C   s   d}d}zD	 zt | |}|W W |dur|  d}d}S S  ty4 } z|jtvr* W Y d}~nd}~ww |du rCt }|j| d}|| q|durU|  d}d}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.
        Nr9      )	_writer   r   r)   r;   r%   r&   r<   r(   )r   bufr.   r    r>   r?   r   r   r   r2      r@   r2   c                 C      t  jt| |fS )zRead 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=   r   r   r   r     s   c                 C   rD   )zWrite bytes from buffer *buf* to file descriptor *fd*. Return the
    number of bytes written.

    Writing is done using the threadpool.
    )r%   rE   rF   rB   )r   rC   r   r   r   r     s   c                  C   sP   ddl } |   | dt t }W d   n1 sw   Y  |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
        r   Nignore)warningscatch_warningssimplefilterDeprecationWarning	_raw_forkr   )rH   r>   r   r   r   fork_gevent'  s   
rM   c                   C      t  S )zL
        A wrapper for :func:`fork_gevent` for non-POSIX platforms.
        rM   r   r   r   r   r   H  s   forkptyc                  C   s   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_fdr   r   r   forkpty_geventQ  s   
rT   WNOWAITWNOHANGc                   C   r   r   r   r   r   r   r   <lambda>q  s    rW   c                 C   sR   |    z| j| jt ft| j< |r||  t  t  W |   d S |   w r   )r   rR   rstatustime_watched_children_on_child_hook_reap_childrenr   )r/   callbackr   r   r   	_on_childv  s   r^   <   c                    s:   t   }||    fddt D }|D ]}t|= qd S )Nc                    s*   g | ]\}}t |tr|d   k r|qS )rA   )
isinstancetuple).0rR   valoldest_allowedr   r   
<listcomp>  s    z"_reap_children.<locals>.<listcomp>)rY   rZ   items)timeoutnowdeadrR   r   rd   r   r\     s   
r\   c           	      C   sZ  | dkr^| dkr(d}t  D ]\}}t|tr'|du s!|d |k r'|} |d }q| dkr^| dkrY|dkrYt }|jdd}|| |j|j	fW  d   S 1 sTw   Y  t
| |S | t v r|t@ smtt |  trt |  }t|trt | = |dd S dS t |  }|j| d}t | W d   n1 sw   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   NrA   F)r   r   )rZ   rg   r`   ra   r%   r&   childr(   rpidrX   _waitpid_WNOHANG)	rR   optionsfinished_atkvr.   r/   r>   new_watcherr   r   r   waitpid  s>   !

 

	

ru   Fc                 C   s4   |pt  j}|j| |d}|t| < |t|| d S )N)ref)r%   r&   rl   rZ   r,   r^   )rR   r]   r&   rv   r/   r   r   r   _watch_child  s   rw   c                 C   s   | }|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`.
            )rw   )r]   r&   rv   r   rR   r   r   r   fork_and_watch  s   rx   c                    s(   g  fdd}t | ||| d S )z
                Like :func:`fork_and_watch`, except using :func:`forkpty_gevent`.

                Availability: Some Unix systems.

                .. versionadded:: 1.1b5
                c                     s     }  |  | d S )Nr   )append)
pid_and_fdrP   r>   r   r   _forkB  s   
z forkpty_and_watch.<locals>._forkr   rx   )r]   r&   rv   rP   r|   r   r{   r   forkpty_and_watch8  s   r~   c                  O      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
                r}   argskwargsr   r   r   r   O  s   c                  O   r   )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
                    )r~   r   r   r   r   rP   ^  s   posix_spawnc                  O      t | i |}t| |S r   )_raw_posix_spawnrw   r   r   rR   r   r   r   r   p     c                  O   r   r   )_raw_posix_spawnprw   r   r   r   r   posix_spawnpu  r   r   c                   C   rN   )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.
                rO   r   r   r   r   r   }  s   	c                   C   rN   )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
                    )rT   r   r   r   r   rP     s   
r   )names_to_ignoredunder_names_to_keep)r_   )NNF)H__doc__
__future__r   r5   statr   
gevent.hubr   r%   r   gevent.eventr   gevent._configr   gevent._utilr   r)   getattrr	   r   ImportError__implements____extensions__readr:   writerB   r   r   fstatr"   EINTRr;   r$   r   r   r0   r1   r2   r   r   hasattrr   rL   rM   rP   rQ   rT   ry   rY   ru   rn   rV   ro   r[   rZ   r^   r\   rw   rx   r~   disable_watch_childrenr   r   r   r   removeglobals__imports__listr   __all__r   r   r   r   <module>   s    +


h
	!




p










