B
    u9asF  ã               @   sà   d dl mZmZmZmZ d dlZd dlZd dlZd dlm	Z	 d dl
Z
d dlZd dlZd dlZd dlZd dlZG dd„ dejƒZG dd„ dejƒZG dd	„ d	ejƒZG d
d„ dejƒZG dd„ dejƒZdd„ ZedkrÜeƒ  dS )é    )Úrun_unittestÚunloadÚcheck_warningsÚCleanImportN)Úspec_from_file_locationc               @   sL   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S )ÚPkgutilTestsc             C   s.   t  ¡ | _|  tj| j¡ tj d| j¡ d S )Nr   )	ÚtempfileÚmkdtempÚdirnameÚ
addCleanupÚshutilÚrmtreeÚsysÚpathÚinsert)Úself© r   ú"/usr/lib/python3.7/test_pkgutil.pyÚsetUp   s    
zPkgutilTests.setUpc             C   s   t jd= d S )Nr   )r   r   )r   r   r   r   ÚtearDown   s    zPkgutilTests.tearDownc             C   sÜ   d}d}t j | j|¡}t  |¡ tt j |d¡dƒ}| ¡  tt j |d¡dƒ}| |¡ | ¡  t  t j |d¡¡ tt j |dd¡dƒ}| |¡ | ¡  t 	|d¡}|  
||¡ t 	|d¡}|  
||¡ tj|= d S )NÚtest_getdata_filesyss%   Hello, world!
Second line
Third linez__init__.pyÚwbzres.txtÚsubzsub/res.txt)Úosr   Újoinr
   ÚmkdirÚopenÚcloseÚwriteÚpkgutilÚget_dataÚassertEqualr   Úmodules)r   ÚpkgÚRESOURCE_DATAZpackage_dirÚfÚres1Úres2r   r   r   r      s$    


z!PkgutilTests.test_getdata_filesysc       
      C   sô   d}d}d}t j | j|¡}t |d¡}| |d d¡ | |d |¡ | |d |¡ | ¡  tj 	d	|¡ t
 |d
¡}|  ||¡ t
 |d¡}|  ||¡ g }x.t
 |g¡D ]}	|  |	t
j¡ | |	j¡ q°W |  |dg¡ tjd	= tj|= d S )Nztest_getdata_zipfile.zipÚtest_getdata_zipfiles%   Hello, world!
Second line
Third lineÚwz/__init__.pyÚ z/res.txtz/sub/res.txtr   zres.txtzsub/res.txt)r   r   r   r
   ÚzipfileÚZipFileÚwritestrr   r   r   r   r    r!   Ziter_modulesÚassertIsInstanceZ
ModuleInfoÚappendÚnamer"   )
r   Úzipr#   r$   Úzip_fileÚzr&   r'   ÚnamesZ
moduleinfor   r   r   r(   :   s*    z!PkgutilTests.test_getdata_zipfilec             C   sV   d}t j | j|¡}t  |d¡ |  t j|¡ x"tj| jgdD ]}|  	d¡ q@W d S )NZunreadable_packager   )r   zunexpected package found)
r   r   r   r
   r   r   Úrmdirr   Úwalk_packagesZfail)r   Zpackage_nameÚdÚtr   r   r   Útest_unreadable_dir_on_syspath]   s    z+PkgutilTests.test_unreadable_dir_on_syspathc       	      C   st  d}t j | j|¡}t  |¡ tt j |d¡dƒ}| ¡  t  t j |d¡¡ tt j |dd¡dƒ}| ¡  tt j |dd¡dƒ}| ¡  d}t j | j|¡}t  |¡ tt j |d¡dƒ}| ¡  t  t j |d¡¡ tt j |dd¡dƒ}| ¡  tt j |dd¡dƒ}| ¡  dddddd	g}d
