cdef int32_t _binary_null = -1
-def format_row_binary(
- row: Sequence[Any], tx: Transformer, out: bytearray = None
+def _format_row_binary(
+ row: Sequence[Any], tx: Transformer, out: bytearray
) -> bytearray:
"""Convert a row of adapted data to the data to send for binary copy"""
cdef Py_ssize_t rowlen
rowlen = len(row)
cdef uint16_t berowlen = endian.htobe16(<int16_t>rowlen)
- cdef Py_ssize_t pos # offset in 'out' where to write
- if out is None:
- out = PyByteArray_FromStringAndSize("", 0)
- pos = 0
- else:
- pos = PyByteArray_GET_SIZE(out)
+ # offset in 'out' where to write
+ cdef Py_ssize_t pos = PyByteArray_GET_SIZE(out)
cdef char *target = CDumper.ensure_size(out, pos, sizeof(berowlen))
return out
+def format_row_binary(
+ row: Sequence[Any], tx: Transformer, out: bytearray = None
+) -> bytearray:
+ cdef Py_ssize_t size
+ if out is None:
+ out = PyByteArray_FromStringAndSize("", 0)
+ size = 0
+ else:
+ size = PyByteArray_GET_SIZE(out)
+
+ try:
+ _format_row_binary(row, tx, out)
+ except Exception as e:
+ PyByteArray_Resize(out, size)
+ raise e
+
+ return out
+
+
cdef int _append_binary_none(bytearray out, Py_ssize_t *pos) except -1:
cdef char *target
target = CDumper.ensure_size(out, pos[0], sizeof(_binary_null))
return 0
-def format_row_text(
- row: Sequence[Any], tx: Transformer, out: bytearray = None
+def _format_row_text(
+ row: Sequence[Any], tx: Transformer, out: bytearray
) -> bytearray:
- cdef Py_ssize_t pos # offset in 'out' where to write
- if out is None:
- out = PyByteArray_FromStringAndSize("", 0)
- pos = 0
- else:
- pos = PyByteArray_GET_SIZE(out)
-
- cdef Py_ssize_t rowlen
- if type(row) is list:
- rowlen = PyList_GET_SIZE(row)
- elif type(row) is tuple:
- rowlen = PyTuple_GET_SIZE(row)
- else:
- rowlen = len(row)
-
- if rowlen == 0:
- PyByteArray_Resize(out, pos + 1)
- out[pos] = b"\n"
- return out
+ # offset in 'out' where to write
+ cdef Py_ssize_t pos = PyByteArray_GET_SIZE(out)
cdef Py_ssize_t size, tmpsize
cdef char *buf
cdef int with_tab
cdef PyObject *fmt = <PyObject *>PG_TEXT
cdef PyObject *row_dumper
+ cdef Py_ssize_t rowlen = len(row)
# try to get preloaded dumpers from set_types
if not tx._row_dumpers:
return out
+def format_row_text(
+ row: Sequence[Any], tx: Transformer, out: bytearray = None
+) -> bytearray:
+ cdef Py_ssize_t size
+ if out is None:
+ out = PyByteArray_FromStringAndSize("", 0)
+ size = 0
+ else:
+ size = PyByteArray_GET_SIZE(out)
+
+ # exit early, if the row is empty
+ cdef Py_ssize_t rowlen = len(row)
+ if rowlen == 0:
+ PyByteArray_Resize(out, size + 1)
+ out[size] = b"\n"
+ return out
+
+ try:
+ _format_row_text(row, tx, out)
+ except Exception as e:
+ PyByteArray_Resize(out, size)
+ raise e
+
+ return out
+
+
cdef int _append_text_none(bytearray out, Py_ssize_t *pos, int with_tab) except -1:
cdef char *target