o
    0׾g<#                     @   s   d dl mZ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d	d
dddddddddddZe ZdZdd ZG dd deZdd dd fddZG d d! d!eZdS )"    )absolute_importprint_functionN)	iteritems)imp_acquire_lock)imp_release_lock)
__import___threading_localsocketselect	selectorsssl_thread
subprocessos	threadingbuiltinssignaltimequeuecontextvars)zgevent.localzgevent.socketzgevent.selectzgevent.selectorsz
gevent.sslzgevent.threadzgevent.subprocessz	gevent.oszgevent.threadingzgevent.builtinszgevent.signalzgevent.timezgevent.queuezgevent.contextvars__g_patched_module_c               	   C   sH   i } t tD ]\}}z	t|| |< W q ty!   |tv r Y q w | S )z
    Return a map from standard library name to
    imported gevent module that provides the same API.

    Optional modules are skipped if they cannot be imported.
    )r   MAPPING	importlibimport_moduleImportErrorOPTIONAL_STDLIB_MODULES)resultgevent_namestdlib_name r   Y/var/www/html/backend_erp/backend_erp_env/lib/python3.10/site-packages/gevent/_patcher.py_collect_stdlib_gevent_modules,   s   r!   c                   @   sX   e Zd Zdd fddZdd Zdd Zd	d
 Zdd ZdZdd Z	dd Z
dd ZdS )_SysModulesPatcherc                 C      dS Nr   r   mod_namer   r   r    <lambda>A       z_SysModulesPatcher.<lambda>c                 C   s   || _ || _t | _i | _d S N)	extra_all	importingr!   _green_modules_t_modules_to_restore)selfr+   r*   r   r   r    __init__A   s   
z_SysModulesPatcher.__init__c                 C   s~   i | _ | jD ]}tj|d | j |< qtttjD ]\}}|| jr-|| j |< tj|= qt| jD ]	\}}|tj|< q3d S r)   )	r-   r,   sysmodulesgetlistr   
startswithr+   )r.   modnamemodnamer   r   r    _saveN   s   

z_SysModulesPatcher._savec              	   C   s   t ttjD ]\}}|| jr|| j|< tj|= qt| jD ]\}}|d ur.|tj|< q ztj|= W q  ty=   Y q w d S r)   )	r3   r   r0   r1   r4   r+   r,   r-   KeyError)r.   r5   r6   r   r   r    _restorec   s   
z_SysModulesPatcher._restorec                 C   s*   z|    W t  d | _d S t  d | _w r)   )r:   r   r-   )r.   tvtbr   r   r    __exit__w   s   

z_SysModulesPatcher.__exit__c                 C   s   t   |   | S r)   )r   r8   r.   r   r   r    	__enter__   s   z_SysModulesPatcher.__enter__Nc                 C   s^   | j d u r-|  | | j|| _ | | j _| j tjt| j < W d    | S 1 s(w   Y  | S r)   )module
import_oner+   __gevent_patcher__r0   r1   _PATCH_PREFIX)r.   after_import_hookr   r   r    __call__   s   

z_SysModulesPatcher.__call__c                 C   st   t | }|tjv rtj| S || jsJ tj|d  t|i i |dd d }|| _| 	|g || |S )N.)
rD   r0   r1   r4   r+   popg_importsplitrA   _import_all)r.   module_namerE   patched_namerA   r   r   r    rB      s   

z_SysModulesPatcher.import_onec              	   C   s   |rI| d}|j}tt|dd| | }|D ]+}zt|| W q tyD   |jd | }t|i i |}t||| || Y qw |sd S d S )Nr   __all__r   rG   )	rI   __name__tuplegetattrr*   AttributeErrorrJ   setattrappend)r.   r   rA   r7   mod_all	attr_namerM   
new_moduler   r   r    rL      s   
z_SysModulesPatcher._import_all)rP   
__module____qualname__r/   r8   r:   r>   r@   rA   rF   rB   rL   r   r   r   r    r"   ?   s    r"   c                 C   r#   r$   r   r%   r   r   r    r'      r(   r'   c                 C   s   d S r)   r   )rA   r   r   r    r'      r(   c                 C   s>   t   t| |}|| W d   |S 1 sw   Y  |S )a  
    Import *module_name* with gevent monkey-patches active,
    and return an object holding the greened module as *module*.

    Any sub-modules that were imported by the package are also
    saved.

    .. versionchanged:: 1.5a4
       If the module defines ``__all__``, then each of those
       attributes/modules is also imported as part of the same transaction,
       recursively. The order of ``__all__`` is respected. Anything passed in
       *extra_all* (which must be in the same namespace tree) is also imported.

    .. versionchanged:: 1.5a4
       You must now do all patching for a given module tree
       with one call to this method, or at least by using the returned
       object.
    N)cached_platform_architecturer"   )rM   r*   rE   patcherr   r   r    import_patched   s   


r]   c                   @   s,   e Zd ZdZdZdZdZdd Zdd ZdS )r[   a
  
    Context manager that caches ``platform.architecture``.

    Some things that load shared libraries (like Cryptodome, via
    dnspython) invoke ``platform.architecture()`` for each one. That
    in turn wants to fork and run commands , which in turn wants to
    call ``threading._after_fork`` if the GIL has been initialized.
    All of that means that certain imports done early may wind up
    wanting to have the hub initialized potentially much earlier than
    before.

    Part of the fix is to observe when that happens and delay
    initializing parts of gevent until as late as possible (e.g., we
    delay importing and creating the resolver until the hub needs it,
    unless explicitly configured).

    The rest of the fix is to avoid the ``_after_fork`` issues by
    first caching the results of platform.architecture before doing
    patched imports.

    (See events.py for similar issues with platform, and
    test__threading_2.py for notes about threading._after_fork if the
    GIL has been initialized)
    Nc                    s6   dd l }| _|  _|j _ fdd}||_ S )Nr   c                     s   | s|s j S  j| i |S r)   )_arch_result
_orig_arch)argskwargsr?   r   r    arch   s   z4cached_platform_architecture.__enter__.<locals>.arch)platform	_platformarchitecturer^   r_   )r.   rc   rb   r   r?   r    r@      s   
z&cached_platform_architecture.__enter__c                 G   s   | j | j_d | _d S r)   )r_   rd   re   )r.   _argsr   r   r    r>      s   

z%cached_platform_architecture.__exit__)	rP   rY   rZ   __doc__r^   r_   rd   r@   r>   r   r   r   r    r[      s    r[   )
__future__r   r   r   r0   gevent._compatr   r   r   gevent.builtinsr   rJ   r   	frozensetr   rD   r!   objectr"   r]   r[   r   r   r   r    <module>   s<   	t
