@@ -176,6 +176,7 @@ class Argparse(ThemeSection):
176176class Syntax (ThemeSection ):
177177 prompt : str = ANSIColors .BOLD_MAGENTA
178178 keyword : str = ANSIColors .BOLD_BLUE
179+ keyword_constant : str = ANSIColors .BOLD_BLUE
179180 builtin : str = ANSIColors .CYAN
180181 comment : str = ANSIColors .RED
181182 string : str = ANSIColors .GREEN
@@ -272,21 +273,29 @@ def decolor(text: str) -> str:
272273
273274
274275def can_colorize (* , file : IO [str ] | IO [bytes ] | None = None ) -> bool :
276+
277+ def _safe_getenv (k : str , fallback : str | None = None ) -> str | None :
278+ """Exception-safe environment retrieval. See gh-128636."""
279+ try :
280+ return os .environ .get (k , fallback )
281+ except Exception :
282+ return fallback
283+
275284 if file is None :
276285 file = sys .stdout
277286
278287 if not sys .flags .ignore_environment :
279- if os . environ . get ("PYTHON_COLORS" ) == "0" :
288+ if _safe_getenv ("PYTHON_COLORS" ) == "0" :
280289 return False
281- if os . environ . get ("PYTHON_COLORS" ) == "1" :
290+ if _safe_getenv ("PYTHON_COLORS" ) == "1" :
282291 return True
283- if os . environ . get ("NO_COLOR" ):
292+ if _safe_getenv ("NO_COLOR" ):
284293 return False
285294 if not COLORIZE :
286295 return False
287- if os . environ . get ("FORCE_COLOR" ):
296+ if _safe_getenv ("FORCE_COLOR" ):
288297 return True
289- if os . environ . get ("TERM" ) == "dumb" :
298+ if _safe_getenv ("TERM" ) == "dumb" :
290299 return False
291300
292301 if not hasattr (file , "fileno" ):
@@ -329,7 +338,8 @@ def get_theme(
329338 environment (including environment variable state and console configuration
330339 on Windows) can also change in the course of the application life cycle.
331340 """
332- if force_color or (not force_no_color and can_colorize (file = tty_file )):
341+ if force_color or (not force_no_color and
342+ can_colorize (file = tty_file )):
333343 return _theme
334344 return theme_no_color
335345
0 commit comments