Change the contract of dynamic_builtin to reject all functions it can't process.
authorA. Unique TensorFlower <gardener@tensorflow.org>
Tue, 17 Apr 2018 17:13:09 +0000 (10:13 -0700)
committerTensorFlower Gardener <gardener@tensorflow.org>
Tue, 17 Apr 2018 17:16:11 +0000 (10:16 -0700)
PiperOrigin-RevId: 193215246

tensorflow/contrib/autograph/utils/builtins.py
tensorflow/contrib/autograph/utils/builtins_test.py

index 349b7b6..dfc3c86 100644 (file)
@@ -28,24 +28,17 @@ from tensorflow.python.framework import tensor_util
 from tensorflow.python.ops import array_ops
 from tensorflow.python.ops import logging_ops
 from tensorflow.python.ops import math_ops
-from tensorflow.python.util import tf_inspect
 
 
 def dynamic_builtin(f, *args, **kwargs):
   """Converts a builtin function call inline."""
-  # Some built-ins may be objects.
-  if not tf_inspect.isbuiltin(f) and f not in (range,):
-    return f(*args, **kwargs)
-
   if f is len:
     return dynamic_len(*args, **kwargs)
   if six.PY2 and f is xrange:
     return dynamic_range(*args, **kwargs)
   if f is range:
     return dynamic_range(*args, **kwargs)
-
-  raise NotImplementedError(
-      'The "%s" builtin is not yet supported.' % f.__name__)
+  raise ValueError('%s is not supported' % f)
 
 
 def dynamic_len(list_or_tensor):
index d9f7913..163e698 100644 (file)
@@ -76,8 +76,9 @@ class BuiltinsTest(test.TestCase):
     def range(x):  # pylint:disable=redefined-builtin
       return x
 
-    # Functions that just have the names of builtins are ignored.
-    self.assertEqual(builtins.dynamic_builtin(range, 1), 1)
+    # Functions that just have the names of builtins are rejected.
+    with self.assertRaises(ValueError):
+      self.assertEqual(builtins.dynamic_builtin(range, 1), 1)
     if six.PY2:
       self.assertListEqual(
           list(builtins.dynamic_builtin(xrange, 3)), [0, 1, 2])