Better detection of lambda functions. Some wrapper libraries attempt to override the function's __name__, so we double check against the function's code object.

PiperOrigin-RevId: 346827026
Change-Id: I7be7ac8981a4f1c499c48aa126c213d2c54162f2
diff --git a/tensorflow/python/autograph/pyct/inspect_utils.py b/tensorflow/python/autograph/pyct/inspect_utils.py
index cbe4477..ed35b4e 100644
--- a/tensorflow/python/autograph/pyct/inspect_utils.py
+++ b/tensorflow/python/autograph/pyct/inspect_utils.py
@@ -60,9 +60,13 @@
 def islambda(f):
   if not tf_inspect.isfunction(f):
     return False
-  if not hasattr(f, '__name__'):
+  # TODO(mdan): Look into checking the only the code object.
+  if not (hasattr(f, '__name__') and hasattr(f, '__code__')):
     return False
-  return f.__name__ == '<lambda>'
+  # Some wrappers can rename the function, but changing the name of the
+  # code object is harder.
+  return (
+      (f.__name__ == '<lambda>') or (f.__code__.co_name == '<lambda>'))
 
 
 def isnamedtuple(f):
diff --git a/tensorflow/python/autograph/pyct/inspect_utils_test.py b/tensorflow/python/autograph/pyct/inspect_utils_test.py
index 890f9e3..fbfa01f 100644
--- a/tensorflow/python/autograph/pyct/inspect_utils_test.py
+++ b/tensorflow/python/autograph/pyct/inspect_utils_test.py
@@ -105,6 +105,11 @@
     self.assertTrue(inspect_utils.islambda(lambda x: x))
     self.assertFalse(inspect_utils.islambda(test_fn))
 
+  def test_islambda_renamed_lambda(self):
+    l = lambda x: 1
+    l.__name__ = 'f'
+    self.assertTrue(inspect_utils.islambda(l))
+
   def test_isnamedtuple(self):
     nt = collections.namedtuple('TestNamedTuple', ['a', 'b'])