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
This commit is contained in:
Sergei Lebedev 2019-06-20 13:04:08 -07:00 committed by TensorFlower Gardener
parent 3e3b915613
commit 7b1ebf50c9

View File

@ -121,10 +121,14 @@ class Metric(object):
self._metric = self._metric_methods[self._label_length].create(*args)
def __del__(self):
if hasattr(self, '_metric'):
try:
deleter = self._metric_methods[self._label_length].delete
if deleter is not None:
deleter(self._metric)
metric = self._metric
except AttributeError:
return
if deleter is not None:
deleter(metric)
def get_cell(self, *labels):
"""Retrieves the cell."""