Skip to content

Commit

Permalink
Merge pull request #444 from dmbuck32/fix/unbound-local-error
Browse files Browse the repository at this point in the history
Ensure nprocess exists when table is empty.
  • Loading branch information
rbonghi authored Dec 25, 2023
2 parents 49d6fcd + 6008d37 commit e163a96
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions jtop/gui/lib/process_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ def draw(self, pos_y, pos_x, width, height, key, mouse):
# Sort table for selected line
sorted_processes = sorted(processes, key=lambda x: x[self.line_sort], reverse=self.type_reverse)
# Draw all processes
# Instantiate the number of process variable to avoid an unbound local error if the process table is empty.
nprocess = 0
for nprocess, process in enumerate(sorted_processes):
# Skip unit size process
counter = 0
Expand Down
63 changes: 63 additions & 0 deletions jtop/tests_gui/test_process_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: UTF-8 -*-
# This file is part of the jetson_stats package (https://github.com/rbonghi/jetson_stats or http://rnext.it).
# Copyright (c) 2019-2023 Raffaello Bonghi.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import curses
# GUI jtop interface
from ..gui import JTOPGUI, Page
from ..gui.lib.process_table import ProcessTable
# jtop client
from ..jtop import jtop

# How to run
# python3 -m jtop.tests_gui.test_process_table


class TestJtop(jtop):
def __init__(self):
super(TestJtop, self).__init__()

@property
def processes(self):
''' Return an empty list of processes for testing. '''
return []


class TestPage(Page):

def __init__(self, stdscr, jetson):
super(TestPage, self).__init__("Test", stdscr, jetson)

self.process_table = ProcessTable(stdscr, jetson)

def draw(self, key, mouse):
# Screen size
height, width, first = self.size_page()
line_counter = first + 1

# Draw process table
line_counter += self.process_table.draw(line_counter, 0, width, height, key, mouse)


def main():

with TestJtop() as jetson:
curses.wrapper(JTOPGUI, jetson, [TestPage])


if __name__ == "__main__":
main()
# EOF

0 comments on commit e163a96

Please sign in to comment.