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

login
register
mail settings
Submitter Sean Farley
Date Dec. 16, 2015, 12:06 a.m.
Message ID <7ceba88969e51bb82574.1450224372@laptop.office.atlassian.com>
Download mbox | patch
Permalink /patch/12063/
State Superseded
Commit 7cc654610204bc495d8082e8d52180e87b0097ae
Delegated to: Laurent Charignon
Headers show

Comments

Sean Farley - Dec. 16, 2015, 12:06 a.m.
# HG changeset patch
# User Sean Farley <sean@farley.io>
# Date 1450223509 28800
#      Tue Dec 15 15:51:49 2015 -0800
# Branch stable
# Node ID 7ceba88969e51bb825748f69b45e4677e0a02815
# 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).
Yuya Nishihara - Dec. 16, 2015, 3:45 p.m.
On Tue, 15 Dec 2015 16:06:12 -0800, Sean Farley wrote:
> # HG changeset patch
> # User Sean Farley <sean@farley.io>
> # Date 1450223509 28800
> #      Tue Dec 15 15:51:49 2015 -0800
> # Branch stable
> # Node ID 7ceba88969e51bb825748f69b45e4677e0a02815
> # 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,16 +28,15 @@ 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:
> +except ImportError:

If demandimport is enabled, these imports won't raise ImportError here.

(I didn't read the patches carefully, so I might be wrong.)
Sean Farley - Dec. 16, 2015, 6:21 p.m.
Yuya Nishihara <yuya@tcha.org> writes:

> On Tue, 15 Dec 2015 16:06:12 -0800, Sean Farley wrote:
>> # HG changeset patch
>> # User Sean Farley <sean@farley.io>
>> # Date 1450223509 28800
>> #      Tue Dec 15 15:51:49 2015 -0800
>> # Branch stable
>> # Node ID 7ceba88969e51bb825748f69b45e4677e0a02815
>> # 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,16 +28,15 @@ 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:
>> +except ImportError:
>
> If demandimport is enabled, these imports won't raise ImportError here.
>
> (I didn't read the patches carefully, so I might be wrong.)

Ah, good catch. I'll send a V2 that fixes that.

Patch

diff --git a/mercurial/crecord.py b/mercurial/crecord.py
--- a/mercurial/crecord.py
+++ b/mercurial/crecord.py
@@ -28,16 +28,15 @@  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:
+except ImportError:
     # I have no idea if wcurses works with crecord...
     try:
         import wcurses as curses
     except ImportError:
         # wcurses is not shipped on Windows by default