aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow
diff options
context:
space:
mode:
authorGravatar Anna R <annarev@google.com>2017-05-04 14:38:05 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-05-04 15:46:49 -0700
commitf696b5d4398f8295fcb2be3c89a94e007ca62287 (patch)
treea1900fdfca5c0cf5ecd40f03b239ab95487c40d6 /tensorflow
parente98357a9fd8a2e0962b2da06d769e3e58aedffd4 (diff)
Added traceback_with_start_lines property to op that includes function start line number as the last element in each traceback tuple.
Change: 155136334
Diffstat (limited to 'tensorflow')
-rw-r--r--tensorflow/python/framework/ops.py35
-rw-r--r--tensorflow/python/framework/ops_test.py22
-rw-r--r--tensorflow/tools/api/golden/tensorflow.-operation.pbtxt4
3 files changed, 53 insertions, 8 deletions
diff --git a/tensorflow/python/framework/ops.py b/tensorflow/python/framework/ops.py
index 93a29d0d8e..05972022d0 100644
--- a/tensorflow/python/framework/ops.py
+++ b/tensorflow/python/framework/ops.py
@@ -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`.
diff --git a/tensorflow/python/framework/ops_test.py b/tensorflow/python/framework/ops_test.py
index 06d03121a0..3e9f047a7d 100644
--- a/tensorflow/python/framework/ops_test.py
+++ b/tensorflow/python/framework/ops_test.py
@@ -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()
diff --git a/tensorflow/tools/api/golden/tensorflow.-operation.pbtxt b/tensorflow/tools/api/golden/tensorflow.-operation.pbtxt
index 0f43a49ee9..64240f7069 100644
--- a/tensorflow/tools/api/golden/tensorflow.-operation.pbtxt
+++ b/tensorflow/tools/api/golden/tensorflow.-operation.pbtxt
@@ -39,6 +39,10 @@ tf_class {
mtype: "<type \'property\'>"
}
member {
+ name: "traceback_with_start_lines"
+ mtype: "<type \'property\'>"
+ }
+ member {
name: "type"
mtype: "<type \'property\'>"
}