Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/autograph/utils/context_managers.py: 33%

15 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 07:57 +0000

1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14# ============================================================================== 

15"""Various context managers.""" 

16 

17import contextlib 

18 

19from tensorflow.python.framework import ops 

20from tensorflow.python.ops import tensor_array_ops 

21 

22 

23def control_dependency_on_returns(return_value): 

24 """Create a TF control dependency on the return values of a function. 

25 

26 If the function had no return value, a no-op context is returned. 

27 

28 Args: 

29 return_value: The return value to set as control dependency. 

30 

31 Returns: 

32 A context manager. 

33 """ 

34 def control_dependency_handle(t): 

35 if isinstance(t, tensor_array_ops.TensorArray): 

36 return t.flow 

37 return t 

38 

39 if return_value is None: 

40 return contextlib.contextmanager(lambda: (yield))() 

41 # TODO(mdan): Filter to tensor objects. 

42 if not isinstance(return_value, (list, tuple)): 

43 return_value = (return_value,) 

44 return_value = tuple(control_dependency_handle(t) for t in return_value) 

45 return ops.control_dependencies(return_value)