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

login
register
mail settings
Submitter Sean Farley
Date Dec. 16, 2015, 6:46 p.m.
Message ID <9cc732d13ec2938638a8.1450291583@laptop.office.atlassian.com>
Download mbox | patch
Permalink /patch/12077/
State Accepted
Headers show

Comments

Sean Farley - Dec. 16, 2015, 6:46 p.m.
# HG changeset patch
# User Sean Farley <sean@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).

Patch

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: