
    dafW                         d dl Z d dlZd dlZd dlZd dlZddlmZ ddlmZmZ ddlm	Z	 d dl
mZmZ d dlmZmZ d dlmZ  G d	 d
      Z G d deej&                        Z G d dee	j*                  ej&                        Zd Z G d dej&                        Z G d dej&                        Z G d de	j4                  ej&                        Z G d de	j4                  ej&                        Ze j:                  d        Z G d dej&                        Z G d dej&                        Z e!dk(  r ejD                          yy)    N   )data01)	zipdata01	zipdata02)util)	resourcesimport_module)import_helper	os_helper)unlinkc                       e Zd Zd Zd Zd Zy)ResourceTestsc                     t        j                  | j                        dz  }| j                  |j	                                y )Nbinary.file)r   filesdata
assertTrueis_fileselftargets     F/root/Python-3.12.4/Lib/test/test_importlib/resources/test_resource.pytest_is_file_existsz!ResourceTests.test_is_file_exists   s,    +m;()    c                     t        j                  | j                        dz  }| j                  |j	                                y )Nz
not-a-file)r   r   r   assertFalser   r   s     r   test_is_file_missingz"ResourceTests.test_is_file_missing   s.    +l:)*r   c                     t        j                  | j                        dz  }| j                  |j	                                | j                  |j                                y )Nsubdirectory)r   r   r   r   r   r   is_dirr   s     r   test_is_dirzResourceTests.test_is_dir   s@    +n<)*(r   N)__name__
__module____qualname__r   r   r!    r   r   r   r      s    *+)r   r   c                       e Zd Zd Zy)ResourceDiskTestsc                     t         | _        y N)r   r   r   s    r   setUpzResourceDiskTests.setUp!   s	    	r   N)r"   r#   r$   r+   r%   r   r   r'   r'       s    r   r'   c                       e Zd Zy)ResourceZipTestsN)r"   r#   r$   r%   r   r   r-   r-   %   s    r   r-   c                 \    | j                         D ch c]  }|j                   c}S c c}w r)   )iterdirname)traversableitems     r   namesr3   )   s#    "-"5"5"78$DII888s   )c                   $    e Zd Zd Zd Zd Zd Zy)ResourceLoaderTestsc                     t        j                  t        t        j                  g d      }| j	                  t        t        j                  |            h d       y )NABCfilepathcontents>   r8   r9   r:   )r   create_packager   __file__assertEqualr3   r   r   r   packages     r   test_resource_contentsz*ResourceLoaderTests.test_resource_contents.   s=    %%foo
 	yw78/Jr   c                     t        j                  t        t        j                  g d      }| j	                  t        j                  |      j                  d      j                                y )Nr8   r9   r:   zD/EzD/Fr;   r9   )	r   r?   r   r@   r   r   r   joinpathr   rB   s     r   test_is_filez ResourceLoaderTests.test_is_file4   sI    %%foo8U
 		099#>FFHIr   c                     t        j                  t        t        j                  g d      }| j	                  t        j                  |      j                  d      j                                y )NrF   r;   D)	r   r?   r   r@   r   r   r   rG   r    rB   s     r   r!   zResourceLoaderTests.test_is_dir:   sI    %%foo8U
 		099#>EEGHr   c                     t        j                  t        t        j                  g d      }| j	                  t        j                  |      j                  d      j                                y )NrF   r;   Z)	r   r?   r   r@   r   r   r   rG   r   rB   s     r   test_resource_missingz)ResourceLoaderTests.test_resource_missing@   sK    %%foo8U
 	1::3?GGIJr   N)r"   r#   r$   rD   rH   r!   rM   r%   r   r   r5   r5   -   s    KJIKr   r5   c                       e Zd Zd Zy)ResourceCornerCaseTestsc                 v   t        j                  t        t        j                  g d      }t	               |_        d|_        |j
                  |j                  _        |j                  |j                  _        | j                  t        j                  |      j                  d      j                                y)z
        Test odd ball packages which:
        # 1. Do not have a ResourceReader as a loader
        # 2. Are not on the file system
        # 3. Are not in a zip file
        r7   r;   z/path/which/shall/not/be/namedr8   N)r   r?   r   r@   object
