[PATCH] color: reset win32 console color in a finally block

Idan Kamara idankk86 at gmail.com
Sat Apr 9 15:55:08 CDT 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1302382403 -10800
# Node ID ec57b9202bb7a9cc7f744fd177bef8826fb992c2
# Parent  36a2b3ffdd067917fc8e0a2da94d3567270058a3
color: reset win32 console color in a finally block

diff -r 36a2b3ffdd06 -r ec57b9202bb7 hgext/color.py
--- a/hgext/color.py	Sat Apr 09 23:53:10 2011 +0300
+++ b/hgext/color.py	Sat Apr 09 23:53:23 2011 +0300
@@ -349,13 +349,15 @@
 
         # Look for ANSI-like codes embedded in text
         m = re.match(ansire, text)
-        while m:
-            for sattr in m.group(1).split(';'):
-                if sattr:
-                    attr = mapcolor(int(sattr), attr)
-            _kernel32.SetConsoleTextAttribute(stdout, attr)
-            orig(m.group(2), **opts)
-            m = re.match(ansire, m.group(3))
 
-        # Explicity reset original attributes
-        _kernel32.SetConsoleTextAttribute(stdout, origattr)
+        try:
+            while m:
+                for sattr in m.group(1).split(';'):
+                    if sattr:
+                        attr = mapcolor(int(sattr), attr)
+                _kernel32.SetConsoleTextAttribute(stdout, attr)
+                orig(m.group(2), **opts)
+                m = re.match(ansire, m.group(3))
+        finally:
+            # Explicity reset original attributes
+            _kernel32.SetConsoleTextAttribute(stdout, origattr)


More information about the Mercurial-devel mailing list