• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Tests for functions used to extract and analyze stacks."""
16
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21import traceback
22
23from tensorflow.python.platform import test
24from tensorflow.python.util import tf_stack
25
26
27class TFStackTest(test.TestCase):
28
29  def testFormatStackSelfConsistency(self):
30    # Both defined on the same line to produce identical stacks.
31    stacks = tf_stack.extract_stack(), traceback.extract_stack()
32    self.assertEqual(
33        traceback.format_list(stacks[0]), traceback.format_list(stacks[1]))
34
35  def testFrameSummaryEquality(self):
36    frames1 = tf_stack.extract_stack()
37    frames2 = tf_stack.extract_stack()
38
39    self.assertNotEqual(frames1[0], frames1[1])
40    self.assertEqual(frames1[0], frames1[0])
41    self.assertEqual(frames1[0], frames2[0])
42
43  def testFrameSummaryEqualityAndHash(self):
44    # Both defined on the same line to produce identical stacks.
45    frame1, frame2 = tf_stack.extract_stack(), tf_stack.extract_stack()
46    self.assertEqual(len(frame1), len(frame2))
47    for f1, f2 in zip(frame1, frame2):
48      self.assertEqual(f1, f2)
49      self.assertEqual(hash(f1), hash(f1))
50      self.assertEqual(hash(f1), hash(f2))
51    self.assertEqual(frame1, frame2)
52    self.assertEqual(hash(tuple(frame1)), hash(tuple(frame2)))
53
54  def testLastUserFrame(self):
55    trace = tf_stack.extract_stack()  # COMMENT
56    frame = trace.last_user_frame()
57    self.assertRegex(frame.line, "# COMMENT")
58
59
60def extract_stack(limit=None):
61  # Both defined on the same line to produce identical stacks.
62  return tf_stack.extract_stack(limit), traceback.extract_stack(limit)
63
64
65if __name__ == "__main__":
66  test.main()
67