
    <|hzQ                     `   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Z eedd	      Z	 ddlZ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
      rdej>                  Z d Z!d Z eed      r3ejD                  Z#d Z$e$Z"ejK                  d       ejK                  d        eed      s
 eed      rddl&Z&ejN                  Z(ejR                  Z*d Z+i Z,d Z-d0dZ.d Z'd1dZ/ddde!fd Z0ejK                  d!       ejK                  d"       dev rddde$fd#Z1ejK                  d$       e	jd                  sed% Zdev rd& Z"ejK                  d'        eed(      rnejf                  Z4ejj                  Z6d) Z3d* Z5ejK                  d(       ejK                  d+       n-d, Zdev rd- Z"ejK                  d'       nejo                  d
        ee e8       eez   d./      Z9 e: e;eez               Z<y# e$ r dZY w xY w)2aQ  
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)copy_globalsEAGAIN   forktp_readtp_write)make_nonblockingnb_readnb_writec                     t        j                   | t         j                  d      }t        |t        j                  z        s7t        j                   | t         j
                  |t        j                  z         yy)zPut 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     H/var/www/html/test/engine/venv/lib/python3.12/site-packages/gevent/os.pyr   r   K   sP     Bq1EBMM)*KKEMM52==+@A +    c                 X   d}d}	 	 	 t        | |      }|||j                          d}d}S S # t        $ r}|j                  t        vr Y d}~nd}~ww xY w|&t               }|j                  j                  | d      }|j                  |       # ||j                          d}d}w 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_errorsget_hubloopiowait)r   nhubeventresultes         r   r   r   V   s     	"2q\F!   !  wwn4 5 ;!)CHHKKA.E    !+   B - 	AA	B A=B B)c                 X   d}d}	 	 	 t        | |      }|||j                          d}d}S S # t        $ r}|j                  t        vr Y d}~nd}~ww xY w|&t               }|j                  j                  | d      }|j                  |       # ||j                          d}d}w 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.
        N   )	_writer   r   r    r!   r"   r#   r$   r%   )r   bufr'   r(   r)   r*   s         r   r   r   s   s     	#B_F!   !  wwn4 5 ;!)CHHKKA.E    !r+   c                 V    t               j                  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&   s     r   r   r      s#     9%%eb!W55r   c                 V    t               j                  j                  t        | |f      S )zWrite bytes from buffer *buf* to file descriptor *fd*. Return the
    number of bytes written.

    Writing is done using the threadpool.
    )r"   r1   r2   r.   )r   r/   s     r   r   r      s#     9%%fr3i88r   c                      ddl } | j                         5  | j                  dt               t	               }ddd       s
t                |S # 1 sw Y   xY w)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   )r6   r)   s     r   fork_geventr;      sQ    . 	 $$& 	!!!(,>?[F	! H	! 	!s   !AAc                      t               S )zL
        A wrapper for :func:`fork_gevent` for non-POSIX platforms.
        r;    r   r   r
   r
      s     }r   forkptyc                  <    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_geventrD      s!     *^NC	>!r   rD   WNOWAITWNOHANGc                       y Nr>   r>   r   r   <lambda>rI      s    r   c                 $   | j                          	 | j                  | j                  t        j                         ft        | j                  <   |r ||        t                t                | j                          y # | j                          w xY wrH   )stoprB   rstatustime_watched_children_on_child_hook_reap_childrenr   )watchercallbacks     r   	_on_childrS      sd     LLN	 29++wPTPYPYP[1\!'++.W%  s   AA= =Bc                     t        j                          }|| z
  }t        j                         D cg c]  \  }}t        |t              r
|d   |k  r|! }}}|D ]	  }t        |=  y c c}}w )Nr-   )rM   rN   items
isinstancetuple)timeoutnowoldest_allowedrB   valdeads         r   rP   rP     sw      ))+C 7]N %**, Sc5)c!f~.E D 
  +%c*+s   $A'c                 "   | dk  r| dk(  rAd}t         j                         D ](  \  }}t        |t              s|	|d   |k  s"|} |d   }* | dk  ro| dk(  r^|dk(  rYt	               }|j
                  j                  dd      5 }|j                  |       |j                  |j                  fcddd       S t        | |      S | t         v r|t        z  st        t         |    t              r&t         |    }t        |t              rt         | = |dd S yt         |    }|j
                  j                  | d      5 }t	               j                  |       ddd       |j                  |j                  fS t        | |      S # 1 sw Y   xY w# 1 sw Y   9xY w)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   Nr-   F)r   r   )rN   rU   rV   rW   r"   r#   childr%   rpidrL   _waitpid_WNOHANG)	rB   optionsfinished_atkvr'   rQ   r)   new_watchers	            r   waitpidrh      s   B ax"9
 #'K 1 7 7 9 /1 *1e 4%0%8AaD;<N"#C*+A$K/ !8 byW\%i XX^^Au5 AHHW-#*<<#@A A $C11'' X%4Ec4JE)R /s3F!&%0 .c2%bqz)! ,C0 \\''U3 0{INN;/0 ||W__44 C))iA AB0 0s   )E93F9FFFc                     |xs t               j                  }|j                  | |      }|t        | <   |j	                  t
        ||       y )N)ref)r"   r#   r_   rN   startrS   )rB   rR   r#   rj   rQ   s        r   _watch_childrl     s?    )79>>Djj#j.G%,c"MM)Wh7r   c                 4     |       }|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`.
            )rl   )rR   r#   rj   r
   rB   s        r   fork_and_watchrn     s!    2 &CS(D#6Jr   rn   r;   c                 <    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                  >            } j                  |        | d   S )Nr   )append)
pid_and_fdr?   r)   s    r   _forkz forkpty_and_watch.<locals>._fork  s     !(JMM*-%a=(r   r   rn   )rR   r#   rj   r?   rs   r)   s      ` @r   forkpty_and_watchru     s)     ) xsE:ay r   ru   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
                rt   argskwargss     r   r
   r
     s     &t6v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
                    )ru   rw   s     r   r?   r?     s     -d=f==r   rh   posix_spawnc                  2    t        | i |}t        |       |S rH   )_raw_posix_spawnrl   rx   ry   rB   s      r   r{   r{     s    *D;F;C %Jr   c                  2    t        | i |}t        |       |S rH   )_raw_posix_spawnprl   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
                    )rD   r>   r   r   r?   r?   
  s     *++r   r>   )names_to_ignoredunder_names_to_keep)<   )NNF)=__doc__
__future__r   r   
gevent.hubr   r"   r   gevent._configr   gevent._utilr   r    getattrr   r   ImportError__implements____extensions__readr   writer.   EINTRr!   r   r   r   r   r   hasattrr
   r:   r;   r?   rA   rD   rq   rM   rh   ra   rF   rb   rO   rN   rS   rP   rl   rn   ru   disable_watch_childrenr{   r}   r   r   removeglobals__imports__listset__all__r>   r   r   <module>r      s  )V ' 	 1  ! % 	"	% Z(
	 %++& 	AAN	::69 2v IB r9zz	"& !i(./r9Y!7:::: & 	 	+4n	*`	8 %)t[ 	> 	./m,&+/d~ !" !!"56 ,,7 N*> !!),r=)#%>> $&OO!

 %%m4%%n5	% N*
, !!), &! 2wy+9N+J024 s>N23
4K  Es   H" "H-,H-