You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
8.5 KiB

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