Refactor libssl import path workarounds (#1202)

This amends the logic introduced for #1167 to only run when a plain
import of the library fails, thus avoiding the slow path traversal
needed to find the underlying library file.
This commit is contained in:
Terrance 2019-06-15 12:42:31 +01:00 committed by Lonami
parent 93f4de8792
commit 634d8a7898

View File

@ -3,13 +3,28 @@ Helper module around the system's libssl library if available for IGE mode.
"""
import ctypes
import ctypes.util
try:
import ctypes.macholib.dyld
except ImportError:
pass
import logging
import os
__log__ = logging.getLogger(__name__)
def _find_ssl_lib():
lib = ctypes.util.find_library('ssl')
if not lib:
raise OSError('no library called "ssl" found')
# First, let ctypes try to handle it itself.
try:
libssl = ctypes.cdll.LoadLibrary(lib)
except OSError:
pass
else:
return libssl
# This is a best-effort attempt at finding the full real path of lib.
#
@ -17,9 +32,8 @@ lib = ctypes.util.find_library('ssl')
# so we have to do that ourselves.
try:
# This is not documented, so it could fail. Be on the safe side.
import ctypes.macholib.dyld
paths = ctypes.macholib.dyld.DEFAULT_LIBRARY_FALLBACK
except (ImportError, AttributeError):
except AttributeError:
paths = [
os.path.expanduser("~/lib"),
"/usr/local/lib",
@ -34,18 +48,17 @@ for path in paths:
# Manually follow symbolic links on *nix systems.
# Fix for https://github.com/LonamiWebs/Telethon/issues/1167
lib = os.path.realpath(os.path.join(root, lib))
break
return ctypes.cdll.LoadLibrary(lib)
else:
raise OSError('no absolute path for "%s" and cannot load by name' % lib)
try:
if not lib:
raise OSError('no library called "ssl" found')
_libssl = ctypes.cdll.LoadLibrary(lib)
_libssl = _find_ssl_lib()
except OSError as e:
# See https://github.com/LonamiWebs/Telethon/issues/1167
# Sometimes `find_library` returns improper filenames.
__log__.info('Failed to load %s: %s (%s)', lib, type(e), e)
__log__.info('Failed to load SSL library: %s (%s)', type(e), e)
_libssl = None
if not _libssl: