<commit_before># -* coding: utf-8 -*
import os
import sys
from ctypes import cdll, c_double, POINTER
from ctypes import sizeof, c_voidp


class FindChemfilesLibrary(object):
    def __init__(self):
        self._cache = None

    def __call__(self):
        if self._cache is None:
            path = _lib_path()
            self._cache = cdll.LoadLibrary(path)

            from .ffi import set_interface
            from .ffi import CHFL_FRAME, CHFL_ATOM, chfl_vector3d
            set_interface(self._cache)
            # We update the arguments here, as ctypes can not pass a NULL value
            # as the last parameter
            self._cache.chfl_frame_add_atom.argtypes = [
                POINTER(CHFL_FRAME), POINTER(CHFL_ATOM),
                chfl_vector3d, POINTER(c_double)
            ]

            from .utils import _set_default_warning_callback
            _set_default_warning_callback()
        return self._cache


def _lib_path():
    root = os.path.dirname(__file__)
    if sys.platform.startswith("darwin"):
        return os.path.join(root, "lib", "libchemfiles.dylib")
    elif sys.platform.startswith("linux"):
        return os.path.join(root, "lib", "libchemfiles.so")
    elif sys.platform.startswith("win"):
        candidates = [
            os.path.join(root, "bin", "libchemfiles.dll"),  # MinGW
            os.path.join(root, "bin", "chemfiles.dll"),     # MSVC
        ]
        for path in candidates:
            if os.path.isfile(path):
                _check_dll(path)
                return path
        raise ImportError("Could not find chemfiles DLL")
    else:
        raise ImportError("Unknown platform. Please edit this file")


def _check_dll(path):
    import struct

    IMAGE_FILE_MACHINE_I386 = 332
    IMAGE_FILE_MACHINE_AMD64 = 34404

    machine = None
    with open(path, 'rb') as fd:
        header = fd.read(2).decode(encoding="utf-8", errors="strict")
        if header != "MZ":
            raise ImportError(path + " is not a DLL")
        else:
            fd.seek(60)
            header = fd.read(4)
            header_offset = struct.unpack("<L", header)[0]
            fd.seek(header_offset + 4)
            header = fd.read(2)
            machine = struct.unpack("<H", header)[0]

    if sizeof(c_voidp) == 4 and machine != IMAGE_FILE_MACHINE_I386:
        raise ImportError("Python is 32-bit, but chemfiles.dll is not")
    elif sizeof(c_voidp) == 8 and machine != IMAGE_FILE_MACHINE_AMD64:
        raise ImportError("Python is 64-bit, but chemfiles.dll is not")


_get_c_library = FindChemfilesLibrary()<commit_msg>Protect against unknown values for sizeof(c_voidp)<commit_after># -* coding: utf-8 -*
import os
import sys
from ctypes import cdll, c_double, POINTER
from ctypes import sizeof, c_voidp


class FindChemfilesLibrary(object):
    def __init__(self):
        self._cache = None

    def __call__(self):
        if self._cache is None:
            path = _lib_path()
            self._cache = cdll.LoadLibrary(path)

            from .ffi import set_interface
            from .ffi import CHFL_FRAME, CHFL_ATOM, chfl_vector3d
            set_interface(self._cache)
            # We update the arguments here, as ctypes can not pass a NULL value
            # as the last parameter
            self._cache.chfl_frame_add_atom.argtypes = [
                POINTER(CHFL_FRAME), POINTER(CHFL_ATOM),
                chfl_vector3d, POINTER(c_double)
            ]

            from .utils import _set_default_warning_callback
            _set_default_warning_callback()
        return self._cache


def _lib_path():
    root = os.path.dirname(__file__)
    if sys.platform.startswith("darwin"):
        return os.path.join(root, "lib", "libchemfiles.dylib")
    elif sys.platform.startswith("linux"):
        return os.path.join(root, "lib", "libchemfiles.so")
    elif sys.platform.startswith("win"):
        candidates = [
            os.path.join(root, "bin", "libchemfiles.dll"),  # MinGW
            os.path.join(root, "bin", "chemfiles.dll"),     # MSVC
        ]
        for path in candidates:
            if os.path.isfile(path):
                _check_dll(path)
                return path
        raise ImportError("Could not find chemfiles DLL")
    else:
        raise ImportError("Unknown platform. Please edit this file")


def _check_dll(path):
    import struct

    IMAGE_FILE_MACHINE_I386 = 332
    IMAGE_FILE_MACHINE_AMD64 = 34404

    machine = None
    with open(path, 'rb') as fd:
        header = fd.read(2).decode(encoding="utf-8", errors="strict")
        if header != "MZ":
            raise ImportError(path + " is not a DLL")
        else:
            fd.seek(60)
            header = fd.read(4)
            header_offset = struct.unpack("<L", header)[0]
            fd.seek(header_offset + 4)
            header = fd.read(2)
            machine = struct.unpack("<H", header)[0]

    if sizeof(c_voidp) == 4 and machine != IMAGE_FILE_MACHINE_I386:
        raise ImportError("Python is 32-bit, but chemfiles.dll is not")
    elif sizeof(c_voidp) == 8 and machine != IMAGE_FILE_MACHINE_AMD64:
        raise ImportError("Python is 64-bit, but chemfiles.dll is not")
    else:
        raise ImportError("Could not determine pointer size of Python")


_get_c_library = FindChemfilesLibrary()
{'fraction_non_alphanumeric': 0.10153148043108338, 'fraction_numerical': 0.013613159387407828, 'mean_word_length': 2.29595015576324, 'pattern_counts': {'":': 2, '<': 7, '<?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': '7613131', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1603, 'n_words': 403}