Skip to content

Commit b5c5fed

Browse files
committed
Improve errors when libmediainfo can't be loaded
1 parent abf927e commit b5c5fed

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

pymediainfo/__init__.py

+30-21
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,26 @@ def _define_library_prototypes(cls, lib: Any) -> Any:
276276
lib.MediaInfo_Close.argtypes = [ctypes.c_void_p]
277277
lib.MediaInfo_Close.restype = None
278278

279+
@staticmethod
280+
def _get_library_paths(os_is_nt: bool) -> Tuple[str]:
281+
if os_is_nt:
282+
library_paths = ("MediaInfo.dll",)
283+
elif sys.platform == "darwin":
284+
library_paths = ("libmediainfo.0.dylib", "libmediainfo.dylib")
285+
else:
286+
library_paths = ("libmediainfo.so.0",)
287+
script_dir = os.path.dirname(__file__)
288+
# Look for the library file in the script folder
289+
for library in library_paths:
290+
absolute_library_path = os.path.join(script_dir, library)
291+
if os.path.isfile(absolute_library_path):
292+
# If we find it, don't try any other filename
293+
library_paths = (absolute_library_path,)
294+
break
295+
return library_paths
296+
279297
@classmethod
280298
def _get_library(
281-
# pylint: disable=too-many-branches
282299
cls,
283300
library_file: Optional[str] = None,
284301
) -> Tuple[Any, Any, str, Tuple[int, ...]]:
@@ -288,25 +305,13 @@ def _get_library(
288305
else:
289306
lib_type = ctypes.CDLL
290307
if library_file is None:
291-
if os_is_nt:
292-
library_names = ("MediaInfo.dll",)
293-
elif sys.platform == "darwin":
294-
library_names = ("libmediainfo.0.dylib", "libmediainfo.dylib")
295-
else:
296-
library_names = ("libmediainfo.so.0",)
297-
script_dir = os.path.dirname(__file__)
298-
# Look for the library file in the script folder
299-
for library in library_names:
300-
lib_path = os.path.join(script_dir, library)
301-
if os.path.isfile(lib_path):
302-
# If we find it, don't try any other filename
303-
library_names = (lib_path,)
304-
break
308+
library_paths = cls._get_library_paths(os_is_nt)
305309
else:
306-
library_names = (library_file,)
307-
for library in library_names:
310+
library_paths = (library_file,)
311+
exceptions = []
312+
for library_path in library_paths:
308313
try:
309-
lib = lib_type(library)
314+
lib = lib_type(library_path)
310315
cls._define_library_prototypes(lib)
311316
# Without a handle, there might be problems when using concurrent threads
312317
# https://github.com/sbraz/pymediainfo/issues/76#issuecomment-574759621
@@ -319,9 +324,13 @@ def _get_library(
319324
else:
320325
raise RuntimeError("Could not determine library version")
321326
return (lib, handle, lib_version_str, lib_version)
322-
except OSError:
323-
pass
324-
raise OSError("Failed to load library")
327+
except OSError as exc:
328+
exceptions.append(str(exc))
329+
raise OSError(
330+
"Failed to load library from {} - {}".format(
331+
", ".join(library_paths), ", ".join(exceptions)
332+
)
333+
)
325334

326335
@classmethod
327336
def can_parse(cls, library_file: Optional[str] = None) -> bool:

tests/test_pymediainfo.py

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import pathlib
77
import pickle
8+
import tempfile
89
import threading
910
import unittest
1011
import xml
@@ -113,6 +114,15 @@ def test_full_option(self):
113114
self.assertEqual(self.media_info.tracks[0].footersize, "59")
114115
self.assertEqual(self.non_full_mi.tracks[0].footersize, None)
115116

117+
def test_raises_on_nonexistent_library(self): # pylint: disable=no-self-use
118+
with tempfile.TemporaryDirectory() as tmp_dir:
119+
nonexistent_library = os.path.join(tmp_dir, "nonexistent-libmediainfo.so")
120+
with pytest.raises(OSError) as exc:
121+
MediaInfo.parse(
122+
os.path.join(data_dir, "sample.mp4"), library_file=nonexistent_library
123+
)
124+
assert rf"Failed to load library from {nonexistent_library}" in str(exc.value)
125+
116126

117127
class MediaInfoFileLikeTest(unittest.TestCase):
118128
def test_can_parse(self): # pylint: disable=no-self-use

0 commit comments

Comments
 (0)