o
    gA                     @   s   d dl Z d dl mZ d dl mZ d dlZd dlZd dlZddlmZ G dd dZG dd dej	Z
G d	d
 d
ejejZG dd dej	ZedkrPe  dS dS )    N)abc)util   c                   @      e Zd Zdd Zdd ZdS )CollectInitc                 O   s   || _ || _d S N)argskwargs)selfr   r	    r   S/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/test/test_importlib/test_lazy.py__init__   s   
zCollectInit.__init__c                 C   s   | S r   r   r
   moduler   r   r   exec_module   s   zCollectInit.exec_moduleN)__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   r   )LazyLoaderFactoryTestsc                 C   sD   t jt}|dddd}|j}| d|j | ddi|j d S )Nmodule namemodule pathkw)r   )r   r   )r   
LazyLoaderfactoryr   loaderassertEqualr   r	   )r
   r   Zlazy_loaderr   r   r   r   	test_init   s
   z LazyLoaderFactoryTests.test_initc                 C   s<   |  t tjt W d    d S 1 sw   Y  d S r   )assertRaises	TypeErrorr   r   r   objectr
   r   r   r   test_validation    s   "z&LazyLoaderFactoryTests.test_validationN)r   r   r   r   r!   r   r   r   r   r      s    	r   c                   @   s4   e Zd ZdZdZdZdeZd	ddZdd Z	dS )
TestingImporterZlazy_loader_testchangedNzattr = 42; __name__ = {!r}c                 C   s    || j krd S t|t| S r   )module_namer   spec_from_loaderr   )r
   namepathtargetr   r   r   	find_spec-   s   
zTestingImporter.find_specc                 C   s   t | j|j || _d S r   )execsource_code__dict__loadedr   r   r   r   r   2   s   
zTestingImporter.exec_moduler   )
r   r   r   r$   mutated_namer-   formatr+   r)   r   r   r   r   r   r"   &   s    

r"   c                   @   sf   e Zd Zdd Zd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dd ZdS )LazyLoaderTestsc                 C   s:   |  t tt W d    d S 1 sw   Y  d S r   )r   r   r   r   r   r    r   r   r   r   9   s   "zLazyLoaderTests.test_initNc                 C   sr   t  }|d ur
||_tt jt|}|j|}|d u r$t	t j}||_
|j|_|j| | |j |S r   )r"   r+   r   r%   r$   r   r   create_moduletypes
ModuleType__spec__
__loader__r   assertIsNoner-   )r
   r+   r   specr   r   r   r   
new_module>   s   zLazyLoaderTests.new_modulec              	   C   s   t  }|jd u s
J t|j% tj|gd t|j}W d    n1 s)w   Y  W d    n1 s8w   Y  | |j | 	|j
| | |j | 	||j d S )N)	meta_path)r"   r-   	test_utiluncacher$   Zimport_state	importlibimport_moduler6   r   r5   ZassertIsNotNone)r
   importerr   r   r   r   test_e2eN   s   zLazyLoaderTests.test_e2ec                 C   s   |   }| tj|j d S r   )r8   r   r"   r.   r   r   r   r   r   test_attr_unchanged[   s   z#LazyLoaderTests.test_attr_unchangedc                 C       |   }d|_| d|j d S )N*   )r8   Znew_attrr   r   r   r   r   test_new_attra   s   zLazyLoaderTests.test_new_attrc                 C   rA   )NZbogus)r8   r   r   r   r   r   r   test_mutated_preexisting_attrg      z-LazyLoaderTests.test_mutated_preexisting_attrc                 C   rA   )N   )r8   attrr   r   r   r   r   test_mutated_attrn   rE   z!LazyLoaderTests.test_mutated_attrc                 C       |   }|`| t|d d S )NrG   )r8   rG   assertFalsehasattrr   r   r   r   test_delete_eventual_attru   s   z)LazyLoaderTests.test_delete_eventual_attrc                 C   rI   )Nr   )r8   r   rJ   rK   r   r   r   r   test_delete_preexisting_attr{   s   z,LazyLoaderTests.test_delete_preexisting_attrc              	   C   s   t tj: ttj}|tjtj< |  }| 	t
d |j W d    n1 s+w   Y  W d    d S W d    d S 1 sCw   Y  d S )NZsubstituted)r:   r;   r"   r$   r2   r3   sysmodulesr8   ZassertRaisesRegex
ValueErrorr   )r
   Zfresh_moduler   r   r   r   test_module_substitution_error   s   "z.LazyLoaderTests.test_module_substitution_errorc                 C   sL   t tj |  }|tjtj< |j W d    d S 1 sw   Y  d S r   )r:   r;   r"   r$   r8   rN   rO   r   r   r   r   r   test_module_already_in_sys   s
   "z*LazyLoaderTests.test_module_already_in_sysr   )r   r   r   r   r8   r?   r@   rC   rD   rH   rL   rM   rQ   rR   r   r   r   r   r0   7   s    
r0   __main__)r<   r   r   rN   r2   Zunittest r:   r   ZTestCaser   ZMetaPathFinderLoaderr"   r0   r   mainr   r   r   r   <module>   s    
Y