Skip to content

Commit

Permalink
Merge pull request #11593 from pfmoore/vendoring_test
Browse files Browse the repository at this point in the history
Vendoring updates (except rich)
  • Loading branch information
pradyunsg authored Nov 12, 2022
2 parents 72a5b08 + bbe83b0 commit c81ab26
Show file tree
Hide file tree
Showing 21 changed files with 1,010 additions and 77 deletions.
1 change: 1 addition & 0 deletions news/colorama.vendor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Upgrade colorama to 0.4.6
1 change: 1 addition & 0 deletions news/distro.vendor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Upgrade distro to 1.8.0
1 change: 1 addition & 0 deletions news/platformdirs.vendor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Upgrade platformdirs to 2.5.3
5 changes: 3 additions & 2 deletions src/pip/_vendor/colorama/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
from .initialise import init, deinit, reinit, colorama_text
from .initialise import init, deinit, reinit, colorama_text, just_fix_windows_console
from .ansi import Fore, Back, Style, Cursor
from .ansitowin32 import AnsiToWin32

__version__ = '0.4.5'
__version__ = '0.4.6'

17 changes: 14 additions & 3 deletions src/pip/_vendor/colorama/ansitowin32.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os

from .ansi import AnsiFore, AnsiBack, AnsiStyle, Style, BEL
from .winterm import WinTerm, WinColor, WinStyle
from .winterm import enable_vt_processing, WinTerm, WinColor, WinStyle
from .win32 import windll, winapi_test


Expand Down Expand Up @@ -94,15 +94,22 @@ def __init__(self, wrapped, convert=None, strip=None, autoreset=False):
# (e.g. Cygwin Terminal). In this case it's up to the terminal
# to support the ANSI codes.
conversion_supported = on_windows and winapi_test()
try:
fd = wrapped.fileno()
except Exception:
fd = -1
system_has_native_ansi = not on_windows or enable_vt_processing(fd)
have_tty = not self.stream.closed and self.stream.isatty()
need_conversion = conversion_supported and not system_has_native_ansi

# should we strip ANSI sequences from our output?
if strip is None:
strip = conversion_supported or (not self.stream.closed and not self.stream.isatty())
strip = need_conversion or not have_tty
self.strip = strip

# should we should convert ANSI sequences into win32 calls?
if convert is None:
convert = conversion_supported and not self.stream.closed and self.stream.isatty()
convert = need_conversion and have_tty
self.convert = convert

# dict of ansi codes to win32 functions and parameters
Expand Down Expand Up @@ -264,3 +271,7 @@ def convert_osc(self, text):
if params[0] in '02':
winterm.set_title(params[1])
return text


def flush(self):
self.wrapped.flush()
51 changes: 46 additions & 5 deletions src/pip/_vendor/colorama/initialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@
from .ansitowin32 import AnsiToWin32


orig_stdout = None
orig_stderr = None
def _wipe_internal_state_for_tests():
global orig_stdout, orig_stderr
orig_stdout = None
orig_stderr = None

global wrapped_stdout, wrapped_stderr
wrapped_stdout = None
wrapped_stderr = None

wrapped_stdout = None
wrapped_stderr = None
global atexit_done
atexit_done = False

global fixed_windows_console
fixed_windows_console = False

atexit_done = False
try:
# no-op if it wasn't registered
atexit.unregister(reset_all)
except AttributeError:
# python 2: no atexit.unregister. Oh well, we did our best.
pass


def reset_all():
Expand Down Expand Up @@ -55,6 +69,29 @@ def deinit():
sys.stderr = orig_stderr


def just_fix_windows_console():
global fixed_windows_console

if sys.platform != "win32":
return
if fixed_windows_console:
return
if wrapped_stdout is not None or wrapped_stderr is not None:
# Someone already ran init() and it did stuff, so we won't second-guess them
return

# On newer versions of Windows, AnsiToWin32.__init__ will implicitly enable the
# native ANSI support in the console as a side-effect. We only need to actually
# replace sys.stdout/stderr if we're in the old-style conversion mode.
new_stdout = AnsiToWin32(sys.stdout, convert=None, strip=None, autoreset=False)
if new_stdout.convert:
sys.stdout = new_stdout
new_stderr = AnsiToWin32(sys.stderr, convert=None, strip=None, autoreset=False)
if new_stderr.convert:
sys.stderr = new_stderr

