From: Daniele Varrazzo Date: Fri, 1 Jan 2021 20:58:01 +0000 (+0100) Subject: Added int4 C dumper X-Git-Tag: 3.0.dev0~207 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a2375a4457201a22f581d6fea3e3eb2207f86358;p=thirdparty%2Fpsycopg.git Added int4 C dumper --- diff --git a/psycopg3/psycopg3/types/numeric.py b/psycopg3/psycopg3/types/numeric.py index 4f9b771eb..54723d80d 100644 --- a/psycopg3/psycopg3/types/numeric.py +++ b/psycopg3/psycopg3/types/numeric.py @@ -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 diff --git a/psycopg3_c/psycopg3_c/types/numeric.pyx b/psycopg3_c/psycopg3_c/types/numeric.pyx index 2114551d1..eeb756237 100644 --- a/psycopg3_c/psycopg3_c/types/numeric.pyx +++ b/psycopg3_c/psycopg3_c/types/numeric.pyx @@ -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 = (&val) + cdef int32_t beval = htobe32(ptvar[0]) + memcpy(buf, &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)