oid = oids.INT2_OID
cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
- cdef int16_t *buf = <int16_t *>CDumper.ensure_size(
- rv, offset, sizeof(int16_t))
- cdef int16_t val = <int16_t>PyLong_AsLongLong(obj)
- # swap bytes if needed
- cdef uint16_t *ptvar = <uint16_t *>(&val)
- buf[0] = endian.htobe16(ptvar[0])
- return sizeof(int16_t)
+ return dump_int_to_int2_binary(obj, rv, offset)
@cython.final
oid = oids.INT4_OID
cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
- cdef int32_t *buf = <int32_t *>CDumper.ensure_size(
- rv, offset, sizeof(int32_t))
- cdef int32_t val = <int32_t>PyLong_AsLongLong(obj)
- # swap bytes if needed
- cdef uint32_t *ptvar = <uint32_t *>(&val)
- buf[0] = endian.htobe32(ptvar[0])
- return sizeof(int32_t)
+ return dump_int_to_int4_binary(obj, rv, offset)
@cython.final
oid = oids.INT8_OID
cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
- cdef int64_t *buf = <int64_t *>CDumper.ensure_size(
- rv, offset, sizeof(int64_t))
- cdef int64_t val = PyLong_AsLongLong(obj)
- # swap bytes if needed
- cdef uint64_t *ptvar = <uint64_t *>(&val)
- buf[0] = endian.htobe64(ptvar[0])
- return sizeof(int64_t)
+ return dump_int_to_int8_binary(obj, rv, offset)
cdef extern from *:
cdef class IntDumper(CDumper):
+ format = PQ_TEXT
+
cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
raise TypeError(
f"{type(self).__name__} is a dispatcher to other dumpers:"
return length
+cdef Py_ssize_t dump_int_to_int2_binary(
+ obj, bytearray rv, Py_ssize_t offset
+) except -1:
+ cdef int16_t *buf = <int16_t *>CDumper.ensure_size(
+ rv, offset, sizeof(int16_t))
+ cdef int16_t val = <int16_t>PyLong_AsLongLong(obj)
+ # swap bytes if needed
+ cdef uint16_t *ptvar = <uint16_t *>(&val)
+ buf[0] = endian.htobe16(ptvar[0])
+ return sizeof(int16_t)
+
+
+cdef Py_ssize_t dump_int_to_int4_binary(
+ obj, bytearray rv, Py_ssize_t offset
+) except -1:
+ cdef int32_t *buf = <int32_t *>CDumper.ensure_size(
+ rv, offset, sizeof(int32_t))
+ cdef int32_t val = <int32_t>PyLong_AsLongLong(obj)
+ # swap bytes if needed
+ cdef uint32_t *ptvar = <uint32_t *>(&val)
+ buf[0] = endian.htobe32(ptvar[0])
+ return sizeof(int32_t)
+
+
+cdef Py_ssize_t dump_int_to_int8_binary(
+ obj, bytearray rv, Py_ssize_t offset
+) except -1:
+ cdef int64_t *buf = <int64_t *>CDumper.ensure_size(
+ rv, offset, sizeof(int64_t))
+ cdef int64_t val = PyLong_AsLongLong(obj)
+ # swap bytes if needed
+ cdef uint64_t *ptvar = <uint64_t *>(&val)
+ buf[0] = endian.htobe64(ptvar[0])
+ return sizeof(int64_t)
+
+
cdef Py_ssize_t dump_int_to_numeric_binary(obj, bytearray rv, Py_ssize_t offset) except -1:
# Calculate the number of PG digits required to store the number
cdef uint16_t ndigits
--- /dev/null
+"""
+Cython adapters for numpy types.
+"""
+
+# Copyright (C) 2020 The Psycopg Team
+
+cimport cython
+
+
+@cython.final
+cdef class NPInt16Dumper(_IntDumper):
+
+ oid = oids.INT2_OID
+
+
+@cython.final
+cdef class NPInt32Dumper(_IntDumper):
+
+ oid = oids.INT4_OID
+
+
+@cython.final
+cdef class NPInt64Dumper(_IntDumper):
+
+ oid = oids.INT8_OID
+
+
+@cython.final
+cdef class NPNumericDumper(_IntDumper):
+
+ oid = oids.NUMERIC_OID
+
+
+@cython.final
+cdef class NPInt16BinaryDumper(_IntDumper):
+
+ oid = oids.INT2_OID
+ format = PQ_BINARY
+
+ cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
+ return dump_int_to_int2_binary(int(obj), rv, offset)
+
+
+@cython.final
+cdef class NPInt32BinaryDumper(_IntDumper):
+
+ oid = oids.INT4_OID
+ format = PQ_BINARY
+
+ cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
+ return dump_int_to_int4_binary(int(obj), rv, offset)
+
+
+@cython.final
+cdef class NPInt64BinaryDumper(_IntDumper):
+
+ oid = oids.INT8_OID
+ format = PQ_BINARY
+
+ cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
+ return dump_int_to_int8_binary(int(obj), rv, offset)
+
+
+@cython.final
+cdef class NPNumericBinaryDumper(_IntDumper):
+
+ oid = oids.NUMERIC_OID
+ format = PQ_BINARY
+
+ cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
+ return dump_int_to_numeric_binary(int(obj), rv, offset)