from .. import pq
from .. import errors as e
from .. import postgres
-from ..abc import AdaptContext, Buffer, Dumper, DumperKey, NoneType, LoadFunc
+from ..abc import AdaptContext, Buffer, Dumper, DumperKey, NoneType, Loader, Transformer
from ..adapt import RecursiveDumper, RecursiveLoader, PyFormat
from .._compat import cache, prod
from .._struct import pack_len, unpack_len
TEXT_ARRAY_OID = postgres.types["text"].array_oid
+PY_TEXT = PyFormat.TEXT
+PQ_BINARY = pq.Format.BINARY
+
class BaseListDumper(RecursiveDumper):
element_oid = 0
if self.sub_dumper:
return self.sub_dumper.dump(item)
else:
- return self._tx.get_dumper(item, PyFormat.TEXT).dump(item)
+ return self._tx.get_dumper(item, PY_TEXT).dump(item)
@cache
delimiter = b","
def load(self, data: Buffer) -> List[Any]:
- load = self._tx.get_loader(self.base_oid, self.format).load
- return load_text(data, load, self.delimiter)
+ loader = self._tx.get_loader(self.base_oid, self.format)
+ return load_text(data, loader, self.delimiter)
class ArrayBinaryLoader(BaseArrayLoader):
format = pq.Format.BINARY
def load(self, data: Buffer) -> List[Any]:
- load = self._tx.get_loader(self.base_oid, self.format).load
- return load_binary(data, load)
+ return load_binary(data, self._tx)
def register_array(info: TypeInfo, context: Optional[AdaptContext] = None) -> None:
def _load_text(
data: Buffer,
- load: LoadFunc,
+ loader: Loader,
delimiter: bytes = b",",
__re_unescape: Pattern[bytes] = re.compile(rb"\\(.)"),
) -> List[Any]:
rv = None
stack: List[Any] = []
+ load = loader.load
# Remove the dimensions information prefix (``[...]=``)
if data and data[0] == b"["[0]:
)
-def _load_binary(data: Buffer, load: LoadFunc) -> List[Any]:
+def _load_binary(data: Buffer, tx: Transformer) -> List[Any]:
ndims, hasnull, oid = _unpack_head(data)
+ load = tx.get_loader(oid, PQ_BINARY).load
if not ndims:
return []
def array_load_text(
- data: Buffer, load: LoadFunc, delimiter: bytes = b","
+ data: Buffer, loader: Loader, delimiter: bytes = b","
) -> List[Any]:
cdef char cdelim = delimiter[0]
cdef char *buf
cdef Py_ssize_t length
_buffer_as_string_and_size(data, &buf, &length)
+ load = loader.load
+
if length == 0:
raise e.DataError("malformed array: empty data")
PyMem_Free(unesc)
-def array_load_binary(data: Buffer, load: LoadFunc) -> List[Any]:
+def array_load_binary(data: Buffer, tx: Transformer) -> List[Any]:
cdef char *buf
cdef Py_ssize_t length
_buffer_as_string_and_size(data, &buf, &length)
# head is ndims, hasnull, elem oid
cdef int32_t *buf32 = <int32_t *>buf
cdef int ndims = endian.be32toh(buf32[0])
+ cdef int oid = endian.be32toh(buf32[2])
+
+ load = tx.get_loader(oid, PQ_BINARY).load
if not ndims:
return []