B
    u9a5)  ã               @   s~   d dl Z d dlZd dlmZ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rze  ¡  dS )é    N)Ú
gc_collectÚrequires_type_collecting)Ú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
   ú!/usr/lib/python3.7/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
e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ed9d:„ ƒZ d;d<„ Z!d=S )>ÚModuleTestsc             C   sn   t  t ¡}|  |jd k¡ |  tt|¡ y|j}|  dt	|ƒ ¡ W n t
k
rX   Y nX |  |jt j¡ d S )Nz__name__ = %s)Ú
ModuleTypeÚ__new__Ú
assertTrueÚ__dict__ÚassertRaisesÚSystemErrorÚ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
   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
   r   Útest_no_docstring1   s    zModuleTests.test_no_docstringc          	   C   sD   t ddƒ}|  |jd¡ |  |jd¡ |  |jddd d d dœ¡ d S )Nr   Úfoodoc)r   r   r&   r'   r(   )r   r   r   r   r   )r   r   r
   r
   r   Útest_ascii_docstring=   s    
z ModuleTests.test_ascii_docstringc          	   C   sD   t ddƒ}|  |jd¡ |  |jd¡ |  |jddd d d dœ¡ d S )Nr   u	   foodocáˆ´)r   r   r&   r'   r(   )r   r   r   r   r   )r   r   r
   r
   r   Útest_unicode_docstringG   s    
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|k¡ d S )Nr   u	   foodocáˆ´é*   r+   )r   r   Úbarr&   r'   r(   )r   r/   r   Ú__init__r   r   r   r   )r   r   Údr
   r
   r   Útest_reinitQ   s    
zModuleTests.test_reinitc             C   s&   dd„ }t ƒ  |  |ƒ jd d¡ d S )Nc              S   s   t dƒ} d| _| S )Nr   é   )r   r/   )r   r
   r
   r   Úfa   s    z+ModuleTests.test_dont_clear_dict.<locals>.fr/   r3   )r   r   r   )r   r4   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   r7   r	   r   r
   r
   r   Útest_clear_dict_in_ref_cycleh   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	   Zwrr
   r
   r   Útest_weakrefx   s    
zModuleTests.test_weakrefc          	   C   sx   dd l m} ddl m} |  |d¡ |  |jd¡ |  |jd¡ |  td¡ |j W d Q R X |  |j	d¡ t
jd= d S )	Nr   )ÚtestzThere is testr6   é   z Deprecated, use whatever insteadzThere is whateverztest.good_getattr)Útest.good_getattrÚgood_getattrr=   r   ÚxÚyr"   r   ZyoloZwhateverÚsysÚmodules)r   Úggar=   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 Q R X |  t¡ |j W d Q R X t	j
d= dt	j
kr†t	j
d= d S )Nr   )Úbad_getattr2r6   ztest.bad_getattrztest.bad_getattr2)Útest.bad_getattrÚbad_getattrr=   rG   r   rA   r   Ú	TypeErrorZnoperC   rD   )r   ÚbgarG   r
   r
   r   Útest_module_getattr_errorsŒ   s    
z&ModuleTests.test_module_getattr_errorsc             C   s.   dd l m} |  t|ƒdddg¡ tjd= d S )Nr   ÚaÚbÚcztest.good_getattr)r?   r@   r   r   rC   rD   )r   rE   r
   r
   r   Útest_module_dir™   s    zModuleTests.test_module_dirc          	   C   sr   dd l m} ddlm} |  t¡ t|ƒ W d Q R X |  t¡ t|ƒ W d Q R X tjd= dtjkrntjd= d S )Nr   )rG   ztest.bad_getattrztest.bad_getattr2)	rH   rI   r=   rG   r   rJ   r   rC   rD   )r   rK   rG   r
   r
   r   Útest_module_dir_errorsž   s    
z"ModuleTests.test_module_dir_errorsc          	   C   sZ   ddl m} |  t¡ |j W d Q R X |  t¡ |j W d Q R X dtjkrVtjd= d S )Nr   )Úbad_getattr3ztest.bad_getattr3)r=   rR   r   r   ZoneZ
delgetattrrC   rD   )r   rR   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
   r   Útest_module_repr_with_name¹   s    z&ModuleTests.test_module_repr_with_namec             C   s"   t dƒ}d|_|  t|ƒd¡ d S )Nr   z/tmp/foo.pyz!<module 'foo' from '/tmp/foo.py'>)r   Ú__file__r   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   z/tmp/foo.pyz<module '?' from '/tmp/foo.py'>)r   r   rV   r   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 kst‚|  t|ƒd¡ d S )Nr   z<module 'foo'>)r   r&   ÚAssertionErrorr   r   )r   r	   r
   r
   r   Ú$test_module_repr_with_loader_as_NoneÈ   s    z0ModuleTests.test_module_repr_with_loader_as_Nonec             C   s4   t dƒ}|`t|_ttƒ}|  t|ƒd |¡¡ d S )Nr   z<module '?' ({})>)r   r   r   r&   r   r   r   )r   r	   Ú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   s4   t dƒ}|`t|_ttƒ}|  t|ƒd |¡¡ d S )Nr   z<module '?' ({})>)r   r   r   r&   r   r   r   )r   r	   r[   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   z<module 'foo' (crafted)>)r   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   s(   t dƒ}t|_d|_|  t|ƒd¡ d S )Nr   z/tmp/foo.pyz!<module 'foo' from '/tmp/foo.py'>)r   r   r&   rV   r   r   )r   r	   r
   r
   r   Ú.test_module_repr_with_bare_loader_and_filenameñ   s    z:ModuleTests.test_module_repr_with_bare_loader_and_filenamec             C   s(   t dƒ}t|_d|_|  t|ƒd¡ d S )Nr   z/tmp/foo.pyz<module 'foo' (crafted)>)r   r   r&   rV   r   r   )r   r	   r
   r
   r   Ú.test_module_repr_with_full_loader_and_filenameù   s    z:ModuleTests.test_module_repr_with_full_loader_and_filenamec             C   s   |   ttƒd¡ d S )Nz<module 'sys' (built-in)>)r   r   rC   )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   sB   t ddƒ\}}}|  |¡ | ¡ }|  t|ƒddddddh¡ d S )	Nz-czfrom test import final_as   x = as   x = bs   final_a.x = as   final_b.x = bs	   len = lens   shutil.rmtree = rmtree)r   ZassertFalseÚ
splitlinesr   Úset)r   Z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 ‚d S )N)ÚRuntimeError)r   ÚoÚtr
   r
   r   Ú__get__  s    zCModuleTests.test_descriptor_errors_propagate.<locals>.Descr.__get__N)r   r   r   rp   r
   r
   r
   r   ÚDescr  s   rq   c                   s   e Zd Z” ƒ ZdS )z7ModuleTests.test_descriptor_errors_propagate.<locals>.MN)r   r   r   Úmelonr
   )rq   r
   r   ÚM  s   rs   Zmymodrr   )r   r   rm   r#   )r   rs   r
   )rq   r   Ú test_descriptor_errors_propagate  s    z,ModuleTests.test_descriptor_errors_propagateN)"r   r   r   r    r$   r%   r*   r,   r-   r2   r5   r   r9   r<   rF   rL   rP   rQ   rS   rT   rU   rW   rX   rZ   r\   r]   r^   r_   r`   ra   rb   rf   rl   rt   r
   r
   r
   r   r      s<   

	
		r   Ú__main__)rc   r:   Ztest.supportr   r   Ztest.support.script_helperr   rC   Útyper   r   r   ZTestCaser   r   Úmainr
   r
   r
   r   Ú<module>   s     