diff --git a/tensorflow/python/ops/math_ops.py b/tensorflow/python/ops/math_ops.py index 4c4982c6fd5..749aa89593a 100644 --- a/tensorflow/python/ops/math_ops.py +++ b/tensorflow/python/ops/math_ops.py @@ -82,6 +82,7 @@ from tensorflow.python.framework import graph_util from tensorflow.python.framework import ops from tensorflow.python.framework import sparse_tensor from tensorflow.python.framework import tensor_shape +from tensorflow.python.framework import tensor_util from tensorflow.python.ops import array_ops from tensorflow.python.ops import gen_array_ops from tensorflow.python.ops import gen_data_flow_ops @@ -438,6 +439,9 @@ def divide(x, y, name=None): # override names. Use a dummy class to track the runtime division behavior return DivideDelegateWithName(x, name) / y else: + # We do conversion here to make sure at least x is a tensor. + if not tensor_util.is_tensor(x): + x = ops.convert_to_tensor(x) return x / y diff --git a/tensorflow/python/ops/math_ops_test.py b/tensorflow/python/ops/math_ops_test.py index 2405eec9e49..6171ea037d9 100644 --- a/tensorflow/python/ops/math_ops_test.py +++ b/tensorflow/python/ops/math_ops_test.py @@ -495,6 +495,12 @@ class DivAndModTest(test_util.TensorFlowTestCase): # Consistent with desire to get numerator self.assertAllEqual(tf_result, expanded_nums) + def testWithPythonValue(self): + # Test case for GitHub issue 39475: + # https://github.com/tensorflow/tensorflow/issues/39475 + x = math_ops.divide(5, 2) + self.assertTrue(isinstance(x, ops.Tensor)) + @test_util.run_all_in_graph_and_eager_modes class DivNoNanTest(test_util.TensorFlowTestCase):