<commit_before># Copyright 2012 <NAME>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

from ctypes import cast

from gitypes import GIFunctionInfoPtr, GITypeTag

from util import typeinfo_to_ctypes


def get_array(info, value):
    r = []
    if not value:
        return r
    if info.is_zero_terminated():
        i = 0
        x = value[i]
        while x:
            r.append(x)
            i += 1
            x = value[i]
    return r


def handle_return(return_info, value):
    tag = return_info.get_tag().value
    #ptr = return_info.is_pointer()

    if tag == GITypeTag.UTF8:
        return value
    elif tag == GITypeTag.ARRAY:
        return get_array(return_info, value)
    else:
        return value


class Function(object):
    _handle = None
    _return_info = None
    _is_function = True

    def __init__(self, info, lib):
        func_info = cast(info, GIFunctionInfoPtr)
        return_info = func_info.get_return_type()
        #tag = return_info.get_tag()

        h = getattr(lib, func_info.get_symbol())
        h.restype = typeinfo_to_ctypes(return_info)
        h.argtypes = tuple()

        self._handle = h
        self._return_info = return_info

    def __call__(self, *args):
        value = self._handle()
        return handle_return(self._return_info, value)


def FunctionAttribute(info, namespace, name, lib):
    return type(name, Function.__bases__, dict(Function.__dict__))(info, lib)<commit_msg>Pass some bogus null pointers on function calls so gtk_init doesn't segfault with pypy<commit_after># Copyright 2012 <NAME>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.

from ctypes import cast

from gitypes import GIFunctionInfoPtr, GITypeTag, gpointer

from util import typeinfo_to_ctypes


def get_array(info, value):
    r = []
    if not value:
        return r
    if info.is_zero_terminated():
        i = 0
        x = value[i]
        while x:
            r.append(x)
            i += 1
            x = value[i]
    return r


def handle_return(return_info, value):
    tag = return_info.get_tag().value
    #ptr = return_info.is_pointer()

    if tag == GITypeTag.UTF8:
        return value
    elif tag == GITypeTag.ARRAY:
        return get_array(return_info, value)
    else:
        return value


class Function(object):
    _handle = None
    _return_info = None
    _is_function = True

    def __init__(self, info, lib):
        func_info = cast(info, GIFunctionInfoPtr)
        return_info = func_info.get_return_type()
        #tag = return_info.get_tag()

        h = getattr(lib, func_info.get_symbol())
        h.restype = typeinfo_to_ctypes(return_info)
        h.argtypes = tuple([gpointer, gpointer])

        self._handle = h
        self._return_info = return_info

    def __call__(self, *args):
        value = self._handle(0, 0)
        return handle_return(self._return_info, value)


def FunctionAttribute(info, namespace, name, lib):
    return type(name, Function.__bases__, dict(Function.__dict__))(info, lib)
{'fraction_non_alphanumeric': 0.08637059724349158, 'fraction_numerical': 0.005513016845329249, 'mean_word_length': 2.6987542468856174, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4177289', 'n_tokens_mistral': 1037, 'n_tokens_neox': 1003, 'n_words': 295}