# Path: numcodecs/compat.py
# def ensure_bytes(buf):
#     """Obtain a bytes object from memory exposed by `buf`."""
# 
#     if not isinstance(buf, bytes):
# 
#         # go via numpy, for convenience
#         arr = ensure_ndarray(buf)
# 
#         # check for object arrays, these are just memory pointers,
#         # actual memory holding item data is scattered elsewhere
#         if arr.dtype == object:
#             raise TypeError('object arrays are not supported')
# 
#         # create bytes
#         buf = arr.tobytes(order='A')
# 
#     return buf
# 
# def ensure_ndarray(buf):
#     """Convenience function to coerce `buf` to a numpy array, if it is not already a
#     numpy array.
# 
#     Parameters
#     ----------
#     buf : array-like or bytes-like
#         A numpy array or any object exporting a buffer interface.
# 
#     Returns
#     -------
#     arr : ndarray
#         A numpy array, sharing memory with `buf`.
# 
#     Notes
#     -----
#     This function will not create a copy under any circumstances, it is guaranteed to
#     return a view on memory exported by `buf`.
# 
#     """
# 
#     if isinstance(buf, np.ndarray):
#         # already a numpy array
#         arr = buf
# 
#     elif isinstance(buf, array.array) and buf.typecode in 'cu':
#         # Guard condition, do not support array.array with unicode type, this is
#         # problematic because numpy does not support it on all platforms. Also do not
#         # support char as it was removed in Python 3.
#         raise TypeError('array.array with char or unicode type is not supported')
# 
#     else:
# 
#         # N.B., first take a memoryview to make sure that we subsequently create a
#         # numpy array from a memory buffer with no copy
#         mem = memoryview(buf)
# 
#         # instantiate array from memoryview, ensures no copy
#         arr = np.array(mem, copy=False)
# 
#     return arr
# 
# Path: numcodecs/registry.py
# def get_codec(config):
#     """Obtain a codec for the given configuration.
# 
#     Parameters
#     ----------
#     config : dict-like
#         Configuration object.
# 
#     Returns
#     -------
#     codec : Codec
# 
#     Examples
#     --------
# 
#     >>> import numcodecs as codecs
#     >>> codec = codecs.get_codec(dict(id='zlib', level=1))
#     >>> codec
#     Zlib(level=1)
# 
#     """
#     config = dict(config)
#     codec_id = config.pop('id', None)
#     cls = codec_registry.get(codec_id)
#     if cls is None:
#         if codec_id in entries:
#             logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
#             cls = entries[codec_id].load()
#             register_codec(cls, codec_id=codec_id)
#     if cls:
#         return cls.from_config(config)
#     raise ValueError('codec not available: %r' % codec_id)


import array
import json as _json
import os
import numpy as np
import pytest
from glob import glob
from numpy.testing import assert_array_almost_equal, assert_array_equal
from numcodecs.compat import ensure_bytes, ensure_ndarray
from numcodecs.registry import get_codec
from numcodecs import *  # noqa
                enc = ef.read()
                dec = codec.decode(enc)
                dec_arr = ensure_ndarray(dec).reshape(-1, order='A')
                dec_arr = dec_arr.view(dtype=arr.dtype).reshape(arr.shape, order=order)
                if precision and precision[j] is not None:
                    assert_array_almost_equal(arr, dec_arr, decimal=precision[j])
                elif arr.dtype == 'object':
                    assert_array_items_equal(arr, dec_arr)
                else:
                    assert_array_equal(arr, dec_arr)
                    assert arr_bytes == ensure_bytes(dec)


def check_err_decode_object_buffer(compressor):
    # cannot decode directly into object array, leads to segfaults
    a = np.arange(10)
    enc = compressor.encode(a)
    out = np.empty(10, dtype=object)
    with pytest.raises(TypeError):
        compressor.decode(enc, out=out)


def check_err_encode_object_buffer(compressor):
    # compressors cannot encode object array
    a = np.array(['foo', 'bar', 'baz'], dtype=object)
    with pytest.raises(TypeError):
        compressor.encode(a)


def check_max_buffer_size(codec):

