ó
úR‹_c           @@  s3  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 y d d l Z Wn e k
 rÅ d Z n Xd g Z d d g Z e j Z e j Z e e j g Z e r3e d d d g 7Z d „  Z d „  Z d „  Z n  d „  Z  d „  Z! e" e d ƒ rêe j# Z$ d „  Z% d „  Z# e" e d ƒ r³e j& Z' d „  Z( e( Z& e j) d ƒ e j) d ƒ n  e" e d ƒ sÑe" e d ƒ r÷d d l* Z* e j+ Z, e j- Z. d „  Z/ i  Z0 d „  Z1 d d  „ Z2 d! „  Z+ d d e3 e% d" „ Z4 e j) d# ƒ e j) d$ ƒ d e k r|d d e3 e( d% „ Z5 e j) d& ƒ n  e
 j6 s¶d' „  Z# d e k r¦d( „  Z& n  e j) d) ƒ qçd* „  Z# d e k r×d+ „  Z& n  e j) d) ƒ q÷n e j7 d ƒ e e e8 ƒ  d, e e d- d. ƒZ9 e: e; e e ƒ ƒ Z< d S(/   sQ  
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.)
i    (   t   absolute_importN(   t   _get_hub_noargs(   t   reinit(   t   config(   t   PY3(   t   copy_globalst   EAGAINi   t   forkt   tp_readt   tp_writet   make_nonblockingt   nb_readt   nb_writec         C@  sP   t  j  |  t  j d ƒ } t | t j @ƒ sL t  j  |  t  j | t j Bƒ t Sd S(   s™   Put the file descriptor *fd* into non-blocking mode if
        possible.

        :return: A boolean value that evaluates to True if successful.
        i    N(   t   fcntlt   F_GETFLt   boolt   ost
   O_NONBLOCKt   F_SETFLt   True(   t   fdt   flags(    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR
   L   s    c         C@  sÓ   d } d } zš x“ y t |  | ƒ } | SWn; t k
 rf } | j t k rP ‚  n  t sg t j ƒ  qg n X| d k r” t ƒ  } | j	 j
 |  d ƒ } n  | j | ƒ q WWd | d k	 rÎ | j ƒ  d } d } n  Xd S(   s  
        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.
        i   N(   t   Nonet   _readt   OSErrort   errnot   ignored_errorsR   t   syst	   exc_cleart   get_hubt   loopt   iot   waitt   close(   R   t   nt   hubt   eventt   resultt   e(    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR   W   s(    	
c         C@  sÓ   d } d } zš x“ y t |  | ƒ } | SWn; t k
 rf } | j t k rP ‚  n  t sg t j ƒ  qg n X| d k r” t ƒ  } | j	 j
 |  d ƒ } n  | j | ƒ q WWd | d k	 rÎ | j ƒ  d } d } n  Xd S(   sò   
        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.
        i   N(   R   t   _writeR   R   R   R   R   R   R   R   R   R    R!   (   R   t   bufR#   R$   R%   R&   (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR   v   s(    	
c         C@  s   t  ƒ  j j t |  | f ƒ S(   sÊ   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   t
   threadpoolt   applyR   (   R   R"   (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR   •   s    c         C@  s   t  ƒ  j j t |  | f ƒ S(   sŽ   Write bytes from buffer *buf* to file descriptor *fd*. Return the
    number of bytes written.

    Writing is done using the threadpool.
    (   R   R)   R*   R'   (   R   R(   (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR	   Ÿ   s    c          C@  s   t  ƒ  }  |  s t ƒ  n  |  S(   s÷  
        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
        (   t	   _raw_forkR   (   R%   (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt   fork_gevent­   s    	
c           C@  s   t  ƒ  S(   sL   
        A wrapper for :func:`fork_gevent` for non-POSIX platforms.
        (   R,   (    (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR   É   s    t   forkptyc          C@  s)   t  ƒ  \ }  } |  s t ƒ  n  |  | f S(   s¶  
            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
            (   t   _raw_forkptyR   (   t   pidt	   master_fd(    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt   forkpty_geventÒ   s    
R1   t   WNOWAITt   WNOHANGc           C@  s   d  S(   N(   R   (    (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt   <lambda>ò   t    c         C@  sc   |  j  ƒ  zG |  j |  j t j ƒ  f t |  j <| rB | |  ƒ n  t ƒ  t ƒ  Wd  |  j ƒ  Xd  S(   N(   t   stopR/   t   rstatust   timet   _watched_childrent   _on_child_hookt   _reap_childrenR!   (   t   watchert   callback(    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt	   _on_child÷   s    
"i<   c         C@  sv   t  j  ƒ  } | |  } g  t j ƒ  D]1 \ } } t | t ƒ r# | d | k  r# | ^ q# } x | D] } t | =qa Wd  S(   Ni   (   R8   R9   t   itemst
   isinstancet   tuple(   t   timeoutt   nowt   oldest_allowedR/   t   valt   dead(    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR;     s    
+c         C@  sv  |  d k rÌ |  d k rQ x6 t  j ƒ  D]% \ } } t | t ƒ r% | }  Pq% q% Wn  |  d k rÌ |  d k r¼ | d k r¼ t ƒ  } | j j d t ƒ $ } | j | ƒ | j	 | j
 f SWd QXn  t |  | ƒ Sn  |  t  k ri| t @sõ t t  |  t ƒ r!t  |  } t | t ƒ rt  |  =| d  Sd St  |  } | j j |  t ƒ  } t ƒ  j | ƒ Wd QX| j	 | j
 f St |  | ƒ S(   s#  
            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.

            Availability: POSIX.

            .. versionadded:: 1.1b1
            .. versionchanged:: 1.2a1
               More cases are handled in a cooperative manner.
            i    iÿÿÿÿNi   (   i    i    (   R9   R?   R@   RA   R   R   t   childt   FalseR    t   rpidR7   t   _waitpidt   _WNOHANG(   R/   t   optionst   kt   vR#   R<   R%   t   new_watcher(    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt   waitpid  s2    	


c         C@  sZ   | ƒ  } | rV | p t  ƒ  j } | j | d | ƒ} | t | <| j t | |  ƒ n  | S(   s¿  
            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`.
            t   ref(   R   R   RG   R9   t   startR>   (   R=   R   RQ   R   R/   R<   (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt   fork_and_watch„  s    	
RS   R,   c         @  s3   g  ‰ ‡  ‡ f d †  } t  |  | | | ƒ ˆ d S(   s¾   
                Like :func:`fork_and_watch`, except using :func:`forkpty_gevent`.

                Availability: Some Unix systems.

                .. versionadded:: 1.1b5
                c          @  s   ˆ  ƒ  }  ˆ j  |  ƒ |  d S(   Ni    (   t   append(   t
   pid_and_fd(   R-   R%   (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt   _fork´  s    	i    (   RS   (   R=   R   RQ   R-   RV   (    (   R-   R%   sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt   forkpty_and_watchª  s    RW   c          O@  s   t  |  | Ž  S(   s×  
                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
                (   RS   (   t   argst   kwargs(    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR   Á  s    c          O@  s   t  |  | Ž  S(   s˜  
                    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   (   RX   RY   (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR-   Ð  s    RP   c           C@  s   t  ƒ  S(   s›  
                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,   (    (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR   Þ  s    	c           C@  s   t  ƒ  S(   s~  
                    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
                    (   R1   (    (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyR-   ê  s    
t   names_to_ignoret   dunder_names_to_keep(    (=   t   __doc__t
   __future__R    R   R   t
   gevent.hubR   R   R   t   gevent._configR   t   gevent._compatR   t   gevent._utilR   R   t   getattrR   R   t   ImportErrorR   t   __implements__t   __extensions__t   readR   t   writeR'   t   EINTRR   R
   R   R   R   R	   t   hasattrR   R+   R,   R-   R.   R1   RT   R8   RP   RJ   R3   RK   R:   R9   R>   R;   RH   RS   RW   t   disable_watch_childrent   removet   globalst   __imports__t   listt   sett   __all__(    (    (    sB   /var/www/syncserver/local/lib/python2.7/site-packages/gevent/os.pyt   <module>*   s‚   
						
												f"			
	