<commit_before>import threading

from robotd.native import _cvcapture

class CaptureDevice(object):
    def __init__(self, path=None):
        if path is not None:
            argument_c = _cvcapture.ffi.new(
                'char[]',
                path.encode('utf-8'),
            )
        else:
            argument_c = _cvcapture.ffi.NULL
        self.instance = _cvcapture.lib.cvopen(argument_c)
        self.lock = threading.Lock()

    def capture(self, width, height):
        if self.instance is None:
            raise RuntimeError("capture device is closed")

        capture_buffer = _cvcapture.ffi.new(
            'uint8_t[{}]'.format(width * height),
        )

        with self.lock:
            status = _cvcapture.lib.cvcapture(
                self.instance,
                capture_buffer,
                width,
                height,
            )

        if status == 0:
            raise RuntimeError("cvcapture() failed")

        return bytes(_cvcapture.ffi.buffer(capture_buffer))

    def __enter__(self):
        return self

    def __exit__(self, exc_value, exc_type, exc_traceback):
        self.close()

    def close(self):
        if self.instance is not None:
            with self.lock:
                _cvcapture.lib.cvclose(self.instance)
            self.instance = None

    __del__ = close

def cvcapture(device, width, height):
    capture_buffer = _cvcapture.ffi.new('uint8_t[{}]'.format(width * height))
    print(capture_buffer)

    result = _cvcapture.lib.cvcapture(
        capture_buffer,
        width,
        height,
    )

    if result == 0:
        raise RuntimeError("cvcapture() failed")

    return bytes(_cvcapture.ffi.buffer(capture_buffer))<commit_msg>Delete the old implementation<commit_after>import threading

from robotd.native import _cvcapture

class CaptureDevice(object):
    def __init__(self, path=None):
        if path is not None:
            argument_c = _cvcapture.ffi.new(
                'char[]',
                path.encode('utf-8'),
            )
        else:
            argument_c = _cvcapture.ffi.NULL
        self.instance = _cvcapture.lib.cvopen(argument_c)
        self.lock = threading.Lock()

    def capture(self, width, height):
        if self.instance is None:
            raise RuntimeError("capture device is closed")

        capture_buffer = _cvcapture.ffi.new(
            'uint8_t[{}]'.format(width * height),
        )

        with self.lock:
            status = _cvcapture.lib.cvcapture(
                self.instance,
                capture_buffer,
                width,
                height,
            )

        if status == 0:
            raise RuntimeError("cvcapture() failed")

        return bytes(_cvcapture.ffi.buffer(capture_buffer))

    def __enter__(self):
        return self

    def __exit__(self, exc_value, exc_type, exc_traceback):
        self.close()

    def close(self):
        if self.instance is not None:
            with self.lock:
                _cvcapture.lib.cvclose(self.instance)
            self.instance = None

    __del__ = close
{'fraction_non_alphanumeric': 0.09627228525121556, 'fraction_numerical': 0.002593192868719611, 'mean_word_length': 1.8337924701561066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4290742', 'n_tokens_mistral': 920, 'n_tokens_neox': 904, 'n_words': 191}