File: test_find_libpython.py

package info (click to toggle)
find-libpython 0.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 152 kB
  • sloc: python: 342; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,373 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import ctypes
import sys

import pytest
from find_libpython import (
    _get_proc_library,
    _is_cygwin,
    _is_posix,
    _linked_libpython_unix,
    find_libpython,
)

try:
    ctypes.CDLL("")
    can_get_handle_to_main = True
except OSError:
    print("Platform does not support opening the current process library")
    can_get_handle_to_main = False


def test_find_libpython():
    # find path
    path = find_libpython()
    # ensure we have a path
    assert path is not None
    # check to ensure it is a libpython share object
    lib = ctypes.CDLL(path)
    assert hasattr(lib, "Py_Initialize")
    # ensure it's the right version...
    lib.Py_GetVersion.restype = ctypes.c_char_p
    lib_version = lib.Py_GetVersion().decode().split()[0]
    curr_version = sys.version.split()[0]
    assert lib_version == curr_version


@pytest.mark.skipif(
    not _is_posix or _is_cygwin or not can_get_handle_to_main,
    reason="only linux support this",
)
def test_get_proc_library():
    # Get current library
    libpython = ctypes.CDLL("")

    # Get library for reference
    path_linked = _linked_libpython_unix(libpython)
    # Get library from /proc method
    path_from_proc = next(_get_proc_library())

    # Only compare paths if both return a library (not /usr/bin/python3.x)
    if "libpython" in path_linked:
        assert path_linked == path_from_proc