diff options
author | Xiang Zhang <angwerzx@126.com> | 2018-12-12 12:46:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-12 12:46:55 (GMT) |
commit | 4fb0b8bc25c52aae8dcb4353e69c1c88999a9a53 (patch) | |
tree | aa3b18ad394fbd5056af5a366e2b4ca6b91d5296 /Modules/_dbmmodule.c | |
parent | 5a718e918db6211b633a7afb2bf537eb5b56cb1b (diff) | |
download | cpython-4fb0b8bc25c52aae8dcb4353e69c1c88999a9a53.zip cpython-4fb0b8bc25c52aae8dcb4353e69c1c88999a9a53.tar.gz cpython-4fb0b8bc25c52aae8dcb4353e69c1c88999a9a53.tar.bz2 |
bpo-33106: change dbm key deletion error for readonly file from KeyError to dbm.error (#6295)
Diffstat (limited to 'Modules/_dbmmodule.c')
-rw-r--r-- | Modules/_dbmmodule.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c index 081184a..21784ae 100644 --- a/Modules/_dbmmodule.c +++ b/Modules/_dbmmodule.c @@ -36,6 +36,7 @@ class _dbm.dbm "dbmobject *" "&Dbmtype" typedef struct { PyObject_HEAD + int flags; int di_size; /* -1 means recompute */ DBM *di_dbm; } dbmobject; @@ -60,6 +61,7 @@ newdbmobject(const char *file, int flags, int mode) if (dp == NULL) return NULL; dp->di_size = -1; + dp->flags = flags; /* See issue #19296 */ if ( (dp->di_dbm = dbm_open((char *)file, flags, mode)) == 0 ) { PyErr_SetFromErrnoWithFilename(DbmError, file); @@ -143,13 +145,20 @@ dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w) if (w == NULL) { if ( dbm_delete(dp->di_dbm, krec) < 0 ) { dbm_clearerr(dp->di_dbm); - PyErr_SetObject(PyExc_KeyError, v); + /* we might get a failure for reasons like file corrupted, + but we are not able to distinguish it */ + if (dp->flags & O_RDWR) { + PyErr_SetObject(PyExc_KeyError, v); + } + else { + PyErr_SetString(DbmError, "cannot delete item from database"); + } return -1; } } else { if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, - "dbm mappings have byte or string elements only"); + "dbm mappings have bytes or string elements only"); return -1; } drec.dsize = tmp_size; @@ -335,7 +344,7 @@ _dbm_dbm_setdefault_impl(dbmobject *self, const char *key, else { if ( !PyArg_Parse(default_value, "s#", &val.dptr, &tmp_size) ) { PyErr_SetString(PyExc_TypeError, - "dbm mappings have byte string elements only"); + "dbm mappings have bytes or string elements only"); return NULL; } val.dsize = tmp_size; |