Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1983)

Side by Side Diff: Python/traceback.c

Issue 9319: imp.find_module('test/badsyntax_pep3120') causes segfault
Patch Set: Created 2 years, 2 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Python/import.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Traceback implementation */ 2 /* Traceback implementation */
3 3
4 #include "Python.h" 4 #include "Python.h"
5 5
6 #include "code.h" 6 #include "code.h"
7 #include "frameobject.h" 7 #include "frameobject.h"
8 #include "structmember.h" 8 #include "structmember.h"
9 #include "osdefs.h" 9 #include "osdefs.h"
10 #ifdef HAVE_FCNTL_H 10 #ifdef HAVE_FCNTL_H
11 #include <fcntl.h> 11 #include <fcntl.h>
12 #endif 12 #endif
13 13
14 #define OFF(x) offsetof(PyTracebackObject, x) 14 #define OFF(x) offsetof(PyTracebackObject, x)
15
16 /* Method from Parser/tokenizer.c */
17 extern char * PyTokenizer_FindEncoding(int);
18 15
19 static PyObject * 16 static PyObject *
20 tb_dir(PyTracebackObject *self) 17 tb_dir(PyTracebackObject *self)
21 { 18 {
22 return Py_BuildValue("[ssss]", "tb_frame", "tb_next", 19 return Py_BuildValue("[ssss]", "tb_frame", "tb_next",
23 "tb_lasti", "tb_lineno"); 20 "tb_lasti", "tb_lineno");
24 } 21 }
25 22
26 static PyMethodDef tb_methods[] = { 23 static PyMethodDef tb_methods[] = {
27 {"__dir__", (PyCFunction)tb_dir, METH_NOARGS}, 24 {"__dir__", (PyCFunction)tb_dir, METH_NOARGS},
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 char *found_encoding; 216 char *found_encoding;
220 char *encoding; 217 char *encoding;
221 PyObject *io; 218 PyObject *io;
222 PyObject *binary; 219 PyObject *binary;
223 PyObject *fob = NULL; 220 PyObject *fob = NULL;
224 PyObject *lineobj = NULL; 221 PyObject *lineobj = NULL;
225 PyObject *res; 222 PyObject *res;
226 char buf[MAXPATHLEN+1]; 223 char buf[MAXPATHLEN+1];
227 Py_UNICODE *u, *p; 224 Py_UNICODE *u, *p;
228 Py_ssize_t len; 225 Py_ssize_t len;
226 char *filename_utf8;
229 227
230 /* open the file */ 228 /* open the file */
231 if (filename == NULL) 229 if (filename == NULL)
232 return 0; 230 return 0;
233 231
234 io = PyImport_ImportModuleNoBlock("io"); 232 io = PyImport_ImportModuleNoBlock("io");
235 if (io == NULL) 233 if (io == NULL)
236 return -1; 234 return -1;
237 binary = PyObject_CallMethod(io, "open", "Os", filename, "rb"); 235 binary = PyObject_CallMethod(io, "open", "Os", filename, "rb");
238 236
239 if (binary == NULL) { 237 if (binary == NULL) {
240 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io); 238 binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
241 if (binary == NULL) { 239 if (binary == NULL) {
242 Py_DECREF(io); 240 Py_DECREF(io);
243 return 0; 241 return 0;
244 } 242 }
245 } 243 }
246 244
247 /* use the right encoding to decode the file as unicode */ 245 /* use the right encoding to decode the file as unicode */
248 fd = PyObject_AsFileDescriptor(binary); 246 fd = PyObject_AsFileDescriptor(binary);
249 found_encoding = PyTokenizer_FindEncoding(fd); 247 filename_utf8 = _PyUnicode_AsString(filename);
248 if (filename_utf8 == NULL)
249 PyErr_Clear();
250 found_encoding = PyTokenizer_FindEncodingFilename(fd, filename_utf8);
250 encoding = (found_encoding != NULL) ? found_encoding : "utf-8"; 251 encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
251 lseek(fd, 0, 0); /* Reset position */ 252 lseek(fd, 0, 0); /* Reset position */
252 fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding); 253 fob = PyObject_CallMethod(io, "TextIOWrapper", "Os", binary, encoding);
253 Py_DECREF(io); 254 Py_DECREF(io);
254 Py_DECREF(binary); 255 Py_DECREF(binary);
255 PyMem_FREE(found_encoding); 256 PyMem_FREE(found_encoding);
256 257
257 if (fob == NULL) { 258 if (fob == NULL) {
258 PyErr_Clear(); 259 PyErr_Clear();
259 return 0; 260 return 0;
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 else if (limit <= 0) { 396 else if (limit <= 0) {
396 limit = PyTraceBack_LIMIT; 397 limit = PyTraceBack_LIMIT;
397 } 398 }
398 PyErr_Restore(exc_type, exc_value, exc_tb); 399 PyErr_Restore(exc_type, exc_value, exc_tb);
399 } 400 }
400 err = PyFile_WriteString("Traceback (most recent call last):\n", f); 401 err = PyFile_WriteString("Traceback (most recent call last):\n", f);
401 if (!err) 402 if (!err)
402 err = tb_printinternal((PyTracebackObject *)v, f, limit); 403 err = tb_printinternal((PyTracebackObject *)v, f, limit);
403 return err; 404 return err;
404 } 405 }
OLDNEW
« no previous file with comments | « Python/import.c ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7