<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cobjects
from cobjects import CBuffer, CObject, CField

import ctypes as ct

from .stcommon import st_Buffer, st_Null, st_NullChar, st_NullUChar, \
    st_ARCH_STATUS_GENERAL_FAILURE, st_ARCH_STATUS_SUCCESS, st_arch_status_t, \
    st_buffer_size_t, st_arch_size_t, st_uchar_p, st_buffer_addr_t, \
    st_Buffer, st_Buffer_p, st_NullBuffer, st_Buffer_preset, \
    st_Buffer_new, st_Buffer_new_on_memory, st_Buffer_new_on_data, \
    st_Buffer_new_from_file, st_Buffer_init_from_data, st_Buffer_get_slot_size,\
    st_Buffer_get_num_of_objects, st_Buffer_get_size, st_Buffer_get_capacity, \
    st_Buffer_new_mapped_on_cbuffer, st_Buffer_get_header_size, \
    st_Buffer_get_data_begin_addr, st_Buffer_get_data_end_addr, \
    st_Buffer_get_objects_begin_addr, st_Buffer_get_objects_end_addr, \
    st_Buffer_get_num_of_slots, st_Buffer_get_max_num_of_slots, \
    st_Buffer_get_num_of_objects, st_Buffer_get_max_num_of_objects, \
    st_Buffer_get_num_of_dataptrs, st_Buffer_get_max_num_of_dataptrs, \
    st_Buffer_get_num_of_garbage_ranges, st_Buffer_get_max_num_of_garbage_ranges, \
    st_Buffer_read_from_file, st_Buffer_write_to_file, \
    st_Buffer_write_to_file_normalized_addr, st_Buffer_reset, \
    st_Buffer_reset_detailed, st_Buffer_reserve, st_Buffer_reserve_capacity, \
    st_Buffer_needs_remapping, st_Buffer_remap, st_Buffer_refresh, \
    st_Buffer_clear, st_Buffer_free, st_Buffer_delete, \
    st_AssignAddressItem_p, st_NullAssignAddressItem


class AssignAddressItem(CObject):
    _typeid=513
    dest_elem_type_id = CField(0, 'uint64', default=0, alignment=8)
    dest_buffer_id = CField(1, 'uint64', default=0, alignment=8)
    dest_elem_index = CField(2, 'uint64', default=1, alignment=8)
    dest_pointer_offset = CField(3, 'uint64', default=0, alignment=8)
    src_elem_type_id = CField(4, 'uint64', default=0, alignment=8)
    src_buffer_id = CField(5, 'uint64', default=0, alignment=8)
    src_elem_index = CField(6, 'uint64', default=1, alignment=8)
    src_pointer_offset = CField(7, 'uint64', default=0, alignment=8)


