dbm.gnu and dbm.ndbm accept both strings and bytes as keys and values… · pythoncapi/cpython@7317c1e · GitHub
Skip to content

Commit 7317c1e

Browse files
committed
dbm.gnu and dbm.ndbm accept both strings and bytes as keys and values. For the
former they are converted to bytes before being written to the DB. Closes issue 3799. Reviewed by Skip Montanaro.
1 parent 50d5a1c commit 7317c1e

7 files changed

Lines changed: 40 additions & 19 deletions

File tree

Doc/library/dbm.rst

Lines changed: 24 additions & 15 deletions

Lib/test/test_dbm_dumb.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ def test_str_write_contains(self):
115115
self.init_db()
116116
f = dumbdbm.open(_fname)
117117
f['\u00fc'] = b'!'
118+
f['1'] = 'a'
118119
f.close()
119120
f = dumbdbm.open(_fname, 'r')
120121
self.assert_('\u00fc' in f)
121122
self.assertEqual(f['\u00fc'.encode('utf-8')],
122123
self._dict['\u00fc'.encode('utf-8')])
124+
self.assertEqual(f[b'1'], b'a')
123125

124126
def test_line_endings(self):
125127
# test for bug #1172763: dumbdbm would die if the line endings

Lib/test/test_dbm_gnu.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ def test_key_methods(self):
2020
self.assertEqual(self.g.keys(), [])
2121
self.g['a'] = 'b'
2222
self.g['12345678910'] = '019237410982340912840198242'
23+
self.g[b'bytes'] = b'data'
2324
key_set = set(self.g.keys())
2425
self.assertEqual(key_set, set([b'a', b'12345678910']))
2526
self.assert_(b'a' in self.g)
27+
self.assertEqual(self.g[b'bytes'], b'data')
2628
key = self.g.firstkey()
2729
while key:
2830
self.assert_(key in key_set)

Lib/test/test_dbm_ndbm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ def test_keys(self):
2020
self.d = dbm.ndbm.open(self.filename, 'c')
2121
self.assert_(self.d.keys() == [])
2222
self.d['a'] = 'b'
23+
self.d[b'bytes'] = b'data'
2324
self.d['12345678910'] = '019237410982340912840198242'
2425
self.d.keys()
2526
self.assert_(b'a' in self.d)
27+
self.assertEqual(self.d[b'bytes'], b'data')
2628
self.d.close()
2729

2830
def test_modes(self):

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ Library
2828
- Issue #4383: When IDLE cannot make the connection to its subprocess, it would
2929
fail to properly display the error message.
3030

31+
Docs
32+
----
33+
34+
- Issue #3799: Document that dbm.gnu and dbm.ndbm will accept string arguments
35+
for keys and values which will be converted to bytes before committal.
36+
3137

3238
What's New in Python 3.0 release candidate 3?
3339
=============================================

Modules/_dbmmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
122122

123123
if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
124124
PyErr_SetString(PyExc_TypeError,
125-
"dbm mappings have string keys only");
125+
"dbm mappings have bytes or string keys only");
126126
return -1;
127127
}
128128
krec.dsize = tmp_size;
@@ -140,7 +140,7 @@ dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
140140
} else {
141141
if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
142142
PyErr_SetString(PyExc_TypeError,
143-
"dbm mappings have byte string elements only");
143+
"dbm mappings have byte or string elements only");
144144
return -1;
145145
}
146146
drec.dsize = tmp_size;

Modules/_gdbmmodule.c

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)