`usecols` must point to a Python object that is Py_None or a 1-d contiguous numpy array with data type int32. `dtype` must point to a Python object that is Py_None or a numpy dtype instance. If the latter, code and sizes must be arrays of length num_dtype_fields, holding the flattened data field type codes and byte sizes. (num_dtype_fields, codes, and sizes can be inferred from dtype, but we do
| 35 | // number of columns in the file must match the number of fields in `dtype`. |
| 36 | // |
| 37 | static PyObject * |
| 38 | _readtext_from_stream(stream *s, |
| 39 | parser_config *pc, Py_ssize_t num_usecols, Py_ssize_t usecols[], |
| 40 | Py_ssize_t skiplines, Py_ssize_t max_rows, |
| 41 | PyObject *converters, PyObject *dtype) |
| 42 | { |
| 43 | PyArrayObject *arr = NULL; |
| 44 | PyArray_Descr *out_dtype = NULL; |
| 45 | field_type *ft = NULL; |
| 46 | |
| 47 | /* |
| 48 | * If dtypes[0] is dtype the input was not structured and the result |
| 49 | * is considered "homogeneous" and we have to discover the number of |
| 50 | * columns/ |
| 51 | */ |
| 52 | out_dtype = (PyArray_Descr *)dtype; |
| 53 | Py_INCREF(out_dtype); |
| 54 | |
| 55 | Py_ssize_t num_fields = field_types_create(out_dtype, &ft); |
| 56 | if (num_fields < 0) { |
| 57 | goto finish; |
| 58 | } |
| 59 | bool homogeneous = num_fields == 1 && ft[0].descr == out_dtype; |
| 60 | |
| 61 | if (!homogeneous && usecols != NULL && num_usecols != num_fields) { |
| 62 | PyErr_Format(PyExc_TypeError, |
| 63 | "If a structured dtype is used, the number of columns in " |
| 64 | "`usecols` must match the effective number of fields. " |
| 65 | "But %zd usecols were given and the number of fields is %zd.", |
| 66 | num_usecols, num_fields); |
| 67 | goto finish; |
| 68 | } |
| 69 | |
| 70 | arr = read_rows( |
| 71 | s, max_rows, num_fields, ft, pc, |
| 72 | num_usecols, usecols, skiplines, converters, |
| 73 | NULL, out_dtype, homogeneous); |
| 74 | if (arr == NULL) { |
| 75 | goto finish; |
| 76 | } |
| 77 | |
| 78 | finish: |
| 79 | Py_XDECREF(out_dtype); |
| 80 | field_types_xclear(num_fields, ft); |
| 81 | return (PyObject *)arr; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static int |
no test coverage detected