@@ -908,25 +908,32 @@ class PyListConverter : public ListConverter<T, PyConverter, PyConverterTrait> {
908908
909909 Status AppendNdarray (PyObject* value) {
910910 PyArrayObject* ndarray = reinterpret_cast <PyArrayObject*>(value);
911+ if (PyArray_ISBYTESWAPPED (ndarray)) {
912+ // TODO
913+ return Status::NotImplemented (" Byte-swapped arrays not supported" );
914+ }
911915 OwnedRef flattened;
912916 if (PyArray_NDIM (ndarray) != 1 ) {
913- // GH-49644: a fixed-size list (e.g. the storage of a fixed-shape tensor)
914- // can be built from a multi-dimensional array by flattening it in C
915- // order. The total number of elements must still match the list size,
916- // which the builder validates below. 0-dimensional arrays and
917- // variable-sized lists remain restricted to 1-dimensional values.
917+ // GH-49644: a fixed-size list (e.g. fixed-shape-tensor storage) can be
918+ // built from a multi-dimensional array, always flattened in C order
919+ // regardless of the input's memory layout.
918920 if (PyArray_NDIM (ndarray) < 2 || this ->list_type_ ->id () != Type::FIXED_SIZE_LIST ) {
919- return Status::Invalid (" Can only convert 1-dimensional array values" );
921+ return Status::Invalid (
922+ " Can only convert 1-dimensional array values to a variable-sized list" );
920923 }
921- flattened.reset (PyArray_Ravel (ndarray, NPY_CORDER ));
924+ // Get an aligned, C-contiguous array (copying only if needed), then view
925+ // it as 1-D so its values can be read directly in C order.
926+ PyObject* contiguous =
927+ PyArray_CheckFromAny (value, nullptr , /* min_depth=*/ 0 , /* max_depth=*/ 0 ,
928+ NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED , nullptr );
929+ RETURN_IF_PYERROR ();
930+ flattened.reset (
931+ PyArray_Ravel (reinterpret_cast <PyArrayObject*>(contiguous), NPY_CORDER ));
932+ Py_DECREF (contiguous);
922933 RETURN_IF_PYERROR ();
923934 value = flattened.obj ();
924935 ndarray = reinterpret_cast <PyArrayObject*>(value);
925936 }
926- if (PyArray_ISBYTESWAPPED (ndarray)) {
927- // TODO
928- return Status::NotImplemented (" Byte-swapped arrays not supported" );
929- }
930937 const int64_t size = PyArray_SIZE (ndarray);
931938 RETURN_NOT_OK (AppendTo (this ->list_type_ , size));
932939 RETURN_NOT_OK (this ->list_builder_ ->ValidateOverflow (size));
0 commit comments