File tree Expand file tree Collapse file tree 7 files changed +38
-7
lines changed
Filter options
Expand file tree Collapse file tree 7 files changed +38
-7
lines changed
Original file line number Diff line number Diff line change @@ -86,12 +86,21 @@ def test_anydbm_modification(self):
86
86
f = dbm .open (_fname , 'c' )
87
87
self ._dict ['g' ] = f [b'g' ] = b"indented"
88
88
self .read_helper (f )
89
+ # setdefault() works as in the dict interface
90
+ self .assertEqual (f .setdefault (b'xxx' , b'foo' ), b'foo' )
91
+ self .assertEqual (f [b'xxx' ], b'foo' )
89
92
f .close ()
90
93
91
94
def test_anydbm_read (self ):
92
95
self .init_db ()
93
96
f = dbm .open (_fname , 'r' )
94
97
self .read_helper (f )
98
+ # get() works as in the dict interface
99
+ self .assertEqual (f .get (b'a' ), self ._dict ['a' ])
100
+ self .assertEqual (f .get (b'xxx' , b'foo' ), b'foo' )
101
+ self .assertIsNone (f .get (b'xxx' ))
102
+ with self .assertRaises (KeyError ):
103
+ f [b'xxx' ]
95
104
f .close ()
96
105
97
106
def test_anydbm_keys (self ):
Original file line number Diff line number Diff line change @@ -74,6 +74,9 @@ def test_dumbdbm_modification(self):
74
74
f = dumbdbm .open (_fname , 'w' )
75
75
self ._dict [b'g' ] = f [b'g' ] = b"indented"
76
76
self .read_helper (f )
77
+ # setdefault() works as in the dict interface
78
+ self .assertEqual (f .setdefault (b'xxx' , b'foo' ), b'foo' )
79
+ self .assertEqual (f [b'xxx' ], b'foo' )
77
80
f .close ()
78
81
79
82
def test_dumbdbm_read (self ):
@@ -86,6 +89,12 @@ def test_dumbdbm_read(self):
86
89
with self .assertWarnsRegex (DeprecationWarning ,
87
90
'The database is opened for reading only' ):
88
91
del f [b'a' ]
92
+ # get() works as in the dict interface
93
+ self .assertEqual (f .get (b'b' ), self ._dict [b'b' ])
94
+ self .assertEqual (f .get (b'xxx' , b'foo' ), b'foo' )
95
+ self .assertIsNone (f .get (b'xxx' ))
96
+ with self .assertRaises (KeyError ):
97
+ f [b'xxx' ]
89
98
f .close ()
90
99
91
100
def test_dumbdbm_keys (self ):
Original file line number Diff line number Diff line change @@ -32,9 +32,12 @@ def test_key_methods(self):
32
32
self .assertIn (key , key_set )
33
33
key_set .remove (key )
34
34
key = self .g .nextkey (key )
35
- self .assertRaises (KeyError , lambda : self .g ['xxx' ])
36
35
# get() and setdefault() work as in the dict interface
36
+ self .assertEqual (self .g .get (b'a' ), b'b' )
37
+ self .assertIsNone (self .g .get (b'xxx' ))
37
38
self .assertEqual (self .g .get (b'xxx' , b'foo' ), b'foo' )
39
+ with self .assertRaises (KeyError ):
40
+ self .g ['xxx' ]
38
41
self .assertEqual (self .g .setdefault (b'xxx' , b'foo' ), b'foo' )
39
42
self .assertEqual (self .g [b'xxx' ], b'foo' )
40
43
Original file line number Diff line number Diff line change @@ -18,14 +18,22 @@ def tearDown(self):
18
18
19
19
def test_keys (self ):
20
20
self .d = dbm .ndbm .open (self .filename , 'c' )
21
- self .assertTrue (self .d .keys () == [])
21
+ self .assertEqual (self .d .keys (), [])
22
22
self .d ['a' ] = 'b'
23
23
self .d [b'bytes' ] = b'data'
24
24
self .d ['12345678910' ] = '019237410982340912840198242'
25
25
self .d .keys ()
26
26
self .assertIn ('a' , self .d )
27
27
self .assertIn (b'a' , self .d )
28
28
self .assertEqual (self .d [b'bytes' ], b'data' )
29
+ # get() and setdefault() work as in the dict interface
30
+ self .assertEqual (self .d .get (b'a' ), b'b' )
31
+ self .assertIsNone (self .d .get (b'xxx' ))
32
+ self .assertEqual (self .d .get (b'xxx' , b'foo' ), b'foo' )
33
+ with self .assertRaises (KeyError ):
34
+ self .d ['xxx' ]
35
+ self .assertEqual (self .d .setdefault (b'xxx' , b'foo' ), b'foo' )
36
+ self .assertEqual (self .d [b'xxx' ], b'foo' )
29
37
self .d .close ()
30
38
31
39
def test_modes (self ):
Original file line number Diff line number Diff line change
1
+ Fixed crash in the get() method of the :mod: `dbm.ndbm ` database object when
2
+ it is called with a single argument.
Original file line number Diff line number Diff line change @@ -275,7 +275,7 @@ static PySequenceMethods dbm_as_sequence = {
275
275
_dbm.dbm.get
276
276
277
277
key: str(accept={str, robuffer}, zeroes=True)
278
- default: object(c_default="NULL") = b''
278
+ default: object = None
279
279
/
280
280
281
281
Return the value for key if present, otherwise default.
@@ -284,7 +284,7 @@ Return the value for key if present, otherwise default.
284
284
static PyObject *
285
285
_dbm_dbm_get_impl (dbmobject * self , const char * key ,
286
286
Py_ssize_clean_t key_length , PyObject * default_value )
287
- /*[clinic end generated code: output=b44f95eba8203d93 input=a3a279957f85eb6d ]*/
287
+ /*[clinic end generated code: output=b44f95eba8203d93 input=b788eba0ffad2e91 ]*/
288
288
/*[clinic end generated code: output=4f5c0e523eaf1251 input=9402c0af8582dc69]*/
289
289
{
290
290
datum dbm_key , val ;
Original file line number Diff line number Diff line change @@ -39,7 +39,7 @@ _dbm_dbm_keys(dbmobject *self, PyObject *Py_UNUSED(ignored))
39
39
}
40
40
41
41
PyDoc_STRVAR (_dbm_dbm_get__doc__ ,
42
- "get($self, key, default=b\'\' , /)\n"
42
+ "get($self, key, default=None , /)\n"
43
43
"--\n"
44
44
"\n"
45
45
"Return the value for key if present, otherwise default." );
@@ -57,7 +57,7 @@ _dbm_dbm_get(dbmobject *self, PyObject *args)
57
57
PyObject * return_value = NULL ;
58
58
const char * key ;
59
59
Py_ssize_clean_t key_length ;
60
- PyObject * default_value = NULL ;
60
+ PyObject * default_value = Py_None ;
61
61
62
62
if (!PyArg_ParseTuple (args , "s#|O:get" ,
63
63
& key , & key_length , & default_value )) {
@@ -141,4 +141,4 @@ dbmopen(PyObject *module, PyObject *args)
141
141
exit :
142
142
return return_value ;
143
143
}
144
- /*[clinic end generated code: output=001fabffcecb99f1 input=a9049054013a1b77]*/
144
+ /*[clinic end generated code: output=919cc4337be4a5d3 input=a9049054013a1b77]*/
You can’t perform that action at this time.
0 commit comments