Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25bb0fd

Browse files
author
Victor Stinner
committed
Merged revisions 82059,82061 via svnmerge from
svn+ssh://[email protected]/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 ........
1 parent 9b16326 commit 25bb0fd

6 files changed

Lines changed: 71 additions & 57 deletions

File tree

Include/traceback.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ struct _frame;
1010
/* Traceback interface */
1111

1212
typedef struct _traceback {
13-
PyObject_HEAD
14-
struct _traceback *tb_next;
15-
struct _frame *tb_frame;
16-
int tb_lasti;
17-
int tb_lineno;
13+
PyObject_HEAD
14+
struct _traceback *tb_next;
15+
struct _frame *tb_frame;
16+
int tb_lasti;
17+
int tb_lineno;
1818
} PyTracebackObject;
1919

2020
PyAPI_FUNC(int) PyTraceBack_Here(struct _frame *);
2121
PyAPI_FUNC(int) PyTraceBack_Print(PyObject *, PyObject *);
22-
PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, const char *, int, int);
22+
PyAPI_FUNC(int) _Py_DisplaySourceLine(PyObject *, PyObject *, int, int);
2323

2424
/* Reveal traceback type so we can typecheck traceback objects */
2525
PyAPI_DATA(PyTypeObject) PyTraceBack_Type;

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 3.1.3?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #6543: Write the traceback in the terminal encoding instead of utf-8.
16+
Fix the encoding of the modules filename. Patch written by Amaury Forgeot
17+
d'Arc.
18+
1519
- Issue #9011: Remove buggy and unnecessary (in 3.x) ST->AST
1620
compilation code dealing with unary minus applied to a constant.
1721
The removed code was mutating the ST, causing a second compilation

Python/_warnings.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ show_warning(PyObject *filename, int lineno, PyObject *text, PyObject
282282
PyFile_WriteString("\n", f_stderr);
283283
}
284284
else
285-
if (_Py_DisplaySourceLine(f_stderr, _PyUnicode_AsString(filename),
286-
lineno, 2) < 0)
285+
if (_Py_DisplaySourceLine(f_stderr, filename, lineno, 2) < 0)
287286
return;
288287
PyErr_Clear();
289288
}

Python/compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4046,7 +4046,7 @@ makecode(struct compiler *c, struct assembler *a)
40464046
freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
40474047
if (!freevars)
40484048
goto error;
4049-
filename = PyUnicode_DecodeFSDefault(c->c_filename);
4049+
filename = PyUnicode_FromString(c->c_filename);
40504050
if (!filename)
40514051
goto error;
40524052

Python/pythonrun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
11601160
d = PyModule_GetDict(m);
11611161
if (PyDict_GetItemString(d, "__file__") == NULL) {
11621162
PyObject *f;
1163-
f = PyUnicode_DecodeFSDefault(filename);
1163+
f = PyUnicode_FromString(filename);
11641164
if (f == NULL)
11651165
return -1;
11661166
if (PyDict_SetItemString(d, "__file__", f) < 0) {

Python/traceback.c

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -134,33 +134,38 @@ PyTraceBack_Here(PyFrameObject *frame)
134134
return 0;
135135
}
136136

137-
static int
138-
_Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open_flags)
137+
static PyObject *
138+
_Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io)
139139
{
140-
int i;
141-
int fd = -1;
140+
Py_ssize_t i;
141+
PyObject *binary;
142142
PyObject *v;
143-
Py_ssize_t _npath;
144-
int npath;
143+
Py_ssize_t npath;
145144
size_t taillen;
146145
PyObject *syspath;
147146
const char* path;
148147
const char* tail;
148+
const char* filepath;
149149
Py_ssize_t len;
150150

151+
filepath = _PyUnicode_AsString(filename);
152+
if (filepath == NULL) {
153+
PyErr_Clear();
154+
return NULL;
155+
}
156+
151157
/* Search tail of filename in sys.path before giving up */
152-
tail = strrchr(filename, SEP);
158+
tail = strrchr(filepath, SEP);
153159
if (tail == NULL)
154-
tail = filename;
160+
tail = filepath;
155161
else
156162
tail++;
157163
taillen = strlen(tail);
158164

159165
syspath = PySys_GetObject("path");
160166
if (syspath == NULL || !PyList_Check(syspath))
161-
return -1;
162-
_npath = PyList_Size(syspath);
163-
npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
167+
return NULL;
168+
npath = PyList_Size(syspath);
164169

165170
for (i = 0; i < npath; i++) {
166171
v = PyList_GetItem(syspath, i);
@@ -171,6 +176,10 @@ _Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open
171176
if (!PyUnicode_Check(v))
172177
continue;
173178
path = _PyUnicode_AsStringAndSize(v, &len);
179+
if (path == NULL) {
180+
PyErr_Clear();
181+
continue;
182+
}
174183
if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1)
175184
continue; /* Too long */
176185
strcpy(namebuf, path);
@@ -179,59 +188,60 @@ _Py_FindSourceFile(const char* filename, char* namebuf, size_t namelen, int open
179188
if (len > 0 && namebuf[len-1] != SEP)
180189
namebuf[len++] = SEP;
181190
strcpy(namebuf+len, tail);
182-
Py_BEGIN_ALLOW_THREADS
183-
fd = open(namebuf, open_flags);
184-
Py_END_ALLOW_THREADS
185-
if (0 <= fd) {
186-
return fd;
187-
}
191+
192+
binary = PyObject_CallMethod(io, "open", "ss", namebuf, "rb");
193+
if (binary != NULL)
194+
return binary;
195+
PyErr_Clear();
188196
}
189-
return -1;
197+
return NULL;
190198
}
191199

