Skip to content

Commit 378bd01

Browse files
committed
Fix ppaged UnboundLocalError on Windows
Split the try/except block in 'ppaged' to handle 'ImportError' for 'termios' separately. This prevents 'UnboundLocalError' when accessing 'termios.error' in the 'except' clause on platforms where 'termios' is not available (like Windows).
1 parent d51f2b8 commit 378bd01

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

cmd2/cmd2.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,22 +1617,25 @@ def ppaged(
16171617
try:
16181618
import signal
16191619
import termios
1620-
1621-
# Ensure we are in the foreground process group
1622-
if hasattr(os, 'tcsetpgrp') and hasattr(os, 'getpgrp'):
1623-
# Ignore SIGTTOU to avoid getting stopped when calling tcsetpgrp from background
1624-
old_handler = signal.signal(signal.SIGTTOU, signal.SIG_IGN)
1625-
try:
1626-
os.tcsetpgrp(self.stdin.fileno(), os.getpgrp())
1627-
finally:
1628-
signal.signal(signal.SIGTTOU, old_handler)
1629-
1630-
# Restore terminal attributes
1631-
if self._initial_termios_settings is not None:
1632-
termios.tcsetattr(self.stdin.fileno(), termios.TCSANOW, self._initial_termios_settings)
1633-
1634-
except (ImportError, OSError, termios.error):
1620+
except ImportError:
16351621
pass
1622+
else:
1623+
try:
1624+
# Ensure we are in the foreground process group
1625+
if hasattr(os, 'tcsetpgrp') and hasattr(os, 'getpgrp'):
1626+
# Ignore SIGTTOU to avoid getting stopped when calling tcsetpgrp from background
1627+
old_handler = signal.signal(signal.SIGTTOU, signal.SIG_IGN)
1628+
try:
1629+
os.tcsetpgrp(self.stdin.fileno(), os.getpgrp())
1630+
finally:
1631+
signal.signal(signal.SIGTTOU, old_handler)
1632+
1633+
# Restore terminal attributes
1634+
if self._initial_termios_settings is not None:
1635+
termios.tcsetattr(self.stdin.fileno(), termios.TCSANOW, self._initial_termios_settings)
1636+
1637+
except (OSError, termios.error):
1638+
pass
16361639

16371640
else:
16381641
self.poutput(

0 commit comments

Comments
 (0)