Python 使用 curses 的脚本在 macOS 上失败
Python script using curses fails on macOS
我正在尝试使用 Python 3.6.4 运行 nettop.py
script from the psutil
存储库。它主要是为了演示 Python 的 psutil
模块的用法。
它在 Ubuntu 上运行良好,但 运行在 macOS 上运行它失败并出现以下错误:
Traceback (most recent call last):
File "nettop.py", line 167, in <module>
main()
File "nettop.py", line 160, in main
refresh_window(*args)
File "nettop.py", line 138, in refresh_window
stats_after.bytes_recv - stats_before.bytes_recv) + '/s',
File "nettop.py", line 67, in print_line
win.addstr(lineno, 0, line, 0)
_curses.error: addwstr() returned ERR
Traceback 最后一行中的 win
对象在 nettop.py:53
中定义并且来自 curses
模块:
win = curses.initscr()
我不知道 addwstr()
函数的来源。
有人可以解释一下吗?关于如何在 macOS 上 运行 有什么想法吗?
屏幕尺寸可能是相关的,因为它假设在将 lineno
变量重置为零之前将写入一整套消息循环。
print_line
函数通过引发异常而不是检查会导致诅咒的条件来加剧这种情况 addstr return 一个错误:
- 线太线
- 超过屏幕末尾的行
例如,这里有一个(粗略的)解决方法:
diff --git a/scripts/nettop.py b/scripts/nettop.py
index e13903c1..d263eb99 100755
--- a/scripts/nettop.py
+++ b/scripts/nettop.py
@@ -59,9 +59,11 @@ lineno = 0
def print_line(line, highlight=False):
"""A thin wrapper around curses's addstr()."""
global lineno
+ if lineno >= win.getmaxyx()[0]:
+ lineno = win.getmaxyx()[0] - 1
try:
if highlight:
- line += " " * (win.getmaxyx()[1] - len(line))
+ line += " " * (win.getmaxyx()[1] - len(line) - 1)
win.addstr(lineno, 0, line, curses.A_REVERSE)
else:
win.addstr(lineno, 0, line, 0)
不会很深(因为脚本还假设屏幕总是足够宽)。
我正在尝试使用 Python 3.6.4 运行 nettop.py
script from the psutil
存储库。它主要是为了演示 Python 的 psutil
模块的用法。
它在 Ubuntu 上运行良好,但 运行在 macOS 上运行它失败并出现以下错误:
Traceback (most recent call last):
File "nettop.py", line 167, in <module>
main()
File "nettop.py", line 160, in main
refresh_window(*args)
File "nettop.py", line 138, in refresh_window
stats_after.bytes_recv - stats_before.bytes_recv) + '/s',
File "nettop.py", line 67, in print_line
win.addstr(lineno, 0, line, 0)
_curses.error: addwstr() returned ERR
Traceback 最后一行中的 win
对象在 nettop.py:53
中定义并且来自 curses
模块:
win = curses.initscr()
我不知道 addwstr()
函数的来源。
有人可以解释一下吗?关于如何在 macOS 上 运行 有什么想法吗?
屏幕尺寸可能是相关的,因为它假设在将 lineno
变量重置为零之前将写入一整套消息循环。
print_line
函数通过引发异常而不是检查会导致诅咒的条件来加剧这种情况 addstr return 一个错误:
- 线太线
- 超过屏幕末尾的行
例如,这里有一个(粗略的)解决方法:
diff --git a/scripts/nettop.py b/scripts/nettop.py
index e13903c1..d263eb99 100755
--- a/scripts/nettop.py
+++ b/scripts/nettop.py
@@ -59,9 +59,11 @@ lineno = 0
def print_line(line, highlight=False):
"""A thin wrapper around curses's addstr()."""
global lineno
+ if lineno >= win.getmaxyx()[0]:
+ lineno = win.getmaxyx()[0] - 1
try:
if highlight:
- line += " " * (win.getmaxyx()[1] - len(line))
+ line += " " * (win.getmaxyx()[1] - len(line) - 1)
win.addstr(lineno, 0, line, curses.A_REVERSE)
else:
win.addstr(lineno, 0, line, 0)
不会很深(因为脚本还假设屏幕总是足够宽)。