From f587b281c0878e29a44dac44ca078fc6b900fae9 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sat, 20 Dec 2025 15:33:17 +0100 Subject: [PATCH] Catch error on ADM GPU plugin and Python 3.9 --- glances/plugins/gpu/__init__.py | 25 +++++++++++++++++++------ tests/test_core.py | 6 +----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/glances/plugins/gpu/__init__.py b/glances/plugins/gpu/__init__.py index a87a3810..eb7ab9c1 100644 --- a/glances/plugins/gpu/__init__.py +++ b/glances/plugins/gpu/__init__.py @@ -15,6 +15,7 @@ Currently supported: """ from glances.globals import to_fahrenheit +from glances.logger import logger from glances.plugins.gpu.cards.amd import AmdGPU from glances.plugins.gpu.cards.nvidia import NvidiaGPU from glances.plugins.plugin.model import GlancesPluginModel @@ -73,11 +74,21 @@ class GpuPlugin(GlancesPluginModel): stats_init_value=[], fields_description=fields_description, ) - # Init the GPU API - self.nvidia = NvidiaGPU() - self.amd = AmdGPU() + # Init the Nvidia GPU API + try: + self.nvidia = NvidiaGPU() + except Exception as e: + logger.debug(f'Nvidia GPU initialization error: {e}') + self.nvidia = None + + # Init the AMD GPU API # Just for test purpose (uncomment to test on computer without AMD GPU) # self.amd = AmdGPU(drm_root_folder='./tests-data/plugins/gpu/amd/sys/class/drm') + try: + self.amd = AmdGPU() + except Exception as e: + logger.debug(f'AMD GPU initialization error: {e}') + self.amd = None # We want to display the stat in the curse interface self.display_curse = True @@ -102,11 +113,13 @@ class GpuPlugin(GlancesPluginModel): stats = self.get_init_value() # Get the stats - stats.extend(self.nvidia.get_device_stats()) - stats.extend(self.amd.get_device_stats()) + if self.nvidia: + stats.extend(self.nvidia.get_device_stats()) + if self.amd: + stats.extend(self.amd.get_device_stats()) # !!! - # Uncomment to test on computer without GPU + # Uncomment to test on computer without Nvidia GPU # One GPU sample: # stats = [ # { diff --git a/tests/test_core.py b/tests/test_core.py index 0099f19c..8c5cf8fd 100755 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -365,11 +365,7 @@ class TestGlances(unittest.TestCase): def test_014_gpu(self): """Check GPU plugin.""" print('INFO: [TEST_014] Check GPU stats') - gpu_plugin = stats.get_plugin('gpu') - if not gpu_plugin: - print('INFO: GPU plugin is disabled, skipping test') - return - stats_grab = gpu_plugin.get_raw() + stats_grab = stats.get_plugin('gpu').get_raw() self.assertTrue(isinstance(stats_grab, list), msg='GPU stats is not a list') print(f'INFO: GPU stats: {stats_grab}')