_oid = builtins["int8"].oid
-@Dumper.builtin(int)
-class IntBinaryDumper(IntDumper):
-
- format = Format.BINARY
-
- def dump(self, obj: int) -> bytes:
- return _pack_int8(obj)
-
-
@Dumper.builtin(float)
class FloatDumper(SpecialValuesDumper):
return _pack_int4(obj)
-@Dumper.builtin(Int8)
+@Dumper.builtin(int, Int8)
class Int8BinaryDumper(Int8Dumper):
format = Format.BINARY
from cpython.long cimport PyLong_FromLongLong, PyLong_FromUnsignedLong
from cpython.float cimport PyFloat_FromDouble, PyFloat_AsDouble
-from psycopg3_c._psycopg3.endian cimport be16toh, be32toh, be64toh, htobe64
+from psycopg3_c._psycopg3.endian cimport (
+ be16toh, be32toh, be64toh, htobe32, htobe64)
cdef extern from "Python.h":
# work around https://github.com/cython/cython/issues/3909
return rv
-cdef class IntBinaryDumper(IntDumper):
+cdef class Int4BinaryDumper(CDumper):
format = Format.BINARY
+ def __cinit__(self):
+ self.oid = oids.INT4_OID
+
+ cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
+ cdef char *buf = CDumper.ensure_size(rv, offset, sizeof(int32_t))
+ cdef long long val = PyLong_AsLongLong(obj)
+ # swap bytes if needed
+ cdef uint32_t *ptvar = <uint32_t *>(&val)
+ cdef int32_t beval = htobe32(ptvar[0])
+ memcpy(buf, <void *>&beval, sizeof(int32_t))
+ return sizeof(int32_t)
+
+
+cdef class Int8BinaryDumper(CDumper):
+
+ format = Format.BINARY
+
+ def __cinit__(self):
+ self.oid = oids.INT8_OID
+
cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
cdef char *buf = CDumper.ensure_size(rv, offset, sizeof(int64_t))
cdef long long val = PyLong_AsLongLong(obj)
logger.debug("registering optimised numeric c adapters")
IntDumper.register(int)
- IntBinaryDumper.register(int)
+ Int8BinaryDumper.register(int)
IntLoader.register(oids.INT2_OID)
IntLoader.register(oids.INT4_OID)