d„ t | jg¡D ƒ}|  	||¡ x&|D ]}| 
d¡rbqNtj|= qNW d S )NÚtest_walkpackages_filesysz__init__.pyr   r   zmod.pyzsub.test_walkpackages_filesysz!sub.test_walkpackages_filesys.modztest_walkpackages_filesys.subz!test_walkpackages_filesys.sub.modc             S   s   g | ]}|d  ‘qS )é   r   )Ú.0Úer   r   r   ú
<listcomp>ˆ   s    z:PkgutilTests.test_walkpackages_filesys.<locals>.<listcomp>Úmod)r   r   r   r
   r   r   r   r   r6   r!   Úendswithr   r"   )	r   Úpkg1Zpkg1_dirr%   Úpkg2Zpkg2_dirÚexpectedÚactualr#   r   r   r   r:   h   s@    


z&PkgutilTests.test_walkpackages_filesysc       	      C   s   d}d}d}t j | j|¡}t |d¡}| |d d¡ | |d | d d¡ | |d | d d¡ | |d d¡ | |d | d d¡ | |d | d d¡ | ¡  tj 	d	|¡ dd
ddddg}dd„ t
 |g¡D ƒ}|  ||¡ tjd	= x"|D ]}| d¡rqþtj|= qþW dS )zBTests the same as test_walkpackages_filesys, only with a zip file.ztest_walkpackages_zipfile.zipÚtest_walkpackages_zipfiler   r)   z/__init__.pyr*   ú/z/mod.pyr   zsub.test_walkpackages_zipfilez!sub.test_walkpackages_zipfile.modztest_walkpackages_zipfile.subz!test_walkpackages_zipfile.sub.modc             S   s   g | ]}|d  ‘qS )r;   r   )r<   r=   r   r   r   r>   ª   s    z:PkgutilTests.test_walkpackages_zipfile.<locals>.<listcomp>r?   N)r   r   r   r
   r+   r,   r-   r   r   r   r   r6   r!   r@   r"   )	r   r1   rA   rB   r2   r3   rC   rD   r#   r   r   r   rE      s4    
z&PkgutilTests.test_walkpackages_zipfilec          	   C   s\   d}|   ttf¡ tt |¡ƒ W d Q R X d}|   ttf¡ tt |¡ƒ W d Q R X d S )NZtest_dirs   test_dir)ÚassertRaisesÚ	TypeErrorÚ
ValueErrorÚlistr   r6   )r   Z	str_inputZbytes_inputr   r   r   Ú2test_walk_packages_raises_on_string_or_bytes_input³   s    z?PkgutilTests.test_walk_packages_raises_on_string_or_bytes_inputN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r(   r9   r:   rE   rK   r   r   r   r   r      s   #(#r   c               @   sL   e Zd ZG dd„ deƒZG dd„ deƒZdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )ÚPkgutilPEP302Testsc               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )zPkgutilPEP302Tests.MyTestLoaderc             C   s   d S )Nr   )r   Úspecr   r   r   Úcreate_moduleÁ   s    z-PkgutilPEP302Tests.MyTestLoader.create_modulec             C   s   |j  dd¡d |j d< d S )NÚloadsr   r;   )Ú__dict__Úget)r   r?   r   r   r   Úexec_moduleÄ   s    z+PkgutilPEP302Tests.MyTestLoader.exec_modulec             C   s   dS )NzHello, world!r   )r   r   r   r   r   r    È   s    z(PkgutilPEP302Tests.MyTestLoader.get_dataN)rL   rM   rN   rQ   rU   r    r   r   r   r   ÚMyTestLoaderÀ   s   rV   c               @   s   e Zd Zddd„ZdS )z!PkgutilPEP302Tests.MyTestImporterNc             C   s    t  ¡ }t|d|jj |g dS )Nz<%s>)ÚloaderÚsubmodule_search_locations)rO   rV   r   Ú	__class__rL   )r   Úfullnamer   ÚtargetrW   r   r   r   Ú	find_specÌ   s
    
z+PkgutilPEP302Tests.MyTestImporter.find_spec)NN)rL   rM   rN   r\   r   r   r   r   ÚMyTestImporterË   s   r]   c             C   s   t j d|  ¡ ¡ d S )Nr   )r   Ú	meta_pathr   r]   )r   r   r   r   r   Ó   s    zPkgutilPEP302Tests.setUpc             C   s   t jd= d S )Nr   )r   r^   )r   r   r   r   r   Ö   s    zPkgutilPEP302Tests.tearDownc             C   s    |   t dd¡d¡ tjd= d S )NÚfooÚdummyzHello, world!)r!   r   r    r   r"   )r   r   r   r   Útest_getdata_pep302Ù   s    z&PkgutilPEP302Tests.test_getdata_pep302c             C   sD   dd l }|  |jd¡ |  t dd¡d¡ |  |jd¡ tjd= d S )Nr   r;   r_   r`   zHello, world!)r_   r!   rR   r   r    r   r"   )r   r_   r   r   r   Útest_alreadyloadedÞ   s
    z%PkgutilPEP302Tests.test_alreadyloadedN)
