summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_dbm_ndbm.py
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/test_dbm_ndbm.py
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/test_dbm_ndbm.py')
-rw-r--r--Lib/test/test_dbm_ndbm.py37
1 files changed, 37 insertions, 0 deletions
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()