Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ Release notes

.. _unreleased:

Unreleased
----------

Enhancements
~~~~~~~~~~~~

Improvements
~~~~~~~~~~~~

* In ``vlen``, define and use ``const`` ``HEADER_LENGTH``.
By :user:`John Kirkham <jakirkham>`, :issue:`723`

Fixes
~~~~~

Maintenance
~~~~~~~~~~~


0.16.0
------

Expand Down
34 changes: 19 additions & 15 deletions numcodecs/vlen.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

cimport cython

from libc.stdint cimport uint8_t
from libc.stdint cimport uint8_t, uint32_t
from libc.string cimport memcpy

from cpython.buffer cimport PyBuffer_IsContiguous
Expand Down Expand Up @@ -39,8 +39,12 @@ from .abc import Codec
from .compat import ensure_contiguous_ndarray


# 4 bytes to store number of items
cdef Py_ssize_t HEADER_LENGTH = 4
# Define header size used to store number of items that follow.
cdef extern from *:
"""
const Py_ssize_t HEADER_LENGTH = sizeof(uint32_t);
"""
const Py_ssize_t HEADER_LENGTH


def check_out_param(out, n_items):
Expand Down Expand Up @@ -116,7 +120,7 @@ class VLenUTF8(Codec):
b = PyUnicode_AsUTF8String(u)
l = PyBytes_GET_SIZE(b)
encoded_values[i] = b
data_length += l + 4 # 4 bytes to store item length
data_length += l + HEADER_LENGTH
encoded_lengths[i] = l

# setup output
Expand All @@ -132,7 +136,7 @@ class VLenUTF8(Codec):
for i in range(n_items):
l = encoded_lengths[i]
store_le32(<uint8_t*>data, l)
data += 4
data += HEADER_LENGTH
encv = PyBytes_AS_STRING(encoded_values[i])
memcpy(data, encv, l)
data += l
Expand Down Expand Up @@ -178,10 +182,10 @@ class VLenUTF8(Codec):
# https://github.com/cython/cython/issues/1608
data += HEADER_LENGTH
for i in range(n_items):
if data + 4 > data_end:
if data + HEADER_LENGTH > data_end:
raise ValueError('corrupt buffer, data seem truncated')
l = load_le32(<uint8_t*>data)
data += 4
data += HEADER_LENGTH
if data + l > data_end:
raise ValueError('corrupt buffer, data seem truncated')
out[i] = PyUnicode_FromStringAndSize(data, l)
Expand Down Expand Up @@ -247,7 +251,7 @@ class VLenBytes(Codec):
elif not PyBytes_Check(b):
raise TypeError('expected byte string, found %r' % b)
l = PyBytes_GET_SIZE(b)
data_length += l + 4 # 4 bytes to store item length
data_length += l + HEADER_LENGTH
lengths[i] = l

# setup output
Expand All @@ -263,7 +267,7 @@ class VLenBytes(Codec):
for i in range(n_items):
l = lengths[i]
store_le32(<uint8_t*>data, l)
data += 4
data += HEADER_LENGTH
encv = PyBytes_AS_STRING(values[i])
memcpy(data, encv, l)
data += l
Expand Down Expand Up @@ -309,10 +313,10 @@ class VLenBytes(Codec):
# https://github.com/cython/cython/issues/1608
data += HEADER_LENGTH
for i in range(n_items):
if data + 4 > data_end:
if data + HEADER_LENGTH > data_end:
raise ValueError('corrupt buffer, data seem truncated')
l = load_le32(<uint8_t*>data)
data += 4
data += HEADER_LENGTH
if data + l > data_end:
raise ValueError('corrupt buffer, data seem truncated')
out[i] = PyBytes_FromStringAndSize(data, l)
Expand Down Expand Up @@ -399,7 +403,7 @@ class VLenArray(Codec):
raise ValueError('only 1-dimensional arrays are supported')
l = v.nbytes
normed_values[i] = v
data_length += l + 4 # 4 bytes to store item length
data_length += l + HEADER_LENGTH
lengths[i] = l

# setup output
Expand All @@ -415,7 +419,7 @@ class VLenArray(Codec):
for i in range(n_items):
l = lengths[i]
store_le32(<uint8_t*>data, l)
data += 4
data += HEADER_LENGTH

value_mv = ensure_continguous_memoryview(normed_values[i])
value_pb = PyMemoryView_GET_BUFFER(value_mv)
Expand Down Expand Up @@ -468,10 +472,10 @@ class VLenArray(Codec):
# https://github.com/cython/cython/issues/1608
data += HEADER_LENGTH
for i in range(n_items):
if data + 4 > data_end:
if data + HEADER_LENGTH > data_end:
raise ValueError('corrupt buffer, data seem truncated')
l = load_le32(<uint8_t*>data)
data += 4
data += HEADER_LENGTH
if data + l > data_end:
raise ValueError('corrupt buffer, data seem truncated')

Expand Down
Loading