A `py_func` that will be called to invoke the iterator.
(iterator_id)
| 632 | """ |
| 633 | |
| 634 | def generator_py_func(iterator_id): |
| 635 | """A `py_func` that will be called to invoke the iterator.""" |
| 636 | # `next()` raises `StopIteration` when there are no more |
| 637 | # elements remaining to be generated. |
| 638 | values = next(generator_state.get_iterator(iterator_id)) |
| 639 | |
| 640 | # Use the same _convert function from the py_func() implementation to |
| 641 | # convert the returned values to arrays early, so that we can inspect |
| 642 | # their values. |
| 643 | try: |
| 644 | flattened_values = nest.flatten_up_to(output_types, values) |
| 645 | except (TypeError, ValueError): |
| 646 | raise TypeError( |
| 647 | "`generator` yielded an element that did not match the expected " |
| 648 | "structure. The expected structure was %s, but the yielded " |
| 649 | "element was %s." % (output_types, values)) |
| 650 | ret_arrays = [] |
| 651 | for ret, dtype in zip(flattened_values, flattened_types): |
| 652 | try: |
| 653 | ret_arrays.append(script_ops.FuncRegistry._convert( # pylint: disable=protected-access |
| 654 | ret, dtype=dtype.as_numpy_dtype)) |
| 655 | except (TypeError, ValueError): |
| 656 | raise TypeError( |
| 657 | "`generator` yielded an element that could not be converted to " |
| 658 | "the expected type. The expected type was %s, but the yielded " |
| 659 | "element was %s." % (dtype.name, ret)) |
| 660 | |
| 661 | # Additional type and shape checking to ensure that the components |
| 662 | # of the generated element match the `output_types` and `output_shapes` |
| 663 | # arguments. |
| 664 | for (ret_array, expected_dtype, expected_shape) in zip( |
| 665 | ret_arrays, flattened_types, flattened_shapes): |
| 666 | if ret_array.dtype != expected_dtype.as_numpy_dtype: |
| 667 | raise TypeError( |
| 668 | "`generator` yielded an element of type %s where an element " |
| 669 | "of type %s was expected." % (ret_array.dtype, |
| 670 | expected_dtype.as_numpy_dtype)) |
| 671 | if not expected_shape.is_compatible_with(ret_array.shape): |
| 672 | raise ValueError( |
| 673 | "`generator` yielded an element of shape %s where an element " |
| 674 | "of shape %s was expected." % (ret_array.shape, expected_shape)) |
| 675 | |
| 676 | return ret_arrays |
| 677 | |
| 678 | flat_values = script_ops.numpy_function(generator_py_func, |
| 679 | [iterator_id_t], flattened_types) |
nothing calls this directly
no test coverage detected