U
    @fU                  	   @  s  d dl mZ zd dlZW n ek
r4   d dlZY nX d dlZzd dlmZ W n ek
rf   dZY nX d dlZddddgZ	dd	d
hZ
G dd dZdd Zdd Zdd Zdd Zdd Zi Zdd Zdd Zdd Zd1ddZdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zg dfd-d.Ze d/krej!"d  e  e#ej!d  $Z$e%e$& ej!d  d0Z'e(e' W 5 Q R X dS )2    )annotationsNregister_at_forkinjectimport_patchedmonkey_patchis_monkey_patched__builtins____file____name__c                   @  s*   e Zd ZdZd
ddZdd Zdd Zd	S )SysModulesSaverzClass that captures some subset of the current state of
    sys.modules.  Pass in an iterator of module names to the
    constructor. c                 C  s   i | _ t  | j|  d S N)_savedimpacquire_locksave)selfmodule_namesr   r   J/var/www/html/chatgpt/venv/lib/python3.8/site-packages/eventlet/patcher.py__init__   s    zSysModulesSaver.__init__c                 G  s"   |D ]}t j|d| j|< qdS )z&Saves the named modules to the object.N)sysmodulesgetr   )r   r   modnamer   r   r   r       s    zSysModulesSaver.savec              	   C  s^   zN| j D ]>\}}|dk	r(|tj|< qztj|= W q tk
rH   Y qX qW 5 t   X dS )zRRestores the modules that the saver knows about into
        sys.modules.
        N)r   release_lockr   itemsr   r   KeyError)r   r   modr   r   r   restore%   s    zSysModulesSaver.restoreN)r   )r   
__module____qualname____doc__r   r   r   r   r   r   r   r      s   
r   c           	      G  s  d|  }|t jkrt j| S |s>t t  t  t  t  }tdd |D }||  |D ]\}}|t j|< q^t j	| d t
t j D ] }|| d rt j	|d qzVt| i i | ddd }|dk	rt|D ]}|tkrt||||< q|t j|< W 5 |  X |S )a  Base method for "injecting" greened modules into an imported module.  It
    imports the module specified in *module_name*, arranging things so
    that the already-imported modules in *additional_modules* are used when
    *module_name* makes its imports.

    **Note:** This function does not create or change any sys.modules item, so
    if your greened module use code like 'sys.modules["your_module_name"]', you
    need to update sys.modules by yourself.

    *new_globals* is either None or a globals dictionary that gets populated
    with the contents of the *module_name* module.  This is useful when creating
    a "green" version of some other module.

    *additional_modules* should be a collection of two-element tuples, of the
    form (<name>, <module>).  If it's not specified, a default selection of
    name/module pairs is used, which should cover all use cases but may be
    slower because there are inevitably redundant or unnecessary imports.
    Z__patched_module_c                 S  s   g | ]\}}|qS r   r   ).0namemr   r   r   
<listcomp>[   s     zinject.<locals>.<listcomp>N.)r   r   _green_os_modules_green_select_modules_green_socket_modules_green_thread_modules_green_time_modulesr   r   poplistkeys
startswithr   
__import__splitdir	__excludegetattr)	module_nameZnew_globalsadditional_modulesZpatched_namesaverr$   r   Zimported_module_namemoduler   r   r   r   6   s>    




c                 O  s   t | df|t|   S )zImports a module in a way that ensures that the module uses "green"
    versions of the standard library modules, so that everything works
    nonblockingly.

    The only required argument is the name of the module to be imported.
    N)r   tupler   )r7   r8   Zkw_additional_modulesr   r   r   r   }   s    c                   s4    s"t  t  t  t  t    fdd}|S )a   Decorator that returns a version of the function that patches
    some modules for the duration of the function call.  This is
    deeply gross and should only be used for functions that import
    network libraries within their function bodies that there is no
    way of getting around.c                    sF   t  } D ]\}}|| |tj|< q
z| |W S |  X d S r   )r   r   r   r   r   )argskwr9   r$   r   r8   funcr   r   patched   s    
zpatch_function.<locals>.patched)r)   r*   r+   r,   r-   )r?   r8   r@   r   r>   r   patch_function   s    	rA   c                   s    fdd}|S )au  Kind of the contrapositive of patch_function: decorates a
    function such that when it's called, sys.modules is populated only
    with the unpatched versions of the specified modules.  Unlike
    patch_function, only the names of the modules need be supplied,
    and there are no defaults.  This is a gross hack; tell your kids not
    to import inside function bodies!c                    s>   t }D ]}t|tj|< qz | |W S |  X d S r   )r   originalr   r   r   )r<   r=   r9   r$   r?   r   r   r   r@      s    z)_original_patch_function.<locals>.patchedr   )r?   r   r@   r   rC   r   _original_patch_function   s    rD   c                 C  s   d|  }|t jkrt j|S t| f}t j| d ddd}| |krh||  }|| t|t j|< zNt| i i | 	ddd }| dkrt
