pyenchant / pyenchant

spellchecking library for python
https://pyenchant.github.io/pyenchant/
GNU Lesser General Public License v2.1
597 stars 97 forks source link

Enchant changes dll directory permanently #207

Closed mahfiaz closed 4 years ago

mahfiaz commented 4 years ago

On my installation importing vlc then enchant works fine, but importing enchant and then vlc fails:

Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 09:44:33) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import enchant
>>> import vlc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Program Files (x86)\Python37-32\lib\site-packages\vlc.py", line 210, in <module>
    dll, plugin_path  = find_lib()
  File "c:\Program Files (x86)\Python37-32\lib\site-packages\vlc.py", line 166, in find_lib
    dll = ctypes.CDLL(libname)
  File "c:\Program Files (x86)\Python37-32\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

Setting back the previous SetDllDirectoryW value as shown here should make it work. https://stackoverflow.com/questions/23804438/find-library-in-ctypes/23805306#23805306

@contextmanager
def use_dll_dir(dll_dir):
    size = newsize = 0
    while newsize >= size:
        size = newsize
        prev = (ctypes.c_wchar * size)()
        newsize = kernel32.GetDllDirectoryW(size, prev)
    kernel32.SetDllDirectoryW(os.path.abspath(dll_dir))
    try:
        yield # load enchant dll here
    finally:
        kernel32.SetDllDirectoryW(prev)
dmerejkowsky commented 4 years ago

Hello,

I think this is fixed with #208 - can you please try it out?

I went with the "prepend to path" approach which seemed less risky.

Also, got to ask: what kind of project requires both a remote client for VLC and a spell checker?

mahfiaz commented 4 years ago

Hey, this fix works perfectly. Thanks a lot, I would have never dared to hope for so quick fix.

The project using both is OpenLP: https://gitlab.com/openlp/openlp

dmerejkowsky commented 4 years ago

You're welcome. You can expect a new release soon, there are other fixes waiting to be released. Cheers!

mahfiaz commented 4 years ago

This is awesome. Thanks again.

dmerejkowsky commented 4 years ago

Version 3.1.1 is out. Enjoy.