summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2022-09-09 02:32:40 (GMT)
committerGitHub <noreply@github.com>2022-09-09 02:32:40 (GMT)
commitdf50938f583b6abd9f31f1ff1f5ad52d7b04ecbb (patch)
treeaa1fbb0e797563aae648fb1499b3297dcca59b50 /Lib
parent95d6330a3edacacd081f55699bd6f0a787908924 (diff)
downloadcpython-df50938f583b6abd9f31f1ff1f5ad52d7b04ecbb.zip
cpython-df50938f583b6abd9f31f1ff1f5ad52d7b04ecbb.tar.gz
cpython-df50938f583b6abd9f31f1ff1f5ad52d7b04ecbb.tar.bz2
GH-46412: More efficient bool() for ndbm/_gdbmmodule (#96692)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_dbm_gnu.py14
-rw-r--r--Lib/test/test_dbm_ndbm.py14
2 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_dbm_gnu.py b/Lib/test/test_dbm_gnu.py
index 4eaa0f4..73602ca 100644
--- a/Lib/test/test_dbm_gnu.py
+++ b/Lib/test/test_dbm_gnu.py
@@ -118,6 +118,20 @@ class TestGdbm(unittest.TestCase):
self.assertEqual(str(cm.exception),
"GDBM object has already been closed")
+ def test_bool_empty(self):
+ with gdbm.open(filename, 'c') as db:
+ self.assertFalse(bool(db))
+
+ def test_bool_not_empty(self):
+ with gdbm.open(filename, 'c') as db:
+ db['a'] = 'b'
+ self.assertTrue(bool(db))
+
+ def test_bool_on_closed_db_raises(self):
+ with gdbm.open(filename, 'c') as db:
+ db['a'] = 'b'
+ self.assertRaises(gdbm.error, bool, db)
+
def test_bytes(self):
with gdbm.open(filename, 'c') as db:
db[b'bytes key \xbd'] = b'bytes value \xbd'
diff --git a/Lib/test/test_dbm_ndbm.py b/Lib/test/test_dbm_ndbm.py
index e57d9ca..8f37e3c 100644
--- a/Lib/test/test_dbm_ndbm.py
+++ b/Lib/test/test_dbm_ndbm.py
@@ -133,6 +133,20 @@ class DbmTestCase(unittest.TestCase):
def test_open_with_pathlib_bytes_path(self):
dbm.ndbm.open(os_helper.FakePath(os.fsencode(self.filename)), "c").close()
+ def test_bool_empty(self):
+ with dbm.ndbm.open(self.filename, 'c') as db:
+ self.assertFalse(bool(db))
+
+ def test_bool_not_empty(self):
+ with dbm.ndbm.open(self.filename, 'c') as db:
+ db['a'] = 'b'
+ self.assertTrue(bool(db))
+
+ def test_bool_on_closed_db_raises(self):
+ with dbm.ndbm.open(self.filename, 'c') as db:
+ db['a'] = 'b'
+ self.assertRaises(dbm.ndbm.error, bool, db)
+
if __name__ == '__main__':
unittest.main()