def check_type_size(conf, type_name, headers=None, expect=None):
    """\
    This check can be used to get the size of a given type, or to
    check whether the type is of expected size.

    Arguments
    ---------
    conf : object
        configure context instance
    type_name : str
        the type to check
    includes : sequence
        list of headers to include in the test code before testing the
        type
    expect : sequence
        if given, will test wether the type has the given number of
            bytes.  If not given, will automatically find the size.
    """

    conf.start_message("Checking for sizeof %s ..." % type_name)
    body = r"""
typedef %(type)s yaku_check_sizeof_type;
int main ()
{
    static int test_array [1 - 2 * !(((long) (sizeof (yaku_check_sizeof_type))) >= 0)];
    test_array [0] = 0

    ;
    return 0;
}
""" % {"type": type_name}

    ret = conf.builders["ctasks"].try_compile("check_type_size", body, headers)
    if not ret:
        conf.end_message("no (cannot compile type)")
        return False

    if expect is None:
        # this fails to *compile* if size > sizeof(type)
        body = r"""
typedef %(type)s npy_check_sizeof_type;
int main ()
{
    static int test_array [1 - 2 * !(((long) (sizeof (npy_check_sizeof_type))) <= %(size)s)];
    test_array [0] = 0

    ;
    return 0;
}
"""
        # The principle is simple: we first find low and high bounds
        # of size for the type, where low/high are looked up on a log
        # scale. Then, we do a binary search to find the exact size
        # between low and high
        low = 0
        mid = 0
        while True:
            code = body % {'type': type_name, 'size': mid}
            ret = conf.builders["ctasks"].try_compile("check_type_size",
                    code, headers)
            if ret:
                break
            #log.info("failure to test for bound %d" % mid)
            low = mid + 1
            mid = 2 * mid + 1

        high = mid
        # Binary search:
        while low != high:
            mid = (high - low) / 2 + low
            code = body % {'type': type_name, 'size': mid}
            if conf.builders["ctasks"].try_compile("check_type_size",
                    code, headers):
                high = mid
            else:
                low = mid + 1
        ret = low
        size = low
        conf.end_message("%d" % low)
    else:
        raise NotImplementedError("Expect arg not yet implemented")

    conf.conf_results.append({"type": "type_size", "value": type_name,
                              "result": ret})
    return ret
dataset/ETHPy150Open cournape/Bento/bento/private/_yaku/yaku/conftests/conftests.py/check_type_size/original