Added traceback_with_start_lines property to op that includes function start line number as the last element in each traceback tuple.
Change: 155136334
This commit is contained in:
parent
e98357a9fd
commit
f696b5d439
tensorflow
@ -70,25 +70,33 @@ def _override_helper(clazz_object, operator, func):
|
||||
setattr(clazz_object, operator, func)
|
||||
|
||||
|
||||
def _convert_stack(stack):
|
||||
def _convert_stack(stack, include_func_start_lineno=False):
|
||||
"""Converts a stack extracted using _extract_stack() to a traceback stack.
|
||||
|
||||
Args:
|
||||
stack: A list of n 4-tuples, (filename, lineno, name, frame_globals).
|
||||
stack: A list of n 5-tuples,
|
||||
(filename, lineno, name, frame_globals, func_start_lineno).
|
||||
include_func_start_lineno: True if function start line number should be
|
||||
included as the 5th entry in return tuples.
|
||||
|
||||
Returns:
|
||||
A list of n 4-tuples (filename, lineno, name, code), where the code tuple
|
||||
element is calculated from the corresponding elements of the input tuple.
|
||||
A list of n 4-tuples or 5-tuples
|
||||
(filename, lineno, name, code, [optional: func_start_lineno]), where the
|
||||
code tuple element is calculated from the corresponding elements of the
|
||||
input tuple.
|
||||
"""
|
||||
ret = []
|
||||
for filename, lineno, name, frame_globals in stack:
|
||||
for filename, lineno, name, frame_globals, func_start_lineno in stack:
|
||||
linecache.checkcache(filename)
|
||||
line = linecache.getline(filename, lineno, frame_globals)
|
||||
if line:
|
||||
line = line.strip()
|
||||
else:
|
||||
line = None
|
||||
ret.append((filename, lineno, name, line))
|
||||
if include_func_start_lineno:
|
||||
ret.append((filename, lineno, name, line, func_start_lineno))
|
||||
else:
|
||||
ret.append((filename, lineno, name, line))
|
||||
return ret
|
||||
|
||||
|
||||
@ -103,7 +111,8 @@ def _extract_stack():
|
||||
be formatted etc. using traceback methods.
|
||||
|
||||
Returns:
|
||||
A list of 4-tuples (filename, lineno, name, frame_globals) corresponding to
|
||||
A list of 5-tuples
|
||||
(filename, lineno, name, frame_globals, func_start_lineno) corresponding to
|
||||
the call stack of the current thread.
|
||||
"""
|
||||
# pylint: enable=line-too-long
|
||||
@ -118,7 +127,8 @@ def _extract_stack():
|
||||
filename = co.co_filename
|
||||
name = co.co_name
|
||||
frame_globals = f.f_globals
|
||||
ret.append((filename, lineno, name, frame_globals))
|
||||
func_start_lineno = co.co_firstlineno
|
||||
ret.append((filename, lineno, name, frame_globals, func_start_lineno))
|
||||
f = f.f_back
|
||||
ret.reverse()
|
||||
return ret
|
||||
@ -1505,6 +1515,15 @@ class Operation(object):
|
||||
"""Returns the call stack from when this operation was constructed."""
|
||||
return _convert_stack(self._traceback)
|
||||
|
||||
@property
|
||||
def traceback_with_start_lines(self):
|
||||
"""Same as traceback but includes start line of function definition.
|
||||
|
||||
Returns:
|
||||
A list of 5-tuples (filename, lineno, name, code, func_start_lineno).
|
||||
"""
|
||||
return _convert_stack(self._traceback, include_func_start_lineno=True)
|
||||
|
||||
def get_attr(self, name):
|
||||
"""Returns the value of the attr of this op with the given `name`.
|
||||
|
||||
|
@ -22,6 +22,7 @@ import gc
|
||||
import weakref
|
||||
|
||||
from tensorflow.core.framework import attr_value_pb2
|
||||
from tensorflow.core.protobuf import config_pb2
|
||||
from tensorflow.python.client import session
|
||||
from tensorflow.python.framework import common_shapes
|
||||
from tensorflow.python.framework import constant_op
|
||||
@ -1703,5 +1704,26 @@ class NameScopeTest(test_util.TensorFlowTestCase):
|
||||
self.assertEqual("", g.get_name_scope())
|
||||
|
||||
|
||||
class TracebackTest(test_util.TensorFlowTestCase):
|
||||
|
||||
def testTracebackWithStartLines(self):
|
||||
with self.test_session() as sess:
|
||||
a = constant_op.constant(2.0)
|
||||
sess.run(
|
||||
a,
|
||||
options=config_pb2.RunOptions(
|
||||
trace_level=config_pb2.RunOptions.FULL_TRACE))
|
||||
self.assertTrue(sess.graph.get_operations())
|
||||
|
||||
# Tests that traceback_with_start_lines is the same as traceback
|
||||
# but includes one more element at the end.
|
||||
for op in sess.graph.get_operations():
|
||||
self.assertEquals(len(op.traceback), len(op.traceback_with_start_lines))
|
||||
for frame, frame_with_start_line in zip(
|
||||
op.traceback, op.traceback_with_start_lines):
|
||||
self.assertEquals(5, len(frame_with_start_line))
|
||||
self.assertEquals(frame, frame_with_start_line[:-1])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
googletest.main()
|
||||
|
@ -38,6 +38,10 @@ tf_class {
|
||||
name: "traceback"
|
||||
mtype: "<type \'property\'>"
|
||||
}
|
||||
member {
|
||||
name: "traceback_with_start_lines"
|
||||
mtype: "<type \'property\'>"
|
||||
}
|
||||
member {
|
||||
name: "type"
|
||||
mtype: "<type \'property\'>"
|
||||
|
Loading…
Reference in New Issue
Block a user