]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Make Dumper.oid a class attribute for C classes too
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 26 Aug 2021 22:20:45 +0000 (00:20 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 27 Aug 2021 22:23:47 +0000 (00:23 +0200)
psycopg_c/psycopg_c/_psycopg/adapt.pyx
psycopg_c/psycopg_c/types/bool.pyx
psycopg_c/psycopg_c/types/datetime.pyx
psycopg_c/psycopg_c/types/numeric.pyx
psycopg_c/psycopg_c/types/string.pyx

index 51d75ce690f7c6b47eab4381020013815c4c18b1..81cb3793c2a901d59bbd306fbc23a7668fe60f54 100644 (file)
@@ -32,10 +32,12 @@ logger = logging.getLogger("psycopg.adapt")
 
 @cython.freelist(8)
 cdef class CDumper:
+
     cdef readonly object cls
-    cdef public libpq.Oid oid
     cdef pq.PGconn _pgconn
 
+    oid = oids.INVALID_OID
+
     def __init__(self, cls, context: Optional[AdaptContext] = None):
         self.cls = cls
         conn = context.connection if context is not None else None
index c0f8c229df625079426ee30238d15ecf39c791cd..a1ed9252df9df7989d3969ded5698a688ec7c133 100644 (file)
@@ -11,9 +11,7 @@ cimport cython
 cdef class BoolDumper(CDumper):
 
     format = PQ_TEXT
-
-    def __cinit__(self):
-        self.oid = oids.BOOL_OID
+    oid = oids.BOOL_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef char *buf = CDumper.ensure_size(rv, offset, 1)
@@ -43,9 +41,7 @@ cdef class BoolDumper(CDumper):
 cdef class BoolBinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.BOOL_OID
+    oid = oids.BOOL_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef char *buf = CDumper.ensure_size(rv, offset, 1)
index 01c73f70f2c6b3bac5c82f9bec71408e2ce39a48..f8c58649390192a5864731a75e74a41b56cb3dc2 100644 (file)
@@ -71,9 +71,7 @@ cdef object _month_abbr = {
 cdef class DateDumper(CDumper):
 
     format = PQ_TEXT
-
-    def __cinit__(self):
-        self.oid = oids.DATE_OID
+    oid = oids.DATE_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef Py_ssize_t size;
@@ -93,9 +91,7 @@ cdef class DateDumper(CDumper):
 cdef class DateBinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.DATE_OID
+    oid = oids.DATE_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef int32_t days = PyObject_CallFunctionObjArgs(
@@ -140,8 +136,7 @@ cdef class _BaseTimeTextDumper(_BaseTimeDumper):
 @cython.final
 cdef class TimeDumper(_BaseTimeTextDumper):
 
-    def __cinit__(self):
-        self.oid = oids.TIME_OID
+    oid = oids.TIME_OID
 
     cpdef upgrade(self, obj, format):
         if not obj.tzinfo:
@@ -153,17 +148,14 @@ cdef class TimeDumper(_BaseTimeTextDumper):
 @cython.final
 cdef class TimeTzDumper(_BaseTimeTextDumper):
 
-    def __cinit__(self):
-        self.oid = oids.TIMETZ_OID
+    oid = oids.TIMETZ_OID
 
 
 @cython.final
 cdef class TimeBinaryDumper(_BaseTimeDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.TIME_OID
+    oid = oids.TIME_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef int64_t micros = cdt.time_microsecond(obj) + 1000000 * (
@@ -187,9 +179,7 @@ cdef class TimeBinaryDumper(_BaseTimeDumper):
 cdef class TimeTzBinaryDumper(_BaseTimeDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.TIMETZ_OID
+    oid = oids.TIMETZ_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef int64_t micros = cdt.time_microsecond(obj) + 1_000_000 * (
@@ -244,8 +234,7 @@ cdef class _BaseDatetimeTextDumper(_BaseDatetimeDumper):
 @cython.final
 cdef class DatetimeDumper(_BaseDatetimeTextDumper):
 
-    def __cinit__(self):
-        self.oid = oids.TIMESTAMPTZ_OID
+    oid = oids.TIMESTAMPTZ_OID
 
     cpdef upgrade(self, obj, format):
         if obj.tzinfo:
@@ -257,17 +246,14 @@ cdef class DatetimeDumper(_BaseDatetimeTextDumper):
 @cython.final
 cdef class DatetimeNoTzDumper(_BaseDatetimeTextDumper):
 
-    def __cinit__(self):
-        self.oid = oids.TIMESTAMP_OID
+    oid = oids.TIMESTAMP_OID
 
 
 @cython.final
 cdef class DatetimeBinaryDumper(_BaseDatetimeDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.TIMESTAMPTZ_OID
+    oid = oids.TIMESTAMPTZ_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         delta = obj - pg_datetimetz_epoch
@@ -291,9 +277,7 @@ cdef class DatetimeBinaryDumper(_BaseDatetimeDumper):
 cdef class DatetimeNoTzBinaryDumper(_BaseDatetimeDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.TIMESTAMP_OID
+    oid = oids.TIMESTAMP_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         delta = obj - pg_datetime_epoch
@@ -311,13 +295,11 @@ cdef class DatetimeNoTzBinaryDumper(_BaseDatetimeDumper):
 cdef class TimedeltaDumper(CDumper):
 
     format = PQ_TEXT
+    oid = oids.INTERVAL_OID
     cdef int _style
 
-    def __cinit__(self):
-        self.oid = oids.INTERVAL_OID
-
-    def __init__(self, oid: int, context: Optional[AdaptContext] = None):
-        super().__init__(oid, context)
+    def __init__(self, cls, context: Optional[AdaptContext] = None):
+        super().__init__(cls, context)
 
         cdef const char *ds = _get_intervalstyle(self._pgconn)
         if ds[0] == b's':  # sql_standard
@@ -349,9 +331,7 @@ cdef class TimedeltaDumper(CDumper):
 cdef class TimedeltaBinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.INTERVAL_OID
+    oid = oids.INTERVAL_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef int64_t micros = (
index 37084c379faf1952be69a098aa686fb8a0bb1369..953868912d6472d0cfe18639a141ed3567185a07 100644 (file)
@@ -88,38 +88,32 @@ cdef class _NumberDumper(CDumper):
 @cython.final
 cdef class Int2Dumper(_NumberDumper):
 
-    def __cinit__(self):
-        self.oid = oids.INT2_OID
+    oid = oids.INT2_OID
 
 
 @cython.final
 cdef class Int4Dumper(_NumberDumper):
 
-    def __cinit__(self):
-        self.oid = oids.INT4_OID
+    oid = oids.INT4_OID
 
 
 @cython.final
 cdef class Int8Dumper(_NumberDumper):
 
-    def __cinit__(self):
-        self.oid = oids.INT8_OID
+    oid = oids.INT8_OID
 
 
 @cython.final
 cdef class IntNumericDumper(_NumberDumper):
 
-    def __cinit__(self):
-        self.oid = oids.NUMERIC_OID
+    oid = oids.NUMERIC_OID
 
 
 @cython.final
 cdef class Int2BinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.INT2_OID
+    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(
@@ -135,9 +129,7 @@ cdef class Int2BinaryDumper(CDumper):
 cdef class Int4BinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.INT4_OID
+    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(
@@ -153,9 +145,7 @@ cdef class Int4BinaryDumper(CDumper):
 cdef class Int8BinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.INT8_OID
+    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(
@@ -182,9 +172,7 @@ DEF NUMERIC_NINF = 0xF000
 cdef class IntNumericBinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.NUMERIC_OID
+    oid = oids.NUMERIC_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         # Calculate the number of PG digits required to store the number
@@ -342,9 +330,7 @@ cdef class OidBinaryLoader(CLoader):
 cdef class FloatDumper(CDumper):
 
     format = PQ_TEXT
-
-    def __cinit__(self):
-        self.oid = oids.FLOAT8_OID
+    oid = oids.FLOAT8_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef double d = PyFloat_AsDouble(obj)
@@ -375,9 +361,7 @@ cdef dict _special_float = {
 cdef class FloatBinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.FLOAT8_OID
+    oid = oids.FLOAT8_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef double d = PyFloat_AsDouble(obj)
@@ -428,9 +412,7 @@ cdef class Float8BinaryLoader(CLoader):
 cdef class DecimalDumper(CDumper):
 
     format = PQ_TEXT
-
-    def __cinit__(self):
-        self.oid = oids.NUMERIC_OID
+    oid = oids.NUMERIC_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef char *src
@@ -546,9 +528,7 @@ static const int pydigit_weights[] = {1000, 100, 10, 1};
 cdef class DecimalBinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.NUMERIC_OID
+    oid = oids.NUMERIC_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
 
index 950cb109debb47aa3cbfaaa0cfd5487960d941be..239b804d8d0fcfda308cec66f4f4aaa9c7abf548 100644 (file)
@@ -73,9 +73,7 @@ cdef class _BaseStrDumper(CDumper):
 cdef class StrBinaryDumper(_BaseStrDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.TEXT_OID
+    oid = oids.TEXT_OID
 
 
 cdef class _StrDumper(_BaseStrDumper):
@@ -97,8 +95,7 @@ cdef class _StrDumper(_BaseStrDumper):
 @cython.final
 cdef class StrDumper(_StrDumper):
 
-    def __cinit__(self):
-        self.oid = oids.TEXT_OID
+    oid = oids.TEXT_OID
 
 
 @cython.final
@@ -158,12 +155,12 @@ cdef class TextBinaryLoader(_TextLoader):
 cdef class BytesDumper(CDumper):
 
     format = PQ_TEXT
+    oid = oids.BYTEA_OID
 
     # 0: not set, 1: just  single "'" quote, 3: " E'" qoute
     cdef int _qplen
 
     def __cinit__(self):
-        self.oid = oids.BYTEA_OID
         self._qplen = 0
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
@@ -252,9 +249,7 @@ cdef class BytesDumper(CDumper):
 cdef class BytesBinaryDumper(CDumper):
 
     format = PQ_BINARY
-
-    def __cinit__(self):
-        self.oid = oids.BYTEA_OID
+    oid = oids.BYTEA_OID
 
     cdef Py_ssize_t cdump(self, obj, bytearray rv, Py_ssize_t offset) except -1:
         cdef char *src