
    ;eN,                         d 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ddlmZ	 ddl
mZ ddlmZ ddlmZ ej        Zd Z G d dej                  Zed	k    r ej                     dS dS )
zATest script for the dumbdbm module
   Original by Roger E. Masse
    N)support)	os_helper)partialc                  j    dD ]/} 	 t          j        t          | z               # t          $ r Y ,w xY wd S )N.dir.dat.bak)osunlink_fnameOSError)exts    )/usr/lib/python3.11/test/test_dbm_dumb.py_delete_filesr      sW    '  	Ifsl#### 	 	 	D	 s   #
00c                      e Zd Zddddddddd	d
ddddd                    d          diZd Z ej         ee	d          d          e
j        d                         Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd  Zd! Zd" Zd# Zd$ Zd% Zd& Zd' Zd( Zd) Z e
j        d*             Z! ej        e
j"        d+          d,             Z#d- Z$d. Z%d/ Z&d0 Z'd1 Z(d2S )3DumbDBMTestCase   0       as   Python:   bs   Programming   cs   the   ds   way   fs   Guido   gs   intended   üutf-8   !c                 X   t          j        t          j        t          d                    5 }|                     t          |                                          g            | j        D ]}| j        |         ||<   | 	                    |           d d d            d S # 1 swxY w Y   d S Nc)

contextlibclosingdumbdbmopenr   assertEquallistkeys_dictread_helper)selffkeys      r   test_dumbdbm_creationz%DumbDBMTestCase.test_dumbdbm_creation%   s    VS 9 9:: 	 aT!&&((^^R000z ) )C#Q		  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   A%BB#&B#umaskztest needs os.umask()c                 .   	 t          j        d          }t          j        t          dd          }|                                 t          j        |           n# t          j        |           w xY wd}t           j        dk    rd}dd l}t          j        t          dz             }|                      |j	        |j
                  |           t          j        t          d	z             }|                      |j	        |j
                  |           d S )
N   r!   i  i  posixi  r   r	   r   )r   r/   r$   r%   r   closenamestatr&   S_IMODEst_mode)r+   	old_umaskr,   expected_moder5   sts         r   test_dumbdbm_creation_modez*DumbDBMTestCase.test_dumbdbm_creation_mode,   s    	 IVS%00AGGIIIHYBHY7g "MWVf_%%bj11=AAAWVf_%%bj11=AAAAAs   AA A0c                     t          j        t                    }d|d<   |                     |d         d           |                                 |                                 d S )Nr   r   )r$   r%   r   r&   r3   r+   r,   s     r   test_close_twicez DumbDBMTestCase.test_close_twiceB   sR    L  $4$'''										r   c                    |                                   t          j        t          j        t
          d                    5 }dx| j        d<   |d<   |                     |           |                     |	                    dd          d           |                     |d         d           d d d            d S # 1 swxY w Y   d S )Nws   indentedr      xxx   foo)
init_dbr"   r#   r$   r%   r   r)   r*   r&   
setdefaultr=   s     r   test_dumbdbm_modificationz)DumbDBMTestCase.test_dumbdbm_modificationI   s    VS 9 9:: 	0a)44DJtqwQQ\\&&996BBBQvY///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   A+B99B= B=c                 R   |                                   t          j        t          j        t
          d                    5 }|                     |           |                     t          j        d          5  d|d<   d d d            n# 1 swxY w Y   |                     t          j        d          5  |d= d d d            n# 1 swxY w Y   | 	                    |
                    d          | j        d                    | 	                    |
                    dd          d           |                     |
                    d                     |                     t                    5  |d          d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nrz'The database is opened for reading only   xr   r   rA   rB   )rC   r"   r#   r$   r%   r   r*   assertRaisesRegexerrorr&   getr)   assertIsNoneassertRaisesKeyErrorr=   s     r   test_dumbdbm_readz!DumbDBMTestCase.test_dumbdbm_readR   sx   VS 9 9:: 	aQ''$MO O  $               ''$MO O  dG               QUU4[[$*T*:;;;QUU6622F;;;aeeFmm,,,""8,,  &		              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   6F7B	=F	B	FB	#F4C8FC	FC	B#F/	F8FF	FF	FF #F c                     |                                   t          j        t          j        t
                              5 }|                     |          }d d d            d S # 1 swxY w Y   d S N)rC   r"   r#   r$   r%   r   keys_helper)r+   r,   r(   s      r   test_dumbdbm_keysz!DumbDBMTestCase.test_dumbdbm_keysc   s    V 4 455 	'##A&&D	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's    A##A'*A'c                     t          j        t          j        t                              5 }d|d<   |                     d|           d d d            d S # 1 swxY w Y   d S )N   hello   1)r"   r#   r$   r%   r   assertInr=   s     r   test_write_containsz#DumbDBMTestCase.test_write_containsh   s    V 4 455 	#AdGMM$"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   AAAc                 `   t          j        t          j        t                              5 }d|d<   d|d<   d d d            n# 1 swxY w Y   t          j        t          j        t                              5 }|                     |d         d           d d d            d S # 1 swxY w Y   d S )NrU   rV      hello2)r"   r#   r$   r%   r   r&   r=   s     r   test_write_write_readz%DumbDBMTestCase.test_write_write_readm   s   V 4 455 	 AdGAdG	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  V 4 455 	1QtWi000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s#   AA
