From: Daniele Varrazzo Date: Wed, 30 Dec 2020 22:34:23 +0000 (+0100) Subject: Using new types registration system on the C implementation too X-Git-Tag: 3.0.dev0~215 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22e6aa42446a9c8143d273e3f9472b96edb89527;p=thirdparty%2Fpsycopg.git Using new types registration system on the C implementation too --- diff --git a/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx b/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx index 786b15f5f..aa6d30bfb 100644 --- a/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx +++ b/psycopg3_c/psycopg3_c/_psycopg3/adapt.pyx @@ -100,7 +100,7 @@ cdef class CDumper: else: from psycopg3.adapt import global_adapters as adapters - adapters.register_dumper(src, cls, format=format) + adapters.register_dumper(src, cls) cdef class CLoader: @@ -132,7 +132,7 @@ cdef class CLoader: else: from psycopg3.adapt import global_adapters as adapters - adapters.register_loader(oid, cls, format=format) + adapters.register_loader(oid, cls) def register_builtin_c_adapters(): diff --git a/psycopg3_c/psycopg3_c/types/numeric.pyx b/psycopg3_c/psycopg3_c/types/numeric.pyx index f52447015..67a947653 100644 --- a/psycopg3_c/psycopg3_c/types/numeric.pyx +++ b/psycopg3_c/psycopg3_c/types/numeric.pyx @@ -21,6 +21,8 @@ cdef extern from "Python.h": cdef class IntDumper(CDumper): + format = Format.TEXT + def __cinit__(self): self.oid = oids.INT8_OID @@ -46,6 +48,9 @@ cdef class IntDumper(CDumper): cdef class IntBinaryDumper(IntDumper): + + format = Format.BINARY + def dump(self, obj) -> bytes: cdef long long val = PyLong_AsLongLong(obj) cdef uint64_t *ptvar = (&val) @@ -55,37 +60,58 @@ cdef class IntBinaryDumper(IntDumper): cdef class IntLoader(CLoader): + + format = Format.TEXT + cdef object cload(self, const char *data, size_t length): return PyLong_FromString(data, NULL, 10) cdef class Int2BinaryLoader(CLoader): + + format = Format.BINARY + cdef object cload(self, const char *data, size_t length): return PyLong_FromLong(be16toh((data)[0])) cdef class Int4BinaryLoader(CLoader): + + format = Format.BINARY + cdef object cload(self, const char *data, size_t length): return PyLong_FromLong(be32toh((data)[0])) cdef class Int8BinaryLoader(CLoader): + + format = Format.BINARY + cdef object cload(self, const char *data, size_t length): return PyLong_FromLongLong(be64toh((data)[0])) cdef class OidBinaryLoader(CLoader): + + format = Format.BINARY + cdef object cload(self, const char *data, size_t length): return PyLong_FromUnsignedLong(be32toh((data)[0])) cdef class FloatLoader(CLoader): + + format = Format.TEXT + cdef object cload(self, const char *data, size_t length): cdef double d = PyOS_string_to_double(data, NULL, OverflowError) return PyFloat_FromDouble(d) cdef class Float4BinaryLoader(CLoader): + + format = Format.BINARY + cdef object cload(self, const char *data, size_t length): cdef uint32_t asint = be32toh((data)[0]) # avoid warning: @@ -95,6 +121,9 @@ cdef class Float4BinaryLoader(CLoader): cdef class Float8BinaryLoader(CLoader): + + format = Format.BINARY + cdef object cload(self, const char *data, size_t length): cdef uint64_t asint = be64toh((data)[0]) cdef char *swp = &asint @@ -105,7 +134,7 @@ cdef void register_numeric_c_adapters(): logger.debug("registering optimised numeric c adapters") IntDumper.register(int) - IntBinaryDumper.register(int, format=Format.BINARY) + IntBinaryDumper.register(int) IntLoader.register(oids.INT2_OID) IntLoader.register(oids.INT4_OID) @@ -114,9 +143,9 @@ cdef void register_numeric_c_adapters(): FloatLoader.register(oids.FLOAT4_OID) FloatLoader.register(oids.FLOAT8_OID) - Int2BinaryLoader.register(oids.INT2_OID, format=Format.BINARY) - Int4BinaryLoader.register(oids.INT4_OID, format=Format.BINARY) - Int8BinaryLoader.register(oids.INT8_OID, format=Format.BINARY) - OidBinaryLoader.register(oids.OID_OID, format=Format.BINARY) - Float4BinaryLoader.register(oids.FLOAT4_OID, format=Format.BINARY) - Float8BinaryLoader.register(oids.FLOAT8_OID, format=Format.BINARY) + Int2BinaryLoader.register(oids.INT2_OID) + Int4BinaryLoader.register(oids.INT4_OID) + Int8BinaryLoader.register(oids.INT8_OID) + OidBinaryLoader.register(oids.OID_OID) + Float4BinaryLoader.register(oids.FLOAT4_OID) + Float8BinaryLoader.register(oids.FLOAT8_OID) diff --git a/psycopg3_c/psycopg3_c/types/singletons.pyx b/psycopg3_c/psycopg3_c/types/singletons.pyx index 1a383cd0d..58fcaace7 100644 --- a/psycopg3_c/psycopg3_c/types/singletons.pyx +++ b/psycopg3_c/psycopg3_c/types/singletons.pyx @@ -8,6 +8,9 @@ from psycopg3.pq import Format cdef class BoolDumper(CDumper): + + format = Format.TEXT + def __cinit__(self): self.oid = oids.BOOL_OID @@ -30,6 +33,9 @@ cdef class BoolDumper(CDumper): cdef class BoolBinaryDumper(BoolDumper): + + format = Format.BINARY + def dump(self, obj) -> bytes: if obj is True: return b"\x01" @@ -40,12 +46,18 @@ cdef class BoolBinaryDumper(BoolDumper): cdef class BoolLoader(CLoader): + + format = Format.TEXT + cdef object cload(self, const char *data, size_t length): # this creates better C than `return data[0] == b't'` return True if data[0] == b't' else False cdef class BoolBinaryLoader(CLoader): + + format = Format.BINARY + cdef object cload(self, const char *data, size_t length): return True if data[0] else False @@ -54,7 +66,7 @@ cdef void register_singletons_c_adapters(): logger.debug("registering optimised singletons c adapters") BoolDumper.register(bool) - BoolBinaryDumper.register(bool, format=Format.BINARY) + BoolBinaryDumper.register(bool) BoolLoader.register(oids.BOOL_OID) - BoolBinaryLoader.register(oids.BOOL_OID, format=Format.BINARY) + BoolBinaryLoader.register(oids.BOOL_OID) diff --git a/psycopg3_c/psycopg3_c/types/text.pyx b/psycopg3_c/psycopg3_c/types/text.pyx index 5c21096ef..a6a2701d2 100644 --- a/psycopg3_c/psycopg3_c/types/text.pyx +++ b/psycopg3_c/psycopg3_c/types/text.pyx @@ -34,6 +34,9 @@ cdef class _StringDumper(CDumper): cdef class StringBinaryDumper(_StringDumper): + + format = Format.BINARY + def dump(self, obj) -> bytes: # the server will raise DataError subclass if the string contains 0x00 if self.is_utf8: @@ -43,6 +46,9 @@ cdef class StringBinaryDumper(_StringDumper): cdef class StringDumper(_StringDumper): + + format = Format.TEXT + def dump(self, obj) -> bytes: cdef bytes rv cdef char *buf @@ -66,6 +72,9 @@ cdef class StringDumper(_StringDumper): cdef class TextLoader(CLoader): + + format = Format.TEXT + cdef int is_utf8 cdef char *encoding cdef bytes _bytes_encoding # needed to keep `encoding` alive @@ -94,7 +103,14 @@ cdef class TextLoader(CLoader): return data[:length] +cdef class TextBinaryLoader(TextLoader): + format = Format.BINARY + + cdef class BytesDumper(CDumper): + + format = Format.TEXT + cdef Escaping esc def __cinit__(self): @@ -109,11 +125,17 @@ cdef class BytesDumper(CDumper): cdef class BytesBinaryDumper(BytesDumper): + + format = Format.BINARY + def dump(self, obj): return obj cdef class ByteaLoader(CLoader): + + format = Format.TEXT + cdef object cload(self, const char *data, size_t length): cdef size_t len_out cdef unsigned char *out = libpq.PQunescapeBytea( @@ -129,6 +151,9 @@ cdef class ByteaLoader(CLoader): cdef class ByteaBinaryLoader(CLoader): + + format = Format.BINARY + cdef object cload(self, const char *data, size_t length): return data[:length] @@ -137,25 +162,25 @@ cdef void register_text_c_adapters(): logger.debug("registering optimised text c adapters") StringDumper.register(str) - StringBinaryDumper.register(str, format=Format.BINARY) + StringBinaryDumper.register(str) TextLoader.register(oids.INVALID_OID) TextLoader.register(oids.BPCHAR_OID) - TextLoader.register(oids.BPCHAR_OID, format=Format.BINARY) TextLoader.register(oids.NAME_OID) - TextLoader.register(oids.NAME_OID, format=Format.BINARY) TextLoader.register(oids.TEXT_OID) - TextLoader.register(oids.TEXT_OID, format=Format.BINARY) TextLoader.register(oids.VARCHAR_OID) - TextLoader.register(oids.VARCHAR_OID, format=Format.BINARY) + TextBinaryLoader.register(oids.BPCHAR_OID) + TextBinaryLoader.register(oids.NAME_OID) + TextBinaryLoader.register(oids.TEXT_OID) + TextBinaryLoader.register(oids.VARCHAR_OID) BytesDumper.register(bytes) BytesDumper.register(bytearray) BytesDumper.register(memoryview) - BytesBinaryDumper.register(bytes, format=Format.BINARY) - BytesBinaryDumper.register(bytearray, format=Format.BINARY) - BytesBinaryDumper.register(memoryview, format=Format.BINARY) + BytesBinaryDumper.register(bytes) + BytesBinaryDumper.register(bytearray) + BytesBinaryDumper.register(memoryview) ByteaLoader.register(oids.BYTEA_OID) - ByteaBinaryLoader.register(oids.BYTEA_OID, format=Format.BINARY) - ByteaBinaryLoader.register(oids.INVALID_OID, format=Format.BINARY) + ByteaBinaryLoader.register(oids.BYTEA_OID) + ByteaBinaryLoader.register(oids.INVALID_OID)