rL   rM   rN   ÚobjectrV   r]   r   r   ra   rb   r   r   r   r   rO   ¾   s   rO   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚExtendPathTestsc          	   C   s\   t  ¡ }tj d|¡ tj ||¡}t |¡ ttj |d¡dƒ}| 	d¡ W d Q R X |S )Nr   z__init__.pyr)   zKfrom pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
)
r   r	   r   r   r   r   r   r   r   r   )r   Úpkgnamer
   ZpkgdirÚflr   r   r   Úcreate_initì   s    
zExtendPathTests.create_initc          	   C   s@   t j |||d ¡}t|dƒ}td |¡|d W d Q R X d S )Nz.pyr)   zvalue={})Úfile)r   r   r   r   ÚprintÚformat)r   r
   re   Zsubmodule_nameÚvalueZmodule_namerf   r   r   r   Úcreate_submodule÷   s    z ExtendPathTests.create_submodulec          	   C   sÖ   d}|   |¡}|   |¡}|  ||dd¡ |  ||dd¡ dd l}dd l}|  |jjd¡ |  |jjd¡ |  t|j	ƒtt
j ||¡t
j ||¡gƒ¡ t |¡ t |¡ tjd= tjd= tjd= tjd= tjd= d S )Nr_   Úbarr   Úbazr;   zfoo.barzfoo.baz)rg   rl   Úfoo.barÚfoo.bazr!   rm   rk   rn   ÚsortedÚ__path__r   r   r   r   r   r   r"   )r   re   Ú	dirname_0Ú	dirname_1r_   r   r   r   Útest_simpleü   s&    



zExtendPathTests.test_simplec             C   sŽ  t j}t j}d}d}|  |¡}tj ||¡}d ||¡}tj	 
|d ¡ tj	 
|d ¡ zô|  |||d¡ t |¡ t||ƒƒ}||ƒ}	x||D ]t}
t  |
|¡}|j}y
|j}W n tk
r¾   Y nX |  |
tjj¡ |  |
|	¡ |  |tjj¡ |  t  |
|¡¡ qŠW |  t¡ t|dƒƒ W d Q R X |  t¡ t|dƒƒ W d Q R X W d t |¡ tjd= ytj	d= tj	d= W n tk
r†   Y nX X d S )NÚspamZeggsz{}.{}r   zinvalid.modulez.spamz	spam.eggs)r   Úiter_importersÚget_importerrg   r   r   r   rj   r   r"   Úpoprl   Ú	importlibÚimport_modulerJ   Ú	_get_specrW   ÚAttributeErrorr.   Ú	machineryÚ
FileFinderr!   ÚSourceFileLoaderÚassertIsNonerG   ÚImportErrorr   r   ÚKeyError)r   rw   rx   re   Úmodnamer
   ZpathitemrZ   Z	importersZexpected_importerÚfinderrP   rW   r   r   r   Útest_iter_importers  sJ    





