Coverage for core / io_guard.py: 77.8%

9 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-12 04:49 +0000

1"""Redirect stdout/stderr to devnull in frozen builds. 

2 

3cx_Freeze frozen builds may have stdout/stderr closed or pointing to 

4invalid file descriptors. This causes crashes when any library tries to 

5print (LangChain, autogen, etc.). Redirecting to devnull prevents these 

6crashes while preserving logging (which uses its own handlers). 

7 

8Single source of truth — imported by create_recipe.py, reuse_recipe.py, 

9and hart_intelligence_entry.py instead of copy-pasting the guard. 

10""" 

11import os 

12import sys 

13 

14 

15def silence_stdio(): 

16 """Redirect stdout/stderr to devnull if they're broken (frozen builds).""" 

17 try: 

18 if sys.stdout is None or sys.stdout.closed: 

19 sys.stdout = open(os.devnull, 'w') 

20 except Exception: 

21 sys.stdout = open(os.devnull, 'w') 

22 

23 try: 

24 if sys.stderr is None or sys.stderr.closed: 

25 sys.stderr = open(os.devnull, 'w') 

26 except Exception: 

27 sys.stderr = open(os.devnull, 'w')