Skip to content

Commit 517857f

Browse files
authored
Merge pull request #143 from UMEssen/142-optionalimporterror-when-running-on-linux-with-python-312
142 optionalimporterror when running on linux with python 312
2 parents 4b97c3d + 41b9274 commit 517857f

4 files changed

Lines changed: 2075 additions & 1415 deletions

File tree

fhir_pyrate/dicom_downloader.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ctypes
12
import hashlib
23
import io
34
import logging
@@ -12,6 +13,7 @@
1213
import traceback
1314
import warnings
1415
from contextlib import contextmanager
16+
from ctypes.util import find_library
1517
from functools import partial
1618
from types import TracebackType
1719
from typing import (
@@ -45,15 +47,12 @@
4547
# https://stackoverflow.com/questions/64674495/how-to-catch-simpleitk-warnings
4648
# https://github.com/hankcs/HanLP/blob/doc-zh/hanlp/utils/io_util.py
4749
try:
48-
import ctypes
49-
from ctypes.util import find_library
50-
except ImportError:
51-
libc = None
52-
else:
53-
try:
54-
libc = ctypes.cdll.msvcrt # Windows
55-
except OSError:
56-
libc = ctypes.cdll.LoadLibrary(find_library("c")) # type: ignore
50+
# Windows succeeds, POSIX raises
51+
libc: Optional[ctypes.CDLL] = ctypes.cdll.msvcrt # Windows
52+
except (OSError, AttributeError):
53+
# POSIX fallback — "libc.so.*" on Linux, "libc.dylib" on macOS
54+
c_name = find_library("c")
55+
libc = ctypes.CDLL(c_name) if c_name else None
5756

5857

5958
def flush(stream: TextIO) -> None:

0 commit comments

Comments
 (0)