]> granicus.if.org Git - python/commitdiff
Issue #8468: bz2.BZ2File() accepts str with surrogates and bytes filenames
authorVictor Stinner <victor.stinner@haypocalc.com>
Fri, 23 Apr 2010 10:56:17 +0000 (10:56 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Fri, 23 Apr 2010 10:56:17 +0000 (10:56 +0000)
Misc/NEWS
Modules/bz2module.c

index d2279730d75c1059d9d4fddca8f078410e74008d..3ccc011a9713c805376b726fec7b9590425aa86e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -329,6 +329,8 @@ C-API
 Library
 -------
 
+- Issue #8468: bz2.BZ2File() accepts str with surrogates and bytes filenames
+
 - Issue #8451: Syslog module now uses basename(sys.argv[0]) instead of
   the string "python" as the *ident*.  openlog() arguments are all optional
   and keywords.
index 550f1cf9999102252e3c88e83ff2cfe6a98607a1..f0ddf5c9276f8b6f597c8ef6d08b1555c5a2a8cc 100644 (file)
@@ -1162,6 +1162,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
 {
        static char *kwlist[] = {"filename", "mode", "buffering",
                                 "compresslevel", 0};
+       PyObject *name_obj = NULL;
        char *name;
        char *mode = "r";
        int buffering = -1;
@@ -1171,14 +1172,17 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
 
        self->size = -1;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|sii:BZ2File",
-                                        kwlist, &name, &mode, &buffering,
+       if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|sii:BZ2File",
+                                        kwlist, PyUnicode_FSConverter, &name_obj,
+                                        &mode, &buffering,
                                         &compresslevel))
                return -1;
 
+       name = PyBytes_AsString(name_obj);
        if (compresslevel < 1 || compresslevel > 9) {
                PyErr_SetString(PyExc_ValueError,
                                "compresslevel must be between 1 and 9");
+               Py_DECREF(name_obj);
                return -1;
        }
 
@@ -1202,6 +1206,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
                if (error) {
                        PyErr_Format(PyExc_ValueError,
                                     "invalid mode char %c", *mode);
+                       Py_DECREF(name_obj);
                        return -1;
                }
                mode++;
@@ -1216,6 +1221,7 @@ BZ2File_init(BZ2FileObject *self, PyObject *args, PyObject *kwargs)
        mode = (mode_char == 'r') ? "rb" : "wb";
 
        self->rawfp = fopen(name, mode);
+       Py_DECREF(name_obj);
        if (self->rawfp == NULL) {
                PyErr_SetFromErrno(PyExc_IOError);
                return -1;