Skip to content

Commit a66fdfb

Browse files
committed
Set tempfile.tempdir instead of environment variables
Use tempfile.tempdir directly instead of TEMP/TMP environment variables. The tempfile module caches the temp directory, so setting env vars after it's imported doesn't work. Setting tempfile.tempdir forces it to use the video directory even if already imported.
1 parent 0dbcaaa commit a66fdfb

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

fastflix/widgets/background_tasks.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,15 @@ def _convert_sup_to_srt(self, sup_filepath: str) -> bool:
242242
mkvtoolnix_dir = str(Path(self.app.fastflix.config.mkvmerge_path).parent)
243243
os.environ["PATH"] = f"{mkvtoolnix_dir}{os.pathsep}{os.environ.get('PATH', '')}"
244244

245-
# CRITICAL FIX: Set TEMP/TMP to video directory before importing pgsrip
246-
# This forces Python's tempfile module to use the video directory
247-
original_temp = os.environ.get("TEMP")
248-
original_tmp = os.environ.get("TMP")
249-
os.environ["TEMP"] = video_dir
250-
os.environ["TMP"] = video_dir
245+
# CRITICAL FIX: Set tempfile.tempdir to video directory
246+
# tempfile module caches the temp dir, so we must set tempfile.tempdir directly
247+
import tempfile
248+
249+
original_tempdir = tempfile.tempdir
250+
tempfile.tempdir = video_dir
251251

252252
try:
253-
# Import pgsrip AFTER setting TEMP/TMP
253+
# Import pgsrip AFTER setting tempfile.tempdir
254254
from pgsrip import pgsrip, Mkv, Options
255255
from babelfish import Language as BabelLanguage
256256

@@ -317,16 +317,8 @@ def _convert_sup_to_srt(self, sup_filepath: str) -> bool:
317317
return True
318318

319319
finally:
320-
# Restore original TEMP/TMP
321-
if original_temp is not None:
322-
os.environ["TEMP"] = original_temp
323-
elif "TEMP" in os.environ:
324-
del os.environ["TEMP"]
325-
326-
if original_tmp is not None:
327-
os.environ["TMP"] = original_tmp
328-
elif "TMP" in os.environ:
329-
del os.environ["TMP"]
320+
# Restore original tempfile.tempdir
321+
tempfile.tempdir = original_tempdir
330322

331323
except Exception as err:
332324
self.main.thread_logging_signal.emit(f"ERROR:{t('OCR conversion failed')}: {err}")

0 commit comments

Comments
 (0)