From 98b81dc67fc18fe3aaee1bd6d0a61540401f2400 Mon Sep 17 00:00:00 2001 From: Polys Georgiou Date: Sat, 31 Aug 2019 13:46:23 +0100 Subject: [PATCH] Add lock for thread-safe access to winterm --- colorama/ansitowin32.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/colorama/ansitowin32.py b/colorama/ansitowin32.py index 359c92b..0a4e529 100644 --- a/colorama/ansitowin32.py +++ b/colorama/ansitowin32.py @@ -2,6 +2,7 @@ import re import sys import os +from threading import Lock from .ansi import AnsiFore, AnsiBack, AnsiStyle, Style from .winterm import WinTerm, WinColor, WinStyle @@ -12,6 +13,7 @@ if windll is not None: winterm = WinTerm() +winterm_lock = Lock() class StreamWrapper(object): ''' @@ -158,13 +160,17 @@ def get_win32_calls(self): return dict() def write(self, text): - if self.strip or self.convert: - self.write_and_convert(text) - else: - self.wrapped.write(text) - self.wrapped.flush() - if self.autoreset: - self.reset_all() + try: + winterm_lock.acquire() + if self.strip or self.convert: + self.write_and_convert(text) + else: + self.wrapped.write(text) + self.wrapped.flush() + if self.autoreset: + self.reset_all() + finally: + winterm_lock.release() def reset_all(self):