A9B##B'*B'c                 ,   |                                   t          j        t          j        t
          d                    5 }|                     |d         | j        d                    d                              d d d            d S # 1 swxY w Y   d S )NrG   r   r   )	rC   r"   r#   r$   r%   r   r&   r)   encoder=   s     r   test_str_readzDumbDBMTestCase.test_str_readu   s    VS 9 9:: 	PaQx[$*X__W5M5M*NOOO	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	P 	Ps   ;B		BBc                 P   |                                   t          j        t          j        t
                              5 }d|d<   d|d<   d d d            n# 1 swxY w Y   t          j        t          j        t
          d                    5 }|                     d|           |                     |d                    d                   | j	        d                    d                              |                     |d         d           d d d            d S # 1 swxY w Y   d S )	Nr   r   a1rG   r   rV   r   )
rC   r"   r#   r$   r%   r   rW   r&   r]   r)   r=   s     r   test_str_write_containsz'DumbDBMTestCase.test_str_write_containsz   s   V 4 455 	AhKAcF	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 VS 9 9:: 	,aMM(A&&&Qxw778!Z(@(@AC C CQtWd+++		, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s%    AAAB DD"Dc                    t          j        t          j        t                              5 }d|d<   d|d<   d d d            n# 1 swxY w Y   t          j        t          dz   d          5 }|                                }d d d            n# 1 swxY w Y   t          j        dk    r|	                    dd	          }n|	                    d	d          }t          j        t          dz   d
          5 }|
                    |           d d d            n# 1 swxY w Y   t          j        t                    }|                     |d         d           |                     |d         d           d S )NrU   rV   rZ      2r   rb
   
s   
wb)r"   r#   r$   r%   r   ioreadr   linesepreplacewriter&   )r+   r,   filedatas       r   test_line_endingsz!DumbDBMTestCase.test_line_endings   s    V 4 455 	 AdGAdG	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 
 WVf_d++ 	t99;;D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	:<<w//DD<<//DWVf_d++ 	tJJt	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 L  4(+++4),,,,,s5   AA
