Exit previously entered sub-context-managers whenever any __enter__() call
throws an exception in tf.name_scope() and tf.variable_scope().
The previous conversion from generator style context managers translated a
nested with block like
with context_a, context_b:
yield
into the following pair of __enter__()/__exit__() methods:
def __enter__(self):
self.context_a.__enter__()
self.context_b.__enter__()
def __exit__(self, *args):
self.context_b.__exit__(*args)
self.context_a.__exit__(*args)
return False
This translation is only correct when self.context_b.__enter__() does not throw
an exception. In the context of tf.name_scope() and tf.variable_scope(),
context_a is the default graph stack, and context_b is the actual scope to
enter. Entering an actual scope throws a ValueError when the scope name is
invalid. In that case, the above implementation leaves __enter__() without
popping the default graph stack. Sub-sequent calls to pop the stack will thus
fail, obscuring the actual exception that was raised in entering the scope.
PiperOrigin-RevId: 182119816
Loading
Please sign in to comment