
    \d                     l    d Z ddlZddlmZ ddlmZ  G d d          Zd Zd Z G d	 d
e          Z	dS )zt
Tests for miscellaneous behaviors of the top-level L{twisted} package (ie, for
the code in C{twisted/__init__.py}.
    N)
ModuleType)TestCasec                   *    e Zd ZdZd Zd Zd Zd ZdS )SetAsideModulez
    L{SetAsideModule} is a context manager for temporarily removing a module
    from C{sys.modules}.

    @ivar name: The name of the module to remove.
    c                     || _         d S N)name)selfr	   s     9lib/python3.11/site-packages/twisted/test/test_twisted.py__init__zSetAsideModule.__init__   s    			    c                       fdt          t          j                                                  D             }|D ]}t          j        |= |S )z
        Find the given module and all of its hierarchically inferior modules in
        C{sys.modules}, remove them from it, and return whatever was found.
        c                 l    i | ]0\  }}|j         k    s|                    j         d z             -||1S ).)r	   
startswith).0
moduleNamemoduler
   s      r   
<dictcomp>z,SetAsideModule._unimport.<locals>.<dictcomp>"   sO     
 
 
$Vdi'':+@+@S+Q+Q' '''r   )listsysmodulesitems)r
   r	   r   s   `  r   	_unimportzSetAsideModule._unimport   sd    

 
 
 
(,S[->->-@-@(A(A
 
 

  	" 	"DD!!r   c                 D    |                      | j                  | _        d S r   )r   r	   r   )r
   s    r   	__enter__zSetAsideModule.__enter__+   s    ~~di00r   c                     |                      | j                   t          j                            | j                   d S r   )r   r	   r   r   update)r
   excTypeexcValue	tracebacks       r   __exit__zSetAsideModule.__exit__.   s4    ty!!!4<(((((r   N)__name__
__module____qualname____doc__r   r   r   r"    r   r   r   r      sZ             1 1 1) ) ) ) )r   r   c                 j    i }t          d| |           t          j                            |           dS )a  
    Take a mapping defining a package and turn it into real C{ModuleType}
    instances in C{sys.modules}.

    Consider these example::

        a = {"foo": "bar"}
        b = {"twisted": {"__version__": "42.6"}}
        c = {"twisted": {"plugin": {"getPlugins": stub}}}

    C{_install(a)} will place an item into C{sys.modules} with C{"foo"} as the
    key and C{"bar" as the value.

    C{_install(b)} will place an item into C{sys.modules} with C{"twisted"} as
    the key.  The value will be a new module object.  The module will have a
    C{"__version__"} attribute with C{"42.6"} as the value.

    C{_install(c)} will place an item into C{sys.modules} with C{"twisted"} as
    the key.  The value will be a new module object with a C{"plugin"}
    attribute.  An item will also be placed into C{sys.modules} with the key
    C{"twisted.plugin"} which refers to that module object.  That module will
    have an attribute C{"getPlugins"} with a value of C{stub}.

    @param modules: A mapping from names to definitions of modules.  The names
        are native strings like C{"twisted"} or C{"unittest"}.  Values may be
        arbitrary objects.  Any value which is not a dictionary will be added to
        C{sys.modules} unmodified.  Any dictionary value indicates the value is
        a new module and its items define the attributes of that module.  The
        definition of this structure is recursive, so a value in the dictionary
        may be a dictionary to trigger another level of processing.

    @return: L{None}
    N)_makePackagesr   r   r   )r   results     r   _installr+   3   s8    D F$(((Kvr   c                    i }t          |                                          D ]\  }}| Yt          |t                    r>t	          |          }|j                            t          |||                     |||<   Z|||<   `t          |t                    rYt	          | j        dz   |z             }|j                            t          |||                     ||| j        dz   |z   <   |||<   |||<   |S )a  
    Construct module objects (for either modules or packages).

    @param parent: L{None} or a module object which is the Python package
        containing all of the modules being created by this function call.  Its
        name will be prepended to the name of all created modules.

    @param attributes: A mapping giving the attributes of the particular module
        object this call is creating.

    @param result: A mapping which is populated with all created module names.
        This is suitable for use in updating C{sys.modules}.

    @return: A mapping of all of the attributes created by this call.  This is
        suitable for populating the dictionary of C{parent}.

    @see: L{_install}.
    Nr   )	r   r   
isinstancedictr   __dict__r   r)   r#   )parent
attributesr*   attrsr	   valuer   s          r   r)   r)   Z   s
   & Ej..0011 $ $u>%&& %#D))&&}VUF'K'KLLL%t$t%&& $#FOc$9D$@AA&&}VUF'K'KLLL7=v,t34$d#dLr   c                   $    e Zd ZdZd Zd Zd ZdS )MakePackagesTestszh
    Tests for L{_makePackages}, a helper for populating C{sys.modules} with
    fictional modules.
    c                     i }t          dt          d          |           |                     |t          d                     dS )z
        A non-C{dict} value in the attributes dictionary passed to L{_makePackages}
        is preserved unchanged in the return value.
        Nreactor)r7   )r)   r.   assertEqualr
   r   s     r   test_nonModulez MakePackagesTests.test_nonModule   sM    
 dD333W===$y"9"9"9:::::r   c                 `   i }t          dt          t          d                    |           |                     |t                     |                     |d         t                     |                     d|d         j                   |                     d|d         j                   dS )z
        A C{dict} value in the attributes dictionary passed to L{_makePackages}
        is turned into a L{ModuleType} instance with attributes populated from
        the items of that C{dict} value.
        N123versiontwistedr@   )r)   r.   assertIsInstancer   r8   r#   r>   r9   s     r   test_moduleWithAttributez*MakePackagesTests.test_moduleWithAttribute   s     dDe)<)<)<===wGGGgt,,,gi0*===GI$6$?@@@	 2 :;;;;;r   c           
         i }t          dt          t          t          d                              |           |                     |t                     |                     |d         t                     |                     d|d         j                   |                     |d         j        t                     |                     d|d         j        j                   |                     d|d         j        j                   dS )z
        Processing of the attributes dictionary is recursive, so a C{dict} value
        it contains may itself contain a C{dict} value to the same effect.
        N321r=   )webr?   r@   ztwisted.web)r)   r.   rA   r   r8   r#   rE   r>   r9   s     r   test_packageWithModulez(MakePackagesTests.test_packageWithModule   s    
 dD$u2E2E2E)F)F)FGGGQQQgt,,,gi0*===GI$6$?@@@gi04jAAA	(:(>(GHHH	 2 6 >?????r   N)r#   r$   r%   r&   r:   rB   rF   r'   r   r   r5   r5      sP         
; ; ;< < <@ @ @ @ @r   r5   )
r&   r   typesr   twisted.trial.unittestr   r   r+   r)   r5   r'   r   r   <module>rI      s     


       + + + + + +
) ) ) ) ) ) ) )B$ $ $N$ $ $N(@ (@ (@ (@ (@ (@ (@ (@ (@ (@r   