mirror of
				https://github.com/python/cpython.git
				synced 2025-10-28 20:25:04 +00:00 
			
		
		
		
	This reverts commit 365451e283.
			
			
This commit is contained in:
		
							parent
							
								
									ed037d229f
								
							
						
					
					
						commit
						a8ffe66154
					
				
					 3 changed files with 8 additions and 48 deletions
				
			
		|  | @ -32,6 +32,14 @@ def get_colors(colorize: bool = False) -> ANSIColors: | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def can_colorize() -> bool: | def can_colorize() -> bool: | ||||||
|  |     if sys.platform == "win32": | ||||||
|  |         try: | ||||||
|  |             import nt | ||||||
|  | 
 | ||||||
|  |             if not nt._supports_virtual_terminal(): | ||||||
|  |                 return False | ||||||
|  |         except (ImportError, AttributeError): | ||||||
|  |             return False | ||||||
|     if not sys.flags.ignore_environment: |     if not sys.flags.ignore_environment: | ||||||
|         if os.environ.get("PYTHON_COLORS") == "0": |         if os.environ.get("PYTHON_COLORS") == "0": | ||||||
|             return False |             return False | ||||||
|  | @ -50,15 +58,6 @@ def can_colorize() -> bool: | ||||||
|     if not hasattr(sys.stderr, "fileno"): |     if not hasattr(sys.stderr, "fileno"): | ||||||
|         return False |         return False | ||||||
| 
 | 
 | ||||||
|     if sys.platform == "win32": |  | ||||||
|         try: |  | ||||||
|             import nt |  | ||||||
| 
 |  | ||||||
|             if not nt._supports_virtual_terminal(): |  | ||||||
|                 return False |  | ||||||
|         except (ImportError, AttributeError): |  | ||||||
|             return False |  | ||||||
| 
 |  | ||||||
|     try: |     try: | ||||||
|         return os.isatty(sys.stderr.fileno()) |         return os.isatty(sys.stderr.fileno()) | ||||||
|     except io.UnsupportedOperation: |     except io.UnsupportedOperation: | ||||||
|  |  | ||||||
|  | @ -50,47 +50,10 @@ def test_colorized_detection_checks_for_environment_variables(self): | ||||||
|                 with unittest.mock.patch("os.environ", |                 with unittest.mock.patch("os.environ", | ||||||
|                                          {'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}): |                                          {'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}): | ||||||
|                     self.assertEqual(_colorize.can_colorize(), False) |                     self.assertEqual(_colorize.can_colorize(), False) | ||||||
|                 with unittest.mock.patch("os.environ", {}): |  | ||||||
|                     self.assertEqual(_colorize.can_colorize(), True) |  | ||||||
| 
 |  | ||||||
|                 isatty_mock.return_value = False |                 isatty_mock.return_value = False | ||||||
|                 with unittest.mock.patch("os.environ", {}): |                 with unittest.mock.patch("os.environ", {}): | ||||||
|                     self.assertEqual(_colorize.can_colorize(), False) |                     self.assertEqual(_colorize.can_colorize(), False) | ||||||
| 
 | 
 | ||||||
|     @force_not_colorized |  | ||||||
|     @unittest.skipUnless(sys.platform == "win32", "Windows only") |  | ||||||
|     def test_colorized_detection_checks_for_environment_variables_no_vt(self): |  | ||||||
|         with (unittest.mock.patch("nt._supports_virtual_terminal", return_value=False), |  | ||||||
|               unittest.mock.patch("os.isatty") as isatty_mock, |  | ||||||
|               unittest.mock.patch("sys.flags", unittest.mock.MagicMock(ignore_environment=False)), |  | ||||||
|               unittest.mock.patch("_colorize.can_colorize", ORIGINAL_CAN_COLORIZE)): |  | ||||||
|             isatty_mock.return_value = True |  | ||||||
|             with unittest.mock.patch("os.environ", {'TERM': 'dumb'}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), False) |  | ||||||
|             with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '1'}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), True) |  | ||||||
|             with unittest.mock.patch("os.environ", {'PYTHON_COLORS': '0'}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), False) |  | ||||||
|             with unittest.mock.patch("os.environ", {'NO_COLOR': '1'}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), False) |  | ||||||
|             with unittest.mock.patch("os.environ", |  | ||||||
|                                      {'NO_COLOR': '1', "PYTHON_COLORS": '1'}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), True) |  | ||||||
|             with unittest.mock.patch("os.environ", {'FORCE_COLOR': '1'}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), True) |  | ||||||
|             with unittest.mock.patch("os.environ", |  | ||||||
|                                      {'FORCE_COLOR': '1', 'NO_COLOR': '1'}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), False) |  | ||||||
|             with unittest.mock.patch("os.environ", |  | ||||||
|                                      {'FORCE_COLOR': '1', "PYTHON_COLORS": '0'}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), False) |  | ||||||
|             with unittest.mock.patch("os.environ", {}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), False) |  | ||||||
| 
 |  | ||||||
|             isatty_mock.return_value = False |  | ||||||
|             with unittest.mock.patch("os.environ", {}): |  | ||||||
|                 self.assertEqual(_colorize.can_colorize(), False) |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     unittest.main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -1,2 +0,0 @@ | ||||||
| Allow to force color output on Windows using environment variables. Patch by |  | ||||||
| Andrey Efremov. |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Hugo van Kemenade
						Hugo van Kemenade