A+BBB1DDDc                     |                      |          }| j        D ])}|                     | j        |         ||                    *d S rQ   )rR   r)   r&   )r+   r,   r(   r-   s       r   r*   zDumbDBMTestCase.read_helper   sS    "": 	6 	6CTZ_af5555	6 	6r   c                     t          j        t          j        t          d                    5 }| j        D ]}| j        |         ||<   	 d d d            d S # 1 swxY w Y   d S Nn)r"   r#   r$   r%   r   r)   r+   r,   ks      r   rC   zDumbDBMTestCase.init_db   s    VS 9 9:: 	%aZ % %z!}!%	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   AAAc                     t          |                                          }t          | j                                                  }|                     ||           |S rQ   )sortedr(   r)   r&   )r+   r,   r(   dkeyss       r   rR   zDumbDBMTestCase.keys_helper   sK    affhhtz(())u%%%r   c                 B   dd l }i }t          d          D ]}t          j        t	          j        t                              5 }t          d          D ]}|                    d          }|                                 dk     r||v r||= ||= :|                    d          |                    d          z  }|||<   |||<   | 	                    ||         |           	 d d d            n# 1 swxY w Y   t          j        t	          j        t                              5 }t          d |                                D                       }t          |                                          }| 	                    ||           d d d            n# 1 swxY w Y   d S )	Nr      d   abcdefghijklmg?)r   r   r   i'  c              3   J   K   | ]\  }}|                     d           |fV  dS )zlatin-1N)r]   ).0rv   vs      r   	<genexpr>z.DumbDBMTestCase.test_random.<locals>.<genexpr>   s7      !Q!Qtq!188I#6#6":!Q!Q!Q!Q!Q!Qr   )randomranger"   r#   r$   r%   r   choice	randranger&   rx   items)	r+   r   ddummyr,   rv   r   expectedgots	            r   test_randomzDumbDBMTestCase.test_random   s   1XX 	0 	0E#GL$8$899 2Q"3ZZ 
2 
2Eo66A}},,66 !! !!"MM*<==@P@PQV@W@WW ! !((1q1111
22 2 2 2 2 2 2 2 2 2 2 2 2 2 2 #GL$8$899 0Q!!Q!Qqwwyy!Q!Q!QQQQWWYY''  3///0 0 0 0 0 0 0 0 0 0 0 0 0 0 0	0 	0s&   BC--C1	4C1	#A#FF	F	c                    t          j        t          d          5 }d|d<   d d d            n# 1 swxY w Y   t          j        t          d          5 }|                     t	          |                                          dg           d d d            n# 1 swxY w Y   |                     t           j                  5  |                                 d d d            d S # 1 swxY w Y   d S )Nr!   zcontext managerzdumbdbm context managerrG   s   dumbdbm context manager)r$   r%   r   r&   r'   r(   rM   rJ   )r+   dbs     r   test_context_managerz$DumbDBMTestCase.test_context_manager   s   \&#&& 	>",=B()	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> \&#&& 	L"T"''))__/I.JKKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L w}-- 	 	GGIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s0   -117BBB?C!!C%(C%c                 f   t          j        t          d          }|                                 t	          t
          j        |          t	          t
          j        |d          t	          t
          j        |          t	          t
          j	        |          fD ]l}| 
                    t           j                  5 } |d           d d d            n# 1 swxY w Y   |                     t          |j                  d           mt          j        d          t          j        d          t          j        d          t           fD ]l}| 
                    t           j                  5 } ||           d d d            n# 1 swxY w Y   |                     t          |j                  d           md S )Nr!   btestz"DBM object has already been closedr(   iterkeysr   )r$   r%   r   r3   r   operatordelitemsetitemgetitemcontainsrM   rJ   r&   str	exceptionmethodcallerlen)r+   r,   methcms       r   test_check_closedz!DumbDBMTestCase.test_check_closed   s   L%%				X-q11X-q#66X-q11X.224 	C 	CD ""7=11 RV              S..AC C C C *622*:66*733 	C 	CD ""7=11 RQ              S..AC C C C	C 	Cs$   7CC	C	%E==F	F	c                 V   t          j        t          d          5 }| j        D ]}| j        |         ||<   	 d d d            n# 1 swxY w Y   t          j        t          d          5 }|                     |                                g            d d d            d S # 1 swxY w Y   d S rs   )r$   r%   r   r)   r&   r(   ru   s      r   test_create_newzDumbDBMTestCase.test_create_new   s    \&#&& 	%!Z % %z!}!%	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% \&#&& 	+!QVVXXr***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s#   AA
A()BB"%B"c                    t          t          dz   dd          5 }|                    d           d d d            n# 1 swxY w Y   t          j                    5 }|                     t                    5  t          j         t                    5 }	 d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   |                     |	                                d           d d d            d S # 1 swxY w Y   d S )Nr   r@   r   )encodingzstr(print('Hacked!')), 0
 )
r%   r   rm   r   captured_stdoutrM   
ValueErrorr$   r&   getvalue)r+   streamstdoutr,   s       r   	test_evalzDumbDBMTestCase.test_eval   s   &6/3999 	7VLL5666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7$&& 	4&"":..  \&)) Q                             V__..333		4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4si   =AAC56B5BB5B""B5%B"&B5)C55B9	9C5<B9	=+C55C9<C9c                    dD ]}t                       |                     t                    5  t          j        t
          |           d d d            n# 1 swxY w Y   |                     t          j        	                    t
          dz                        |                     t          j        	                    t
          dz                        d S )NrG   r@   r   r
   )
r   rM   FileNotFoundErrorr$   r%   r   assertFalser   pathexists)r+   values     r   test_missing_dataz!DumbDBMTestCase.test_missing_data   s     	> 	>EOOO""#455 , ,VU+++, , , , , , , , , , , , , , ,RW^^FVO<<===RW^^FVO<<====	> 	>s   AA	A	c                 .   t          j        t          d          5 }	 d d d            n# 1 swxY w Y   t          j        t          dz              dD ]}|                     t                    5  t          j        t          |           d d d            n# 1 swxY w Y   |                     t          j        	                    t          dz                        |                     t          j        	                    t          dz                        d S )Nrt   r   r   r
   )