# Path: numcodecs/compat.py
# def ensure_bytes(buf):
#     """Obtain a bytes object from memory exposed by `buf`."""
# 
#     if not isinstance(buf, bytes):
# 
#         # go via numpy, for convenience
#         arr = ensure_ndarray(buf)
# 
#         # check for object arrays, these are just memory pointers,
#         # actual memory holding item data is scattered elsewhere
#         if arr.dtype == object:
#             raise TypeError('object arrays are not supported')
# 
#         # create bytes
#         buf = arr.tobytes(order='A')
# 
#     return buf
# 
# def ensure_ndarray(buf):
#     """Convenience function to coerce `buf` to a numpy array, if it is not already a
#     numpy array.
# 
#     Parameters
#     ----------
#     buf : array-like or bytes-like
#         A numpy array or any object exporting a buffer interface.
# 
#     Returns
#     -------
#     arr : ndarray
#         A numpy array, sharing memory with `buf`.
# 
#     Notes
#     -----
#     This function will not create a copy under any circumstances, it is guaranteed to
#     return a view on memory exported by `buf`.
# 
#     """
# 
#     if isinstance(buf, np.ndarray):
#         # already a numpy array
#         arr = buf
# 
#     elif isinstance(buf, array.array) and buf.typecode in 'cu':
#         # Guard condition, do not support array.array with unicode type, this is
#         # problematic because numpy does not support it on all platforms. Also do not
#         # support char as it was removed in Python 3.
#         raise TypeError('array.array with char or unicode type is not supported')
# 
#     else:
# 
#         # N.B., first take a memoryview to make sure that we subsequently create a
#         # numpy array from a memory buffer with no copy
#         mem = memoryview(buf)
# 
#         # instantiate array from memoryview, ensures no copy
#         arr = np.array(mem, copy=False)
# 
#     return arr
# 
# Path: numcodecs/registry.py
# def get_codec(config):
#     """Obtain a codec for the given configuration.
# 
#     Parameters
#     ----------
#     config : dict-like
#         Configuration object.
# 
#     Returns
#     -------
#     codec : Codec
# 
#     Examples
#     --------
# 
#     >>> import numcodecs as codecs
#     >>> codec = codecs.get_codec(dict(id='zlib', level=1))
#     >>> codec
#     Zlib(level=1)
# 
#     """
#     config = dict(config)
#     codec_id = config.pop('id', None)
#     cls = codec_registry.get(codec_id)
#     if cls is None:
#         if codec_id in entries:
#             logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
#             cls = entries[codec_id].load()
#             register_codec(cls, codec_id=codec_id)
#     if cls:
#         return cls.from_config(config)
#     raise ValueError('codec not available: %r' % codec_id)


# Path: numcodecs/tests/common.py
import array
import json as _json
import os
import numpy as np
import pytest
from glob import glob
from numpy.testing import assert_array_almost_equal, assert_array_equal
from numcodecs.compat import ensure_bytes, ensure_ndarray
from numcodecs.registry import get_codec
from numcodecs import *  # noqa

                enc = ef.read()
                dec = codec.decode(enc)
                dec_arr = ensure_ndarray(dec).reshape(-1, order='A')
                dec_arr = dec_arr.view(dtype=arr.dtype).reshape(arr.shape, order=order)
                if precision and precision[j] is not None:
                    assert_array_almost_equal(arr, dec_arr, decimal=precision[j])
                elif arr.dtype == 'object':
                    assert_array_items_equal(arr, dec_arr)
                else:
                    assert_array_equal(arr, dec_arr)
                    assert arr_bytes == ensure_bytes(dec)


def check_err_decode_object_buffer(compressor):
    # cannot decode directly into object array, leads to segfaults
    a = np.arange(10)
    enc = compressor.encode(a)
    out = np.empty(10, dtype=object)
    with pytest.raises(TypeError):
        compressor.decode(enc, out=out)


def check_err_encode_object_buffer(compressor):
    # compressors cannot encode object array
    a = np.array(['foo', 'bar', 'baz'], dtype=object)
    with pytest.raises(TypeError):
        compressor.encode(a)


def check_max_buffer_size(codec):

    for max_buffer_size in [4, 64, 1024]: