Delay some imports

This commit is contained in:
Kovid Goyal 2025-03-26 09:56:04 +05:30
parent 04f6473f38
commit e90b79cf17
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 11 additions and 8 deletions

View File

@ -8,9 +8,7 @@ __docformat__ = 'restructuredtext en'
import errno import errno
import os import os
from calibre import force_unicode
from calibre.constants import filesystem_encoding, get_windows_username, islinux, iswindows from calibre.constants import filesystem_encoding, get_windows_username, islinux, iswindows
from calibre.utils.filenames import ascii_filename
from polyglot.functools import lru_cache from polyglot.functools import lru_cache
VADDRESS = None VADDRESS = None
@ -28,6 +26,8 @@ def eintr_retry_call(func, *args, **kwargs):
@lru_cache() @lru_cache()
def socket_address(which): def socket_address(which):
from calibre import force_unicode
from calibre.utils.filenames import ascii_filename
if iswindows: if iswindows:
ans = r'\\.\pipe\Calibre' + which ans = r'\\.\pipe\Calibre' + which
try: try:

View File

@ -12,9 +12,8 @@ from contextlib import suppress
from functools import wraps from functools import wraps
from threading import Lock from threading import Lock
from calibre.constants import iswindows _plat = sys.platform.lower()
from calibre.utils.filenames import make_long_path_useable iswindows = 'win32' in _plat or 'win64' in _plat
from calibre.utils.ipc.simple_worker import start_pipe_worker
lock = Lock() lock = Lock()
worker = None worker = None
@ -32,12 +31,12 @@ def thread_safe(f):
@thread_safe @thread_safe
def remove_folder(path: str) -> None: def remove_folder_atexit(path: str) -> None:
_send_command(RMTREE_ACTION, os.path.abspath(path)) _send_command(RMTREE_ACTION, os.path.abspath(path))
@thread_safe @thread_safe
def remove_file(path: str) -> None: def remove_file_atexit(path: str) -> None:
_send_command(UNLINK_ACTION, os.path.abspath(path)) _send_command(UNLINK_ACTION, os.path.abspath(path))
@ -51,6 +50,7 @@ def unlink(path):
def ensure_worker(): def ensure_worker():
global worker global worker
if worker is None: if worker is None:
from calibre.utils.ipc.simple_worker import start_pipe_worker
worker = start_pipe_worker('from calibre.utils.safe_atexit import main; main()', stdout=None) worker = start_pipe_worker('from calibre.utils.safe_atexit import main; main()', stdout=None)
def close_worker(): def close_worker():
worker.stdin.close() worker.stdin.close()
@ -68,6 +68,7 @@ def _send_command(action: str, payload: str) -> None:
if iswindows: if iswindows:
def remove_dir(x): def remove_dir(x):
from calibre.utils.filenames import make_long_path_useable
x = make_long_path_useable(x) x = make_long_path_useable(x)
import shutil import shutil
import time import time
@ -107,7 +108,7 @@ def main_for_test(do_forced_exit=False):
tf = 'test-folder' tf = 'test-folder'
os.mkdir(tf) os.mkdir(tf)
open(os.path.join(tf, 'test-file'), 'w').close() open(os.path.join(tf, 'test-file'), 'w').close()
remove_folder(tf) remove_folder_atexit(tf)
if do_forced_exit: if do_forced_exit:
os._exit(os.EX_OK) os._exit(os.EX_OK)
else: else:
@ -118,6 +119,8 @@ def find_tests():
import tempfile import tempfile
import unittest import unittest
from calibre.utils.ipc.simple_worker import start_pipe_worker
class TestSafeAtexit(unittest.TestCase): class TestSafeAtexit(unittest.TestCase):
def wait_for_empty(self, tdir, timeout=10): def wait_for_empty(self, tdir, timeout=10):