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

Skip to content

Commit 3eed765

Browse files
committed
Bug #1772489: make dir() work on traceback objects again.
1 parent ee634a4 commit 3eed765

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

Lib/test/test_builtin.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ def __dir__(self):
283283
f = Foo()
284284
self.assertRaises(TypeError, dir, f)
285285

286+
# dir(traceback)
287+
try:
288+
raise IndexError
289+
except:
290+
self.assertEqual(len(dir(sys.exc_info()[2])), 4)
291+
292+
286293
def test_divmod(self):
287294
self.assertEqual(divmod(12, 7), (1, 5))
288295
self.assertEqual(divmod(-12, 7), (-2, 2))

Python/traceback.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111

1212
#define OFF(x) offsetof(PyTracebackObject, x)
1313

14+
static PyObject *
15+
tb_dir(PyTracebackObject *self)
16+
{
17+
return Py_BuildValue("[ssss]", "tb_frame", "tb_next",
18+
"tb_lasti", "tb_lineno");
19+
}
20+
21+
static PyMethodDef tb_methods[] = {
22+
{"__dir__", (PyCFunction)tb_dir, METH_NOARGS},
23+
{NULL, NULL, 0, NULL},
24+
};
25+
1426
static PyMemberDef tb_memberlist[] = {
1527
{"tb_next", T_OBJECT, OFF(tb_next), READONLY},
1628
{"tb_frame", T_OBJECT, OFF(tb_frame), READONLY},
@@ -73,7 +85,7 @@ PyTypeObject PyTraceBack_Type = {
7385
0, /* tp_weaklistoffset */
7486
0, /* tp_iter */
7587
0, /* tp_iternext */
76-
0, /* tp_methods */
88+
tb_methods, /* tp_methods */
7789
tb_memberlist, /* tp_members */
7890
0, /* tp_getset */
7991
0, /* tp_base */

0 commit comments

Comments
 (0)