Patchwork [3,of,4] progress: obtain stderr from ui

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 8, 2016, 2:47 p.m.
Message ID <2d4f257e45ea19cc7191.1478616447@mimosa>
Download mbox | patch
Permalink /patch/17404/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 8, 2016, 2:47 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1476969168 -32400
#      Thu Oct 20 22:12:48 2016 +0900
# Node ID 2d4f257e45ea19cc71912f285a090036deeacb40
# Parent  e9cec005658fc72458c49965e8c52b4b5ede0309
progress: obtain stderr from ui

This will help Python 3 porting.
Gregory Szorc - Nov. 8, 2016, 3:31 p.m.
> On Nov 8, 2016, at 06:47, Yuya Nishihara <yuya@tcha.org> wrote:
> 
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1476969168 -32400
> #      Thu Oct 20 22:12:48 2016 +0900
> # Node ID 2d4f257e45ea19cc71912f285a090036deeacb40
> # Parent  e9cec005658fc72458c49965e8c52b4b5ede0309
> progress: obtain stderr from ui
> 
> This will help Python 3 porting.
> 
> diff --git a/mercurial/progress.py b/mercurial/progress.py
> --- a/mercurial/progress.py
> +++ b/mercurial/progress.py
> @@ -7,7 +7,6 @@
> 
> from __future__ import absolute_import
> 
> -import sys
> import threading
> import time
> 
> @@ -19,7 +18,7 @@ def spacejoin(*args):
> 
> def shouldprint(ui):
>     return not (ui.quiet or ui.plain('progress')) and (
> -        ui._isatty(sys.stderr) or ui.configbool('progress', 'assume-tty'))
> +        ui._isatty(ui.ferr) or ui.configbool('progress', 'assume-tty'))

This is the only part of the series I could see as problematic. But as long as ferr is always a file descriptor and not some proxy object, this should be fine.

