From: Daniele Varrazzo Date: Thu, 10 Jun 2021 16:08:12 +0000 (+0100) Subject: Fix float parsing when the data buffer is not terminated X-Git-Tag: 3.0.dev0~29 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=401f852d10fc68b19a1046a4973f813f17a7b355;p=thirdparty%2Fpsycopg.git Fix float parsing when the data buffer is not terminated --- 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 #