From 2a27cb5e0cea921df6c75dec4c24409f1c75a9db Mon Sep 17 00:00:00 2001 From: tomeichlersmith Date: Mon, 22 Jan 2024 10:31:33 -0600 Subject: [PATCH] explicitly check if the config is readable before continuing Previously, loading a config as a python module reported this failure to exist check for us rather nicely and so we didn't need to worry about doing it ourselves. Now, we need to do it ourselves since the Py_RunAnyFile* functions do not check for existence manually and will fail strongly if they are given a nullptr. --- src/Framework/ConfigurePython.cxx | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Framework/ConfigurePython.cxx b/src/Framework/ConfigurePython.cxx index a00fd11..6188c50 100644 --- a/src/Framework/ConfigurePython.cxx +++ b/src/Framework/ConfigurePython.cxx @@ -275,6 +275,17 @@ ConfigurePython::ConfigurePython(const std::string& pythonScript, char* args[], // assumes that nargs >= 0 // this is true always because we error out if no python script has been // found + + // load a handle to the config file into memory (and check that it exists) + FILE* config_file{fopen(pythonScript.c_str(), "r")}; + if (config_file == NULL) { + EXCEPTION_RAISE( + "ConfigDNE", + "Passed config script '"+pythonScript+"' is not accessible.\n" + " Did you make a typo in the path to the script?\n" + " Are you referencing a directory that is not mounted to the container?" + ); + } std::string cmd = pythonScript; if (pythonScript.rfind("/") != std::string::npos) { @@ -302,8 +313,6 @@ ConfigurePython::ConfigurePython(const std::string& pythonScript, char* args[], // the name of the python script PySys_SetArgvEx(nargs + 1, targs, 1); - // load a handle to the config file into memory - FILE* config_file{fopen(pythonScript.c_str(), "r")}; // run the file as a python script if (PyRun_AnyFile(config_file, pythonScript.c_str()) != 0) { PyErr_Print();