r$   r%   r   r   r   rM   r   r   r   r   )r+   r,   r   s      r   test_missing_indexz"DumbDBMTestCase.test_missing_index   sa   \&#&& 	!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
	&6/""" 	> 	>E""#455 , ,VU+++, , , , , , , , , , , , , , ,RW^^FVO<<===RW^^FVO<<====		> 	>s   )--.BB	B	c                     dD ]O}|                      t          d          5  t          j        t          |           d d d            n# 1 swxY w Y   Pd S )N)xrfNz)Flag must be one of 'r', 'w', 'c', or 'n')rI   r   r$   r%   r   )r+   flags     r   test_invalid_flagz!DumbDBMTestCase.test_invalid_flag  s    % 	+ 	+D''
)@A A + + VT***+ + + + + + + + + + + + + + +	+ 	+s   AA	A	c                    t          j                    5 }t          j                            |d          }t          j        |d          5 }|                     t          |	                                          g            | j
        D ]}| j
        |         ||<   	 d d d            n# 1 swxY w Y   t          j        |dz   t          j                   t          j        |dz   t          j                   t          j        |t          j        t          j        z             t          j        |d          5 }|                     t          |	                                          t          | j
                             |                                 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr   rt   r   r	   rG   )r   temp_dirr   r   joinr$   r%   r&   r'   r(   r)   chmodr5   S_IRUSRS_IXUSRrx   r3   )r+   dirfnamer,   r-   s        r   test_readonly_filesz#DumbDBMTestCase.test_readonly_files  s$   !! 	SGLLd++EeS)) -Q  affhh444: - -C!Z_AcFF-- - - - - - - - - - - - - - - HUV^T\222HUV^T\222HS$,t|3444eS)) Q  !1!16$*3E3EFFF			              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s\   6F7
AB'F7'B+	+F7.B+	/BF77AFF7F#	#F7&F#	'F77F;>F;z*requires OS support of non-ASCII encodingsc                    t           j        }dD ]%}|                     t           j        ||z              &t	          j        |d          5 }d|d<   d d d            n# 1 swxY w Y   |                     t          j        	                    |dz                        |                     t          j        	                    |dz                        t	          j        |d          5 }| 
                    t          |                                          dg           |                     d|v            | 
                    |d         d           d d d            d S # 1 swxY w Y   d S )Nr   r!   s   values   keyr	   r   rG   )r   TESTFN_NONASCII
addCleanupr   r$   r%   
assertTruer   r   r   r&   r'   r(   )r+   filenamesuffixr   s       r   test_nonascii_filenamez&DumbDBMTestCase.test_nonascii_filename  s    ,. 	A 	AFOOI,h.?@@@@\(C(( 	"B!BvJ	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"x&'899:::x&'899:::\(C(( 	3BT"''))__vh777OOFbL)))RZ222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3s%   
AA #A &A*EE!$E!c                     t          j        t          j        t                    d                                           d S r    )r$   r%   r   FakePathr   r3   r+   s    r   test_open_with_pathlib_pathz+DumbDBMTestCase.test_open_with_pathlib_path+  s1    Y'//55;;=====r   c                     t          j        t          j        t                    d                                           d S r    )r$   r%   r   fsencoder   r3   r   s    r   test_open_with_bytes_pathz)DumbDBMTestCase.test_open_with_bytes_path.  s0    R[((#..4466666r   c                     t          j        t          j        t	          j        t                              d                                           d S r    )r$   r%   r   r   r   r   r   r3   r   s    r   !test_open_with_pathlib_bytes_pathz1DumbDBMTestCase.test_open_with_pathlib_bytes_path1  s;    Y'F(;(;<<cBBHHJJJJJr   c                 "    t                       d S rQ   r   r   s    r   tearDownzDumbDBMTestCase.tearDown4      r   c                 "    t                       d S rQ   r   r   s    r   setUpzDumbDBMTestCase.setUp7  r   r   N))__name__
__module____qualname__r]   r)   r.   unittest
skipUnlesshasattrr   r   skip_unless_working_chmodr;   r>   rE   rO   rS   rX   r[   r^   rb   rp   r*   rC   rR   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r      s       3:>668;__W%%E      XW--/FGG(B B )( HGB(  0 0 0  "' ' '
# # #
1 1 1P P P
	, 	, 	,- - -,6 6 6
% % %
  0 0 0,  C C C,+ + +4 4 4> > >> > >+ + + (  )( X2EG G3 3G G3> > >7 7 7K K K      r   r   __main__)__doc__r"   ri   r   r   r5   r   dbm.dumbdumbr$   r   r   test.supportr   	functoolsr   TESTFNr   r   TestCaser   r   mainr   r   r   <module>r      s        				  				               " " " " " "      		  ^ ^ ^ ^ ^h' ^ ^ ^B	 zHMOOOOO r   