
    Mh8                       U 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m	Z	m
Z
mZmZ ddlm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mZmZ ddlmZ ddlmZ ddlm Z m!Z! ddl"m#Z#m$Z$ da%ddZ& ej'        e&            edd          Z( eddd          Z) G d dee e
e)                   Zee$         Z*de+d<   ddgZ,dS )zPython bindings for 0MQ.    )annotationsN)Lock)AnyCallableGenericTypeVaroverload)warn)WeakSet)	TypeAlias)Context)ContextOptionErrnoSocketOption)ZMQErrorcast_int_addr   )AttributeSetterOptValT)Socket
SyncSocketFreturnNonec                 
    da d S NT)_exiting     Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/zmq/sugar/context.py_notice_atexitr!      s    HHHr   _ContextTyper   )bound_SocketTyper   T)r#   	covariantc                      e Zd ZU dZded<   dZded<    e            ZdZded<   d	Z	dZ
d	Zd
ed<   eZded<   edIdJd            ZedKd            ZedLd            Z	 	 dMdN fdZdOdZdZdPd ZdQd"ZdRd&ZdSdTd(ZeZedUd-            ZedVd/            ZedIdWd0            ZdO fd1ZdXd3ZdYd5ZdYd6ZdSdZd8Z	 dSd[d>Z d\dAZ!d]dCZ"d^dEZ#d_dFZ$d`dHZ% xZ&S )ar   a  Create a zmq Context

    A zmq Context creates sockets via its ``ctx.socket`` method.

    .. versionchanged:: 24

        When using a Context as a context manager (``with zmq.Context()``),
        or deleting a context without closing it first,
        ``ctx.destroy()`` is called,
        closing any leftover sockets,
        instead of `ctx.term()` which requires sockets to be closed first.

        This prevents hangs caused by `ctx.term()` if sockets are left open,
        but means that unclean destruction of contexts
        (with sockets left open) is not safe
        if sockets are managed in other threads.

    .. versionadded:: 25

        Contexts can now be shadowed by passing another Context.
        This helps in creating an async copy of a sync context or vice versa::

            ctx = zmq.Context(async_ctx)

        Which previously had to be::

            ctx = zmq.Context.shadow(async_ctx.underlying)
    zdict[int, Any]sockoptsNr   	_instance
int | None_instance_pidFr   _socketsztype[_SocketType]_socket_classr   selfSyncContext
io_threadsintc                    d S Nr   r-   r/   s     r    __init__zContext.__init__Q   s    :=#r   c                   d S r2   r   r3   s     r    r4   zContext.__init__T   s    =@Sr   shadowContext | intc                   d S r2   r   )r-   r6   s     r    r4   zContext.__init__W   s    ?Bsr   r   int | Contextr   r   c                b   t          |t                    r|}d}d}|rLd| _        || _        t          |t                    s	 |j        }n# t          $ r Y nw xY wt          |          }nd| _        t                      	                    ||           i | _
        t                      | _        d S )Nr   r   TF)r/   r6   )
isinstancer   _shadow_shadow_objr0   
underlyingAttributeErrorr   superr4   r'   r   r+   )r-   r/   r6   shadow_address	__class__s       r    r4   zContext.__init__Z   s    
 j'** 	  FJ 	!DL%Dfc** #.FF%   D*622NN DLJ~FFF		s   A 
AAc                    t                       | j        s^t          sY| j        sTd| _        t
          ,t          | dd          t          d|  t          d|            |                                  dS dS dS dS )a)  Deleting a Context without closing it destroys it and all sockets.

        .. versionchanged:: 24
            Switch from threadsafe `term()` which hangs in the event of open sockets
            to less safe `destroy()` which
            warns about any leftover sockets and closes them.
        TNr+   zUnclosed context    
stacklevelsource)	localsr<   r   closed_warn_destroy_closer
   getattrResourceWarningdestroyr-   s    r    __del__zContext.__del__v   s     	| 
	H 
	T[ 
	'+D$GD*d$C$C$O...# 	    LLNNNNN
	 
	 
	 
	 
	 
	r   zzmq.Contextstrc           
     D   | j         }|j                            dd           }||j         d|j         }| j        rdnd}t          | dd           r&t          | j                  }|dk    rdnd}| d| }nd}d	| d
