8000 Reorder env variable checks · python/cpython@ff19eec · GitHub
[go: up one dir, main page]

Skip to content

Commit ff19eec

Browse files
committed
Reorder env variable checks
1 parent 7671e2d commit ff19eec

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

Lib/test/test_traceback.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,8 +4314,8 @@ def foo():
43144314
self.assertEqual(actual, expected)
43154315

43164316
def test_colorized_detection_checks_for_environment_variables(self):
4317-
with unittest.mock.patch("sys.stderr") as stderr_mock:
4318-
stderr_mock.isatty.return_value = True
4317+
with unittest.mock.patch("os.isatty") as isatty_mock:
4318+
isatty_mock.return_value = True
43194319
with unittest.mock.patch("os.environ", {'TERM': 'dumb'}):
43204320
self.assertEqual(traceback._can_colorize(), False)
43214321
with unittest.mock.patch("os.environ", {'PY_COLORS': '1'}):
@@ -4325,12 +4325,12 @@ def test_colorized_detection_checks_for_environment_variables(self):
43254325
with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}):
43264326
self.assertEqual(traceback._can_colorize(), False)
43274327
with unittest.mock.patch("os.environ", {'NO_COLOR': '1', "PY_COLORS": '1'}):
4328-
self.assertEqual(traceback._can_colorize(), True)
4328+
self.assertEqual(traceback._can_colorize(), False)
43294329
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}):
43304330
self.assertEqual(traceback._can_colorize(), True)
43314331
with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1', 'NO_COLOR': '1'}):
43324332
self.assertEqual(traceback._can_colorize(), False)
4333-
stderr_mock.isatty.return_value = False
4333+
isatty_mock.return_value = False
43344334
self.assertEqual(traceback._can_colorize(), False)
43354335

43364336
if __name__ == "__main__":

Lib/traceback.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Extract, format and print information about Python stack traces."""
22

33
import os
4+
import io
45
import collections.abc
56
import itertools
67
import linecache
@@ -141,19 +142,22 @@ def _can_colorize():
141142
except (ImportError, AttributeError):
142143
return False
143144

144-
if not _COLORIZE:
145+
if "NO_COLOR" in os.environ:
145146
return False
146-
if os.environ.get("PY_COLORS") == "1":
147-
return True
148147
if os.environ.get("PY_COLORS") == "0":
149148
return False
150-
if "NO_COLOR" in os.environ:
149+
if not _COLORIZE:
151150
return False
152151
if "FORCE_COLOR" in os.environ:
153152
return True
154-
return (
155-
hasattr(sys.stderr, "isatty") and sys.stderr.isatty() and os.environ.get("TERM") != "dumb"
156-
)
153+
if os.environ.get("PY_COLORS") == "1":
154+
return True
155+
if os.environ.get("TERM") == "dumb":
156+
return False
157+
try:
158+
return os.isatty(sys.stderr.fileno())
159+
except io.UnsupportedOperation:
160+
return sys.stderr.isatty()
157161

158162
def _print_exception_bltin(exc, /):
159163
file = sys.stderr if sys.stderr is not None else sys.__stderr__

0 commit comments

Comments
 (0)
0