|d	st|jjd|j_|t j|< W 5 |  X t j| S )
zd This returns an unpatched version of a module; this is useful for
    Eventlet itself (i.e. tpool).Z__original_module_N_thread	threading)rF   queuer'   r(   )QueuerG   
_threading)r   r   r   r   r.   r   rB   r   r2   r3   hasattrrD   rH   r   )r   original_namer9   deps
dependencyZreal_modr   r   r   rB      s(    




rB   c               	   K  s  t j  ddddddddd	h	}d
| kr4d| kr4tz| d
}W n tk
rV   Y n
X || d< | dd}|  D ]}||krttd| qt|dkrd|  k}|D ]8}|dkr| 	|d |dkr| 	|d | 	|| q| d rt
dst  g }dtfdtfdtfdtfdtfdtfdtfd	tffD ]2\}}| | r0t
|s0|| 7 }dt
|< q0| d rt
dsz ddlm}	 |	  dt
d< W n tk
r   Y nX td}
t  z|D ]\}}tj|}|dkrt|}|j D ](}t!||d}|dk	rt"||| qt!|dg }|D ]}t#||r.t$|| q.|dkrt%r|
j&j'|fdd}|  qW 5 t  X ddl(}td}||j)_*ddl+}|j,|_-ddl.}|j/|_0dS )a  Globally patches certain system modules to be greenthread-friendly.

    The keyword arguments afford some control over which modules are patched.
    If no keyword arguments are supplied, all possible modules are patched.
    If keywords are set to True, only the specified modules are patched.  E.g.,
    ``monkey_patch(socket=True, select=True)`` patches only the select and
    socket modules.  Most arguments patch the single module of the same name
    (os, time, select).  The exceptions are socket, which also patches the ssl
    module if present; and thread, which patches thread, threading, and Queue.

    It's safe to call monkey_patch multiple times.
    osselectsocketthreadtimeZpsycopgMySQLdbbuiltins
subprocess__builtin__allNz4monkey_patch() got an unexpected keyword argument %rTFr   )psycopg2_patcherrF   Z__deleted__c                   s4   d g fdd} fdd}t ||d d S )Nc                     s    d d< j  d< d S )N_activer   )rY   r   _global_dict_patched_prefork_activer   r   before_forkN  s    z?monkey_patch.<locals>.fix_threading_active.<locals>.before_forkc                     s   d  d< d S )Nr   rY   r   r   )r[   r]   r   r   
after_forkR  s    z>monkey_patch.<locals>.fix_threading_active.<locals>.after_fork)beforeafter_in_parentr   )r[   r\   r^   r_   r   rZ   r   fix_threading_activeF  s    z*monkey_patch.<locals>.fix_threading_activerE   )1eventletZhubsZget_hubAssertionErrorr.   r   r0   	TypeErrorvalues
setdefaultalready_patchedr   _green_existing_locksr)   r*   r+   r,   r-   _green_MySQLdb_green_builtins_green_subprocess_modulesZeventlet.supportrX   Zmake_psycopg_greenImportErrorrB   r   r   r   r   r   r2   Z__patched__r6   setattrrJ   delattrr   current_thread__globals__Zimportlib._bootstrap
_bootstraprE   rF   _PyRLockRLockrG   _PySimpleQueueSimpleQueue)onZaccepted_argsbZ
default_onkr   Zmodules_to_patchr$   Zmodules_functionrX   rI   r   Zorig_mod	attr_nameZpatched_attrZdeletedrb   	importlibrQ   rF   rG   r   r   r   r      s    
    






c                 C  s   | t kpt| ddt kS )a[  Returns True if the given module is monkeypatched currently, False if
    not.  *module* can be either the module itself or its name.

    Based entirely off the name of the module, so if you import a
    module some other way than with the import keyword (including
    import_patched), this might not be correct about that particular
    module.r   N)rh   r6   )r:   r   r   r   r   n  s    c                    s   ddl } ddl}ddl}ddl}t|  |jj fdd}t	t
