aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/asyncpg/pgproto/codecs/float.pyx
blob: 94eda03adc7c4a9e48ca1d7955de9c5b8d5ac162 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Copyright (C) 2016-present the asyncpg authors and contributors
# <see AUTHORS file>
#
# This module is part of asyncpg and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0


from libc cimport math


cdef float4_encode(CodecContext settings, WriteBuffer buf, obj):
    cdef double dval = cpython.PyFloat_AsDouble(obj)
    cdef float fval = <float>dval
    if math.isinf(fval) and not math.isinf(dval):
        raise ValueError('value out of float32 range')

    buf.write_int32(4)
    buf.write_float(fval)


cdef float4_decode(CodecContext settings, FRBuffer *buf):
    cdef float f = hton.unpack_float(frb_read(buf, 4))
    return cpython.PyFloat_FromDouble(f)


cdef float8_encode(CodecContext settings, WriteBuffer buf, obj):
    cdef double dval = cpython.PyFloat_AsDouble(obj)
    buf.write_int32(8)
    buf.write_double(dval)


cdef float8_decode(CodecContext settings, FRBuffer *buf):
    cdef double f = hton.unpack_double(frb_read(buf, 8))
    return cpython.PyFloat_FromDouble(f)