diff mbox series

[v2,1/4] bitbake: progressbar: Check resizable file descriptor.

Message ID 20250122232411.133-2-egyszeregy@freemail.hu
State New
Headers show
Series bitbake: knotty/progressbar: Optimize footer update and prints. | expand

Commit Message

Livius Jan. 22, 2025, 11:24 p.m. UTC
s=20181004; d=freemail.hu;

	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding;

	l=2110; bh=FpTb7TZgCeBDZ6gBQVX9Oz9GJT0bDbIFEUtYaCztHxs=;

	b=GTkEGhTMvm6LL73h+Mrq96Z97vvy2b0TNGGFSdzDK05E7pJ9Lhh6tg4jvh8T8ipO

	hGSnvhx2M2Ko10CnODHOn+VTAli/DTWk8egU3cnUXyUsXEOkRALLLHqvWY3f5snAKaH

	Dii0cxhBcGM+fkDKAHc8YxnF34AX3t6SiUYui4YVLU9KpU/bzl9yv9uYqvKI9oQYEmM

	Ri6hzbgAy72E0nCEEdns/M1MMCJFPX7/n82z64w9ILCdqXgwZCaRknpBh8uATziT+Dk

	idhhtzVulaLNI8kdML7GhFq2aFG6CSfdvVuCY2tjSdLNR8PSXYPCuWZQ2uojixtSINp

	vCngpnaa8Q==
Content-Transfer-Encoding: quoted-printable

From: Benjamin Sz=C5=91ke <egyszeregy@freemail.hu>

- Check if given file descriptor is resizable.
- Introduce self._fd_console as a dedicated attribute of self._handle_res=
ize().

Signed-off-by: Benjamin Sz=C5=91ke <egyszeregy@freemail.hu>
---
 lib/progressbar/progressbar.py | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

=20
         self.__iterable =3D None
         self._update_widgets()
@@ -182,7 +189,7 @@ class ProgressBar(object):
     def _handle_resize(self, signum=3DNone, frame=3DNone):
         """Tries to catch resize signals sent from the terminal."""
=20
-        h, w =3D array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8)=
)[:2]
+        h, w =3D array('h', ioctl(self._fd_console, termios.TIOCGWINSZ, =
'\0' * 8))[:2]
         self.term_width =3D w
=20
=20
--=20
2.47.1.windows.2

Comments

Richard Purdie Jan. 22, 2025, 11:58 p.m. UTC | #1
On Thu, 2025-01-23 at 00:24 +0100, Livius via lists.openembedded.org wrote:
> From: Benjamin Szőke <egyszeregy@freemail.hu>
> 
> - Check if given file descriptor is resizable.
> - Introduce self._fd_console as a dedicated attribute of self._handle_resize().
> 
> Signed-off-by: Benjamin Szőke <egyszeregy@freemail.hu>
> ---
>  lib/progressbar/progressbar.py | 25 ++++++++++++++++---------
>  1 file changed, 16 insertions(+), 9 deletions(-)

The existing code checked if a resize was possible and trapped the
exception if not.

The new code assumes stdout/stderr are resizeable and doesn't have an
exception if not. The commit does not say why either change is
necessary or why they're an improvement. It removes the check, contra
to what the commit message says.

Cheers,

Richard


> diff --git a/lib/progressbar/progressbar.py b/lib/progressbar/progressbar.py
> index d4da10ab7..203ff804f 100644
> --- a/lib/progressbar/progressbar.py
> +++ b/lib/progressbar/progressbar.py
> @@ -110,19 +110,26 @@ class ProgressBar(object):
>          self.widgets = widgets
>          self.fd = fd
>          self.left_justify = left_justify
> +        self._fd_console = None
> +
> +        fd_num = fd.fileno()
>  
>          self.signal_set = False
>          if term_width is not None:
>              self.term_width = term_width
> +        elif (fd_num == sys.stdout.fileno()) or (fd_num == sys.stderr.fileno()):
> +            # Check if given file descriptor is resizable for example belong
> +            # to a terminal/console as STDOUT or STDERR. If file descriptor
> +            # is resizable, let's allow to use for self._handle_resize()
> +            # in a dedicated self._fd_console in order to be able to set
> +            # temporarily/permanently self.fd to any StringIO or other
> +            # file descriptor later.
> +            self._fd_console = fd
> +            self._handle_resize(None, None)
> +            signal.signal(signal.SIGWINCH, self._handle_resize)
> +            self.signal_set = True
>          else:
> -            try:
> -                self._handle_resize(None, None)
> -                signal.signal(signal.SIGWINCH, self._handle_resize)
> -                self.signal_set = True
> -            except (SystemExit, KeyboardInterrupt): raise
> -            except Exception as e:
> -                print("DEBUG 5 %s" % e)
> -                self.term_width = self._env_size()
> +            self.term_width = self._env_size()
>  
>          self.__iterable = None
>          self._update_widgets()
> @@ -182,7 +189,7 @@ class ProgressBar(object):
>      def _handle_resize(self, signum=None, frame=None):
>          """Tries to catch resize signals sent from the terminal."""
>  
> -        h, w = array('h', ioctl(self.fd, termios.TIOCGWINSZ, '\0' * 8))[:2]
> +        h, w = array('h', ioctl(self._fd_console, termios.TIOCGWINSZ, '\0' * 8))[:2]
>          self.term_width = w
>
diff mbox series

Patch

diff --git a/lib/progressbar/progressbar.py b/lib/progressbar/progressbar=
.py
index d4da10ab7..203ff804f 100644
--- a/lib/progressbar/progressbar.py
+++ b/lib/progressbar/progressbar.py
@@ -110,19 +110,26 @@  class ProgressBar(object):
         self.widgets =3D widgets
         self.fd =3D fd
         self.left_justify =3D left_justify
+        self._fd_console =3D None
+
+        fd_num =3D fd.fileno()
=20
         self.signal_set =3D False
         if term_width is not None:
             self.term_width =3D term_width
+        elif (fd_num =3D=3D sys.stdout.fileno()) or (fd_num =3D=3D sys.s=
tderr.fileno()):
+            # Check if given file descriptor is resizable for example be=
long
+            # to a terminal/console as STDOUT or STDERR. If file descrip=
tor
+            # is resizable, let's allow to use for self._handle_resize()
+            # in a dedicated self._fd_console in order to be able to set
+            # temporarily/permanently self.fd to any StringIO or other
+            # file descriptor later.
+            self._fd_console =3D fd
+            self._handle_resize(None, None)
+            signal.signal(signal.SIGWINCH, self._handle_resize)
+            self.signal_set =3D True
         else:
-            try:
-                self._handle_resize(None, None)
-                signal.signal(signal.SIGWINCH, self._handle_resize)
-                self.signal_set =3D True
-            except (SystemExit, KeyboardInterrupt): raise
-            except Exception as e:
-                print("DEBUG 5 %s" % e)
-                self.term_width =3D self._env_size()
+            self.term_width =3D self._env_size()