Kaydet (Commit) 349fd521 authored tarafından Victor Stinner's avatar Victor Stinner

Issue #8468: bz2.BZ2File() accepts str with surrogates and bytes filenames

üst 13daf12b
...@@ -329,6 +329,8 @@ C-API ...@@ -329,6 +329,8 @@ C-API
Library Library
------- -------
- Issue #8468: bz2.BZ2File() accepts str with surrogates and bytes filenames
- Issue #8451: Syslog module now uses basename(sys.argv[0]) instead of - Issue #8451: Syslog module now uses basename(sys.argv[0]) instead of
the string "python" as the *ident*. openlog() arguments are all optional the string "python" as the *ident*. openlog() arguments are all optional
and keywords. and keywords.
......
...@@ -1162,6 +1162,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) ...@@ -1162,6 +1162,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
{ {
static char *kwlist[] = {"filename", "mode", "buffering", static char *kwlist[] = {"filename", "mode", "buffering",
"compresslevel", 0}; "compresslevel", 0};
PyObject *name_obj = NULL;
char *name; char *name;
char *mode = "r"; char *mode = "r";
int buffering = -1; int buffering = -1;
...@@ -1171,14 +1172,17 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) ...@@ -1171,14 +1172,17 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
self->size = -1; self->size = -1;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|sii:BZ2File", if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|sii:BZ2File",
kwlist, &name, &mode, &buffering, kwlist, PyUnicode_FSConverter, &name_obj,
&mode, &buffering,
&compresslevel)) &compresslevel))
return -1; return -1;
name = PyBytes_AsString(name_obj);
if (compresslevel < 1 || compresslevel > 9) { if (compresslevel < 1 || compresslevel > 9) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"compresslevel must be between 1 and 9"); "compresslevel must be between 1 and 9");
Py_DECREF(name_obj);
return -1; return -1;
} }
...@@ -1202,6 +1206,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) ...@@ -1202,6 +1206,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
if (error) { if (error) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"invalid mode char %c", *mode); "invalid mode char %c", *mode);
Py_DECREF(name_obj);
return -1; return -1;
} }
mode++; mode++;
...@@ -1216,6 +1221,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs) ...@@ -1216,6 +1221,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
mode = (mode_char == 'r') ? "rb" : "wb"; mode = (mode_char == 'r') ? "rb" : "wb";
self->rawfp = fopen(name, mode); self->rawfp = fopen(name, mode);
Py_DECREF(name_obj);
if (self->rawfp == NULL) { if (self->rawfp == NULL) {
PyErr_SetFromErrno(PyExc_IOError); PyErr_SetFromErrno(PyExc_IOError);
return -1; return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment