Skip to content

Commit 22edda5

Browse files
committed
Add uint8_{en,de}code helpers
1 parent d4c9571 commit 22edda5

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

codecs/__init__.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ cdef uint4_encode(CodecContext settings, WriteBuffer buf, obj)
8585
cdef uint4_decode(CodecContext settings, FRBuffer * buf)
8686
cdef int8_encode(CodecContext settings, WriteBuffer buf, obj)
8787
cdef int8_decode(CodecContext settings, FRBuffer * buf)
88+
cdef uint8_encode(CodecContext settings, WriteBuffer buf, obj)
89+
cdef uint8_decode(CodecContext settings, FRBuffer * buf)
8890

8991

9092
# Floats

codecs/int.pyx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,29 @@ cdef int8_encode(CodecContext settings, WriteBuffer buf, obj):
116116

117117
cdef int8_decode(CodecContext settings, FRBuffer *buf):
118118
return cpython.PyLong_FromLongLong(hton.unpack_int64(frb_read(buf, 8)))
119+
120+
121+
cdef uint8_encode(CodecContext settings, WriteBuffer buf, obj):
122+
cdef int overflow = 0
123+
cdef unsigned long long val = 0
124+
125+
try:
126+
if type(obj) is not int and hasattr(type(obj), '__int__'):
127+
# Silence a Python warning about implicit __int__
128+
# conversion.
129+
obj = int(obj)
130+
val = cpython.PyLong_AsUnsignedLongLong(obj)
131+
except OverflowError:
132+
overflow = 1
133+
134+
# Just in case for systems with "long long" bigger than 8 bytes
135+
if overflow or (sizeof(val) > 8 and val > UINT64_MAX):
136+
raise OverflowError('value out of uint64 range')
137+
138+
buf.write_int32(8)
139+
buf.write_int64(<int64_t>val)
140+
141+
142+
cdef uint8_decode(CodecContext settings, FRBuffer *buf):
143+
return cpython.PyLong_FromUnsignedLongLong(
144+
<uint64_t>hton.unpack_int64(frb_read(buf, 8)))

pgproto.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ from . cimport cpythonx
1313
from libc.stdint cimport int8_t, uint8_t, int16_t, uint16_t, \
1414
int32_t, uint32_t, int64_t, uint64_t, \
1515
INT16_MIN, INT16_MAX, INT32_MIN, INT32_MAX, \
16-
UINT32_MAX, INT64_MIN, INT64_MAX
16+
UINT32_MAX, INT64_MIN, INT64_MAX, UINT64_MAX
1717

1818

1919
from . cimport hton

0 commit comments

Comments
 (0)