summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
Diffstat (limited to 'Python')
-rw-r--r--Python/import.c13
-rw-r--r--Python/pythonrun.c86
2 files changed, 96 insertions, 3 deletions
diff --git a/Python/import.c b/Python/import.c
index 21dcbd4..323b55a 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -91,6 +91,9 @@ static PyObject *extensions = NULL;
/* This table is defined in config.c: */
extern struct _inittab _PyImport_Inittab[];
+/* Method from Parser/tokenizer.c */
+extern char * PyTokenizer_FindEncoding(FILE *fp);
+
struct _inittab *PyImport_Inittab = _PyImport_Inittab;
/* these tables define the module suffixes that Python recognizes */
@@ -2558,6 +2561,7 @@ call_find_module(char *name, PyObject *path)
struct filedescr *fdp;
char pathname[MAXPATHLEN+1];
FILE *fp = NULL;
+ char *encoding = NULL;
pathname[0] = '\0';
if (path == Py_None)
@@ -2566,7 +2570,14 @@ call_find_module(char *name, PyObject *path)
if (fdp == NULL)
return NULL;
if (fp != NULL) {
- fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
+ if (strchr(fdp->mode, 'b') == NULL) {
+ /* Python text file, get encoding from tokenizer */
+ encoding = PyTokenizer_FindEncoding(fp);
+ encoding = (encoding != NULL) ? encoding :
+ (char*)PyUnicode_GetDefaultEncoding();
+ }
+ fob = PyFile_FromFileEx(fp, pathname, fdp->mode, fclose, -1,
+ (char*)encoding, NULL);
if (fob == NULL) {
fclose(fp);
return NULL;
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 4e239c9..f641547 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -51,6 +51,7 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
/* Forward */
static void initmain(void);
static void initsite(void);
+static int initstdio(void);
static PyObject *run_mod(mod_ty, const char *, PyObject *, PyObject *,
PyCompilerFlags *, PyArena *);
static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
@@ -241,6 +242,9 @@ Py_InitializeEx(int install_sigs)
initsigs(); /* Signal handling stuff, including initintr() */
initmain(); /* Module __main__ */
+ if (initstdio() < 0)
+ Py_FatalError(
+ "Py_Initialize: can't initialize sys standard streams");
if (!Py_NoSiteFlag)
initsite(); /* Module site */
@@ -676,6 +680,81 @@ initsite(void)
}
}
+/* Initialize sys.stdin, stdout, stderr and __builtin__.open */
+static int
+initstdio(void)
+{
+ PyObject *iomod = NULL, *wrapper;
+ PyObject *bimod = NULL;
+ PyObject *m;
+ PyObject *std = NULL;
+ int status = 0;
+
+ /* Hack to avoid a nasty recursion issue when Python is invoked
+ in verbose mode: pre-import the Latin-1 and UTF-8 codecs */
+ if ((m = PyImport_ImportModule("encodings.utf_8")) == NULL) {
+ goto error;
+ }
+ Py_DECREF(m);
+
+ if (!(m = PyImport_ImportModule("encodings.latin_1"))) {
+ goto error;
+ }
+ Py_DECREF(m);
+
+ if (!(bimod = PyImport_ImportModule("__builtin__"))) {
+ goto error;
+ }
+
+ if (!(iomod = PyImport_ImportModule("io"))) {
+ goto error;
+ }
+ if (!(wrapper = PyObject_GetAttrString(iomod, "OpenWrapper"))) {
+ goto error;
+ }
+
+ /* Set __builtin__.open */
+ if (PyObject_SetAttrString(bimod, "open", wrapper) == -1) {
+ goto error;
+ }
+
+ /* Set sys.stdin */
+ if (!(std = PyFile_FromFileEx(stdin, "<stdin>", "r", fclose, -1,
+ NULL, "\n"))) {
+ goto error;
+ }
+ PySys_SetObject("__stdin__", std);
+ PySys_SetObject("stdin", std);
+ Py_DECREF(std);
+
+ /* Set sys.stdout */
+ if (!(std = PyFile_FromFileEx(stdout, "<stdout>", "w", fclose, -1,
+ NULL, "\n"))) {
+ goto error;
+ }
+ PySys_SetObject("__stdout__", std);
+ PySys_SetObject("stdout", std);
+ Py_DECREF(std);
+
+ /* Set sys.stderr */
+ if (!(std = PyFile_FromFileEx(stderr, "<stderr>", "w", fclose, -1,
+ NULL, "\n"))) {
+ goto error;
+ }
+ PySys_SetObject("__stderr__", std);
+ PySys_SetObject("stderr", std);
+ Py_DECREF(std);
+
+ if (0) {
+ error:
+ status = -1;
+ }
+
+ Py_XDECREF(bimod);
+ Py_XDECREF(iomod);
+ return status;
+}
+
/* Parse input from a file and execute it */
int
@@ -1146,10 +1225,10 @@ PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
int err = 0;
PyObject *f = PySys_GetObject("stderr");
Py_INCREF(value);
- if (f == NULL)
+ if (f == NULL) {
_PyObject_Dump(value);
- if (f == NULL)
fprintf(stderr, "lost sys.stderr\n");
+ }
else {
fflush(stdout);
if (tb && tb != Py_None)
@@ -1589,6 +1668,9 @@ void
Py_FatalError(const char *msg)
{
fprintf(stderr, "Fatal Python error: %s\n", msg);
+ if (PyErr_Occurred()) {
+ PyErr_Print();
+ }
#ifdef MS_WINDOWS
OutputDebugString("Fatal Python error: ");
OutputDebugString(msg);