Merge pull request #42067 from yongtang:42001-tf.string.format-unicode

PiperOrigin-RevId: 325373287
Change-Id: I8e98c6be0c159dce590962bcd055b226433f460e
This commit is contained in:
TensorFlower Gardener 2020-08-06 21:52:30 -07:00
commit a4669acca9
2 changed files with 11 additions and 2 deletions

View File

@ -1007,9 +1007,9 @@ inline const strings::AlphaNum& PrintOneElement(const strings::AlphaNum& a,
}
inline string PrintOneElement(const tstring& a, bool print_v2) {
if (print_v2) {
return "\"" + absl::CEscape(a) + "\"";
return "\"" + absl::Utf8SafeCEscape(a) + "\"";
} else {
return absl::CEscape(a);
return absl::Utf8SafeCEscape(a);
}
}
inline float PrintOneElement(const Eigen::half& h, bool print_v2) {

View File

@ -379,6 +379,15 @@ class StringFormatOpTest(test.TestCase):
format_output = string_ops.string_format("{}", (tensor, tensor))
self.evaluate(format_output)
@test_util.run_in_graph_and_eager_modes()
def testTensorAndFormatUnicode(self):
with self.cached_session():
tensor = constant_op.constant("😊")
format_output = string_ops.string_format("😊:{}", tensor)
out = self.evaluate(format_output)
expected = '😊:"😊"'
self.assertEqual(compat.as_text(out), expected)
if __name__ == "__main__":
test.main()