[Python-checkins] r80402 - in python/branches/py3k: Misc/NEWS Modules/bz2module.c

victor.stinner python-checkins at python.org
Fri Apr 23 12:56:17 CEST 2010


Author: victor.stinner
Date: Fri Apr 23 12:56:17 2010
New Revision: 80402

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


Modified:
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Modules/bz2module.c

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Fri Apr 23 12:56:17 2010
@@ -329,6 +329,8 @@
 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.

Modified: python/branches/py3k/Modules/bz2module.c
==============================================================================
--- python/branches/py3k/Modules/bz2module.c	(original)
+++ python/branches/py3k/Modules/bz2module.c	Fri Apr 23 12:56:17 2010
@@ -1162,6 +1162,7 @@
 {
 	static char *kwlist[] = {"filename", "mode", "buffering",
 				 "compresslevel", 0};
+	PyObject *name_obj = NULL;
 	char *name;
 	char *mode = "r";
 	int buffering = -1;
@@ -1171,14 +1172,17 @@
 
 	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 @@
 		if (error) {
 			PyErr_Format(PyExc_ValueError,
 				     "invalid mode char %c", *mode);
+			Py_DECREF(name_obj);
 			return -1;
 		}
 		mode++;
@@ -1216,6 +1221,7 @@
 	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;


More information about the Python-checkins mailing list