192200
int
193-
_Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
201+
_Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
194202
{
195203
int err = 0;
196204
int fd;
197205
int i;
198206
char *found_encoding;
199207
char *encoding;
208+
PyObject *io;
209+
PyObject *binary;
200210
PyObject *fob = NULL;
201211
PyObject *lineobj = NULL;
202-
#ifdef O_BINARY
203-
const int open_flags = O_RDONLY | O_BINARY; /* necessary for Windows */
204-
#else
205-
const int open_flags = O_RDONLY;
206-
#endif
207212
char buf[MAXPATHLEN+1];
208213
Py_UNICODE *u, *p;
209214
Py_ssize_t len;
210215

211216
/* open the file */
212217
if (filename == NULL)
213218
return 0;
214-
Py_BEGIN_ALLOW_THREADS
215-
fd = open(filename, open_flags);
216-
Py_END_ALLOW_THREADS
217-
if (fd < 0) {
218-
fd = _Py_FindSourceFile(filename, buf, sizeof(buf), open_flags);
219-
if (fd < 0)
219+
220+
io = PyImport_ImportModuleNoBlock("io");
221+
if (io == NULL)
222+
return -1;
223+
binary = PyObject_CallMethod(io, "open", "Os", filename, "rb");
224+
225+
if (binary == NULL) {
226+
binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
227+
if (binary == NULL) {
228+
Py_DECREF(io);
220229
return 0;
221-
filename = buf;
230+
}
222231
}
223232

224233
/* use the right encoding to decode the file as unicode */
234+
fd = PyObject_AsFileDescriptor(binary);
225235
found_encoding = PyTokenizer_FindEncoding(fd);
226-
encoding = (found_encoding != NULL) ? found_encoding :
227-
(char*)PyUnicode_GetDefaultEncoding();
236+
encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
228237
lseek(fd, 0, 0); /* Reset position */
229-
fob = PyFile_FromFd(fd, (char*)filename, "r", -1, (char*)encoding,
230-
NULL, NULL, 1);
238+
fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding);
239+
Py_DECREF(io);
240+
Py_DECREF(binary);
231241
PyMem_FREE(found_encoding);
242+
232243
if (fob == NULL) {
233244
PyErr_Clear();
234-
close(fd);
235245
return 0;
236246
}
237247

@@ -288,17 +298,19 @@ _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
288298
}
289299

290300
static int
291-
tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
301+
tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
292302
{
293-
int err = 0;
294-
char linebuf[2000];
303+
int err;
304+
PyObject *line;
295305

296306
if (filename == NULL || name == NULL)
297307
return -1;
298-
/* This is needed by Emacs' compile command */
299-
#define FMT " File \"%.500s\", line %d, in %.500s\n"
300-
PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
301-
err = PyFile_WriteString(linebuf, f);
308+
line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
309+
filename, lineno, name);
310+
if (line == NULL)
311+
return -1;
312+
err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
313+
Py_DECREF(line);
302314
if (err != 0)
303315
return err;
304316
return _Py_DisplaySourceLine(f, filename, lineno, 4);
@@ -317,10 +329,9 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
317329
while (tb != NULL && err == 0) {
318330
if (depth <= limit) {
319331
err = tb_displayline(f,
320-
_PyUnicode_AsString(
321-
tb->tb_frame->f_code->co_filename),
322-
tb->tb_lineno,
323-
_PyUnicode_AsString(tb->tb_frame->f_code->co_name));
332+
tb->tb_frame->f_code->co_filename,
333+
tb->tb_lineno,
334+
tb->tb_frame->f_code->co_name);
324335
}
325336
depth--;
326337
tb = tb->tb_next;

0 commit comments

Comments
 (0)