summaryrefslogtreecommitdiff
path: root/Modules/_dbmmodule.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-02-26 06:25:40 -0800
committerGitHub <noreply@github.com>2018-02-26 06:25:40 -0800
commita9e0b070b3e36701d0139361c769d374c4aacf1a (patch)
treebabb8e457884a5d0fb852b7c7df9d77d9e941db7 /Modules/_dbmmodule.c
parent4cffe2f66b581fa7538f6de884d54a5c7364d8e0 (diff)
downloadcpython-git-a9e0b070b3e36701d0139361c769d374c4aacf1a.tar.gz
bpo-32922: dbm.open() now encodes filename with the filesystem encoding. (GH-5832)
(cherry picked from commit 6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Modules/_dbmmodule.c')
-rw-r--r--Modules/_dbmmodule.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
index 7e1344177b..8afd92cf3c 100644
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -412,7 +412,7 @@ static PyTypeObject Dbmtype = {
_dbm.open as dbmopen
- filename: str
+ filename: unicode
The filename to open.
flags: str="r"
@@ -429,9 +429,9 @@ Return a database object.
[clinic start generated code]*/
static PyObject *
-dbmopen_impl(PyObject *module, const char *filename, const char *flags,
+dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
int mode)
-/*[clinic end generated code: output=5fade8cf16e0755f input=226334bade5764e6]*/
+/*[clinic end generated code: output=9527750f5df90764 input=376a9d903a50df59]*/
{
int iflags;
@@ -450,7 +450,20 @@ dbmopen_impl(PyObject *module, const char *filename, const char *flags,
"arg 2 to open should be 'r', 'w', 'c', or 'n'");
return NULL;
}
- return newdbmobject(filename, iflags, mode);
+
+ PyObject *filenamebytes = PyUnicode_EncodeFSDefault(filename);
+ if (filenamebytes == NULL) {
+ return NULL;
+ }
+ const char *name = PyBytes_AS_STRING(filenamebytes);
+ if (strlen(name) != (size_t)PyBytes_GET_SIZE(filenamebytes)) {
+ Py_DECREF(filenamebytes);
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ return NULL;
+ }
+ PyObject *self = newdbmobject(name, iflags, mode);
+ Py_DECREF(filenamebytes);
+ return self;
}
static PyMethodDef dbmmodule_methods[] = {