Don't segfault if unable to convert non-rectangular inputs to tensor

PiperOrigin-RevId: 216738986
This commit is contained in:
Akshay Modi 2018-10-11 12:13:10 -07:00 committed by TensorFlower Gardener
parent 028410c7f4
commit 55cf8c0db7
2 changed files with 8 additions and 0 deletions
tensorflow/python

View File

@ -347,6 +347,13 @@ class TFETensorUtilTest(test_util.TensorFlowTestCase):
instance_dir.remove("test_attr")
self.assertEqual(instance_dir, type_dir)
def testNonRectangularPackAsConstant(self):
l = [array_ops.zeros((10, 1)).numpy(), array_ops.zeros(1).numpy()]
with self.assertRaisesRegexp(
ValueError, "non-rectangular Python sequence"):
constant_op.constant(l)
if __name__ == "__main__":
test.main()

View File

@ -220,6 +220,7 @@ const char ErrorFoundFloat[] =
/* Iterate over outer dim, and recursively convert each element. */ \
const int64 s = shape.dim_size(0); \
Safe_PyObjectPtr seq = make_safe(PySequence_Fast(obj, "")); \
if (TF_PREDICT_FALSE(seq == nullptr)) return ErrorRectangular; \
if (TF_PREDICT_FALSE(s != PySequence_Fast_GET_SIZE(seq.get()))) { \
return ErrorRectangular; \
} \