symbian-qemu-0.9.1-12/python-2.6.1/Python/traceback.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 
       
     2 /* Traceback implementation */
       
     3 
       
     4 #include "Python.h"
       
     5 
       
     6 #include "code.h"
       
     7 #include "frameobject.h"
       
     8 #include "structmember.h"
       
     9 #include "osdefs.h"
       
    10 #include "traceback.h"
       
    11 
       
    12 #define OFF(x) offsetof(PyTracebackObject, x)
       
    13 
       
    14 static struct memberlist tb_memberlist[] = {
       
    15 	{"tb_next",	T_OBJECT,	OFF(tb_next)},
       
    16 	{"tb_frame",	T_OBJECT,	OFF(tb_frame)},
       
    17 	{"tb_lasti",	T_INT,		OFF(tb_lasti)},
       
    18 	{"tb_lineno",	T_INT,		OFF(tb_lineno)},
       
    19 	{NULL}	/* Sentinel */
       
    20 };
       
    21 
       
    22 static PyObject *
       
    23 tb_getattr(PyTracebackObject *tb, char *name)
       
    24 {
       
    25 	return PyMember_Get((char *)tb, tb_memberlist, name);
       
    26 }
       
    27 
       
    28 static void
       
    29 tb_dealloc(PyTracebackObject *tb)
       
    30 {
       
    31 	PyObject_GC_UnTrack(tb);
       
    32 	Py_TRASHCAN_SAFE_BEGIN(tb)
       
    33 	Py_XDECREF(tb->tb_next);
       
    34 	Py_XDECREF(tb->tb_frame);
       
    35 	PyObject_GC_Del(tb);
       
    36 	Py_TRASHCAN_SAFE_END(tb)
       
    37 }
       
    38 
       
    39 static int
       
    40 tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
       
    41 {
       
    42 	Py_VISIT(tb->tb_next);
       
    43 	Py_VISIT(tb->tb_frame);
       
    44 	return 0;
       
    45 }
       
    46 
       
    47 static void
       
    48 tb_clear(PyTracebackObject *tb)
       
    49 {
       
    50 	Py_CLEAR(tb->tb_next);
       
    51 	Py_CLEAR(tb->tb_frame);
       
    52 }
       
    53 
       
    54 PyTypeObject PyTraceBack_Type = {
       
    55 	PyVarObject_HEAD_INIT(&PyType_Type, 0)
       
    56 	"traceback",
       
    57 	sizeof(PyTracebackObject),
       
    58 	0,
       
    59 	(destructor)tb_dealloc, /*tp_dealloc*/
       
    60 	0,		/*tp_print*/
       
    61 	(getattrfunc)tb_getattr, /*tp_getattr*/
       
    62 	0,		/*tp_setattr*/
       
    63 	0,		/*tp_compare*/
       
    64 	0,		/*tp_repr*/
       
    65 	0,		/*tp_as_number*/
       
    66 	0,		/*tp_as_sequence*/
       
    67 	0,		/*tp_as_mapping*/
       
    68 	0,		/* tp_hash */
       
    69 	0,		/* tp_call */
       
    70 	0,		/* tp_str */
       
    71 	0,		/* tp_getattro */
       
    72 	0,		/* tp_setattro */
       
    73 	0,					/* tp_as_buffer */
       
    74 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
       
    75 	0,             				/* tp_doc */
       
    76  	(traverseproc)tb_traverse,		/* tp_traverse */
       
    77 	(inquiry)tb_clear,			/* tp_clear */
       
    78 	0,					/* tp_richcompare */
       
    79 	0,					/* tp_weaklistoffset */
       
    80 	0,					/* tp_iter */
       
    81 	0,					/* tp_iternext */
       
    82 	0,					/* tp_methods */
       
    83 	0,			/* tp_members */
       
    84 	0,			/* tp_getset */
       
    85 	0,					/* tp_base */
       
    86 	0,					/* tp_dict */
       
    87 };
       
    88 
       
    89 static PyTracebackObject *
       
    90 newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
       
    91 {
       
    92 	PyTracebackObject *tb;
       
    93 	if ((next != NULL && !PyTraceBack_Check(next)) ||
       
    94 			frame == NULL || !PyFrame_Check(frame)) {
       
    95 		PyErr_BadInternalCall();
       
    96 		return NULL;
       
    97 	}
       
    98 	tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
       
    99 	if (tb != NULL) {
       
   100 		Py_XINCREF(next);
       
   101 		tb->tb_next = next;
       
   102 		Py_XINCREF(frame);
       
   103 		tb->tb_frame = frame;
       
   104 		tb->tb_lasti = frame->f_lasti;
       
   105 		tb->tb_lineno = PyCode_Addr2Line(frame->f_code, 
       
   106 						 frame->f_lasti);
       
   107 		PyObject_GC_Track(tb);
       
   108 	}
       
   109 	return tb;
       
   110 }
       
   111 
       
   112 int
       
   113 PyTraceBack_Here(PyFrameObject *frame)
       
   114 {
       
   115 	PyThreadState *tstate = PyThreadState_GET();
       
   116 	PyTracebackObject *oldtb = (PyTracebackObject *) tstate->curexc_traceback;
       
   117 	PyTracebackObject *tb = newtracebackobject(oldtb, frame);
       
   118 	if (tb == NULL)
       
   119 		return -1;
       
   120 	tstate->curexc_traceback = (PyObject *)tb;
       
   121 	Py_XDECREF(oldtb);
       
   122 	return 0;
       
   123 }
       
   124 
       
   125 int
       
   126 _Py_DisplaySourceLine(PyObject *f, const char *filename, int lineno, int indent)
       
   127 {
       
   128 	int err = 0;
       
   129 	FILE *xfp = NULL;
       
   130 	char linebuf[2000];
       
   131 	int i;
       
   132 	char namebuf[MAXPATHLEN+1];
       
   133 
       
   134 	if (filename == NULL)
       
   135 		return -1;
       
   136 	/* This is needed by Emacs' compile command */
       
   137 #define FMT "  File \"%.500s\", line %d, in %.500s\n"
       
   138 	xfp = fopen(filename, "r" PY_STDIOTEXTMODE);
       
   139 	if (xfp == NULL) {
       
   140 		/* Search tail of filename in sys.path before giving up */
       
   141 		PyObject *path;
       
   142 		const char *tail = strrchr(filename, SEP);
       
   143 		if (tail == NULL)
       
   144 			tail = filename;
       
   145 		else
       
   146 			tail++;
       
   147 		path = PySys_GetObject("path");
       
   148 		if (path != NULL && PyList_Check(path)) {
       
   149 			Py_ssize_t _npath = PyList_Size(path);
       
   150 			int npath = Py_SAFE_DOWNCAST(_npath, Py_ssize_t, int);
       
   151 			size_t taillen = strlen(tail);
       
   152 			for (i = 0; i < npath; i++) {
       
   153 				PyObject *v = PyList_GetItem(path, i);
       
   154 				if (v == NULL) {
       
   155 					PyErr_Clear();
       
   156 					break;
       
   157 				}
       
   158 				if (PyString_Check(v)) {
       
   159 					size_t len;
       
   160 					len = PyString_GET_SIZE(v);
       
   161 					if (len + 1 + taillen >= MAXPATHLEN)
       
   162 						continue; /* Too long */
       
   163 					strcpy(namebuf, PyString_AsString(v));
       
   164 					if (strlen(namebuf) != len)
       
   165 						continue; /* v contains '\0' */
       
   166 					if (len > 0 && namebuf[len-1] != SEP)
       
   167 						namebuf[len++] = SEP;
       
   168 					strcpy(namebuf+len, tail);
       
   169 					xfp = fopen(namebuf, "r" PY_STDIOTEXTMODE);
       
   170 					if (xfp != NULL) {
       
   171 						filename = namebuf;
       
   172 						break;
       
   173 					}
       
   174 				}
       
   175 			}
       
   176 		}
       
   177 	}
       
   178 
       
   179         if (xfp == NULL)
       
   180             return err;
       
   181         if (err != 0) {
       
   182             fclose(xfp);
       
   183             return err;
       
   184         }
       
   185 
       
   186 	for (i = 0; i < lineno; i++) {
       
   187 		char* pLastChar = &linebuf[sizeof(linebuf)-2];
       
   188 		do {
       
   189 			*pLastChar = '\0';
       
   190 			if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, xfp, NULL) == NULL)
       
   191 				break;
       
   192 			/* fgets read *something*; if it didn't get as
       
   193 			   far as pLastChar, it must have found a newline
       
   194 			   or hit the end of the file;	if pLastChar is \n,
       
   195 			   it obviously found a newline; else we haven't
       
   196 			   yet seen a newline, so must continue */
       
   197 		} while (*pLastChar != '\0' && *pLastChar != '\n');
       
   198 	}
       
   199 	if (i == lineno) {
       
   200 		char buf[11];
       
   201 		char *p = linebuf;
       
   202 		while (*p == ' ' || *p == '\t' || *p == '\014')
       
   203 			p++;
       
   204 
       
   205 		/* Write some spaces before the line */
       
   206 		strcpy(buf, "          ");
       
   207 		assert (strlen(buf) == 10);
       
   208 		while (indent > 0) {
       
   209 			if(indent < 10)
       
   210 				buf[indent] = '\0';
       
   211 			err = PyFile_WriteString(buf, f);
       
   212 			if (err != 0)
       
   213 				break;
       
   214 			indent -= 10;
       
   215 		}
       
   216 
       
   217 		if (err == 0)
       
   218 			err = PyFile_WriteString(p, f);
       
   219 		if (err == 0 && strchr(p, '\n') == NULL)
       
   220 			err = PyFile_WriteString("\n", f);
       
   221 	}
       
   222 	fclose(xfp);
       
   223 	return err;
       
   224 }
       
   225 
       
   226 static int
       
   227 tb_displayline(PyObject *f, const char *filename, int lineno, const char *name)
       
   228 {
       
   229 	int err = 0;
       
   230         char linebuf[2000];
       
   231 
       
   232 	if (filename == NULL || name == NULL)
       
   233 		return -1;
       
   234 	/* This is needed by Emacs' compile command */
       
   235 #define FMT "  File \"%.500s\", line %d, in %.500s\n"
       
   236 	PyOS_snprintf(linebuf, sizeof(linebuf), FMT, filename, lineno, name);
       
   237 	err = PyFile_WriteString(linebuf, f);
       
   238 	if (err != 0)
       
   239 		return err;
       
   240         return _Py_DisplaySourceLine(f, filename, lineno, 4);
       
   241 }
       
   242 
       
   243 static int
       
   244 tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
       
   245 {
       
   246 	int err = 0;
       
   247 	long depth = 0;
       
   248 	PyTracebackObject *tb1 = tb;
       
   249 	while (tb1 != NULL) {
       
   250 		depth++;
       
   251 		tb1 = tb1->tb_next;
       
   252 	}
       
   253 	while (tb != NULL && err == 0) {
       
   254 		if (depth <= limit) {
       
   255 			err = tb_displayline(f,
       
   256 			    PyString_AsString(
       
   257 				    tb->tb_frame->f_code->co_filename),
       
   258 			    tb->tb_lineno,
       
   259 			    PyString_AsString(tb->tb_frame->f_code->co_name));
       
   260 		}
       
   261 		depth--;
       
   262 		tb = tb->tb_next;
       
   263 		if (err == 0)
       
   264 			err = PyErr_CheckSignals();
       
   265 	}
       
   266 	return err;
       
   267 }
       
   268 
       
   269 int
       
   270 PyTraceBack_Print(PyObject *v, PyObject *f)
       
   271 {
       
   272 	int err;
       
   273 	PyObject *limitv;
       
   274 	long limit = 1000;
       
   275 	if (v == NULL)
       
   276 		return 0;
       
   277 	if (!PyTraceBack_Check(v)) {
       
   278 		PyErr_BadInternalCall();
       
   279 		return -1;
       
   280 	}
       
   281 	limitv = PySys_GetObject("tracebacklimit");
       
   282 	if (limitv && PyInt_Check(limitv)) {
       
   283 		limit = PyInt_AsLong(limitv);
       
   284 		if (limit <= 0)
       
   285 			return 0;
       
   286 	}
       
   287 	err = PyFile_WriteString("Traceback (most recent call last):\n", f);
       
   288 	if (!err)
       
   289 		err = tb_printinternal((PyTracebackObject *)v, f, limit);
       
   290 	return err;
       
   291 }