Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
4fb0b8bc
Commit
4fb0b8bc
authored
Dec 12, 2018
by
Xiang Zhang
Committed by
GitHub
Dec 12, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-33106: change dbm key deletion error for readonly file from KeyError to dbm.error (#6295)
parent
5a718e91
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
57 additions
and
9 deletions
+57
-9
Doc/library/dbm.rst
Doc/library/dbm.rst
+4
-0
Doc/whatsnew/3.8.rst
Doc/whatsnew/3.8.rst
+6
-0
Lib/dbm/dumb.py
Lib/dbm/dumb.py
+2
-2
Lib/test/test_dbm_dumb.py
Lib/test/test_dbm_dumb.py
+2
-2
Lib/test/test_dbm_gnu.py
Lib/test/test_dbm_gnu.py
+11
-0
Lib/test/test_dbm_ndbm.py
Lib/test/test_dbm_ndbm.py
+11
-0
Misc/NEWS.d/next/Library/2018-03-30-01-20-35.bpo-33106.zncfvW.rst
...S.d/next/Library/2018-03-30-01-20-35.bpo-33106.zncfvW.rst
+2
-0
Modules/_dbmmodule.c
Modules/_dbmmodule.c
+12
-3
Modules/_gdbmmodule.c
Modules/_gdbmmodule.c
+7
-2
No files found.
Doc/library/dbm.rst
View file @
4fb0b8bc
...
...
@@ -73,6 +73,10 @@ available, as well as :meth:`get` and :meth:`setdefault`.
.. versionchanged:: 3.2
:meth:`get` and :meth:`setdefault` are now available in all database modules.
.. versionchanged:: 3.8
Deleting a key from a read-only database raises database module specific error
instead of :exc:`KeyError`.
Key and values are always stored as bytes. This means that when
strings are used they are implicitly converted to the default encoding before
being stored.
...
...
Doc/whatsnew/3.8.rst
View file @
4fb0b8bc
...
...
@@ -483,6 +483,12 @@ Changes in the Python API
external entities by default.
(Contributed by Christian Heimes in :issue:`17239`.)
* Deleting a key from a read-only :mod:`dbm` database (:mod:`dbm.dumb`,
:mod:`dbm.gnu` or :mod:`dbm.ndbm`) raises :attr:`error` (:exc:`dbm.dumb.error`,
:exc:`dbm.gnu.error` or :exc:`dbm.ndbm.error`) instead of :exc:`KeyError`.
(Contributed by Xiang Zhang in :issue:`33106`.)
CPython bytecode changes
------------------------
...
...
Lib/dbm/dumb.py
View file @
4fb0b8bc
...
...
@@ -185,7 +185,7 @@ class _Database(collections.abc.MutableMapping):
def
__setitem__
(
self
,
key
,
val
):
if
self
.
_readonly
:
raise
ValueE
rror
(
'The database is opened for reading only'
)
raise
e
rror
(
'The database is opened for reading only'
)
if
isinstance
(
key
,
str
):
key
=
key
.
encode
(
'utf-8'
)
elif
not
isinstance
(
key
,
(
bytes
,
bytearray
)):
...
...
@@ -222,7 +222,7 @@ class _Database(collections.abc.MutableMapping):
def
__delitem__
(
self
,
key
):
if
self
.
_readonly
:
raise
ValueE
rror
(
'The database is opened for reading only'
)
raise
e
rror
(
'The database is opened for reading only'
)
if
isinstance
(
key
,
str
):
key
=
key
.
encode
(
'utf-8'
)
self
.
_verify_open
()
...
...
Lib/test/test_dbm_dumb.py
View file @
4fb0b8bc
...
...
@@ -82,10 +82,10 @@ class DumbDBMTestCase(unittest.TestCase):
self
.
init_db
()
f
=
dumbdbm
.
open
(
_fname
,
'r'
)
self
.
read_helper
(
f
)
with
self
.
assertRaisesRegex
(
ValueE
rror
,
with
self
.
assertRaisesRegex
(
dumbdbm
.
e
rror
,
'The database is opened for reading only'
):
f
[
b'g'
]
=
b'x'
with
self
.
assertRaisesRegex
(
ValueE
rror
,
with
self
.
assertRaisesRegex
(
dumbdbm
.
e
rror
,
'The database is opened for reading only'
):
del
f
[
b'a'
]
# get() works as in the dict interface
...
...
Lib/test/test_dbm_gnu.py
View file @
4fb0b8bc
...
...
@@ -131,6 +131,17 @@ class TestGdbm(unittest.TestCase):
self
.
assertEqual
(
db
[
'Unicode key
\
U0001f40d
'
],
'Unicode value
\
U0001f40d
'
.
encode
())
def
test_write_readonly_file
(
self
):
with
gdbm
.
open
(
filename
,
'c'
)
as
db
:
db
[
b'bytes key'
]
=
b'bytes value'
with
gdbm
.
open
(
filename
,
'r'
)
as
db
:
with
self
.
assertRaises
(
gdbm
.
error
):
del
db
[
b'not exist key'
]
with
self
.
assertRaises
(
gdbm
.
error
):
del
db
[
b'bytes key'
]
with
self
.
assertRaises
(
gdbm
.
error
):
db
[
b'not exist key'
]
=
b'not exist value'
@
unittest
.
skipUnless
(
TESTFN_NONASCII
,
'requires OS support of non-ASCII encodings'
)
def
test_nonascii_filename
(
self
):
...
...
Lib/test/test_dbm_ndbm.py
View file @
4fb0b8bc
...
...
@@ -90,6 +90,17 @@ class DbmTestCase(unittest.TestCase):
self
.
assertEqual
(
db
[
'Unicode key
\
U0001f40d
'
],
'Unicode value
\
U0001f40d
'
.
encode
())
def
test_write_readonly_file
(
self
):
with
dbm
.
ndbm
.
open
(
self
.
filename
,
'c'
)
as
db
:
db
[
b'bytes key'
]
=
b'bytes value'
with
dbm
.
ndbm
.
open
(
self
.
filename
,
'r'
)
as
db
:
with
self
.
assertRaises
(
error
):
del
db
[
b'not exist key'
]
with
self
.
assertRaises
(
error
):
del
db
[
b'bytes key'
]
with
self
.
assertRaises
(
error
):
db
[
b'not exist key'
]
=
b'not exist value'
@
unittest
.
skipUnless
(
support
.
TESTFN_NONASCII
,
'requires OS support of non-ASCII encodings'
)
def
test_nonascii_filename
(
self
):
...
...
Misc/NEWS.d/next/Library/2018-03-30-01-20-35.bpo-33106.zncfvW.rst
0 → 100644
View file @
4fb0b8bc
Deleting a key from a read-only dbm database raises module specfic error
instead of KeyError.
Modules/_dbmmodule.c
View file @
4fb0b8bc
...
...
@@ -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 byte
s
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 byte
s or
string elements only"
);
return
NULL
;
}
val
.
dsize
=
tmp_size
;
...
...
Modules/_gdbmmodule.c
View file @
4fb0b8bc
...
...
@@ -186,14 +186,19 @@ dbm_ass_sub(dbmobject *dp, PyObject *v, PyObject *w)
dp
->
di_size
=
-
1
;
if
(
w
==
NULL
)
{
if
(
gdbm_delete
(
dp
->
di_dbm
,
krec
)
<
0
)
{
PyErr_SetObject
(
PyExc_KeyError
,
v
);
if
(
gdbm_errno
==
GDBM_ITEM_NOT_FOUND
)
{
PyErr_SetObject
(
PyExc_KeyError
,
v
);
}
else
{
PyErr_SetString
(
DbmError
,
gdbm_strerror
(
gdbm_errno
));
}
return
-
1
;
}
}
else
{
if
(
!
PyArg_Parse
(
w
,
"s#"
,
&
drec
.
dptr
,
&
drec
.
dsize
))
{
PyErr_SetString
(
PyExc_TypeError
,
"gdbm mappings have byte or string elements only"
);
"gdbm mappings have byte
s
or string elements only"
);
return
-
1
;
}
errno
=
0
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment