From 5f40935da24c3addfcf9a9fcfb142eac25aa3a48 Mon Sep 17 00:00:00 2001 From: "Bobby R. Bruce" Date: Thu, 1 Sep 2022 18:06:38 -0700 Subject: [PATCH] stdlib: Add 'common.Options' as a banned stdlib module This commit adds the concept of a "banned module" to the stdlib. This blocks the user from importing modules from elsewhere in the project with known incompatibility to the stdlib. 'common.Options' has been added to this as 'common.Options' will import options to an stdlib run which are not supported. Issue-on: https://gem5.atlassian.net/browse/GEM5-1282 Change-Id: I8f2b1e24d03fab2872c735342dc8a1ff6528fb5d Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/63071 Reviewed-by: Jason Lowe-Power Tested-by: kokoro Maintainer: Jason Lowe-Power --- src/python/gem5/simulate/simulator.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/python/gem5/simulate/simulator.py b/src/python/gem5/simulate/simulator.py index 52a166c73b..b6474a41d6 100644 --- a/src/python/gem5/simulate/simulator.py +++ b/src/python/gem5/simulate/simulator.py @@ -32,6 +32,7 @@ from m5.objects import Root from m5.util import warn import os +import sys from pathlib import Path from typing import Optional, List, Tuple, Dict, Generator, Union @@ -70,6 +71,16 @@ class Simulator: This will run a simulation and execute default behavior for exit events. """ + # Here we declare the modules which should not be imported into any gem5 + # standard library run. The key is the module (e.g, + # "import common.Options") and the value is the reason, which will be + # output in the case this module is imported. + # This is checked with the `run` function is executed. + _banned_modules = { + "common.Options": "The options provided by 'Options' are not " + "compatible with the gem5 standard library.", + } + def __init__( self, board: AbstractBoard, @@ -344,6 +355,15 @@ class Simulator: reset. This is the **maximum number of ticks per simululation run**. """ + # Check to ensure no banned module has been imported. + for banned_module in self._banned_modules.keys(): + if banned_module in sys.modules: + raise Exception( + f"The banned module '{banned_module}' has been included. " + "Please do not use this in your simulations. " + f"Reason: {self._banned_modules[banned_module]}" + ) + # We instantiate the board if it has not already been instantiated. self._instantiate()