]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added int4 C dumper
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 1 Jan 2021 20:58:01 +0000 (21:58 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 8 Jan 2021 01:26:53 +0000 (02:26 +0100)
psycopg3/psycopg3/types/numeric.py
psycopg3_c/psycopg3_c/types/numeric.pyx

index 4f9b771eb63170afc7c2a81022695d6620584e8b..54723d80dc04b0ae586b9e0ea01abf41f58e3b06 100644 (file)
@@ -82,15 +82,6 @@ class IntDumper(NumberDumper):
     _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):
 
@@ -164,7 +155,7 @@ class Int4BinaryDumper(Int4Dumper):
         return _pack_int4(obj)
 
 
-@Dumper.builtin(Int8)
+@Dumper.builtin(int, Int8)
 class Int8BinaryDumper(Int8Dumper):
 
     format = Format.BINARY
index 2114551d163229b374f2a78ba2559f68bd61c964..eeb7562373bd2f431f61a7e272510b47a94e3ef8 100644 (file)
@@ -11,7 +11,8 @@ from cpython.long cimport PyLong_FromString, PyLong_FromLong, PyLong_AsLongLong
 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
@@ -53,10 +54,30 @@ cdef class IntDumper(CDumper):
         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)
@@ -191,7 +212,7 @@ cdef void register_numeric_c_adapters():
     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)