| dt          t          |                      | dS )N	_repr_cls.z closed r+   r   sz socket<(z) at >)rB   __dict__get
__module____name__rI   rK   lenr+   hexid)r-   clsrR   rI   	n_socketsrU   socketss          r    __repr__zContext.__repr__   s    nL$$[$77	>::CL::I"k1r4T** 	DM**I 1}}"A"..1..GGGE9EEwEESD]]EFEEEEr   r"   c                    | S r2   r   rN   s    r    	__enter__zContext.__enter__   s    r   exc_type	exc_value	tracebackc                <    d| _         |                                  d S r   )rJ   rM   )r-   rf   rg   rh   s       r    __exit__zContext.__exit__   s    #' r   memoc                @    | j                             | j                  S )z'Copying a Context creates a shadow copy)rB   r6   r>   )r-   rk   s     r    __copy__zContext.__copy__   s    ~$$T_555r   r`   type[_ContextType]addressint | zmq.Contextc                     | |          S )aI  Shadow an existing libzmq context

        address is a zmq.Context or an integer (or FFI pointer)
        representing the address of the libzmq context.

        .. versionadded:: 14.1

        .. versionadded:: 25
            Support for shadowing `zmq.Context` objects,
            instead of just integer addresses.
        r6   r   )r`   ro   s     r    r6   zContext.shadow   s     s'""""r   ctxc                r    ddl m} ddlm} |                    |          } ||          } | |          S )zuShadow an existing pyczmq context

        ctx is the FFI `zctx_t *` pointer

        .. versionadded:: 14.1
        r   )zctxr   rr   )pyczmqru   zmq.utils.interopr   r>   )r`   rs   ru   r   r>   ro   s         r    shadow_pyczmqzContext.shadow_pyczmq   s[     	 333333__S))
-
++s'""""r   c                \   | j         (| j        t          j                    k    s| j         j        rw| j        5  | j         (| j        t          j                    k    s| j         j        r) | |          | _         t          j                    | _        ddd           n# 1 swxY w Y   | j         S )a  Returns a global Context instance.

        Most single-process applications have a single, global Context.
        Use this method instead of passing around Context instances
        throughout your code.

        A common pattern for classes that depend on Contexts is to use
        a default argument to enable programs with multiple Contexts
        but not require the argument for simpler applications::

            class MyClass(object):
                def __init__(self, context=None):
                    self.context = context or Context.instance()

        .. versionchanged:: 18.1

            When called in a subprocess after forking,
            a new global instance is created instead of inheriting
            a Context that won't work from the parent process.
        N)r/   )r(   r*   osgetpidrI   _instance_lock)r`   r/   s     r    instancezContext.instance   s    . M! BIKK//}# 0 # 4 4M)(BIKK77}+ 8 %(C:$>$>$>CM(*	C%4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 }s   ABB #B c                H    t                                                       dS )a  Close or terminate the context.

        Context termination is performed in the following steps:

        - Any blocking operations currently in progress on sockets open within context shall
          raise :class:`zmq.ContextTerminated`.
          With the exception of socket.close(), any further operations on sockets open within this context
          shall raise :class:`zmq.ContextTerminated`.
        - After interrupting all blocking calls, term shall block until the following conditions are satisfied:
            - All sockets open within context have been closed.
            - For each socket within context, all messages sent on the socket have either been
              physically transferred to a network peer,
              or the socket's linger period set with the zmq.LINGER socket option has expired.

        For further details regarding socket linger behaviour refer to libzmq documentation for ZMQ_LINGER.

        This can be called to close the context by hand. If this is not called,
        the context will automatically be closed when it is garbage collected,
        in which case you may see a ResourceWarning about the unclosed context.
        N)r@   term)r-   rB   s    r    r   zContext.term   s    * 	r   	list[str]c                l    t          | j                  }|                    t          j                   |S r2   )dirrB   extendr   __members__)r-   keyss     r    __dir__zContext.__dir__  s+    4>""M-...r   socketc                :    | j                             |           dS )zBAdd a weakref to a socket for Context.destroy / reference countingN)r+   addr-   r   s     r    _add_socketzContext._add_socket  s    &!!!!!r   c                `    t          | dd          | j                            |           dS dS )z8Remove a socket for Context.destroy / reference countingr+   N)rK   r+   discardr   s     r    
_rm_socketzContext._rm_socket  s:     4T**6M!!&))))) 76r   lingerc                Z   | j         rdS t          t          | dd          pg           }|D ]j}|rf|j         s_| j        r"t          t	          d| t
          d|           | |                    t          j        |           |	                                 k| 
                                 dS )a  Close all sockets associated with this context and then terminate
        the context.

        .. warning::

            destroy involves calling :meth:`Socket.close`, which is **NOT** threadsafe.
            If there are active sockets in other threads, this must not be called.

        Parameters
        ----------

        linger : int, optional
            If specified, set LINGER on sockets prior to closing them.
        Nr+   z(Destroying context with unclosed socket    rE   )rI   listrK   rJ   r
   rL   
