<commit_before>#!/usr/bin/env python

'''Generate a Python ctypes wrapper file for a header file.

Usage example::
    wrap.py -lGL -oGL.py /usr/include/GL/gl.h

    >>> from GL import *

'''

__docformat__ = 'restructuredtext'
__version__ = '$Id: wrap.py 738 2007-03-12 04:53:42Z Alex.Holkner $'

from ctypesparser import *
import textwrap
import sys, re
from ctypes import CDLL, RTLD_GLOBAL, c_byte
from ctypes.util import find_library

def load_library(name, mode=RTLD_GLOBAL):
    if os.name == "nt":
        return CDLL(name, mode=mode)
    path = find_library(name)
    if path is None:
        # Maybe 'name' is not a library name in the linker style,
        # give CDLL a last chance to find the library.
        path = name
    return CDLL(path, mode=mode)

class CtypesWrapper(CtypesParser, CtypesTypeVisitor):
    file=None
    def begin_output(self, output_file, library, link_modules=(), 
                     emit_filenames=(), all_headers=False,
                     include_symbols=None, exclude_symbols=None,
                     save_preprocessed_headers=None):
        self.library = library
        self.file = output_file
        self.all_names = []
        self.known_types = {}
        self.structs = set()
        self.opaque_structs = set()
        self.enums = set()
        self.emit_filenames = emit_filenames
        self.all_headers = all_headers
        self.include_symbols = None
        self.exclude_symbols = None
        if include_symbols:<commit_msg>* wrap.py: Work around buggy implementations of find_library by providing a   custom implementation on non-Windows platforms.<commit_after>#!/usr/bin/env python

'''Generate a Python ctypes wrapper file for a header file.

Usage example::
    wrap.py -lGL -oGL.py /usr/include/GL/gl.h

    >>> from GL import *

'''

__docformat__ = 'restructuredtext'
__version__ = '$Id: wrap.py 738 2007-03-12 04:53:42Z Alex.Holkner $'

from ctypesparser import *
import textwrap
import sys, re, os, tempfile
from ctypes import CDLL, RTLD_GLOBAL, c_byte

if os.name == "nt":
    from ctypes.util import find_library
else:
    # ctypes.util.find_library is buggy in Python 2.5, unfortunately,
    # so we have to parse the output of gcc -Wl,-t manually
    def find_library(name):
        expr = r'[^\(\)\s]*lib%s\.[^\(\)\s:]*' % re.escape(name)
        fdout, ccout = tempfile.mkstemp()
        os.close(fdout)
        cmd = 'if type gcc >/dev/null 2>&1; then CC=gcc; else CC=cc; fi;' \
              '$CC -Wl,-t -o %s 2>&1 -l %s' % (ccout, name)
        try:
            f = os.popen(cmd)
            trace = f.read()
            f.close()
        finally:
            if os.path.exists(ccout):
                os.unlink(ccout)
        res = re.search(expr, trace)
        if not res:
            return None
        return res.group(0)

def load_library(name, mode=RTLD_GLOBAL):
    if os.name == "nt":
        return CDLL(name, mode=mode)
    path = find_library(name)
    if path is None:
        # Maybe 'name' is not a library name in the linker style,
        # give CDLL a last chance to find the library.
        path = name
    return CDLL(path, mode=mode)

class CtypesWrapper(CtypesParser, CtypesTypeVisitor):
    file=None
    def begin_output(self, output_file, library, link_modules=(), 
                     emit_filenames=(), all_headers=False,
                     include_symbols=None, exclude_symbols=None,
                     save_preprocessed_headers=None):
        self.library = library
        self.file = output_file
        self.all_names = []
        self.known_types = {}
        self.structs = set()
        self.opaque_structs = set()
        self.enums = set()
        self.emit_filenames = emit_filenames
        self.all_headers = all_headers
        self.include_symbols = None
        self.exclude_symbols = None
        if include_symbols:
{'fraction_non_alphanumeric': 0.10464814333939236, 'fraction_numerical': 0.010646585302518826, 'mean_word_length': 2.381913959613696, 'pattern_counts': {'":': 3, '<': 3, '<?xml version=': 0, '>': 12, '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': '5656440', 'n_tokens_mistral': 1265, 'n_tokens_neox': 1217, 'n_words': 335}