z#ExtendPathTests.test_iter_importersc          	   C   sì   d}|   |¡}|   |¡}|  ||dd¡ t tj ||d¡¡ |  ||dd¡ dd l}dd l}|  |j	j
d¡ |  |jj
d¡ |  t|jƒttj ||¡tj ||¡gƒ¡ t |¡ t |¡ tjd= tjd= tjd= tjd= tjd= d S )	Nr_   rm   r   z__init__.pyrn   r;   zfoo.barzfoo.baz)rg   rl   r   Úunlinkr   r   ro   rp   r!   rm   rk   rn   rq   rr   r   r   r   r"   )r   re   rs   rt   r_   r   r   r   Útest_mixed_namespaceI  s(    



z$ExtendPathTests.test_mixed_namespaceN)rL   rM   rN   rg   rl   ru   r†   rˆ   r   r   r   r   rd   ë   s
   0rd   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚNestedNamespacePackageTestc             C   s   t  ¡ | _tjd d … | _d S )N)r   r	   Úbasedirr   r   Úold_path)r   r   r   r   r   j  s    
z NestedNamespacePackageTest.setUpc             C   s    | j tjd d …< t | j¡ d S )N)r‹   r   r   r   r   rŠ   )r   r   r   r   r   n  s    z#NestedNamespacePackageTest.tearDownc          	   C   sl   |  dd¡\}}tj | j| dtjj¡¡}tj|dd ttj ||d ¡dƒ}| 	|¡ W d Q R X d S )NÚ.r;   T)Úexist_okz.pyr)   )
Úrsplitr   r   r   rŠ   ÚreplaceÚsepÚmakedirsr   r   )r   r0   ÚcontentsÚbaseÚfinalÚ	base_pathr%   r   r   r   rQ   r  s
    z(NestedNamespacePackageTest.create_modulec             C   s  d}|   d|¡ |   d|¡ |   d|¡ |   d|¡ |   dd¡ |   dd	¡ tj d
tj | jd¡¡ tj d
tj | jd¡¡ d
d l}|  t	d¡ |  
t|jƒd¡ d
d l}|  t	d¡ |  
t|jjƒd¡ d
dlm} d
dlm} |  
|d¡ |  
|d¡ d S )NzBimport pkgutil; __path__ = pkgutil.extend_path(__path__, __name__)za.pkg.__init__zb.pkg.__init__za.pkg.subpkg.__init__zb.pkg.subpkg.__init__za.pkg.subpkg.czc = 1zb.pkg.subpkg.dzd = 2r   ÚaÚbr#   é   z
pkg.subpkg)Úc)r7   r;   )rQ   r   r   r   r   r   rŠ   r#   r   r   r!   Úlenrr   Z
pkg.subpkgZsubpkgZpkg.subpkg.cr™   Zpkg.subpkg.dr7   )r   Zpkgutil_boilerplater#   r™   r7   r   r   r   Útest_nestedy  s&    z&NestedNamespacePackageTest.test_nestedN)rL   rM   rN   r   r   rQ   r›   r   r   r   r   r‰   h  s   r‰   c               @   s|   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Ze e d	k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S )ÚImportlibMigrationTestsc             C   s   t dtfƒS )Nz5This emulation is deprecated, use 'importlib' instead)r   ÚDeprecationWarning)r   r   r   r   Úcheck_deprecated–  s    z(ImportlibMigrationTests.check_deprecatedc          	   C   s"   |   ¡  t d¡ W d Q R X d S )Nr*   )rž   r   ZImpImporter)r   r   r   r   Útest_importer_deprecated›  s    
z0ImportlibMigrationTests.test_importer_deprecatedc          	   C   s(   |   ¡  t dddd¡ W d Q R X d S )Nr*   )rž   r   Z	ImpLoader)r   r   r   r   Útest_loader_deprecatedŸ  s    
z.ImportlibMigrationTests.test_loader_deprecatedc          	   C   sX   t ƒ H}|  t d¡¡ |  t d¡¡ |  t d¡¡ |  t|jƒd¡ W d Q R X d S )Nr   r   ztest.supportr   )r   ÚassertIsNotNoner   Ú
get_loaderr!   rš   Úwarnings)r   r)   r   r   r   Ú test_get_loader_avoids_emulation£  s
    z8ImportlibMigrationTests.test_get_loader_avoids_emulationÚ__main__znot compatible with __main__c          	   C   sJ   t }b z8tƒ (}|  t t¡¡ |  t|jƒd¡ W d Q R X W d |a X d S )Nr   )	Ú
