
    dafN,                         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,                          yy)
zATest script for the dumbdbm module
   Original by Roger E. Masse
    N)support)	os_helper)partialc                  j    dD ]  } 	 t        j                  t        | z          ! y # t        $ r Y .w xY w)N.dir.dat.bak)osunlink_fnameOSError)exts    -/root/Python-3.12.4/Lib/test/test_dbm_dumb.py_delete_filesr      s:    ' 	IIfsl#  		s   &	22c                      e Zd Zddddddddd	d
dddddj                  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
jD                  d+      d,        Z#d- Z$d. Z%d/ Z&d0 Z'd1 Z(y2)3DumbDBMTestCase   0       as   Python:   bs   Programming   cs   the   ds   way   fs   Guido   gs   intended   üutf-8   !c                 B   t        j                  t        j                  t        d            5 }| j                  t        |j                               g        | j                  D ]  }| j                  |   ||<    | j                  |       d d d        y # 1 sw Y   y xY w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: 	 aT!&&(^R0zz )C#)Q		  	  	 s   ABBumaskztest needs os.umask()c                 :   	 t        j                  d      }t        j                  t        dd      }|j                          t        j                  |       d}t         j                  dk7  rd}dd l}t        j                  t        dz         }| j                   |j                  |j                        |       t        j                  t        d	z         }| j                   |j                  |j                        |       y # t        j                         w xY w)
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%0AGGIHHY77g "MWWVf_%bjj1=AWWVf_%bjj1=A HHYs   A D Dc                     t        j                  t              }d|d<   | j                  |d   d       |j	                          |j	                          y )Nr   r   )r$   r%   r   r&   r3   r+   r,   s     r   test_close_twicez DumbDBMTestCase.test_close_twiceB   s?    LL $4$'				r   c                 `   | j                          t        j                  t        j                  t
        d            5 }dx| j                  d<   |d<   | j                  |       | j                  |j                  dd      d       | j                  |d   d       d d d        y # 1 sw Y   y xY w)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: 	0a)44DJJtqwQQ\\&&96BQvY/	0 	0 	0s   AB$$B-c                    | j                          t        j                  t        j                  t
        d            5 }| j                  |       | j                  t        j                  d      5  d|d<   d d d        | j                  t        j                  d      5  |d= d d d        | j                  |j                  d      | j                  d          | j                  |j                  dd      d       | j                  |j                  d             | j                  t              5  |d    d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   *xY w# 1 sw Y   y xY w)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   s8   VS 9: 	aQ''$MO $ ''$MO dG QUU4[$**T*:;QUU662F;aeeFm,""8, &		 	   	 	sT   2E*0E6(E*E"BE*/E5E*E	E*E	E*E'	#E**E3c                     | j                          t        j                  t        j                  t
                    5 }| j                  |      }d d d        y # 1 sw Y   y xY wN)rC   r"   r#   r$   r%   r   keys_helper)r+   r,   r(   s      r   test_dumbdbm_keysz!DumbDBMTestCase.test_dumbdbm_keysc   sI    V 45 	'##A&D	' 	' 	's   AA!c                     t        j                  t        j                  t                    5 }d|d<   | j                  d|       d d d        y # 1 sw Y   y xY w)N   hello   1)r"   r#   r$   r%   r   assertInr=   s     r   test_write_containsz#DumbDBMTestCase.test_write_containsh   sG    V 45 	#AdGMM$"	# 	# 	#s   AAc                 F   t        j                  t        j                  t                    5 }d|d<   d|d<   d d d        t        j                  t        j                  t                    5 }| j                  |d   d       d d d        y # 1 sw Y   TxY w# 1 sw Y   y xY w)NrU   rV      hello2)r"   r#   r$   r%   r   r&   r=   s     r   test_write_write_readz%DumbDBMTestCase.test_write_write_readm   s    V 45 	 AdGAdG	  V 45 	1QtWi0	1 	1	  	 	1 	1s   B,BBB c                 
   | j                          t        j                  t        j                  t
        d            5 }| j                  |d   | j                  dj                  d                d d d        y # 1 sw Y   y xY w)NrG   r   r   )	rC   r"   r#   r$   r%   r   r&   r)   encoder=   s     r   test_str_readzDumbDBMTestCase.test_str_readu   se    VS 9: 	PaQx[$**X__W5M*NO	P 	P 	Ps   2A99Bc                    | j                          t        j                  t        j                  t
                    5 }d|d<   d|d<   d d d        t        j                  t        j                  t
        d            5 }| j                  d|       | j                  |dj                  d         | j                  dj                  d                | j                  |d   d       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)	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5 	AhKAcF	 VS 9: 	,aMM(A&Qxw78!ZZ(@ACQtWd+		, 	,	 		, 	,s   C.=A(C:.C7:Dc                    t        j                  t        j                  t                    5 }d|d<   d|d<   d d d        t        j                  t        dz   d      5 }|j                         }d d d        t        j                  dk(  rj                  dd	      }nj                  d	d      }t        j                  t        dz   d
      5 }|j                  |       d d d        t        j                  t              }| j                  |d   d       | j                  |d   d       y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   exY w)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5 	 AdGAdG	 
 WWVf_d+ 	t99;D	::<<w/D<</DWWVf_d+ 	tJJt	 LL 4(+4),!	  	 
	 		 	s#   D)D5E)D25D>E
