mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
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:
parent
93f4de8792
commit
634d8a7898
|
@ -3,23 +3,37 @@ Helper module around the system's libssl library if available for IGE mode.
|
||||||
"""
|
"""
|
||||||
import ctypes
|
import ctypes
|
||||||
import ctypes.util
|
import ctypes.util
|
||||||
|
try:
|
||||||
|
import ctypes.macholib.dyld
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
__log__ = logging.getLogger(__name__)
|
__log__ = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
lib = ctypes.util.find_library('ssl')
|
def _find_ssl_lib():
|
||||||
|
lib = ctypes.util.find_library('ssl')
|
||||||
|
if not lib:
|
||||||
|
raise OSError('no library called "ssl" found')
|
||||||
|
|
||||||
# This is a best-effort attempt at finding the full real path of lib.
|
# First, let ctypes try to handle it itself.
|
||||||
#
|
try:
|
||||||
# Unfortunately ctypes doesn't tell us *where* it finds the library,
|
libssl = ctypes.cdll.LoadLibrary(lib)
|
||||||
# so we have to do that ourselves.
|
except OSError:
|
||||||
try:
|
pass
|
||||||
|
else:
|
||||||
|
return libssl
|
||||||
|
|
||||||
|
# This is a best-effort attempt at finding the full real path of lib.
|
||||||
|
#
|
||||||
|
# Unfortunately ctypes doesn't tell us *where* it finds the library,
|
||||||
|
# so we have to do that ourselves.
|
||||||
|
try:
|
||||||
# This is not documented, so it could fail. Be on the safe side.
|
# This is not documented, so it could fail. Be on the safe side.
|
||||||
import ctypes.macholib.dyld
|
|
||||||
paths = ctypes.macholib.dyld.DEFAULT_LIBRARY_FALLBACK
|
paths = ctypes.macholib.dyld.DEFAULT_LIBRARY_FALLBACK
|
||||||
except (ImportError, AttributeError):
|
except AttributeError:
|
||||||
paths = [
|
paths = [
|
||||||
os.path.expanduser("~/lib"),
|
os.path.expanduser("~/lib"),
|
||||||
"/usr/local/lib",
|
"/usr/local/lib",
|
||||||
|
@ -27,25 +41,24 @@ except (ImportError, AttributeError):
|
||||||
"/usr/lib",
|
"/usr/lib",
|
||||||
]
|
]
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
for root, _, files in os.walk(path):
|
for root, _, files in os.walk(path):
|
||||||
if lib in files:
|
if lib in files:
|
||||||
# Manually follow symbolic links on *nix systems.
|
# Manually follow symbolic links on *nix systems.
|
||||||
# Fix for https://github.com/LonamiWebs/Telethon/issues/1167
|
# Fix for https://github.com/LonamiWebs/Telethon/issues/1167
|
||||||
lib = os.path.realpath(os.path.join(root, lib))
|
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:
|
try:
|
||||||
if not lib:
|
_libssl = _find_ssl_lib()
|
||||||
raise OSError('no library called "ssl" found')
|
|
||||||
|
|
||||||
_libssl = ctypes.cdll.LoadLibrary(lib)
|
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
# See https://github.com/LonamiWebs/Telethon/issues/1167
|
# See https://github.com/LonamiWebs/Telethon/issues/1167
|
||||||
# Sometimes `find_library` returns improper filenames.
|
# 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
|
_libssl = None
|
||||||
|
|
||||||
if not _libssl:
|
if not _libssl:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user