From 401f852d10fc68b19a1046a4973f813f17a7b355 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 10 Jun 2021 17:08:12 +0100 Subject: [PATCH] Fix float parsing when the data buffer is not terminated --- psycopg3_c/psycopg3_c/types/numeric.pyx | 3 ++- tests/types/test_numeric.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/psycopg3_c/psycopg3_c/types/numeric.pyx b/psycopg3_c/psycopg3_c/types/numeric.pyx index 68ecf3301..1c80f9eff 100644 --- a/psycopg3_c/psycopg3_c/types/numeric.pyx +++ b/psycopg3_c/psycopg3_c/types/numeric.pyx @@ -395,8 +395,9 @@ cdef class FloatLoader(CLoader): format = PQ_TEXT cdef object cload(self, const char *data, size_t length): + cdef char *endptr cdef double d = PyOS_string_to_double( - data, NULL, OverflowError) + data, &endptr, OverflowError) return PyFloat_FromDouble(d) diff --git a/tests/types/test_numeric.py b/tests/types/test_numeric.py index 8fc9ab42f..acce86eb8 100644 --- a/tests/types/test_numeric.py +++ b/tests/types/test_numeric.py @@ -295,6 +295,16 @@ def test_load_float_approx(conn, expr, pgtype, want, fmt_out): assert result == pytest.approx(want) +def test_load_float_copy(conn): + cur = conn.cursor(binary=False) + with cur.copy("copy (select 3.14::float8, 'hi'::text) to stdout;") as copy: + copy.set_types(["float8", "text"]) + rec = copy.read_row() + + assert rec[0] == pytest.approx(3.14) + assert rec[1] == "hi" + + # # Tests with decimal # -- 2.47.3