From fe0918bb65c828ec81ce904cece58d450c117eba Mon Sep 17 00:00:00 2001 From: pukkandan Date: Wed, 3 Aug 2022 17:47:38 +0530 Subject: [PATCH] Import ctypes only when necessary Closes #4541 --- yt_dlp/cookies.py | 7 ++++--- yt_dlp/utils.py | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/yt_dlp/cookies.py b/yt_dlp/cookies.py index df8f97b44..1a164bb31 100644 --- a/yt_dlp/cookies.py +++ b/yt_dlp/cookies.py @@ -1,6 +1,5 @@ import base64 import contextlib -import ctypes import http.cookiejar import json import os @@ -876,10 +875,12 @@ def _decrypt_windows_dpapi(ciphertext, logger): References: - https://docs.microsoft.com/en-us/windows/win32/api/dpapi/nf-dpapi-cryptunprotectdata """ - from ctypes.wintypes import DWORD + + import ctypes + import ctypes.wintypes class DATA_BLOB(ctypes.Structure): - _fields_ = [('cbData', DWORD), + _fields_ = [('cbData', ctypes.wintypes.DWORD), ('pbData', ctypes.POINTER(ctypes.c_char))] buffer = ctypes.create_string_buffer(ciphertext) diff --git a/yt_dlp/utils.py b/yt_dlp/utils.py index c0d9c6f79..c3ccb3a78 100644 --- a/yt_dlp/utils.py +++ b/yt_dlp/utils.py @@ -6,7 +6,6 @@ import codecs import collections import contextlib -import ctypes import datetime import email.header import email.utils @@ -1983,6 +1982,7 @@ def __init__(self): # Cross-platform file locking if sys.platform == 'win32': + import ctypes import ctypes.wintypes import msvcrt @@ -2362,9 +2362,10 @@ def fix_xml_ampersands(xml_str): def setproctitle(title): assert isinstance(title, str) - # ctypes in Jython is not complete - # http://bugs.jython.org/issue2148 - if sys.platform.startswith('java'): + # Workaround for https://github.com/yt-dlp/yt-dlp/issues/4541 + try: + import ctypes + except ImportError: return try: