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

View File

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