> 
> def fmtremaining(seconds):
>     """format a number of remaining seconds in human readable way
> @@ -158,14 +157,14 @@ class progbar(object):
>             out = spacejoin(head, prog, tail)
>         else:
>             out = spacejoin(head, tail)
> -        sys.stderr.write('\r' + encoding.trim(out, termwidth))
> +        self.ui.ferr.write('\r' + encoding.trim(out, termwidth))
>         self.lasttopic = topic
> -        sys.stderr.flush()
> +        self.ui.ferr.flush()
> 
>     def clear(self):
>         if not self.printed or not self.lastprint or not shouldprint(self.ui):
>             return
> -        sys.stderr.write('\r%s\r' % (' ' * self.width()))
> +        self.ui.ferr.write('\r%s\r' % (' ' * self.width()))
>         if self.printed:
>             # force immediate re-paint of progress bar
>             self.lastprint = 0
> @@ -176,8 +175,8 @@ class progbar(object):
>         if self.ui.configbool('progress', 'clear-complete', default=True):
>             self.clear()
>         else:
> -            sys.stderr.write('\n')
> -        sys.stderr.flush()
> +            self.ui.ferr.write('\n')
> +        self.ui.ferr.flush()
> 
>     def width(self):
>         tw = self.ui.termwidth()
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Augie Fackler - Nov. 8, 2016, 4:03 p.m.
On Tue, Nov 08, 2016 at 07:31:31AM -0800, Gregory Szorc wrote:
>
>
> > On Nov 8, 2016, at 06:47, Yuya Nishihara <yuya@tcha.org> wrote:
> >
> > # HG changeset patch
> > # User Yuya Nishihara <yuya@tcha.org>
> > # Date 1476969168 -32400
> > #      Thu Oct 20 22:12:48 2016 +0900
> > # Node ID 2d4f257e45ea19cc71912f285a090036deeacb40
> > # Parent  e9cec005658fc72458c49965e8c52b4b5ede0309
> > progress: obtain stderr from ui
> >
> > This will help Python 3 porting.

Queued these, thanks.

> >
> > diff --git a/mercurial/progress.py b/mercurial/progress.py
> > --- a/mercurial/progress.py
> > +++ b/mercurial/progress.py
> > @@ -7,7 +7,6 @@
> >
> > from __future__ import absolute_import
> >
> > -import sys
> > import threading
> > import time
> >
> > @@ -19,7 +18,7 @@ def spacejoin(*args):
> >
> > def shouldprint(ui):
> >     return not (ui.quiet or ui.plain('progress')) and (
> > -        ui._isatty(sys.stderr) or ui.configbool('progress', 'assume-tty'))
> > +        ui._isatty(ui.ferr) or ui.configbool('progress', 'assume-tty'))
>
> This is the only part of the series I could see as problematic. But as long as ferr is always a file descriptor and not some proxy object, this should be fine.

Yeap. Squinting at it, we should catch the attribute error on any
proxy class and decide it's not a tty, so it should be fine.

>
> >
> > def fmtremaining(seconds):
> >     """format a number of remaining seconds in human readable way
> > @@ -158,14 +157,14 @@ class progbar(object):
> >             out = spacejoin(head, prog, tail)
> >         else:
> >             out = spacejoin(head, tail)
> > -        sys.stderr.write('\r' + encoding.trim(out, termwidth))
> > +        self.ui.ferr.write('\r' + encoding.trim(out, termwidth))
> >         self.lasttopic = topic
> > -        sys.stderr.flush()
> > +        self.ui.ferr.flush()
> >
> >     def clear(self):
> >         if not self.printed or not self.lastprint or not shouldprint(self.ui):
> >             return
> > -        sys.stderr.write('\r%s\r' % (' ' * self.width()))
> > +        self.ui.ferr.write('\r%s\r' % (' ' * self.width()))
> >         if self.printed:
> >             # force immediate re-paint of progress bar
> >             self.lastprint = 0
> > @@ -176,8 +175,8 @@ class progbar(object):
> >         if self.ui.configbool('progress', 'clear-complete', default=True):
> >             self.clear()
> >         else:
> > -            sys.stderr.write('\n')
> > -        sys.stderr.flush()
> > +            self.ui.ferr.write('\n')
> > +        self.ui.ferr.flush()
> >
> >     def width(self):
> >         tw = self.ui.termwidth()
> > _______________________________________________
> > Mercurial-devel mailing list
> > Mercurial-devel@mercurial-scm.org
> > https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel

Patch

diff --git a/mercurial/progress.py b/mercurial/progress.py
--- a/mercurial/progress.py
+++ b/mercurial/progress.py
@@ -7,7 +7,6 @@ 
 
 from __future__ import absolute_import
 
-import sys
 import threading
 import time
 
@@ -19,7 +18,7 @@  def spacejoin(*args):
 
 def shouldprint(ui):
     return not (ui.quiet or ui.plain('progress')) and (
-        ui._isatty(sys.stderr) or ui.configbool('progress', 'assume-tty'))
+        ui._isatty(ui.ferr) or ui.configbool('progress', 'assume-tty'))
 
 def fmtremaining(seconds):
     """format a number of remaining seconds in human readable way
@@ -158,14 +157,14 @@  class progbar(object):
             out = spacejoin(head, prog, tail)
         else:
             out = spacejoin(head, tail)
-        sys.stderr.write('\r' + encoding.trim(out, termwidth))
+        self.ui.ferr.write('\r' + encoding.trim(out, termwidth))
         self.lasttopic = topic
-        sys.stderr.flush()
+        self.ui.ferr.flush()
 
     def clear(self):
         if not self.printed or not self.lastprint or not shouldprint(self.ui):
             return
-        sys.stderr.write('\r%s\r' % (' ' * self.width()))
+        self.ui.ferr.write('\r%s\r' % (' ' * self.width()))
         if self.printed:
             # force immediate re-paint of progress bar
             self.lastprint = 0
@@ -176,8 +175,8 @@  class progbar(object):
         if self.ui.configbool('progress', 'clear-complete', default=True):
             self.clear()
         else:
-            sys.stderr.write('\n')
-        sys.stderr.flush()
+            self.ui.ferr.write('\n')
+        self.ui.ferr.flush()
 
     def width(self):
         tw = self.ui.termwidth()