class Buffer(object):
    def __init__(self, size=0, ptr_data=st_NullUChar, path_to_file=None,
                 cbuffer=None, ptr_ext_buffer=st_NullBuffer, orig=None,
                 owns_ptr=True, slot_size=8):
        self._ptr_st_buffer = st_NullBuffer
        self._last_status = st_ARCH_STATUS_SUCCESS.value
        self._on_del = "delete"

        if ptr_ext_buffer is not None and ptr_ext_buffer != st_NullBuffer:
            self._ptr_st_buffer = ptr_ext_buffer
            if owns_ptr is None or not(owns_ptr is True):
                self._on_del = None
        elif cbuffer is not None and isinstance(cbuffer, CBuffer):
            self._ptr_st_buffer = st_Buffer_new_mapped_on_cbuffer(cbuffer)
        elif path_to_file is not None and path_to_file != "":
            _path_to_file = ct.c_char_p(path_to_file.encode('utf-8'))
            self._ptr_st_buffer = st_Buffer_new_from_file(_path_to_file)
        elif ptr_data is not None and ptr_data != st_NullUChar and \
                slot_size is not None and slot_size > 0:
            self._ptr_st_buffer = st_Buffer_new_on_data(
                ptr_data, st_buffer_size_t(slot_size))
        elif size is not None and size >= 0:
            self._ptr_st_buffer = st_Buffer_new(st_buffer_size_t(size))
        else:
            self._ptr_st_buffer = st_Buffer_new(st_buffer_size_t(0))

    def __del__(self):
        if self._ptr_st_buffer != st_NullBuffer and self._on_del is not None:
            if self._on_del == "delete":
                st_Buffer_delete(self._ptr_st_buffer)
                self._ptr_st_buffer = st_NullBuffer
            elif self._on_del == "free":
                st_Buffer_free(self._ptr_st_buffer)
                st_Buffer_preset(self._ptr_st_buffer)

    @property
    def pointer(self):
        return self._ptr_st_buffer

    @property
    def last_status(self):
        return self._last_status.value

    @property
    def last_status_success(self):
        return self._last_status == st_ARCH_STATUS_SUCCESS.value

    @property
    def slot_size(self):
        return st_Buffer_get_slot_size(self._ptr_st_buffer)

    @property
    def size(self):
        return st_Buffer_get_size(self._ptr_st_buffer)

    @property
    def capacity(self):
        return st_Buffer_get_capacity(self._ptr_st_buffer)

    @property
    def num_objects(self):
        return st_Buffer_get_num_of_objects(self._ptr_st_buffer)

    @property
    def max_num_objects(self):
        return st_Buffer_get_max_num_of_objects(self._ptr_st_buffer)

    @property
    def num_slots(self):
        return st_Buffer_get_num_of_slots(self._ptr_st_buffer)

    @property
    def max_num_slots(self):
        return st_Buffer_get_max_num_of_slots(self._ptr_st_buffer)

    @property<commit_msg>python/sixtracklib: adds is_owner property to SixTrackLib Buffer wrapper<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cobjects
from cobjects import CBuffer, CObject, CField

import ctypes as ct

from .stcommon import st_Buffer, st_Null, st_NullChar, st_NullUChar, \
    st_ARCH_STATUS_GENERAL_FAILURE, st_ARCH_STATUS_SUCCESS, st_arch_status_t, \
    st_buffer_size_t, st_arch_size_t, st_uchar_p, st_buffer_addr_t, \
    st_Buffer, st_Buffer_p, st_NullBuffer, st_Buffer_preset, \
    st_Buffer_new, st_Buffer_new_on_memory, st_Buffer_new_on_data, \
    st_Buffer_new_from_file, st_Buffer_init_from_data, st_Buffer_get_slot_size,\
    st_Buffer_get_num_of_objects, st_Buffer_get_size, st_Buffer_get_capacity, \
    st_Buffer_new_mapped_on_cbuffer, st_Buffer_get_header_size, \
    st_Buffer_get_data_begin_addr, st_Buffer_get_data_end_addr, \
    st_Buffer_get_objects_begin_addr, st_Buffer_get_objects_end_addr, \
    st_Buffer_get_num_of_slots, st_Buffer_get_max_num_of_slots, \
    st_Buffer_get_num_of_objects, st_Buffer_get_max_num_of_objects, \
    st_Buffer_get_num_of_dataptrs, st_Buffer_get_max_num_of_dataptrs, \
    st_Buffer_get_num_of_garbage_ranges, st_Buffer_get_max_num_of_garbage_ranges, \
    st_Buffer_read_from_file, st_Buffer_write_to_file, \
    st_Buffer_write_to_file_normalized_addr, st_Buffer_reset, \
    st_Buffer_reset_detailed, st_Buffer_reserve, st_Buffer_reserve_capacity, \
    st_Buffer_needs_remapping, st_Buffer_remap, st_Buffer_refresh, \
    st_Buffer_clear, st_Buffer_free, st_Buffer_delete, \
    st_AssignAddressItem_p, st_NullAssignAddressItem


