[PATCH 1 of 5 stable V2] crecord: use try/except for import of curses

Sean Farley sean at farley.io
Wed Dec 16 18:46:23 UTC 2015


# HG changeset patch
# User Sean Farley <sean at farley.io>
# Date 1450290799 28800
#      Wed Dec 16 10:33:19 2015 -0800
# Branch stable
# Node ID 9cc732d13ec2938638a80be90c4deac0ca52dbb2
# Parent  59d5f619e69ec43f1957eddd85d4e1deddd64925
crecord: use try/except for import of curses

Not only does this improve fragility with 'if os.name == ...' it will help
future patches enable the behavior to fallback to use plain record when curses
is unavailable (e.g. python compiled without curses support).

diff --git a/mercurial/crecord.py b/mercurial/crecord.py
--- a/mercurial/crecord.py
+++ b/mercurial/crecord.py
@@ -28,19 +28,22 @@ from . import (
 
 # This is required for ncurses to display non-ASCII characters in default user
 # locale encoding correctly.  --immerrr
 locale.setlocale(locale.LC_ALL, '')
 
-# os.name is one of: 'posix', 'nt', 'dos', 'os2', 'mac', or 'ce'
-if os.name == 'posix':
+try:
     import curses
     import fcntl
     import termios
-else:
+    curses.error
+    fcntl.ioctl
+    termios.TIOCGWINSZ
+except ImportError:
     # I have no idea if wcurses works with crecord...
     try:
         import wcurses as curses
+        curses.error
     except ImportError:
         # wcurses is not shipped on Windows by default
         pass
 
 try:


More information about the Mercurial-devel mailing list