o
    HXhx1                     @   s   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ZeeZ	G dd dZ
G dd dZG d	d
 d
e jZedkrCe   dS dS )    N)
gc_collect)import_helper)assert_python_okc                   @   s   e Zd Zedd ZdS )
FullLoaderc                 C   s   d |jS )Nz<module '{}' (crafted)>)format__name__)clsm r
   :/opt/python-3.10.19/usr/lib/python3.10/test/test_module.pymodule_repr   s   zFullLoader.module_reprN)r   
__module____qualname__classmethodr   r
   r
   r
   r   r      s    r   c                   @   s   e Zd ZdS )
BareLoaderN)r   r   r   r
   r
   r
   r   r      s    r   c                   @   s  e Z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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'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dES )FModuleTestsc                 C   sl   t t }| |jd u  | tt| z|j}| dt	|  W n	 t
y+   Y nw | |jt j d S )Nz__name__ = %s)
ModuleType__new__
assertTrue__dict__assertRaises	TypeErrordirr   ZfailreprAttributeErrorassertEqual__doc__)selffoosr
   r
   r   test_uninitialized   s   
zModuleTests.test_uninitializedc                 C   s    t t }| tdt|d d S )Nz"module has no attribute 'not_here'not_here)r   r   assertRaisesRegexr   getattrr   r   r
   r
   r   "test_uninitialized_missing_getattr"   s
   
z.ModuleTests.test_uninitialized_missing_getattrc                 C   s   t d}| tdt|d d S )Nr   z(module 'foo' has no attribute 'not_here'r!   )r   r"   r   r#   r$   r
   r
   r   test_missing_getattr*   s
   z ModuleTests.test_missing_getattrc              	   C   sl   t d}| |jd | |jd  | |jd  | |jd  | |jd  | |jdd d d d d d S )Nr   r   r   
__loader____package____spec__)	r   r   r   r   assertIsr(   r)   r*   r   r$   r
   r
   r   test_no_docstring2   s   zModuleTests.test_no_docstringc              	   C   D   t dd}| |jd | |jd | |jddd d d d d S )Nr   foodocr'   r   r   r   r   r   r$   r
   r
   r   test_ascii_docstring>      
z ModuleTests.test_ascii_docstringc              	   C   r-   )Nr   	   foodocሴr'   r/   r$   r
   r
   r   test_unicode_docstringH   r1   z"ModuleTests.test_unicode_docstringc              
   C   s|   t dd}d|_|j}|dd | |jd | |jd | |jd | |jdddd d d d | |j|u  d S )Nr   r2   *   r.   )r   r   barr(   r)   r*   )r   r5   r   __init__r   r   r   r   )r   r   dr
   r
   r   test_reinitR   s   