fixed_windows_console = True

@contextlib.contextmanager
def colorama_text(*args, **kwargs):
init(*args, **kwargs)
Expand All @@ -78,3 +115,7 @@ def wrap_stream(stream, convert, strip, autoreset, wrap):
if wrapper.should_wrap():
stream = wrapper.stream
return stream


# Use this for initial setup as well, to reduce code duplication
_wipe_internal_state_for_tests()
1 change: 1 addition & 0 deletions src/pip/_vendor/colorama/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
76 changes: 76 additions & 0 deletions src/pip/_vendor/colorama/tests/ansi_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file.
import sys
from unittest import TestCase, main

from ..ansi import Back, Fore, Style
from ..ansitowin32 import AnsiToWin32

stdout_orig = sys.stdout
stderr_orig = sys.stderr


class AnsiTest(TestCase):

def setUp(self):
# sanity check: stdout should be a file or StringIO object.
# It will only be AnsiToWin32 if init() has previously wrapped it
self.assertNotEqual(type(sys.stdout), AnsiToWin32)
self.assertNotEqual(type(sys.stderr), AnsiToWin32)

def tearDown(self):
sys.stdout = stdout_orig
sys.stderr = stderr_orig


def testForeAttributes(self):
self.assertEqual(Fore.BLACK, '\033[30m')
self.assertEqual(Fore.RED, '\033[31m')
self.assertEqual(Fore.GREEN, '\033[32m')
self.assertEqual(Fore.YELLOW, '\033[33m')
self.assertEqual(Fore.BLUE, '\033[34m')
self.assertEqual(Fore.MAGENTA, '\033[35m')
self.assertEqual(Fore.CYAN, '\033[36m')
self.assertEqual(Fore.WHITE, '\033[37m')
self.assertEqual(Fore.RESET, '\033[39m')

# Check the light, extended versions.
self.assertEqual(Fore.LIGHTBLACK_EX, '\033[90m')
self.assertEqual(Fore.LIGHTRED_EX, '\033[91m')
self.assertEqual(Fore.LIGHTGREEN_EX, '\033[92m')
self.assertEqual(Fore.LIGHTYELLOW_EX, '\033[93m')
self.assertEqual(Fore.LIGHTBLUE_EX, '\033[94m')
self.assertEqual(Fore.LIGHTMAGENTA_EX, '\033[95m')
self.assertEqual(Fore.LIGHTCYAN_EX, '\033[96m')
self.assertEqual(Fore.LIGHTWHITE_EX, '\033[97m')


def testBackAttributes(self):
self.assertEqual(Back.BLACK, '\033[40m')
self.assertEqual(Back.RED, '\033[41m')
self.assertEqual(Back.GREEN, '\033[42m')
self.assertEqual(Back.YELLOW, '\033[43m')
self.assertEqual(Back.BLUE, '\033[44m')
self.assertEqual(Back.MAGENTA, '\033[45m')
self.assertEqual(Back.CYAN, '\033[46m')
self.assertEqual(Back.WHITE, '\033[47m')
self.assertEqual(Back.RESET, '\033[49m')

# Check the light, extended versions.
self.assertEqual(Back.LIGHTBLACK_EX, '\033[100m')
self.assertEqual(Back.LIGHTRED_EX, '\033[101m')
self.assertEqual(Back.LIGHTGREEN_EX, '\033[102m')
self.assertEqual(Back.LIGHTYELLOW_EX, '\033[103m')
self.assertEqual(Back.LIGHTBLUE_EX, '\033[104m')
self.assertEqual(Back.LIGHTMAGENTA_EX, '\033[105m')
self.assertEqual(Back.LIGHTCYAN_EX, '\033[106m')
self.assertEqual(Back.LIGHTWHITE_EX, '\033[107m')


def testStyleAttributes(self):
self.assertEqual(Style.DIM, '\033[2m')
self.assertEqual(Style.NORMAL, '\033[22m')
self.assertEqual(Style.BRIGHT, '\033[1m')


if __name__ == '__main__':
main()
Loading

0 comments on commit c81ab26

Please sign in to comment.