Skip to content

Commit

Permalink
Use os.add_dll_directory when configuring Windows on >=3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
aiudirog authored and frozencemetery committed Apr 3, 2020
1 parent 1eadb94 commit b95c263
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions gssapi/_win_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import os
import shutil
import ctypes

#: Path to normal KfW installed bin folder
Expand All @@ -18,8 +19,8 @@
KFW_DL = "https://web.mit.edu/KERBEROS/dist"


def k4w_in_path():
"""Return if the main GSSAPI DLL for KfW is available in the PATH"""
def kfw_available():
"""Return if the main GSSAPI DLL for KfW can be loaded"""
try: # to load the main GSSAPI DLL
ctypes.WinDLL('gssapi64.dll')
except OSError: # DLL is not in PATH
Expand All @@ -41,14 +42,31 @@ def error_not_found():
def configure_windows():
"""
Validate that KfW appears to be installed correctly and add it to the
PATH if necessary. In the case that it can't be located, raise an error.
DLL directories/PATH if necessary. In the case that it can't be located,
raise an error.
"""
if k4w_in_path():
if kfw_available():
return # All set, necessary DLLs should be available

if os.path.exists(KFW_BIN): # In standard location
os.environ['PATH'] += os.pathsep + KFW_BIN
if k4w_in_path():
try: # to use Python 3.8's DLL handling
os.add_dll_directory(KFW_BIN)
except AttributeError: # <3.8, use PATH
os.environ['PATH'] += os.pathsep + KFW_BIN
if kfw_available():
return

# Check if kinit is in the PATH which should lead us to the bin folder
kinit_path = shutil.which('kinit') # KfW provided binary
if kinit_path: # Non-standard install location
try: # Most likely >=3.8, otherwise it would have been found already
os.add_dll_directory(os.path.dirname(kinit_path))
except AttributeError: # <3.8, corrupted installation?
pass
else:
if kfw_available():
return

error_not_found()


Expand Down

0 comments on commit b95c263

Please sign in to comment.