22
33import unittest
44import glob
5+ import os
56from test .support import import_helper
67from test .support import os_helper
78
@@ -129,6 +130,15 @@ def test_anydbm_access(self):
129130 assert (f [key ] == b"Python:" )
130131 f .close ()
131132
133+ def test_open_with_bytes (self ):
134+ dbm .open (os .fsencode (_fname ), "c" ).close ()
135+
136+ def test_open_with_pathlib_path (self ):
137+ dbm .open (os_helper .FakePath (_fname ), "c" ).close ()
138+
139+ def test_open_with_pathlib_path_bytes (self ):
140+ dbm .open (os_helper .FakePath (os .fsencode (_fname )), "c" ).close ()
141+
132142 def read_helper (self , f ):
133143 keys = self .keys_helper (f )
134144 for key in self ._dict :
@@ -144,34 +154,41 @@ def setUp(self):
144154
145155class WhichDBTestCase (unittest .TestCase ):
146156 def test_whichdb (self ):
147- for module in dbm_iterator ():
148- # Check whether whichdb correctly guesses module name
149- # for databases opened with "module" module.
150- # Try with empty files first
151- name = module .__name__
152- if name == 'dbm.dumb' :
153- continue # whichdb can't support dbm.dumb
154- delete_files ()
155- f = module .open (_fname , 'c' )
156- f .close ()
157- self .assertEqual (name , self .dbm .whichdb (_fname ))
158- # Now add a key
159- f = module .open (_fname , 'w' )
160- f [b"1" ] = b"1"
161- # and test that we can find it
162- self .assertIn (b"1" , f )
163- # and read it
164- self .assertEqual (f [b"1" ], b"1" )
165- f .close ()
166- self .assertEqual (name , self .dbm .whichdb (_fname ))
157+ _bytes_fname = os .fsencode (_fname )
158+ for path in [_fname , os_helper .FakePath (_fname ),
159+ _bytes_fname , os_helper .FakePath (_bytes_fname )]:
160+ for module in dbm_iterator ():
161+ # Check whether whichdb correctly guesses module name
162+ # for databases opened with "module" module.
163+ # Try with empty files first
164+ name = module .__name__
165+ if name == 'dbm.dumb' :
166+ continue # whichdb can't support dbm.dumb
167+ delete_files ()
168+ f = module .open (path , 'c' )
169+ f .close ()
170+ self .assertEqual (name , self .dbm .whichdb (path ))
171+ # Now add a key
172+ f = module .open (path , 'w' )
173+ f [b"1" ] = b"1"
174+ # and test that we can find it
175+ self .assertIn (b"1" , f )
176+ # and read it
177+ self .assertEqual (f [b"1" ], b"1" )
178+ f .close ()
179+ self .assertEqual (name , self .dbm .whichdb (path ))
167180
168181 @unittest .skipUnless (ndbm , reason = 'Test requires ndbm' )
169182 def test_whichdb_ndbm (self ):
170183 # Issue 17198: check that ndbm which is referenced in whichdb is defined
171184 db_file = '{}_ndbm.db' .format (_fname )
172185 with open (db_file , 'w' ):
173186 self .addCleanup (os_helper .unlink , db_file )
187+ db_file_bytes = os .fsencode (db_file )
174188 self .assertIsNone (self .dbm .whichdb (db_file [:- 3 ]))
189+ self .assertIsNone (self .dbm .whichdb (os_helper .FakePath (db_file [:- 3 ])))
190+ self .assertIsNone (self .dbm .whichdb (db_file_bytes [:- 3 ]))
191+ self .assertIsNone (self .dbm .whichdb (os_helper .FakePath (db_file_bytes [:- 3 ])))
175192
176193 def tearDown (self ):
177194 delete_files ()
0 commit comments