c                     | j                  |      }| j                  D ]$  }| j                  | j                  |   ||          & y rQ   )rR   r)   r&   )r+   r,   r(   r-   s       r   r*   zDumbDBMTestCase.read_helper   sA    ":: 	6CTZZ_af5	6r   c                     t        j                  t        j                  t        d            5 }| j
                  D ]  }| j
                  |   ||<    	 d d d        y # 1 sw Y   y xY wNn)r"   r#   r$   r%   r   r)   r+   r,   ks      r   rC   zDumbDBMTestCase.init_db   sV    VS 9: 	%aZZ %zz!}!%	% 	% 	%s   $AA%c                     t        |j                               }t        | j                  j                               }| j                  ||       |S rQ   )sortedr(   r)   r&   )r+   r,   r(   dkeyss       r   rR   zDumbDBMTestCase.keys_helper   s<    affhtzz()u%r   c                    dd l }i }t        d      D ];  }t        j                  t	        j
                  t                    5 }t        d      D ]t  }|j                  d      }|j                         dk  r||v s,||= ||= 3|j                  d      |j                  d      z  }|||<   |||<   | j                  ||   |       v 	 d d d        t        j                  t	        j
                  t                    5 }t        d |j                         D              }t        |j                               }| j                  ||       d d d        > y # 1 sw Y   xY w# 1 sw Y   VxY w)	Nr      d   abcdefghijklmg?)r   r   r   i'  c              3   H   K   | ]  \  }}|j                  d       |f  yw)zlatin-1N)r]   ).0rv   vs      r   	<genexpr>z.DumbDBMTestCase.test_random.<locals>.<genexpr>   s"     !Qtq!188I#6":!Qs    ")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@   1X 	0E##GLL$89 2Q"3Z 
2Eo6A}},6 !! !!"MM*<=@P@PQV@WW ! !((1q1
22 ##GLL$89 0Q!!Qqwwy!QQQWWY'  3/0 0	02 20 0s    7E:AE:AEE	E(	c                    t        j                  t        d      5 }d|d<   d d d        t        j                  t        d      5 }| j                  t	        |j                               dg       d d d        | j                  t         j                        5  j                          d d d        y # 1 sw Y   xY w# 1 sw Y   NxY w# 1 sw Y   y xY w)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	L w}}- 	GGI	 		> 	>	L 	L	 	s#   B/+B;C/B8;CCc                 D   t        j                  t        d      }|j                          t	        t
        j                  |      t	        t
        j                  |d      t	        t
        j                  |      t	        t
        j                  |      fD ]W  }| j                  t         j                        5 } |d       d d d        | j                  t        j                        d       Y t        j                  d      t        j                  d      t        j                  d      t         fD ]W  }| j                  t         j                        5 } ||       d d d        | j                  t        j                        d       Y y # 1 sw Y   xY w# 1 sw Y   =xY w)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   sP   LL%		X--q1X--q#6X--q1X..24 	CD ""7==1 RVS.AC	C **62**:6**73 	CD ""7==1 RQS.AC	C  s   4	F
	F
