Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 7b1ebf5

Browse files
superbobrytensorflower-gardener
authored andcommitted
Do not assume hasattr is available in Metric.__del__
Python does not guarantee that builtins are available by the time __del__ is called, so using hasattr is unsafe. PiperOrigin-RevId: 254255638
1 parent 3e3b915 commit 7b1ebf5

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

tensorflow/python/eager/monitoring.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,14 @@ def __init__(self, metric_name, metric_methods, label_length, *args):
121121
self._metric = self._metric_methods[self._label_length].create(*args)
122122

123123
def __del__(self):
124-
if hasattr(self, '_metric'):
124+
try:
125125
deleter = self._metric_methods[self._label_length].delete
126-
if deleter is not None:
127-
deleter(self._metric)
126+
metric = self._metric
127+
except AttributeError:
128+
return
129+
130+
if deleter is not None:
131+
deleter(metric)
128132

129133
def get_cell(self, *labels):
130134
"""Retrieves the cell."""

0 commit comments

Comments
 (0)