使用诅咒的Python脚本在macOS上失败
我正在尝试使用Python 3.6.4从存储库运行脚本。它主要是为了演示Python的使用诅咒的Python脚本在macOS上失败,macos,ncurses,curses,python-curses,Macos,Ncurses,Curses,Python Curses,我正在尝试使用Python 3.6.4从存储库运行脚本。它主要是为了演示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)
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
我不知道这个addwstr()
函数来自哪里
有人能解释一下吗?关于如何在macOS上运行它,有什么想法吗?屏幕大小可能是相关的,因为它假设在循环结束时将
lineno
变量重置为零之前会写入一整套消息
print\u line
函数会引发异常,而不是检查可能导致curses addstr返回错误的条件,从而加剧这种情况:
- 行太行了
- 穿过屏幕末端的线
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)
这并不深入(因为脚本还假设屏幕总是足够宽)
它是调用还是在下面并不重要,因为两者都进行相同类型的检查。Hmm,我在我的Mac上运行了这个(Python 3.6.0,但我想已经足够接近了),它对我来说工作得很好。@cag51这很奇怪。我甚至在一个新安装的macOS(在虚拟机中)上,使用内置的Python2.7.10,得到了上述相同的结果。碰巧我在Mac电脑上的接口比Ubuntu电脑上的接口多得多,所以在Mac电脑上,终端不够大,无法容纳所有接口的输出,但在Ubuntu电脑上,输出要小得多,因此适合而不会导致错误。无论是macOS还是Ubuntu都是巧合。
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)