]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Fix dumping Decimal of the form 0E+n
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 9 May 2021 23:22:12 +0000 (01:22 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 9 May 2021 23:22:12 +0000 (01:22 +0200)
psycopg3/psycopg3/types/numeric.py
psycopg3_c/psycopg3_c/types/numeric.pyx

index ebfeb989605fa027c3a16f027056f41f7af72a5c..0108e95aafeab4b474b473ed4bb8d41773ceabbe 100644 (file)
@@ -401,9 +401,6 @@ class DecimalBinaryDumper(Dumper):
         while nzdigits > 0 and digits[nzdigits - 1] == 0:
             nzdigits -= 1
 
-        if not nzdigits:
-            return _pack_numeric_head(0, 0, NUMERIC_POS, -exp)
-
         if exp <= 0:
             dscale = -exp
         else:
@@ -411,6 +408,9 @@ class DecimalBinaryDumper(Dumper):
             # align the py digits to the pg digits if there's some py exponent
             ndigits += exp % DEC_DIGITS
 
+        if not nzdigits:
+            return _pack_numeric_head(0, 0, NUMERIC_POS, dscale)
+
         # Equivalent of 0-padding left to align the py digits to the pg digits
         # but without changing the digits tuple.
         mod = (ndigits - dscale) % DEC_DIGITS
index 72e6586efc7381d7a3a19fb075f238b3100fb8a2..a2646047fa616b9a637f8eca4012d34211c30925 100644 (file)
@@ -595,15 +595,6 @@ cdef class DecimalBinaryDumper(CDumper):
         while nzdigits > 0 and digits[nzdigits - 1] == 0:
             nzdigits -= 1
 
-        if nzdigits == 0:
-            length = 4 * sizeof(uint16_t)
-            buf = <uint16_t *>CDumper.ensure_size(rv, offset, length)
-            buf[0] = 0  # ndigits
-            buf[1] = 0  # weight
-            buf[2] = endian.htobe16(NUMERIC_POS)  # sign
-            buf[3] = endian.htobe16(-exp)  # dscale
-            return length
-
         cdef uint16_t dscale
         if exp <= 0:
             dscale = -exp
@@ -612,6 +603,15 @@ cdef class DecimalBinaryDumper(CDumper):
             # align the py digits to the pg digits if there's some py exponent
             ndigits += exp % DEC_DIGITS
 
+        if nzdigits == 0:
+            length = 4 * sizeof(uint16_t)
+            buf = <uint16_t *>CDumper.ensure_size(rv, offset, length)
+            buf[0] = 0  # ndigits
+            buf[1] = 0  # weight
+            buf[2] = endian.htobe16(NUMERIC_POS)  # sign
+            buf[3] = endian.htobe16(dscale)
+            return length
+
         # Equivalent of 0-padding left to align the py digits to the pg digits
         # but without changing the digits tuple.
         cdef int wi = 0