zModuleTests.test_reinitc                 C   s&   dd }t   | | jd d d S )Nc                  S   s   t d} d| _| S )Nr      )r   r5   )r   r
   r
   r   fb   s   z+ModuleTests.test_dont_clear_dict.<locals>.fr5   r9   )r   r   r   )r   r:   r
   r
   r   test_dont_clear_dict`   s   z ModuleTests.test_dont_clear_dictc                 C   s<   g }t d}||_d}t||j ~t  | |dg d S )Nr   zwclass A:
    def __init__(self, l):
        self.l = l
    def __del__(self):
        self.l.append(1)
a = A(destroyed)   )r   	destroyedexecr   r   r   )r   r=   r	   r   r
   r
   r   test_clear_dict_in_ref_cyclei   s   z(ModuleTests.test_clear_dict_in_ref_cyclec                 C   s:   t d}t|}| | | ~t  | | d  d S Nr   )r   weakrefrefr+   r   )r   r	   wrr
   r
   r   test_weakrefx   s   
zModuleTests.test_weakrefc                 C   s   dd l m} ddl m} | |d | |jd | |jd | td |j W d    n1 s4w   Y  | |j	d t
jd= d S )	Nr   )testzThere is testr<      z Deprecated, use whatever insteadzThere is whatevertest.good_getattr)test.good_getattrgood_getattrrE   r   xyr"   r   ZyoloZwhateversysmodules)r   ggarE   r
   r
   r   test_module_getattr   s   zModuleTests.test_module_getattrc                 C   s   dd l m} ddlm} | |jd | |jd | t |j W d    n1 s-w   Y  | t |j W d    n1 sEw   Y  t	j
d= dt	j
v rYt	j
d= d S d S )Nr   bad_getattr2r<   test.bad_getattrtest.bad_getattr2)test.bad_getattrbad_getattrrE   rQ   r   rJ   r   r   ZnoperL   rM   r   ZbgarQ   r
   r
   r   test_module_getattr_errors   s   
z&ModuleTests.test_module_getattr_errorsc                 C   s,   dd l m} | t|g d tjd= d S )Nr   )abcrG   )rH   rI   r   r   rL   rM   )r   rN   r
   r
   r   test_module_dir   s   zModuleTests.test_module_dirc                 C   s   dd l m} ddlm} | t t| W d    n1 s w   Y  | t t| W d    n1 s9w   Y  tjd= dtjv rMtjd= d S d S )Nr   rP   rR   rS   )	rT   rU   rE   rQ   r   r   r   rL   rM   rV   r
   r
   r   test_module_dir_errors   s   


z"ModuleTests.test_module_dir_errorsc                 C   s   ddl m} | t |j W d    n1 sw   Y  | t |j W d    n1 s1w   Y  dtjv rAtjd= d S d S )Nr   )bad_getattr3ztest.bad_getattr3)rE   r]   r   r   ZoneZ
delgetattrrL   rM   )r   r]   r
   r
   r   test_module_getattr_tricky   s   
z&ModuleTests.test_module_getattr_trickyc                 C   s    t d}|`| t|d d S )Nr   z<module '?'>)r   r   r   r   r   r	   r
   r
   r   test_module_repr_minimal   s   z$ModuleTests.test_module_repr_minimalc                 C   s   t d}| t|d d S Nr   z<module 'foo'>)r   r   r   r_   r
   r
   r   test_module_repr_with_name   s   z&ModuleTests.test_module_repr_with_namec                 C   s"   t d}d|_| t|d d S Nr   /tmp/foo.pyz!<module 'foo' from '/tmp/foo.py'>)r   __file__r   r   r_   r
   r
   r   'test_module_repr_with_name_and_filename   s   z3ModuleTests.test_module_repr_with_name_and_filenamec                 C   s&   t d}|`d|_| t|d d S )Nr   rd   z<module '?' from '/tmp/foo.py'>)r   r   re   r   r   r_   r
   r
   r   #test_module_repr_with_filename_only   s   z/ModuleTests.test_module_repr_with_filename_onlyc                 C   s*   t d}|jd u sJ | t|d d S ra   )r   r(   r   r   r_   r
   r
   r   $test_module_repr_with_loader_as_None   s   z0ModuleTests.test_module_repr_with_loader_as_Nonec                 C   4   t d}|`t|_tt}| t|d| d S Nr   z<module '?' ({})>)r   r   r   r(   r   r   r   r   r	   Zloader_reprr
   r
   r   -test_module_repr_with_bare_loader_but_no_name   s   z9ModuleTests.test_module_repr_with_bare_loader_but_no_namec                 C   ri   rj   )r   r   r   r(   r   r   r   rk   r
   r
   r   -test_module_repr_with_full_loader_but_no_name   s   z9ModuleTests.test_module_repr_with_full_loader_but_no_namec                 C   s0   t d}t|_tt}| t|d| d S )Nr   z<module 'foo' ({})>)r   r   r(   r   r   r   )r   r	   r   r
   r
   r   !test_module_repr_with_bare_loader   s   z-ModuleTests.test_module_repr_with_bare_loaderc                 C   s"   t d}t|_| t|d d S )Nr   <module 'foo' (crafted)>)r   r   r(   r   r   r_   r
   r
   r   !test_module_repr_with_full_loader   s
   z-ModuleTests.test_module_repr_with_full_loaderc                 C   (   t d}t|_d|_| t|d d S rc   )r   r   r(   re   r   r   r_   r
   r
   r   .test_module_repr_with_bare_loader_and_filename      z:ModuleTests.test_module_repr_with_bare_loader_and_filenamec                 C   rq   )Nr   rd   ro   )r   r   r(   re   r   r   r_   r
   r
   r   .test_module_repr_with_full_loader_and_filename   rs   z:ModuleTests.test_module_repr_with_full_loader_and_filenamec                 C   s   |  ttd d S )Nz<module 'sys' (built-in)>)r   r   rL   )r   r
   r
   r   test_module_repr_builtin  s   z$ModuleTests.test_module_repr_builtinc                 C   sZ   t t}d}d}| |d t| |d|| | |t| d  |d|| d S )Nz<module 'unittest' from 'z__init__.py'>z{!r} does not start with {!r}z{!r} does not end with {!r})r   unittestr   lenr   )r   rZstarts_withZ	ends_withr
   r
   r   test_module_repr_source  s   

z#ModuleTests.test_module_repr_sourcec                 C   s:   t dd\}}}| | | }| t|h d d S )Nz-czfrom test import final_a>   s   final_b.x = bs   shutil.rmtree = rmtrees   final_a.x = as   x = bs	   len = lens   x = a)r   assertFalse
splitlinesr   set)r   rcouterrlinesr
   r
   r   $test_module_finalization_at_shutdown  s   
z0ModuleTests.test_module_finalization_at_shutdownc                    s:   G dd d G  fdddt }| tt|dd d S )Nc                   @   s   e Zd Zdd ZdS )z;ModuleTests.test_descriptor_errors_propagate.<locals>.Descrc                 S   s   t )N)RuntimeError)r   otr
   r
   r   __get__  s   zCModuleTests.test_descriptor_errors_propagate.<locals>.Descr.__get__N)r   r   r   r   r
   r
   r
   r   Descr  s    r   c                       s   e Zd Z  ZdS )z7ModuleTests.test_descriptor_errors_propagate.<locals>.MN)r   r   r   melonr
   r   r
   r   M  s    
r   Zmymodr   )r   r   r   r#   )r   r   r
   r   r    test_descriptor_errors_propagate  s   z,ModuleTests.test_descriptor_errors_propagatec                 C   sv   t d}tdD ]0}| d|jv  |j}| d|jv  | |j| | |jd | |d r4|`q|jd= qd S )Nr   r9   __annotations__rF   )r   rangerz   r   r   r   r   r   r   ir7   r
   r
   r   test_lazy_create_annotations"  s   