__loader____spec__loaderoriginr   r   r   rG   r   )r   modules     r   #test_package_has_no_reader_fallbackz;ResourceCornerCaseTests.test_package_has_no_reader_fallbackH   s     $$foo
 #H:!'!2!2!'099#>FFHIr   N)r"   r#   r$   rW   r%   r   r   rO   rO   G   s    Jr   rO   c                   .    e Zd ZeZd Zd Zd Zd Zd Z	y)ResourceFromZipsTest01c                     t        d      }| j                  t        j                  |      j	                  d      j                                y Nziptestdata.subdirectoryr   )r	   r   r   r   rG   r   r   	submodules     r   test_is_submodule_resourcez1ResourceFromZipsTest01.test_is_submodule_resource^   s6    !"<=			2;;MJRRTUr   c                     | j                  t        j                  d      j                  d      j	                                y r[   r   r   r   rG   r   r*   s    r   $test_read_submodule_resource_by_namez;ResourceFromZipsTest01.test_read_submodule_resource_by_nameb   s,    OO67Xm$WY	
r   c                 z    t        d      }| j                  t        t        j                  |            ddh       y Nr\   __init__.pyr   )r	   rA   r3   r   r   r]   s     r   test_submodule_contentsz.ResourceFromZipsTest01.test_submodule_contentsi   s3    !"<=	)//),-}/M	
r   c                 d    | j                  t        t        j                  d            ddh       y rd   rA   r3   r   r   r*   s    r   test_submodule_contents_by_namez6ResourceFromZipsTest01.test_submodule_contents_by_nameo   s*    )//"<=>M*	
r   c                    t        j                  t        j                  d            5 }|j                  dk(  sJ |j	                         sJ |j                  d      j	                         sJ t        t        |j                                     sJ 	 d d d        j                  j                         rJ y # 1 sw Y   &xY w)Nziptestdatar   )r   as_filer   r0   r    rG   lenlistr/   parentexists)r   r   s     r   test_as_file_directoryz-ResourceFromZipsTest01.test_as_file_directoryu   s    y}=> 	-$99---;;= ===077999tDLLN+,,,		-
 ;;%%''''	- 	-s   A)B88CN)
r"   r#   r$   r   
ZIP_MODULEr_   rb   rf   ri   rq   r%   r   r   rY   rY   [   s!    JV


(r   rY   c                       e Zd ZeZd Zy)ResourceFromZipsTest02c                     | j                  t        t        j                  d            ddh       | j                  t        t        j                  d            ddh       y)z
        Test thata zip with two unrelated subpackages return
        distinct resources. Ref python/importlib_resources#44.
        zziptestdata.onere   zresource1.txtzziptestdata.twozresource2.txtNrh   r*   s    r   test_unrelated_contentsz.ResourceFromZipsTest02.test_unrelated_contents   sV    
 	)//"345O,	
 	)//"345O,	
r   N)r"   r#   r$   r   rr   rv   r%   r   r   rt   rt   ~   s    J
r   rt   c              #   >  K   t        j                  t        j                        }|j                  j                  d      }t        j                  |       t        j                          dz  }|j                  |j                                t        j                  j                  t        |             t        d       	 d  t        j                   t"              5  t        j                  j%                  t        |             d d d        t        j                   t&              5  t        j(                  t        |      = t        j*                  d= d d d        t        j                   t,              5  t/        |       d d d        y # 1 sw Y   xY w# 1 sw Y   CxY w# 1 sw Y   y xY w# t        j                   t"              5  t        j                  j%                  t        |             d d d        n# 1 sw Y   nxY wt        j                   t&              5  t        j(                  t        |      = t        j*                  d= d d d        n# 1 sw Y   nxY wt        j                   t,              5  t/        |       d d d        w # 1 sw Y   w xY wxY ww)Nzziptestdata.zipz.ziprk   )pathlibPathr   r@   ro   rG   uuiduuid4write_bytes
read_bytessysr=   appendstrr	   
contextlibsuppress
ValueErrorremoveKeyErrorpath_importer_cachemodulesOSErrorr   )dir	data_pathsource_zip_pathzip_paths       r   zip_on_pathr      s    Y//0I&&//0ABO||C djjl^4#88H3356HHOOCM"-   , 	+HHOOCM*	+   * 	+''H6M*	+   ) 	8	 		+ 	+	+ 	+	 	   , 	+HHOOCM*	+ 	+ 	+   * 	+''H6M*	+ 	+ 	+   ) 	8	 	 	s   B>JF. J)F
!J(,F!J5F"	J
FJFJ"F+'J.J)G:1	J:H? J,I	JI J9J	JJJJc                   j    e Zd ZdZd Zd Zd Zd Z ej                  d      d        Z
d Zd	 Zd
 Zy)DeletingZipsTestz^Having accessed resources in a zip file should not keep an open
    reference to the zip.
    c                    t        j                         | _        | j                  | j                  j                         t        j                         } | j                  t
        j                  g|  | j                  j                  t        j                               }| j                  j                  t        |             y r)   )r   	ExitStackfixtures
addCleanupcloser
   modules_setupmodules_cleanupenter_contextr   temp_dirr   )r   r   r   s      r   r+   zDeletingZipsTest.setUp   s    ",,.++,--/55@@==..y/A/A/CD##K$9:r   c                     t        j                  d      j                         D cg c]  }|j                   c} y c c}w )Nrk   )r   r   r/   r0   )r   r2   s     r   test_iterdir_does_not_keep_openz0DeletingZipsTest.test_iterdir_does_not_keep_open   s)    (}=EEGHtHHs   =c                 h    t        j                  d      j                  d      j                          y Nrk   r   r   r   rG   r   r*   s    r   test_is_file_does_not_keep_openz0DeletingZipsTest.test_is_file_does_not_keep_open   "    &//>FFHr   c                 h    t        j                  d      j                  d      j                          y )Nrk   znot-presentr   r*   s    r   'test_is_file_failure_does_not_keep_openz8DeletingZipsTest.test_is_file_failure_does_not_keep_open   r   r   zDesired but not supported.c                 Z    t        j                  t        j                  d      dz         y r   )r   rl   r   r*   s    r   test_as_file_does_not_keep_openz0DeletingZipsTest.test_as_file_does_not_keep_open   s    )//-8=HIr   c                 v    t        j                  t        j                  d      dz        j                          y)zr
        Mimic what certifi does on import to make its bundle
        available for the process duration.
        rk   r   N)r   rl   r   	__enter__r*   s    r   $test_entered_path_does_not_keep_openz5DeletingZipsTest.test_entered_path_does_not_keep_open   s)    
 	)//-8=HISSUr   c                 h    t        j                  d      j                  d      j                          y r   )r   r   rG   r}   r*   s    r   #test_read_binary_does_not_keep_openz4DeletingZipsTest.test_read_binary_does_not_keep_open   s"    &//>IIKr   c                 l    t        j                  d      j                  d      j                  d       y )Nrk   
utf-8.filezutf-8)encoding)r   r   rG   	read_textr*   s    r   !test_read_text_does_not_keep_openz2DeletingZipsTest.test_read_text_does_not_keep_open   s-    &//=GG 	H 	
r   N)r"   r#   r$   __doc__r+   r   r   r   unittestskipr   r   r   r   r%   r   r   r   r      sQ    ;III X]]/0J 1JVL
r   r   c                       e Zd Z e ej
                  e      j                        Ze	d        Z
e	d        Zd Zd Zd Zd Zy)ResourceFromNamespaceTest01c                 V    t         j                  j                  | j                         y r)   )r~   r=   r   site_dirclss    r   
setUpClassz&ResourceFromNamespaceTest01.setUpClass       %r   c                 V    t         j                  j                  | j                         y r)   )r~   r=   r   r   r   s    r   tearDownClassz)ResourceFromNamespaceTest01.tearDownClass   r   r   c                     | j                  t        j                  t        d            j	                  d      j                                y Nnamespacedata01r   )r   r   r   r	   rG   r   r*   s    r   r_   z6ResourceFromNamespaceTest01.test_is_submodule_resource   s1    OOM*;<=Xm$WY	
r   c                     | j                  t        j                  d      j                  d      j	                                y r   ra   r*   s    r   rb   z@ResourceFromNamespaceTest01.test_read_submodule_resource_by_name   s/    OO-.77FNNP	
r   c                     t        t        j                  t        d                  }	 |j	                  d       | j                  |h d       y # t
        $ r Y  w xY wNr   __pycache__>   utf-16.filer   r   )r3   r   r   r	   r   r   rA   r   r>   s     r   rf   z3ResourceFromNamespaceTest01.test_submodule_contents   sS    7H)IJK	OOM* 	#OP  		s   A 	AAc                     t        t        j                  d            }	 |j                  d       | j                  |h d       y # t        $ r Y  w xY wr   )r3   r   r   r   r   rA   r   s     r   ri   z;ResourceFromNamespaceTest01.test_submodule_contents_by_name   sN    ):;<	OOM* 	#OP  		s   A 	AAN)r"   r#   r$   r   rx   ry   r@   ro   r   classmethodr   r   r_   rb   rf   ri   r%   r   r   r   r      sY    <7<<)001H& & & &


QQr   r   __main__)#r   r~   r   rz   rx    r   r   r   r   	importlibr   r	   test.supportr
   r   test.support.os_helperr   r   TestCaser'   ZipSetupr-   r3   r5   rO   ZipSetupBaserY   rt   contextmanagerr   r   r   r"   mainr%   r   r   <module>r      s    
     "  . 1 )) )"x'8'8 
	}dmmX5F5F 	9K(++ K4Jh// J( (T..0A0A  (F
T..0A0A 
$  ,)
x(( )
X%Q("3"3 %QP zHMMO r   