diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-02-26 14:02:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-26 14:02:22 (GMT) |
commit | 6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e (patch) | |
tree | 3dc8ecabed25ffe13d7e79630a9eea9c3c8f3177 /Modules/_gdbmmodule.c | |
parent | 973cae07d6ce7f5a93bd9cd3bcb724a96cfe14e9 (diff) | |
download | cpython-6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e.zip cpython-6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e.tar.gz cpython-6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e.tar.bz2 |
bpo-32922: dbm.open() now encodes filename with the filesystem encoding. (GH-5832)
Diffstat (limited to 'Modules/_gdbmmodule.c')
-rw-r--r-- | Modules/_gdbmmodule.c | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index 12d973b..9996d8c 100644 --- a/Modules/_gdbmmodule.c +++ b/Modules/_gdbmmodule.c @@ -527,7 +527,7 @@ static PyTypeObject Dbmtype = { /*[clinic input] _gdbm.open as dbmopen - filename as name: str + filename: unicode flags: str="r" mode: int(py_default="0o666") = 0o666 / @@ -557,8 +557,9 @@ when the database has to be created. It defaults to octal 0o666. [clinic start generated code]*/ static PyObject * -dbmopen_impl(PyObject *module, const char *name, const char *flags, int mode) -/*[clinic end generated code: output=31aa1bafdf5da688 input=55563cd60e51984a]*/ +dbmopen_impl(PyObject *module, PyObject *filename, const char *flags, + int mode) +/*[clinic end generated code: output=9527750f5df90764 input=3be0b0875974b928]*/ { int iflags; @@ -606,7 +607,19 @@ dbmopen_impl(PyObject *module, const char *name, const char *flags, int mode) } } - return newdbmobject(name, 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 const char dbmmodule_open_flags[] = "rwcn" |