setsockoptr   LINGERcloser   )r-   r   rb   rU   s       r    rM   zContext.destroy!  s     ; 	F%)'$
D*I*I*OR%P%P 	 	A  + 0@F1FF'#$ 	    %LL!4f===					r   socket_typesocket_class1Callable[[_ContextType, int], _SocketType] | Nonekwargsr$   c                "   | j         rt          t          j                  || j        } || |fi |}| j                                        D ],\  }}	 |                    ||           # t          $ r Y )w xY w|                     |           |S )a[  Create a Socket associated with this Context.

        Parameters
        ----------
        socket_type : int
            The socket type, which can be any of the 0MQ socket types:
            REQ, REP, PUB, SUB, PAIR, DEALER, ROUTER, PULL, PUSH, etc.

        socket_class: zmq.Socket
            The socket class to instantiate, if different from the default for this Context.
            e.g. for creating an asyncio socket attached to a default Context or vice versa.

            .. versionadded:: 25

        kwargs:
            will be passed to the __init__ method of the socket class.
        )	rI   r   r   ENOTSUPr,   r'   itemsr   r   )r-   r   r   r   rU   optvalues          r    r   zContext.socketD  s    . ; 	*5=)))-LLk %+  	

 ---// 	 	JCS%((((    	
 	s   A**
A76A7r   r   c                    || j         |<   dS )zkset default socket options for new sockets created by this Context

        .. versionadded:: 13.0
        Nr'   )r-   r   r   s      r    r   zContext.setsockopto  s    
 #cr   r   c                    | j         |         S )zkget default socket options for new sockets created by this Context

        .. versionadded:: 13.0
        r   )r-   r   s     r    
getsockoptzContext.getsockoptv  s    
 }S!!r   namec                    |t           j        v r|                     ||          S |t          j        v r|| j        |<   dS t          d|           )z"set default sockopts as attributesz"No such context or socket option: N)r   r   setr   r'   r?   )r-   r   r   r   s       r    _set_attr_optzContext._set_attr_opt}  s[    =,,,88C'''\---!&DM# !Ld!L!LMMMr   c                    |t           j        v r|                     |          S || j        vrt	          |          | j        |         S )z"get default sockopts as attributes)r   r   rZ   r'   r?   )r-   r   r   s      r    _get_attr_optzContext._get_attr_opt  sH    =,,,88C== $-''$T***}S))r   keyc                $   || j         v r| j                             |           dS |                                }	 t          t          |          }|| j        vrt          |          | j        |= dS # t          $ r t          d|          w xY w)z%delete default sockopts as attributesNzNo such socket option: )rY   popupperrK   r   r'   r?   )r-   r   r   s      r    __delattr__zContext.__delattr__  s    $-Mc"""Fiikk	',,,C $-''$S)))M#&&&  	D 	D 	D !B3!B!BCCC	Ds   A2 2B)r   )r-   r.   r/   r0   )r-   r.   r/   r   )r-   r.   r6   r7   )r   r   )r-   r.   r/   r9   r6   r7   r   r   r   r   )r   rP   )r-   r"   r   r"   )rf   r   rg   r   rh   r   r   r   r2   )r-   r"   rk   r   r   r"   )r`   rn   ro   rp   r   r"   )r`   rn   rs   r   r   r"   )r`   rn   r/   r0   r   r"   )r   r   )r   r   r   r   )r   r)   r   r   )
r-   r"   r   r0   r   r   r   r   r   r$   )r   r0   r   r   r   r   )r   r0   r   r   )r   rP   r   r0   r   r   r   r   )r   rP   r   r0   r   r   )r   rP   r   r   )'r\   r[   __qualname____doc____annotations__r(   r   r|   r*   r<   r=   rJ   r   r,   r	   r4   rO   rR   rc   re   rj   rm   __deepcopy__classmethodr6   rx   r}   r   r   r   r   rM   r   r   r   r   r   r   __classcell__)rB   s   @r    r   r   (   s         : ITVVN $M$$$$GK'-M----==== X=@@@ X@BBB XB %& !" " " " " " "8   0 IF F F F       
6 6 6 6 6 L# # # [# # # # [#  " " " " ["H     6   " " " "* * * *! ! ! ! !L KO) ) ) ) )V# # # #" " " "N N N N* * * *' ' ' ' ' ' ' 'r   r   r.   r   )-r   
__future__r   atexitrz   	threadingr   typingr   r   r   r   r	   warningsr
   weakrefr   zmqzmq._typingr   zmq.backendr   ContextBasezmq.constantsr   r   r   	zmq.errorr   rw   r   	attrsettrr   r   r   r   r   r   r!   registerr"   r$   r.   r   __all__r   r   r    <module>r      s     
 # " " " " "  				       < < < < < < < < < < < < < <             



 ! ! ! ! ! ! . . . . . . < < < < < < < < < <       + + + + + + / / / / / / / / & & & & & & & &    
    w~Y777gm8tDDDv' v' v' v' v'k?GK,@ v' v' v'r !, , , , , m
$r   