From 319df5224cf9db2135b05f6a3d3a803b3938d5a3 Mon Sep 17 00:00:00 2001 From: Shanqing Cai Date: Mon, 8 Jun 2020 08:08:07 -0700 Subject: [PATCH] [tfdbg2] Fix bug in which enable_check_numerics() errors due to empty file path The helper function `guess_is_tensorflow_py_library`() throws errors for file paths that do not have expected extensions for Python source files. This is why it errors out when the file path is an empty string, which has been observed to happen by users. This CL makes the helper function not throw errors and return False for such malformed file paths instead. PiperOrigin-RevId: 315276482 Change-Id: Ib19750d502cd55fcfbef6f95a2064adc2e0816e9 --- tensorflow/python/debug/lib/source_utils.py | 11 +++-------- tensorflow/python/debug/lib/source_utils_test.py | 14 ++++++++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/tensorflow/python/debug/lib/source_utils.py b/tensorflow/python/debug/lib/source_utils.py index 033e9c4361e..1e9f7ee82a2 100644 --- a/tensorflow/python/debug/lib/source_utils.py +++ b/tensorflow/python/debug/lib/source_utils.py @@ -69,18 +69,13 @@ def guess_is_tensorflow_py_library(py_file_path): py_file_path: full path of the Python source file in question. Returns: - (`bool`) Whether the file is a part of the tensorflow library. - - Raises: - ValueError: if the extension name of py_file_path does not indicate a Python - source file (compiled or uncompiled). + (`bool`) Whether the file is inferred to be a part of the tensorflow + library. """ if (not is_extension_uncompiled_python_source(py_file_path) and not is_extension_compiled_python_source(py_file_path)): - raise ValueError( - "Input file path (%s) is not a Python source file." % py_file_path) + return False py_file_path = _norm_abs_path(py_file_path) - return ((py_file_path.startswith(_TENSORFLOW_BASEDIR) or py_file_path.startswith(_ABSL_BASEDIR)) and not py_file_path.endswith("_test.py") and diff --git a/tensorflow/python/debug/lib/source_utils_test.py b/tensorflow/python/debug/lib/source_utils_test.py index 89964a21ba7..c9934c4aac8 100644 --- a/tensorflow/python/debug/lib/source_utils_test.py +++ b/tensorflow/python/debug/lib/source_utils_test.py @@ -126,10 +126,16 @@ class GuessIsTensorFlowLibraryTest(test_util.TensorFlowTestCase): source_utils.guess_is_tensorflow_py_library(os.path.normpath( "site-packages/tensorflow/python/debug/examples/v3/example_v3.py"))) - def testNonPythonFileRaisesException(self): - with self.assertRaisesRegexp(ValueError, r"is not a Python source file"): - source_utils.guess_is_tensorflow_py_library( - os.path.join(os.path.dirname(self.curr_file_path), "foo.cc")) + def testReturnsFalseForNonPythonFile(self): + self.assertFalse( + source_utils.guess_is_tensorflow_py_library( + os.path.join(os.path.dirname(self.curr_file_path), "foo.cc"))) + + def testReturnsFalseForStdin(self): + self.assertFalse(source_utils.guess_is_tensorflow_py_library("")) + + def testReturnsFalseForEmptyFileName(self): + self.assertFalse(source_utils.guess_is_tensorflow_py_library("")) class SourceHelperTest(test_util.TensorFlowTestCase):