| 1502 | } |
| 1503 | |
| 1504 | int Fory_PyPrimitiveCollectionReadFromBuffer(PyObject *collection, |
| 1505 | Buffer *buffer, Py_ssize_t size, |
| 1506 | uint8_t type_id) { |
| 1507 | if (FORY_PREDICT_FALSE(size < 0)) { |
| 1508 | PyErr_SetString(PyExc_ValueError, "negative collection size"); |
| 1509 | return -1; |
| 1510 | } |
| 1511 | |
| 1512 | const PythonCollectionKind kind = resolve_python_collection_kind(collection); |
| 1513 | if (FORY_PREDICT_FALSE(PyErr_Occurred() != nullptr)) { |
| 1514 | return -1; |
| 1515 | } |
| 1516 | if (kind == PythonCollectionKind::List && Py_SIZE(collection) < size) { |
| 1517 | PyErr_SetString(PyExc_ValueError, |
| 1518 | "list collection size is smaller than requested read size"); |
| 1519 | return -1; |
| 1520 | } |
| 1521 | if (kind == PythonCollectionKind::Tuple && Py_SIZE(collection) < size) { |
| 1522 | PyErr_SetString( |
| 1523 | PyExc_ValueError, |
| 1524 | "tuple collection size is smaller than requested read size"); |
| 1525 | return -1; |
| 1526 | } |
| 1527 | if (!buffer->has_input_stream() && kind == PythonCollectionKind::List) { |
| 1528 | return read_primitive_sequence_indexed( |
| 1529 | buffer, size, type_id, [collection](Py_ssize_t i, PyObject *item) { |
| 1530 | PyList_SET_ITEM(collection, i, item); |
| 1531 | }); |
| 1532 | } |
| 1533 | if (!buffer->has_input_stream() && kind == PythonCollectionKind::Tuple) { |
| 1534 | return read_primitive_sequence_indexed( |
| 1535 | buffer, size, type_id, [collection](Py_ssize_t i, PyObject *item) { |
| 1536 | PyTuple_SET_ITEM(collection, i, item); |
| 1537 | }); |
| 1538 | } |
| 1539 | |
| 1540 | for (Py_ssize_t i = 0; i < size; ++i) { |
| 1541 | PyObject *item = read_primitive_item(buffer, type_id); |
| 1542 | if (FORY_PREDICT_FALSE(item == nullptr)) { |
| 1543 | return -1; |
| 1544 | } |
| 1545 | if (kind == PythonCollectionKind::List) { |
| 1546 | PyList_SET_ITEM(collection, i, item); |
| 1547 | } else if (kind == PythonCollectionKind::Tuple) { |
| 1548 | PyTuple_SET_ITEM(collection, i, item); |
| 1549 | } else { |
| 1550 | if (FORY_PREDICT_FALSE(PySet_Add(collection, item) < 0)) { |
| 1551 | Py_DECREF(item); |
| 1552 | return -1; |
| 1553 | } |
| 1554 | Py_DECREF(item); |
| 1555 | } |
| 1556 | } |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
| 1560 | int Fory_PyWriteBasicFieldToBuffer(PyObject *value, Buffer *buffer, |
| 1561 | uint8_t type_id) { |
nothing calls this directly
no test coverage detected