Commit b5f605b6 authored by Victor Stinner's avatar Victor Stinner

Merged revisions 82059,82061 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r82059 | victor.stinner | 2010-06-18 01:08:50 +0200 (ven., 18 juin 2010) | 5 lines

  Issue #6543: Write the traceback in the terminal encoding instead of utf-8.
  Fix the encoding of the modules filename.

  Reindent also traceback.h, just because I hate tabs :-)
........
  r82061 | victor.stinner | 2010-06-18 01:17:37 +0200 (ven., 18 juin 2010) | 2 lines

  Issue #6543: Mention the author of the patch, Amaury Forgeot d'Arc
........
parent 27d253af
...@@ -10,16 +10,16 @@ struct _frame; ...@@ -10,16 +10,16 @@ struct _frame;
/* Traceback interface */ /* Traceback interface */
typedef struct _traceback { typedef struct _traceback {
PyObject_HEAD PyObject_HEAD
struct _traceback *tb_next; struct _traceback *tb_next;
struct _frame *tb_frame; struct _frame *tb_frame;
int tb_lasti; int tb_lasti;
int tb_lineno; int tb_lineno;
} PyTracebackObject; } PyTracebackObject;
PyAPI_FUNC(int) PyTraceBack_Here(struct _frame *); PyAPI_FUNC(int) PyTraceBack_Here(struct _frame *);
PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *); PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *);
PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, const char *, int, int); PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int);
/* Reveal traceback type so we can typecheck traceback objects */ /* Reveal traceback type so we can typecheck traceback objects */
PyAPI_DATA(PyTypeObject) PyTraceBack_Type; PyAPI_DATA(PyTypeObject) PyTraceBack_Type;
......
...@@ -12,6 +12,10 @@ What's New in Python 3.1.3? ...@@ -12,6 +12,10 @@ What's New in Python 3.1.3?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #6543: Write the traceback in the terminal encoding instead of utf-8.
Fix the encoding of the modules filename. Patch written by Amaury Forgeot
d'Arc.
- Issue #9011: Remove buggy and unnecessary (in 3.x) ST->AST - Issue #9011: Remove buggy and unnecessary (in 3.x) ST->AST
compilation code dealing with unary minus applied to a constant. compilation code dealing with unary minus applied to a constant.
The removed code was mutating the ST, causing a second compilation The removed code was mutating the ST, causing a second compilation
......
...@@ -282,8 +282,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject ...@@ -282,8 +282,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
PyFile_WriteString("\n", f_stderr); PyFile_WriteString("\n", f_stderr);
} }
else else
if (_Py_DisplaySourceLine(f_stderr, _PyUnicode_AsString(filename), if (_Py_DisplaySourceLine(f_stderr, filename, lineno, 2) < 0)
lineno, 2) < 0)
return; return;
PyErr_Clear(); PyErr_Clear();
} }
......
...@@ -4046,7 +4046,7 @@ makecode(struct compiler *c, struct assembler *a) ...@@ -4046,7 +4046,7 @@ makecode(struct compiler *c, struct assembler *a)
freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
if (!freevars) if (!freevars)
goto error; goto error;
filename = PyUnicode_DecodeFSDefault(c->c_filename); filename = PyUnicode_FromString(c->c_filename);
if (!filename) if (!filename)
goto error; goto error;
......
...@@ -1160,7 +1160,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, ...@@ -1160,7 +1160,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
if (PyDict_GetItemString(d, "__file__") == NULL) { if (PyDict_GetItemString(d, "__file__") == NULL) {
PyObject *f; PyObject *f;
f = PyUnicode_DecodeFSDefault(filename); f = PyUnicode_FromString(filename);
if (f == NULL) if (f == NULL)
return -1; return -1;
if (PyDict_SetItemString(d, "__file__", f) < 0) { if (PyDict_SetItemString(d, "__file__", f) < 0) {
......
...@@ -134,33 +134,38 @@ PyTraceBack_Here(PyFrameObject *frame) ...@@ -134,33 +134,38 @@ PyTraceBack_Here(PyFrameObject *frame)
return 0; return 0;
} }
static int static PyObject *
_Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open_flags) _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io)
{ {
int i; Py_ssize_t i;
int fd = -1; PyObject *binary;
PyObject *v; PyObject *v;
Py_ssize_t _npath; Py_ssize_t npath;
int npath;
size_t taillen; size_t taillen;
PyObject *syspath; PyObject *syspath;
const char* path; const char* path;
const char* tail; const char* tail;
const char* filepath;
Py_ssize_t len; Py_ssize_t len;
filepath = _PyUnicode_AsString(filename);
if (filepath == NULL) {
PyErr_Clear();
return NULL;
}
/* Search tail of filename in sys.path before giving up */ /* Search tail of filename in sys.path before giving up */
tail = strrchr(filename, SEP); tail = strrchr(filepath, SEP);
if (tail == NULL) if (tail == NULL)
tail = filename; tail = filepath;
else else
tail++; tail++;
taillen = strlen(tail); taillen = strlen(tail);
syspath = PySys_GetObject("path"); syspath = PySys_GetObject("path");
if (syspath == NULL || !PyList_Check(syspath)) if (syspath == NULL || !PyList_Check(syspath))
return -1; return NULL;
_npath = PyList_Size(syspath); npath = PyList_Size(syspath);
npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
for (i = 0; i < npath; i++) { for (i = 0; i < npath; i++) {
v = PyList_GetItem(syspath, i); v = PyList_GetItem(syspath, i);
...@@ -171,6 +176,10 @@ _Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open ...@@ -171,6 +176,10 @@ _Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open
if (!PyUnicode_Check(v)) if (!PyUnicode_Check(v))
continue; continue;
path = _PyUnicode_AsStringAndSize(v, &len); path = _PyUnicode_AsStringAndSize(v, &len);
if (path == NULL) {
PyErr_Clear();
continue;
}
if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1)
continue; /* Too long */ continue; /* Too long */
strcpy(namebuf, path); strcpy(namebuf, path);
...@@ -179,31 +188,27 @@ _Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open ...@@ -179,31 +188,27 @@ _Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open
if (len > 0 && namebuf[len-1] != SEP) if (len > 0 && namebuf[len-1] != SEP)
namebuf[len++] = SEP; namebuf[len++] = SEP;
strcpy(namebuf+len, tail); strcpy(namebuf+len, tail);
Py_BEGIN_ALLOW_THREADS
fd = open(namebuf, open_flags); binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb");
Py_END_ALLOW_THREADS if (binary != NULL)
if (0 <= fd) { return binary;
return fd; PyErr_Clear();
}
} }
return -1; return NULL;
} }
int int
_Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent) _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
{ {
int err = 0; int err = 0;
int fd; int fd;
int i; int i;
char *found_encoding; char *found_encoding;
char *encoding; char *encoding;
PyObject *io;
PyObject *binary;
PyObject *fob = NULL; PyObject *fob = NULL;
PyObject *lineobj = NULL; PyObject *lineobj = NULL;
#ifdef O_BINARY
const int open_flags = O_RDONLY | O_BINARY; /* necessary for Windows */
#else
const int open_flags = O_RDONLY;
#endif
char buf[MAXPATHLEN+1]; char buf[MAXPATHLEN+1];
Py_UNICODE *u, *p; Py_UNICODE *u, *p;
Py_ssize_t len; Py_ssize_t len;
...@@ -211,27 +216,32 @@ _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent) ...@@ -211,27 +216,32 @@ _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
/* open the file */ /* open the file */
if (filename == NULL) if (filename == NULL)
return 0; return 0;
Py_BEGIN_ALLOW_THREADS
fd = open(filename, open_flags); io = PyImport_ImportModuleNoBlock("io");
Py_END_ALLOW_THREADS if (io == NULL)
if (fd < 0) { return -1;
fd = _Py_FindSourceFile(filename, buf, sizeof(buf), open_flags); binary = PyObject_CallMethod(io, "open", "Os", filename, "rb");
if (fd < 0)
if (binary == NULL) {
binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
if (binary == NULL) {
Py_DECREF(io);
return 0; return 0;
filename = buf; }
} }
/* use the right encoding to decode the file as unicode */ /* use the right encoding to decode the file as unicode */
fd = PyObject_AsFileDescriptor(binary);
found_encoding = PyTokenizer_FindEncoding(fd); found_encoding = PyTokenizer_FindEncoding(fd);
encoding = (found_encoding != NULL) ? found_encoding : encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
(char*)PyUnicode_GetDefaultEncoding();
lseek(fd, 0, 0); /* Reset position */ lseek(fd, 0, 0); /* Reset position */
fob = PyFile_FromFd(fd, (char*)filename, "r", -1, (char*)encoding, fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding);
NULL, NULL, 1); Py_DECREF(io);
Py_DECREF(binary);
PyMem_FREE(found_encoding); PyMem_FREE(found_encoding);
if (fob == NULL) { if (fob == NULL) {
PyErr_Clear(); PyErr_Clear();
close(fd);
return 0; return 0;
} }
...@@ -288,17 +298,19 @@ _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent) ...@@ -288,17 +298,19 @@ _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
} }
static int static int
tb_displayline(PyObject *f, const char *filename, int lineno, const char *name) tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
{ {
int err = 0; int err;
char linebuf[2000]; PyObject *line;
if (filename == NULL || name == NULL) if (filename == NULL || name == NULL)
return -1; return -1;
/* This is needed by Emacs' compile command */ line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
#define FMT " File \"%.500s\", line %d, in %.500s\n" filename, lineno, name);
PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name); if (line == NULL)
err = PyFile_WriteString(linebuf, f); return -1;
err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
Py_DECREF(line);
if (err != 0) if (err != 0)
return err; return err;
return _Py_DisplaySourceLine(f, filename, lineno, 4); return _Py_DisplaySourceLine(f, filename, lineno, 4);
...@@ -317,10 +329,9 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) ...@@ -317,10 +329,9 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
while (tb != NULL && err == 0) { while (tb != NULL && err == 0) {
if (depth <= limit) { if (depth <= limit) {
err = tb_displayline(f, err = tb_displayline(f,
_PyUnicode_AsString( tb->tb_frame->f_code->co_filename,
tb->tb_frame->f_code->co_filename), tb->tb_lineno,
tb->tb_lineno, tb->tb_frame->f_code->co_name);
_PyUnicode_AsString(tb->tb_frame->f_code->co_name));
} }
depth--; depth--;
tb = tb->tb_next; tb = tb->tb_next;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment