[Python-checkins] r52415 - in python/trunk: Doc/lib/libos.tex Modules/posixmodule.c

ronald.oussoren python-checkins at python.org
Sun Oct 22 12:45:19 CEST 2006


Author: ronald.oussoren
Date: Sun Oct 22 12:45:18 2006
New Revision: 52415

Modified:
   python/trunk/Doc/lib/libos.tex
   python/trunk/Modules/posixmodule.c
Log:
Patch #1580674: with this patch os.readlink uses the filesystem encoding to
decode unicode objects and returns an unicode object when the argument is one.


Modified: python/trunk/Doc/lib/libos.tex
==============================================================================
--- python/trunk/Doc/lib/libos.tex	(original)
+++ python/trunk/Doc/lib/libos.tex	Sun Oct 22 12:45:18 2006
@@ -920,6 +920,8 @@
 points.  The result may be either an absolute or relative pathname; if
 it is relative, it may be converted to an absolute pathname using
 \code{os.path.join(os.path.dirname(\var{path}), \var{result})}.
+\versionchanged [If the \var{path} is a Unicode object the result will also
+be a Unicode object]{2.6}
 Availability: Macintosh, \UNIX.
 \end{funcdesc}
 

Modified: python/trunk/Modules/posixmodule.c
==============================================================================
--- python/trunk/Modules/posixmodule.c	(original)
+++ python/trunk/Modules/posixmodule.c	Sun Oct 22 12:45:18 2006
@@ -5687,17 +5687,53 @@
 static PyObject *
 posix_readlink(PyObject *self, PyObject *args)
 {
+	PyObject* v;
 	char buf[MAXPATHLEN];
 	char *path;
 	int n;
-	if (!PyArg_ParseTuple(args, "s:readlink", &path))
+#ifdef Py_USING_UNICODE
+	int arg_is_unicode = 0;
+#endif
+
+	if (!PyArg_ParseTuple(args, "et:readlink", 
+				Py_FileSystemDefaultEncoding, &path))
 		return NULL;
+#ifdef Py_USING_UNICODE
+	v = PySequence_GetItem(args, 0);
+	if (v == NULL) return NULL;
+
+	if (PyUnicode_Check(v)) {
+		arg_is_unicode = 1;
+	}
+	Py_DECREF(v);
+#endif
+
 	Py_BEGIN_ALLOW_THREADS
 	n = readlink(path, buf, (int) sizeof buf);
 	Py_END_ALLOW_THREADS
 	if (n < 0)
 		return posix_error_with_filename(path);
-	return PyString_FromStringAndSize(buf, n);
+
+	v = PyString_FromStringAndSize(buf, n);
+#ifdef Py_USING_UNICODE
+	if (arg_is_unicode) {
+		PyObject *w;
+
+		w = PyUnicode_FromEncodedObject(v,
+				Py_FileSystemDefaultEncoding,
+				"strict");
+		if (w != NULL) {
+			Py_DECREF(v);
+			v = w;
+		}
+		else {
+			/* fall back to the original byte string, as
+			   discussed in patch #683592 */
+			PyErr_Clear();
+		}
+	}
+#endif
+	return v;
 }
 #endif /* HAVE_READLINK */
 


More information about the Python-checkins mailing list