summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-02-26 14:02:22 (GMT)
committerGitHub <noreply@github.com>2018-02-26 14:02:22 (GMT)
commit6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e (patch)
tree3dc8ecabed25ffe13d7e79630a9eea9c3c8f3177 /Lib/test
parent973cae07d6ce7f5a93bd9cd3bcb724a96cfe14e9 (diff)
downloadcpython-6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e.zip
cpython-6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e.tar.gz
cpython-6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e.tar.bz2
bpo-32922: dbm.open() now encodes filename with the filesystem encoding. (GH-5832)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_dbm_dumb.py15
-rw-r--r--Lib/test/test_dbm_gnu.py36
-rw-r--r--Lib/test/test_dbm_ndbm.py37
3 files changed, 87 insertions, 1 deletions
diff --git a/Lib/test/test_dbm_dumb.py b/Lib/test/test_dbm_dumb.py
index 21f29af..652a355 100644
--- a/Lib/test/test_dbm_dumb.py
+++ b/Lib/test/test_dbm_dumb.py
@@ -281,6 +281,21 @@ class DumbDBMTestCase(unittest.TestCase):
self.assertEqual(sorted(f.keys()), sorted(self._dict))
f.close() # don't write
+ @unittest.skipUnless(support.TESTFN_NONASCII,
+ 'requires OS support of non-ASCII encodings')
+ def test_nonascii_filename(self):
+ filename = support.TESTFN_NONASCII
+ for suffix in ['.dir', '.dat', '.bak']:
+ self.addCleanup(support.unlink, filename + suffix)
+ with dumbdbm.open(filename, 'c') as db:
+ db[b'key'] = b'value'
+ self.assertTrue(os.path.exists(filename + '.dat'))
+ self.assertTrue(os.path.exists(filename + '.dir'))
+ with dumbdbm.open(filename, 'r') as db:
+ self.assertEqual(list(db.keys()), [b'key'])
+ self.assertTrue(b'key' in db)
+ self.assertEqual(db[b'key'], b'value')
+
def tearDown(self):
_delete_files()
diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py
index 304b332..d96df92 100644
--- a/Lib/test/test_dbm_gnu.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -2,7 +2,7 @@ from test import support
gdbm = support.import_module("dbm.gnu") #skip if not supported
import unittest
import os
-from test.support import TESTFN, unlink
+from test.support import TESTFN, TESTFN_NONASCII, unlink
filename = TESTFN
@@ -93,5 +93,39 @@ class TestGdbm(unittest.TestCase):
self.assertEqual(str(cm.exception),
"GDBM object has already been closed")
+ def test_bytes(self):
+ with gdbm.open(filename, 'c') as db:
+ db[b'bytes key \xbd'] = b'bytes value \xbd'
+ with gdbm.open(filename, 'r') as db:
+ self.assertEqual(list(db.keys()), [b'bytes key \xbd'])
+ self.assertTrue(b'bytes key \xbd' in db)
+ self.assertEqual(db[b'bytes key \xbd'], b'bytes value \xbd')
+
+ def test_unicode(self):
+ with gdbm.open(filename, 'c') as db:
+ db['Unicode key \U0001f40d'] = 'Unicode value \U0001f40d'
+ with gdbm.open(filename, 'r') as db:
+ self.assertEqual(list(db.keys()), ['Unicode key \U0001f40d'.encode()])
+ self.assertTrue('Unicode key \U0001f40d'.encode() in db)
+ self.assertTrue('Unicode key \U0001f40d' in db)
+ self.assertEqual(db['Unicode key \U0001f40d'.encode()],
+ 'Unicode value \U0001f40d'.encode())
+ self.assertEqual(db['Unicode key \U0001f40d'],
+ 'Unicode value \U0001f40d'.encode())
+
+ @unittest.skipUnless(TESTFN_NONASCII,
+ 'requires OS support of non-ASCII encodings')
+ def test_nonascii_filename(self):
+ filename = TESTFN_NONASCII
+ self.addCleanup(unlink, filename)
+ with gdbm.open(filename, 'c') as db:
+ db[b'key'] = b'value'
+ self.assertTrue(os.path.exists(filename))
+ with gdbm.open(filename, 'r') as db:
+ self.assertEqual(list(db.keys()), [b'key'])
+ self.assertTrue(b'key' in db)
+ self.assertEqual(db[b'key'], b'value')
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_dbm_ndbm.py b/Lib/test/test_dbm_ndbm.py
index 49f4426..fb7d0e8 100644
--- a/Lib/test/test_dbm_ndbm.py
+++ b/Lib/test/test_dbm_ndbm.py
@@ -1,5 +1,6 @@
from test import support
support.import_module("dbm.ndbm") #skip if not supported
+import os
import unittest
import dbm.ndbm
from dbm.ndbm import error
@@ -47,6 +48,42 @@ class DbmTestCase(unittest.TestCase):
self.assertEqual(str(cm.exception),
"DBM object has already been closed")
+ def test_bytes(self):
+ with dbm.ndbm.open(self.filename, 'c') as db:
+ db[b'bytes key \xbd'] = b'bytes value \xbd'
+ with dbm.ndbm.open(self.filename, 'r') as db:
+ self.assertEqual(list(db.keys()), [b'bytes key \xbd'])
+ self.assertTrue(b'bytes key \xbd' in db)
+ self.assertEqual(db[b'bytes key \xbd'], b'bytes value \xbd')
+
+ def test_unicode(self):
+ with dbm.ndbm.open(self.filename, 'c') as db:
+ db['Unicode key \U0001f40d'] = 'Unicode value \U0001f40d'
+ with dbm.ndbm.open(self.filename, 'r') as db:
+ self.assertEqual(list(db.keys()), ['Unicode key \U0001f40d'.encode()])
+ self.assertTrue('Unicode key \U0001f40d'.encode() in db)
+ self.assertTrue('Unicode key \U0001f40d' in db)
+ self.assertEqual(db['Unicode key \U0001f40d'.encode()],
+ 'Unicode value \U0001f40d'.encode())
+ self.assertEqual(db['Unicode key \U0001f40d'],
+ 'Unicode value \U0001f40d'.encode())
+
+ @unittest.skipUnless(support.TESTFN_NONASCII,
+ 'requires OS support of non-ASCII encodings')
+ def test_nonascii_filename(self):
+ filename = support.TESTFN_NONASCII
+ for suffix in ['', '.pag', '.dir', '.db']:
+ self.addCleanup(support.unlink, filename + suffix)
+ with dbm.ndbm.open(filename, 'c') as db:
+ db[b'key'] = b'value'
+ self.assertTrue(any(os.path.exists(filename + suffix)
+ for suffix in ['', '.pag', '.dir', '.db']))
+ with dbm.ndbm.open(filename, 'r') as db:
+ self.assertEqual(list(db.keys()), [b'key'])
+ self.assertTrue(b'key' in db)
+ self.assertEqual(db[b'key'], b'value')
+
+
if __name__ == '__main__':
unittest.main()