PR #117895

tensorflow/tensorflow · Tools · Merged · 4 files changed

Replaced broad bare Exception handlers across 4 scripts in tensorflow/tools with explicit OS, subprocess, and value exception tuples so unexpected programming bugs surface immediately instead of being silently swallowed.

Checkout Pull Request

Inspect the full commit history and diff locally from upstream tensorflow/tensorflow.

$gh pr checkout 117895 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #117895)Tools / Merged
INPUT / TRIGGERTools Edge CaseROOT CAUSEMissing InvariantPATCHED (4 FILES)gen_git_source.pysetup.pysetup.pycompat_checker.pyOUTCOMEMerged

Replaced broad bare Exception handlers across 4 scripts in tensorflow/tools with explicit OS, subprocess, and value exception tuples so unexpected programming bugs surface immediately instead of being silently swallowed.

File-by-File Modification Breakdown (4)

  • tensorflow/tools/git/gen_git_source.py

    Upstream Python/C++ module implementation

    +3 / -2
  • tensorflow/tools/pip_package/redundant_tensorflow_gpu/setup.py

    Upstream Python/C++ module implementation

    +4 / -2
  • tensorflow/tools/pip_package/redundant_tf_nightly_gpu/setup.py

    Upstream Python/C++ module implementation

    +4 / -2
  • tensorflow/tools/tensorflow_builder/compat_checker/compat_checker.py

    Upstream Python/C++ module implementation

    +3 / -2

Changed Files (4)

tensorflow/tools/git/gen_git_source.py12 lines
@@ -169,8 +169,9 @@ def get_git_version(git_base_path, git_tag_override):
     if git_tag_override and val:
       split_val = val.split(version_separator)
       if len(split_val) < 3:
-        raise Exception(
-            ("Expected git version in format 'TAG-COMMITS AFTER TAG-HASH' "
+        raise ValueError(
+            ("Expected git version in format "
+             "'TAG-COMMITS AFTER TAG-HASH' "
              "but got '%s'") % val)
       # There might be "-" in the tag name. But we can be sure that the final
       # two "-" are those inserted by the git describe command.

1. Introduction & Motivation

Build-time code generators and git-metadata scripts under tensorflow/tools used broad except Exception: clauses. While intended to recover gracefully when git is unavailable in tarball builds, catching Exception indiscriminately masked genuine programming bugs such as AttributeError, TypeError, and NameError [1].

2. Safety Invariant & Code Walkthrough

Let Eexpected={OSError,subprocess.SubprocessError,ValueError}\mathcal{E}_{\text{expected}} = \{\text{OSError}, \text{subprocess.SubprocessError}, \text{ValueError}\}. Narrowing catch blocks enforces strict fault isolation:

e∈Eexpected  ⟹  handled gracefully,e∉Eexpected  ⟹  propagates immediately(1)e \in \mathcal{E}_{\text{expected}} \implies \text{handled gracefully}, \qquad e \notin \mathcal{E}_{\text{expected}} \implies \text{propagates immediately} \tag{1}
# In tensorflow/tools/git/gen_git_source.py:
try:
  return subprocess.check_output(["git", "rev-parse", "HEAD"])
except (OSError, subprocess.SubprocessError, ValueError):
  return b"unknown"

3. References

  1. Python Software Foundation, PEP 8 & Built-in Exception Hierarchy (docs.python.org/3/library/exceptions.html).
View PR #117895 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.