F	F	c                 H   t        j                  t        d      5 }| j                  D ]  }| j                  |   ||<    	 d d d        t        j                  t        d      5 }| j	                  |j                         g        d d d        y # 1 sw Y   MxY w# 1 sw Y   y xY wrs   )r$   r%   r   r)   r&   r(   ru   s      r   test_create_newzDumbDBMTestCase.test_create_new   s    \\&#& 	%!ZZ %zz!}!%	% \\&#& 	+!QVVXr*	+ 	+		% 	%	+ 	+s   $B"!BBB!c                    t        t        dz   dd      5 }|j                  d       d d d        t        j                         5 }| j                  t              5  t        j                   t              5 }	 d d d        d d d        | j                  |j                         d       d d d        y # 1 sw Y   xY w# 1 sw Y   FxY w# 1 sw Y   JxY w# 1 sw Y   y xY w)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 	7VLL56	7$$& 	4&"":. \\&) Q V__.3		4 	4	7 	7  	4 	4sG   B/CC4B;6C>(C/B8;C CC	CCc                    dD ]  }t                | j                  t              5  t        j                  t
        |       d d d        | j                  t        j                  j                  t
        dz                | j                  t        j                  j                  t
        dz                 y # 1 sw Y   vxY w)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O""#45 ,VU+,RWW^^FVO<=RWW^^FVO<=	>, ,s   B55B>	c                    t        j                  t        d      5 }	 d d d        t        j                  t        dz          dD ]  }| j                  t              5  t        j                  t        |       d d d        | j                  t        j                  j                  t        dz                | j                  t        j                  j                  t        dz                 y # 1 sw Y   xY w# 1 sw Y   xY w)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   s    \\&#& 	!	
		&6/" 	>E""#45 ,VU+,RWW^^FVO<=RWW^^FVO<=		>	 	, ,s   C+C7+C47D 	c                     dD ];  }| j                  t        d      5  t        j                  t        |       d d d        = y # 1 sw Y   HxY w)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  sR    % 	+D''
)@A + VT*+ +	++ +s   AA	c                 |   t        j                         5 }t        j                  j	                  |d      }t        j                  |d      5 }| j                  t        |j                               g        | j                  D ]  }| j                  |   ||<    	 d d d        t        j                  |dz   t        j                         t        j                  |dz   t        j                         t        j                  |t        j                  t        j                  z         t        j                  |d      5 }| j                  t        |j                               t        | j                               |j!                          d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)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GGLLd+EeS) -Q  affh4:: -C!ZZ_AcF-- HHUV^T\\2HHUV^T\\2HHS$,,t||34eS) Q  !16$**3EF		 	- - 	 	s>   7F2AFB!F2;AF&F2F#	F2&F/	+F22F;z*requires OS support of non-ASCII encodingsc                    t         j                  }dD ]%  }| j                  t         j                  ||z          ' t	        j
                  |d      5 }d|d<   d d d        | j                  t        j                  j                  |dz                | j                  t        j                  j                  |dz                t	        j
                  |d      5 }| j                  t        |j                               dg       | j                  d|v        | j                  |d   d       d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)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FOOI,,h.?@	A\\(C( 	"B!BvJ	"x&'89:x&'89:\\(C( 	3BT"'')_vh7OOFbL)RZ2	3 	3		" 	"	3 	3s   D3AD?3D<?Ec                 z    t        j                  t        j                  t              d      j                          y r    )r$   r%   r   FakePathr   r3   r+   s    r   test_open_with_pathlib_pathz+DumbDBMTestCase.test_open_with_pathlib_path+  s$    Y''/5;;=r   c                 z    t        j                  t        j                  t              d      j                          y r    )r$   r%   r   fsencoder   r3   r   s    r   test_open_with_bytes_pathz)DumbDBMTestCase.test_open_with_bytes_path.  s"    R[[(#.446r   c                     t        j                  t        j                  t	        j
                  t                    d      j                          y 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HHJr   c                     t                y rQ   r   r   s    r   tearDownzDumbDBMTestCase.tearDown4      r   c                     t                y 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      sC   3:>668;__W%E  XW-/FG((B ) HB(0"'
#
1P
	,-,6
%
0,C,+4>>+ (( ) X22EG3G3>7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      si     	  	     " 			^h'' ^B	 zHMMO r   