aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/util/compat.py
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow/python/util/compat.py')
-rw-r--r--tensorflow/python/util/compat.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/tensorflow/python/util/compat.py b/tensorflow/python/util/compat.py
index 07382d93df..3ab0bd16fa 100644
--- a/tensorflow/python/util/compat.py
+++ b/tensorflow/python/util/compat.py
@@ -21,6 +21,7 @@ In addition to the functions below, `as_str` converts an object to a `str`.
@@as_bytes
@@as_text
@@as_str_any
+@@path_to_str
## Types
The compatibility module also provides the following types:
@@ -108,6 +109,20 @@ def as_str_any(value):
return str(value)
+def path_to_str(path):
+ """Returns the file system path representation of a `PathLike` object, else as it is.
+
+ Args:
+ path: An object that can be converted to path representation.
+
+ Returns:
+ A `str` object.
+ """
+ if hasattr(path, "__fspath__"):
+ path = as_str_any(path.__fspath__())
+ return path
+
+
# Numpy 1.8 scalars don't inherit from numbers.Integral in Python 3, so we
# need to check them specifically. The same goes from Real and Complex.
integral_types = (_numbers.Integral, _np.integer)