Forward-port PYTHONIOENCODING.
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 2e93a3a..f2f14ed 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -701,6 +701,7 @@
 	PyObject *std = NULL;
 	int status = 0, fd;
 	PyObject * encoding_attr;
+	char *encoding, *errors;
 
 	/* Hack to avoid a nasty recursion issue when Python is invoked
 	   in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
@@ -730,6 +731,16 @@
 		goto error;
 	}
 
+	encoding = Py_GETENV("PYTHONIOENCODING");
+	if (encoding) {
+		encoding = strdup(encoding);
+		errors = strchr(encoding, ':');
+		if (errors) {
+			*errors = '\0';
+			errors++;
+		}
+	}
+
 	/* Set sys.stdin */
 	fd = fileno(stdin);
 	/* Under some conditions stdin, stdout and stderr may not be connected
@@ -745,8 +756,8 @@
 #endif
 	}
 	else {
-		if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, NULL, NULL,
-					  "\n", 0))) {
+		if (!(std = PyFile_FromFd(fd, "<stdin>", "r", -1, encoding, 
+					  errors, "\n", 0))) {
 			goto error;
 		}
 	} /* if (fd < 0) */
@@ -765,8 +776,8 @@
 #endif
 	}
 	else {
-		if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, NULL, NULL,
-					  "\n", 0))) {
+		if (!(std = PyFile_FromFd(fd, "<stdout>", "w", -1, encoding, 
+					  errors, "\n", 0))) {
 			goto error;
 		}
 	} /* if (fd < 0) */
@@ -786,8 +797,8 @@
 #endif
 	}
 	else {
-		if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, NULL, NULL,
-					  "\n", 0))) {
+		if (!(std = PyFile_FromFd(fd, "<stderr>", "w", -1, encoding,
+					  errors, "\n", 0))) {
 			goto error;
 		}
 	} /* if (fd < 0) */