j | d|jkr`dS |   t fdd|  D }|rddl}|d}|d	|d
 d  dS )a  Make locks created before monkey-patching safe.

    RLocks rely on a Lock and on Python 2, if an unpatched Lock blocks, it
    blocks the native thread. We need to replace these with green Locks.

    This was originally noticed in the stdlib logging module.r   Nc                   s
   t |  S r   )_convert_py3_rlock)Zold_lock)tidr   r   upgrade  s    z&_green_existing_locks.<locals>.upgradeZPYTEST_CURRENT_TESTc                   s   h | ]}t | r|qS r   )
isinstance)r#   o)
rlock_typer   r   	<setcomp>  s     
 z(_green_existing_locks.<locals>.<setcomp>rc   z{} RLock(s) were not greened,z= to fix this error make sure you run eventlet.monkey_patch() z#before importing any other modules.)gcrN   rF   eventlet.green.threadtypert   greenrQ   	get_ident_upgrade_instancesr   r   environZcollectlenZget_objectsloggingLoggererrorformat)r   rN   rF   rc   r~   Zremaining_rlocksr   loggerr   )r   r}   r   ri   z  s(    

ri   c                   s,  dkri dkri | t | <  fdd}t| trnt|  D ] \}}||}|dk	rL|| |< qLt| trt| D ] \}	}||}|dk	r|| |	< qzt| }
W n tk
r   Y nfX z6t|
 D ]$\}}||}|dk	rt| || qW n,   ddl	}|
d}|jddd Y nX dS )	a|  
    Starting with a Python object, find all instances of ``klass``, following
    references in ``dict`` values, ``list`` items, and attributes.

    Once an object is found, replace all instances with
    ``upgrade(found_object)``, again limited to the criteria above.

    In practice this is used only for ``threading.RLock``, so we can assume
    instances are hashable.
    Nc                   sX   t | krd S t|  r@| kr*|  S | }|| < |S nt|   d S d S r   )idr   r   )objnewklass
old_to_newr~   visitedr   r   upgrade_or_traverse  s    
z/_upgrade_instances.<locals>.upgrade_or_traverser   rc   zAn exception was thrown while monkey_patching for eventlet. to fix this error make sure you run eventlet.monkey_patch() before importing any other modules.T)exc_info)r   r   dictr/   r   	enumeratevarsre   rn   r   r   	exception)	containerr   r~   r   r   r   ry   vr   iZcontainer_varsr   r   r   r   r   r     s>    




r   c                 C  s   ddl }ddlm} | }t|dr0t|ds8td| |_d}|  rb|   |	  d}qD|  rv|	  d}|r||_
|S )	z
    Convert a normal RLock to one implemented in Python.

    This is necessary to make RLocks work with eventlet, but also introduces
    bugs, e.g. https://bugs.python.org/issue13697.  So more of a downgrade,
    really.
    r   N)allocate_lock_block_ownerzINTERNAL BUG. Perhaps you are using a major version of Python that is unsupported by eventlet? Please file a bug at https://github.com/eventlet/eventlet/issues/newFT)rF   r   r   rs   rJ   RuntimeErrorr   	_is_ownedreleaseacquirer   )oldr}   rF   r   r   Zacquiredr   r   r   r|     s&    r|   c                  C  s   ddl m}  d| fgS )Nr   rN   rN   )eventlet.greenrN   r   r   r   r   r)     s    r)   c                  C  s4   ddl m}  d| fg}ddl m} |d|f |S )Nr   )rO   rO   )	selectorsr   )r   rO   r   append)rO   r   r   r   r   r   r*   	  s
    
r*   c                  C  sN   ddl m}  zddl m} d| fd|fgW S  tk
rH   d| fg Y S X d S )Nr   )rP   )sslrP   r   )r   rP   r   rm   )rP   r   r   r   r   r+     s    r+   c                  C  s   ddl m}  d| fgS )Nr   rU   rU   )r   rU   r   r   r   r   rl     s    rl   c                  C  s:   ddl m}  ddl m} ddl m} d| fd|fd|fgS )Nr   )rH   )rQ   )rF   rG   rE   rF   )r   rH   rQ   rF   )rH   rQ   rF   r   r   r   r,   !  s    r,   c                  C  s   ddl m}  d| fgS )Nr   rR   rR   )r   rR   r   r   r   r   r-   (  s    r-   c                  C  s6   zddl m}  d| fgW S  tk
r0   g  Y S X d S )Nr   rS   rS   )r   rS   rm   r   r   r   r   rj   -  s
    rj   c                  C  s6   zddl m}  d| fgW S  tk
r0   g  Y S X d S )Nr   builtinrT   )r   r   rm   r   r   r   r   rk   5  s
    rk   c                   s,   |dkrj }| fdd|D  dS )a   Copy properties from *source* (assumed to be a module) to
    *destination* (assumed to be a dict).

    *ignore* lists properties that should not be thusly copied.
    *srckeys* is a list of keys to copy, if the source's __all__ is
    untrustworthy.
    Nc                   s*   i | ]"}| d s| ks|t|qS )__)r1   r6   )r#   r$   ignoresourcer   r   
<dictcomp>G  s
   
  z$slurp_properties.<locals>.<dictcomp>)__all__update)r   destinationr   Zsrckeysr   r   r   slurp_properties=  s
    r   __main__exec)NN))
__future__r   _impr   rm   r   rN   r   rc   r   r5   r   r   r   rA   rD   rB   rh   r   r   ri   r   r|   r)   r*   r+   rl   r,   r-   rj   rk   r   r   argvr.   openfcompilereadcoder   r   r   r   r   <module>   sP   

 G( *
@ 
	
