From 6f600ff1734ca2fdcdd37a809adf8130f0d8cc4e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 26 Feb 2018 16:02:22 +0200 Subject: bpo-32922: dbm.open() now encodes filename with the filesystem encoding. (GH-5832) --- Modules/_gdbmmodule.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'Modules/_gdbmmodule.c') diff --git a/Modules/_gdbmmodule.c b/Modules/_gdbmmodule.c index 12d973b5ce..9996d8c26f 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" -- cgit v1.2.1