| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| 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 | |
|---|---|---|---|
@@ -73,6 +73,9 @@ def test_dumbdbm_modification(self): | |||
| 73 | 73 | f = dumbdbm.open(_fname, 'w') | |
| 74 | 74 | self._dict[b'g'] = f[b'g'] = b"indented" | |
| 75 | 75 | self.read_helper(f) | |
| 76 | + # setdefault() works as in the dict interface | ||
| 77 | + self.assertEqual(f.setdefault(b'xxx', b'foo'), b'foo') | ||
| 78 | + self.assertEqual(f[b'xxx'], b'foo') | ||
| 76 | 79 | f.close() | |
| 77 | 80 | ||
| 78 | 81 | def test_dumbdbm_read(self): | |
@@ -85,6 +88,12 @@ def test_dumbdbm_read(self): | |||
| 85 | 88 | with self.assertWarnsRegex(DeprecationWarning, | |
| 86 | 89 | 'The database is opened for reading only'): | |
| 87 | 90 | del f[b'a'] | |
| 91 | + # get() works as in the dict interface | ||
| 92 | + self.assertEqual(f.get(b'b'), self._dict[b'b']) | ||
| 93 | + self.assertEqual(f.get(b'xxx', b'foo'), b'foo') | ||
| 94 | + self.assertIsNone(f.get(b'xxx')) | ||
| 95 | + with self.assertRaises(KeyError): | ||
| 96 | + f[b'xxx'] | ||
| 88 | 97 | f.close() | |
| 89 | 98 | ||
| 90 | 99 | 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 | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 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 | |
|---|---|---|---|
@@ -274,7 +274,7 @@ static PySequenceMethods dbm_as_sequence = { | |||
| 274 | 274 | _dbm.dbm.get | |
| 275 | 275 | ||
| 276 | 276 | key: str(accept={str, robuffer}, zeroes=True) | |
| 277 | - default: object(c_default="NULL") = b'' | ||
| 277 | + default: object = None | ||
| 278 | 278 | / | |
| 279 | 279 | ||
| 280 | 280 | Return the value for key if present, otherwise default. | |
@@ -283,7 +283,7 @@ Return the value for key if present, otherwise default. | |||
| 283 | 283 | static PyObject * | |
| 284 | 284 | _dbm_dbm_get_impl(dbmobject *self, const char *key, | |
| 285 | 285 | Py_ssize_clean_t key_length, PyObject *default_value) | |
| 286 | - /*[clinic end generated code: output=b44f95eba8203d93 input=a3a279957f85eb6d]*/ | ||
| 286 | + /*[clinic end generated code: output=b44f95eba8203d93 input=b788eba0ffad2e91]*/ | ||
| 287 | 287 | /*[clinic end generated code: output=4f5c0e523eaf1251 input=9402c0af8582dc69]*/ | |
| 288 | 288 | { | |
| 289 | 289 | 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 *const *args, Py_ssize_t nargs) | |||
| 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_ParseStack(args, nargs, "s#|O:get", | |
| 63 | 63 | &key, &key_length, &default_value)) { | |
@@ -141,4 +141,4 @@ dbmopen(PyObject *module, PyObject *const *args, Py_ssize_t nargs) | |||
| 141 | 141 | exit: | |
| 142 | 142 | return return_value; | |
| 143 | 143 | } | |
| 144 | - /*[clinic end generated code: output=5c858b4080a011a4 input=a9049054013a1b77]*/ | ||
| 144 | + /*[clinic end generated code: output=1cba297bc8d7c2c2 input=a9049054013a1b77]*/ | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments