• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trolldbois / ctypeslib / 4773350134

pending completion
4773350134

push

github

Loic Jaquemet
don't use glob things in working directory

1601 of 1912 relevant lines covered (83.73%)

50.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.2
/ctypeslib/__init__.py
1
# -*- coding: utf-8 -*-
2
# ctypeslib package
3

4
from pkg_resources import get_distribution, DistributionNotFound
60✔
5
import ctypes
60✔
6
from ctypes.util import find_library
60✔
7
import os
60✔
8
import logging
60✔
9
import re
60✔
10
import sys
60✔
11
import warnings
60✔
12

13
try:
60✔
14
    __dist = get_distribution('ctypeslib2')
60✔
15
    # Normalize case for Windows systems
16
    # if you are in a virtualenv, ./local/* are aliases to ./*
17
    __dist_loc = os.path.normcase(os.path.realpath(__dist.location))
60✔
18
    __here = os.path.normcase(os.path.realpath(__file__))
60✔
19
    if not __here.startswith(os.path.join(__dist_loc, 'ctypeslib')):
60✔
20
        # not installed, but there is another version that *is*
21
        raise DistributionNotFound
60✔
22
except DistributionNotFound:
60✔
23
    __version__ = 'Please install the latest version of this python package'
60✔
24
else:
×
25
    __version__ = __dist.version
×
26

27

28
def __find_clang_libraries():
60✔
29
    """ configure python-clang to use the local clang library """
30
    _libs = []
31
    # try for a file with a version match with the clang python package version
32
    version_major = __clang_py_version__.split('.')[0]
33
    # try default system name
34
    v0 = [f"clang-{__clang_py_version__}", f"clang-{version_major}", "libclang", "clang"]
35
    # tries clang version 16 to 7
36
    v1 = ["clang-%d" % _ for _ in range(16, 6, -1)]
37
    # with the dotted form of clang 6.0 to 4.0
38
    v2 = ["clang-%.1f" % _ for _ in range(6, 3, -1)]
39
    # clang 3 supported versions
40
    v3 = ["clang-3.9", "clang-3.8", "clang-3.7"]
41
    v_list = v0 + v1 + v2 + v3
42
    for _version in v_list:
43
        _filename = find_library(_version)
44
        if _filename:
45
            _libs.append(_filename)
46
    # On darwin, also consider either Xcode or CommandLineTools.
47
    if os.name == "posix" and sys.platform == "darwin":
48
        for _ in ['/Library/Developer/CommandLineTools/usr/lib/libclang.dylib',
49
                  '/Applications/Xcode.app/Contents/Frameworks/libclang.dylib',
50
                  ]:
51
            if os.path.exists(_):
52
                _libs.insert(0, _)
53
    return _libs
54

55

56
def clang_version():
57
    """Pull the clang C library version from the API"""
58
    # avoid loading the cindex API (cindex.conf.lib) to avoid version conflicts
59
    get_version = cindex.conf.get_cindex_library().clang_getClangVersion
60✔
60
    get_version.restype = ctypes.c_char_p
60✔
61
    version_string = get_version()
60✔
62
    version = 'Unknown'
60✔
63
    if version_string and len(version_string) > 0:
60✔
64
        version_groups = re.match(br'.+version ((\d+\.)?(\d+\.)?(\*|\d+))', version_string)
60✔
65
        if version_groups and len(version_groups.groups()) > 0:
60✔
66
            version = version_groups.group(1).decode()
60✔
67
    return version
60✔
68

69

70
def clang_py_version():
60✔
71
    """Return the python clang package version"""
72
    return __clang_py_version__
73

74

75
def __configure_clang_cindex():
76
    """
77
    First we attempt to configure clang with the library path set in environment variable
×
78
    Second we attempt to configure clang with a clang library version similar to the clang python package version
×
79
    Third we attempt to configure clang with any clang library we can find
×
80
    """
×
81
    global __clang_py_version__
×
82
    __clang_py_version__ = get_distribution('clang').version
60✔
83
    # first, use environment variables set by user
84
    __libs = []
60✔
85
    __lib_path = os.environ.get('CLANG_LIBRARY_PATH')
60✔
86
    if __lib_path is not None:
60✔
87
        if not os.path.exists(__lib_path):
×
88
            warnings.warn("Filepath in CLANG_LIBRARY_PATH does not exist", RuntimeWarning)
×
89
        else:
×
90
            __libs.append(__lib_path)
×
91
    __libs.extend(__find_clang_libraries())
60✔
92
    for __library_path in __libs:
60✔
93
        try:
60✔
94
            if os.path.isdir(__library_path):
60✔
95
                cindex.Config.set_library_path(__library_path)
×
96
            else:
×
97
                cindex.Config.set_library_file(__library_path)
60✔
98
            # force-check that clang is configured properly
99
            clang_version()
60✔
100
        except cindex.LibclangError:
×
101
            warnings.warn(f"Could not configure clang with library_path={__library_path}", RuntimeWarning)
×
102
            continue
×
103
        return __library_path
60✔
104
    return None
×
105

106

107
# check which clang python module is available
108
# check which clang library is available
109
try:
60✔
110
    from clang import cindex
60✔
111

112
    __clang_py_version__ = 'not-installed'
60✔
113
    _filename = __configure_clang_cindex()
60✔
114
    if _filename is None:
60✔
115
        warnings.warn("Could not find the clang library. please install llvm libclang", RuntimeWarning)
×
116
        # do not fail - maybe the user has a plan
117
    else:
×
118
        # set a warning if major versions differs.
119
        if clang_version().split('.')[0] != clang_py_version().split('.')[0]:
60✔
120
            clang_major = clang_version().split('.')[0]
×
121
            warnings.warn("Version of python-clang (%s) and clang C library (%s) are different. "
×
122
                          "Did you try pip install clang==%s.*" % (
123
                              clang_py_version(), clang_version(), clang_major), RuntimeWarning)
124
except ImportError:
×
125
    __clang_py_version__ = None
×
126
    warnings.warn("Could not find a version of python-clang installed. please pip install clang", RuntimeWarning)
×
127

128
from clang import cindex
60✔
129
from ctypeslib.codegen.codegenerator import translate, translate_files
60✔
130

131
__all__ = ['translate', 'translate_files', 'clang_version']
60✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc