Windows: Use cytpes.load_library to look for DLL

This commit is contained in:
jaseg 2019-12-04 00:05:21 +01:00
parent 3452ac87bc
commit 63f9b74247
2 changed files with 17 additions and 9 deletions

View file

@ -19,16 +19,21 @@ Installation
Requirements Requirements
~~~~~~~~~~~~ ~~~~~~~~~~~~
libmpv (no kidding!) libmpv
.................... ......
``libmpv.so`` either locally (in your current working directory) or somewhere in your system library search path. This ``libmpv.so`` either locally (in your current working directory) or somewhere in your system library search path. This
module is somewhat lenient as far as ``libmpv`` versions are concerned but since ``libmpv`` is changing quite frequently module is somewhat lenient as far as ``libmpv`` versions are concerned but since ``libmpv`` is changing quite frequently
you'll only get all the newest features when using an up-to-date version of this module. The unit tests for this module you'll only get all the newest features when using an up-to-date version of this module. The unit tests for this module
do some basic automatic version compatibility checks. If you discover anything missing here, please open an `issue`_ or do some basic automatic version compatibility checks. If you discover anything missing here, please open an `issue`_ or
submit a `pull request`_ on github. submit a `pull request`_ on github.
Python 2.7, 3.5 or 3.6 (officially) On Windows you can place libmpv anywhere in your ``%PATH%`` (e.g. next to ``python.exe``) or next to this module's
................................... ``mpv.py``. Before falling back to looking in the mpv module's directory, python-mpv uses the DLL search order built
into ctypes, which is different to the one Windows uses internally. Consult `this stackoverflow post
<https://stackoverflow.com/a/23805306>`__ for details.
Python >= 3.5 (officially)
..........................
The ``master`` branch officially only supports recent python releases (3.5 onwards), but there is the somewhat outdated The ``master`` branch officially only supports recent python releases (3.5 onwards), but there is the somewhat outdated
but functional `py2compat branch`_ providing Python 2 compatibility. but functional `py2compat branch`_ providing Python 2 compatibility.

13
mpv.py
View file

@ -28,10 +28,13 @@ import re
import traceback import traceback
if os.name == 'nt': if os.name == 'nt':
try: dll = ctypes.util.find_library('mpv-1.dll')
backend = CDLL('mpv-1.dll') if dll is None:
except FileNotFoundError: raise OSError('Cannot find mpv-1.dll in your system %PATH%. One way to deal with this is to ship mpv-1.dll '
backend = CDLL(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'mpv-1.dll')) 'with your script and put the directory your script is in into %PATH% before "import mpv": '
'os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"] '
'If mpv-1.dll is located elsewhere, you can add that path to os.environ["PATH"].')
backend = CDLL(dll)
fs_enc = 'utf-8' fs_enc = 'utf-8'
else: else:
import locale import locale
@ -43,7 +46,7 @@ else:
sofile = ctypes.util.find_library('mpv') sofile = ctypes.util.find_library('mpv')
if sofile is None: if sofile is None:
raise OSError("Cannot find libmpv in the usual places. Depending on your distro, you may try installing an " raise OSError("Cannot find libmpv in the usual places. Depending on your distro, you may try installing an "
"mpv-devel or mpv-libs package. If you have libmpv around but this script can't find it, maybe consult " "mpv-devel or mpv-libs package. If you have libmpv around but this script can't find it, consult "
"the documentation for ctypes.util.find_library which this script uses to look up the library " "the documentation for ctypes.util.find_library which this script uses to look up the library "
"filename.") "filename.")
backend = CDLL(sofile) backend = CDLL(sofile)