Add tf.print an an alias for tf.Print (#19234)
authorGeoffrey Irving <irving@naml.us>
Thu, 17 May 2018 17:16:07 +0000 (10:16 -0700)
committerRasmus Munk Larsen <rmlarsen@google.com>
Thu, 17 May 2018 17:16:07 +0000 (10:16 -0700)
Users with Python 3 or `from __future__ import print_function` can now
use lowercase `tf.print`.  `create_python_api.py` needed some adjustment
to ensure that `print_function` doesn't appear as part of the API.

Fixes #18053.

tensorflow/python/ops/logging_ops.py
tensorflow/tools/api/generator/create_python_api.py
tensorflow/tools/api/golden/tensorflow.pbtxt

index 222b8eb..8276047 100644 (file)
@@ -35,8 +35,9 @@ from tensorflow.python.util.tf_export import tf_export
 
 
 # Assert and Print are special symbols in python, so we must
-# use an upper-case version of them.
-@tf_export("Print")
+# have an upper-case version of them.  For users with Python 3 or Python 2.7
+# with `from __future__ import print_function`, we also allow lowercase.
+@tf_export("Print", "print")
 def Print(input_, data, message=None, first_n=None, summarize=None,
           name=None):
   """Prints a list of tensors.
index 9cb137d..1818209 100644 (file)
@@ -37,7 +37,11 @@ _GENERATED_FILE_HEADER = """\"\"\"Imports for Python API.
 This file is MACHINE GENERATED! Do not edit.
 Generated by: tensorflow/tools/api/generator/create_python_api.py script.
 \"\"\"
+
+from __future__ import print_function
+
 """
+_GENERATED_FILE_FOOTER = "\n\ndel print_function\n"
 
 
 class SymbolExposedTwiceError(Exception):
@@ -145,6 +149,7 @@ class _ModuleInitCodeBuilder(object):
 _names_with_underscore = [%s]
 __all__ = [s for s in dir() if not s.startswith('_')]
 __all__.extend([s for s in _names_with_underscore])
+__all__.remove('print_function')
 ''' % underscore_names_str
 
     return module_text_map
@@ -270,7 +275,7 @@ def create_api_files(output_files, package):
       missing_output_files.append(module_file_path)
       continue
     with open(module_name_to_file_path[module], 'w') as fp:
-      fp.write(_GENERATED_FILE_HEADER + text)
+      fp.write(_GENERATED_FILE_HEADER + text + _GENERATED_FILE_FOOTER)
 
   if missing_output_files:
     raise ValueError(
index dc2bd40..3051c44 100644 (file)
@@ -1533,6 +1533,10 @@ tf_module {
     argspec: "args=[\'x\', \'y\', \'name\'], varargs=None, keywords=None, defaults=[\'None\'], "
   }
   member_method {
+    name: "print"
+    argspec: "args=[\'input_\', \'data\', \'message\', \'first_n\', \'summarize\', \'name\'], varargs=None, keywords=None, defaults=[\'None\', \'None\', \'None\', \'None\'], "
+  }
+  member_method {
     name: "py_func"
     argspec: "args=[\'func\', \'inp\', \'Tout\', \'stateful\', \'name\'], varargs=None, keywords=None, defaults=[\'True\', \'None\'], "
   }