class AssignAddressItem(CObject):
    _typeid=513
    dest_elem_type_id = CField(0, 'uint64', default=0, alignment=8)
    dest_buffer_id = CField(1, 'uint64', default=0, alignment=8)
    dest_elem_index = CField(2, 'uint64', default=1, alignment=8)
    dest_pointer_offset = CField(3, 'uint64', default=0, alignment=8)
    src_elem_type_id = CField(4, 'uint64', default=0, alignment=8)
    src_buffer_id = CField(5, 'uint64', default=0, alignment=8)
    src_elem_index = CField(6, 'uint64', default=1, alignment=8)
    src_pointer_offset = CField(7, 'uint64', default=0, alignment=8)


class Buffer(object):
    def __init__(self, size=0, ptr_data=st_NullUChar, path_to_file=None,
                 cbuffer=None, ptr_ext_buffer=st_NullBuffer, orig=None,
                 owns_ptr=True, slot_size=8):
        self._ptr_st_buffer = st_NullBuffer
        self._last_status = st_ARCH_STATUS_SUCCESS.value
        self._on_del = "delete"
        self._owns_buffer = owns_ptr

        if ptr_ext_buffer is not None and ptr_ext_buffer != st_NullBuffer:
            self._ptr_st_buffer = ptr_ext_buffer
            if owns_ptr is None or not(owns_ptr is True):
                self._on_del = None
                self._owns_buffer = False
        elif cbuffer is not None and isinstance(cbuffer, CBuffer):
            self._ptr_st_buffer = st_Buffer_new_mapped_on_cbuffer(cbuffer)
        elif path_to_file is not None and path_to_file != "":
            _path_to_file = ct.c_char_p(path_to_file.encode('utf-8'))
            self._ptr_st_buffer = st_Buffer_new_from_file(_path_to_file)
        elif ptr_data is not None and ptr_data != st_NullUChar and \
                slot_size is not None and slot_size > 0:
            self._ptr_st_buffer = st_Buffer_new_on_data(
                ptr_data, st_buffer_size_t(slot_size))
        elif size is not None and size >= 0:
            self._ptr_st_buffer = st_Buffer_new(st_buffer_size_t(size))
        else:
            self._ptr_st_buffer = st_Buffer_new(st_buffer_size_t(0))

    def __del__(self):
        if self._ptr_st_buffer != st_NullBuffer and self._on_del is not None:
            if self._on_del == "delete":
                st_Buffer_delete(self._ptr_st_buffer)
                self._ptr_st_buffer = st_NullBuffer
            elif self._on_del == "free":
                st_Buffer_free(self._ptr_st_buffer)
                st_Buffer_preset(self._ptr_st_buffer)

    @property
    def pointer(self):
        return self._ptr_st_buffer

    @property
    def last_status(self):
        return self._last_status.value

    @property
    def last_status_success(self):
        return self._last_status == st_ARCH_STATUS_SUCCESS.value

    @property
    def is_owner(self):
        return self._owns_buffer

    @property
    def slot_size(self):
        return st_Buffer_get_slot_size(self._ptr_st_buffer)

    @property
    def size(self):
        return st_Buffer_get_size(self._ptr_st_buffer)

    @property
    def capacity(self):
        return st_Buffer_get_capacity(self._ptr_st_buffer)

    @property
    def num_objects(self):
        return st_Buffer_get_num_of_objects(self._ptr_st_buffer)

    @property
    def max_num_objects(self):
        return st_Buffer_get_max_num_of_objects(self._ptr_st_buffer)

    @property
    def num_slots(self):
        return st_Buffer_get_num_of_slots(self._ptr_st_buffer)

    @property
    def max_num_slots(self):
        return st_Buffer_get_max_num_of_slots(self._ptr_st_buffer)

    @property
{'fraction_non_alphanumeric': 0.07540556779491288, 'fraction_numerical': 0.01001401962747847, 'mean_word_length': 3.6407992565055762, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 7, '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': '5602409', 'n_tokens_mistral': 3581, 'n_tokens_neox': 3503, 'n_words': 575}