z(ModuleTests.test_lazy_create_annotationsc                 C   s~   t d}tdD ]4}| d|jv  dti}||_| d|jv  | |j| | |jd | |d r8|`q|jd= qd S )Nr   r9   r   rX   rF   )r   r   rz   r   intr   r   r   r   r
   r
   r   test_setting_annotations2  s   
z$ModuleTests.test_setting_annotationsc                 C   s   t t }| t t|j W d    n1 sw   Y  | t i |_W d    n1 s2w   Y  | t
 |`W d    n1 sIw   Y  t d}i |_|`| t |`W d    d S 1 sjw   Y  d S r@   )r   r   r   r   printr   r   r$   r
   r
   r   test_annotations_getset_raises@  s    
"z*ModuleTests.test_annotations_getset_raisesc                 C   s2   t d}| d|jv  |`| d|jv  d S )Nztest.ann_module4r   )r   Zimport_fresh_moduler   r   r   rz   )r   Zann_module4r
   r
   r   &test_annotations_are_created_correctlyQ  s   
z2ModuleTests.test_annotations_are_created_correctlyN)%r   r   r   r    r%   r&   r,   r0   r3   r8   r;   r?   rD   rO   rW   r[   r\   r^   r`   rb   rf   rg   rh   rl   rm   rn   rp   rr   rt   ru   ry   r   r   r   r   r   r   r
   r
   r
   r   r      sF    

	
		r   __main__)rv   rA   Ztest.supportr   r   Ztest.support.script_helperr   rL   typer   r   r   ZTestCaser   r   mainr
   r
   r
   r   <module>   s     I