Skip to content
Permalink
Browse files Browse the repository at this point in the history
Merge pull request #46974 from yongtang:46900-tf.strings.substr
PiperOrigin-RevId: 356520125
Change-Id: Ifd11ff02aa51023007201dccfa02eb8213c08f7a
  • Loading branch information
tensorflower-gardener committed Feb 9, 2021
2 parents 4312178 + 7edb8c9 commit 890f716
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tensorflow/core/kernels/substr_op.cc
Expand Up @@ -51,6 +51,11 @@ class SubstrOp : public OpKernel {
const Tensor& len_tensor = context->input(2);
const TensorShape& input_shape = input_tensor.shape();
const TensorShape& pos_shape = pos_tensor.shape();
const TensorShape& len_shape = len_tensor.shape();
OP_REQUIRES(context, (pos_shape == len_shape),
errors::InvalidArgument(
"pos and len should have the same shape, got: ",
pos_shape.DebugString(), " vs. ", len_shape.DebugString()));

bool is_scalar = TensorShapeUtils::IsScalar(pos_shape);

Expand Down
10 changes: 10 additions & 0 deletions tensorflow/python/kernel_tests/substr_op_test.py
Expand Up @@ -492,6 +492,16 @@ def testInvalidUnit(self):
with self.assertRaises(ValueError):
string_ops.substr(b"test", 3, 1, unit="UTF8")

def testInvalidPos(self):
# Test case for GitHub issue 46900.
with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
x = string_ops.substr(b"abc", len=1, pos=[1, -1])
self.evaluate(x)

with self.assertRaises((ValueError, errors_impl.InvalidArgumentError)):
x = string_ops.substr(b"abc", len=1, pos=[1, 2])
self.evaluate(x)


if __name__ == "__main__":
test.main()

0 comments on commit 890f716

Please sign in to comment.