__loader__r   r¡   r   r¢   rL   r!   rš   r£   )r   Zthis_loaderr)   r   r   r   Ú0test_get_loader_handles_missing_loader_attributeª  s     zHImportlibMigrationTests.test_get_loader_handles_missing_loader_attributec          	   C   sJ   d}t tƒ|ƒ}|`t|ƒ |tj|< t |¡}W d Q R X |  |¡ d S )Nrv   )Útyper   Ú__spec__r   r"   r   r¢   r   )r   r0   r?   rW   r   r   r   Ú.test_get_loader_handles_missing_spec_attribute¶  s    

zFImportlibMigrationTests.test_get_loader_handles_missing_spec_attributec          	   C   sL   d}t tƒ|ƒ}d |_t|ƒ |tj|< t |¡}W d Q R X |  |¡ d S )Nrv   )r¨   r   r©   r   r"   r   r¢   r   )r   r0   r?   rW   r   r   r   Ú+test_get_loader_handles_spec_attribute_none¿  s    

zCImportlibMigrationTests.test_get_loader_handles_spec_attribute_nonec             C   s6   d}d t j|< zt |¡}W d t j|= X |  |¡ d S )Nztotally bogus)r   r"   r   r¢   r   )r   r0   rW   r   r   r   Ú#test_get_loader_None_in_sys_modulesÈ  s    

z;ImportlibMigrationTests.test_get_loader_None_in_sys_modulesc             C   s   d}t  |¡}|  |¡ d S )Nztotally bogus)r   Úfind_loaderr   )r   r0   rW   r   r   r   Útest_find_loader_missing_moduleÑ  s    
z7ImportlibMigrationTests.test_find_loader_missing_modulec          	   C   sX   t ƒ H}|  t d¡¡ |  t d¡¡ |  t d¡¡ |  t|jƒd¡ W d Q R X d S )Nr   r   ztest.supportr   )r   r¡   r   r­   r!   rš   r£   )r   r)   r   r   r   Ú!test_find_loader_avoids_emulationÖ  s
    z9ImportlibMigrationTests.test_find_loader_avoids_emulationc          	   C   s8   t ƒ (}|  t d¡¡ |  t|jƒd¡ W d Q R X d S )Nz*??r   )r   r   r   rx   r!   rš   r£   )r   r)   r   r   r   Ú"test_get_importer_avoids_emulationÝ  s    z:ImportlibMigrationTests.test_get_importer_avoids_emulationc          	   C   s:   t ƒ *}xt ¡ D ]}qW |  t|jƒd¡ W d Q R X d S )Nr   )r   r   rw   r!   rš   r£   )r   r)   Zimporterr   r   r   Ú$test_iter_importers_avoids_emulationã  s     z<ImportlibMigrationTests.test_iter_importers_avoids_emulationN)rL   rM   rN   rž   rŸ   r    r¤   ÚunittestZskipIfr§   rª   r«   r¬   r®   r¯   r°   r±   r   r   r   r   rœ   ‘  s   			rœ   c              C   s6   t tttttƒ dd l} dd l}| j 	¡  | 
¡  d S )Nr   )r   r   rO   rd   r‰   rœ   Ú	zipimportrz   Ú_zip_directory_cacheÚclearÚinvalidate_caches)r³   rz   r   r   r   Ú	test_mainé  s    
r·   r¥   )Ztest.supportr   r   r   r   r²   r   rz   Úimportlib.utilr   r   r   Zos.pathr   r   r+   ZTestCaser   rO   rd   r‰   rœ   r·   rL   r   r   r   r   Ú<module>   s&    ,-})X
