diff --git a/Device/Data/Datafield.cpp b/Device/Data/Datafield.cpp index d79a326dbf13c8356ec4eaeca1b4de27fa836701..cd22ebc69e941b5f7fd03651ffcc27f998137aff 100644 --- a/Device/Data/Datafield.cpp +++ b/Device/Data/Datafield.cpp @@ -205,13 +205,13 @@ Datafield* Datafield::crop(double xmin, double xmax) const #ifdef BORNAGAIN_PYTHON -#include "PyCore/Embed/PythonInterpreter.h" // Numpy::arrayND, Numpy::getDataPtr +#include "PyCore/Embed/PyInterpreter.h" // Numpy::arrayND, Numpy::getDataPtr PyObject* Datafield::npArray() const { // TODO: Thoroughly check this function regarding index manipulations - PythonInterpreter::Numpy::initialize(); + PyInterpreter::Numpy::initialize(); ASSERT(rank() <= 2); @@ -224,12 +224,12 @@ PyObject* Datafield::npArray() const std::swap(dimensions[0], dimensions[1]); // creating ndarray objects describing size of dimensions - PyObjectPtr pyarray{PythonInterpreter::Numpy::arrayND(dimensions)}; + PyObjectPtr pyarray{PyInterpreter::Numpy::arrayND(dimensions)}; if (!pyarray.valid()) return nullptr; // get the pointer to the data buffer of the array (assumed to be C-contiguous) - double* data{PythonInterpreter::Numpy::getDataPtr(pyarray.get())}; + double* data{PyInterpreter::Numpy::getDataPtr(pyarray.get())}; if (!data) return nullptr; diff --git a/GUI/View/Project/PyImportAssistant.cpp b/GUI/View/Project/PyImportAssistant.cpp index 169d4cdde7f874d33000ef4af59bb10c98d7dd9b..f360317bb4f717b09d1f0221fc43a230b70e700f 100644 --- a/GUI/View/Project/PyImportAssistant.cpp +++ b/GUI/View/Project/PyImportAssistant.cpp @@ -29,7 +29,7 @@ #include "GUI/View/Info/MessageBox.h" #include "GUI/View/Project/ProjectManager.h" #include "GUI/View/Tool/Globals.h" -#include "PyCore/Embed/PythonInterpreter.h" // listOfFunctions +#include "PyCore/Embed/PyInterpreter.h" // listOfFunctions #include "PyCore/Sample/ImportMultiLayer.h" // createMultiLayerFromPython #include "Sample/Multilayer/MultiLayer.h" #include <QApplication> @@ -104,7 +104,7 @@ std::unique_ptr<MultiLayer> createMultiLayer(const QString& snippet, const QStri { QApplication::setOverrideCursor(Qt::WaitCursor); void* result_ptr = nullptr; - PyObjectPtr result{PythonInterpreter::createMultiLayerFromPython( + PyObjectPtr result{PyInterpreter::createMultiLayerFromPython( result_ptr, snippet.toStdString(), funcName.toStdString(), bornagainDir())}; std::unique_ptr<MultiLayer> multilayer_ptr(reinterpret_cast<MultiLayer*>(result_ptr)->clone()); @@ -153,7 +153,7 @@ QString getPySampleFunctionName(const QString& snippet) QApplication::setOverrideCursor(Qt::WaitCursor); std::vector<std::string> funcs_res{ - PythonInterpreter::BornAgain::listOfFunctions(snippet.toStdString(), bornagainDir())}; + PyInterpreter::BornAgain::listOfFunctions(snippet.toStdString(), bornagainDir())}; if (funcs_res.empty()) { QApplication::restoreOverrideCursor(); diff --git a/PyCore/Embed/PythonInterpreter.cpp b/PyCore/Embed/PyInterpreter.cpp similarity index 77% rename from PyCore/Embed/PythonInterpreter.cpp rename to PyCore/Embed/PyInterpreter.cpp index 923471d29f0697cb7c1f272f3551ec7d3cb2d7e8..10bea67078587f8b34dbbf53c70d6c8c7a255be6 100644 --- a/PyCore/Embed/PythonInterpreter.cpp +++ b/PyCore/Embed/PyInterpreter.cpp @@ -2,7 +2,7 @@ // // BornAgain: simulate and fit reflection and scattering // -//! @file PyCore/Embed/PythonInterpreter.h +//! @file PyCore/Embed/PyInterpreter.cpp //! @brief Implements functions to expose Python-interpreter functionality to C++. //! //! @homepage http://www.bornagainproject.org @@ -31,7 +31,7 @@ References: #include "PyCore/Embed/PyCore.h" #undef INCLUDE_NUMPY -#include "PythonInterpreter.h" +#include "PyInterpreter.h" #include <cstddef> // NULL #include <cstring> // memcpy #include <iostream> // cerr @@ -79,16 +79,16 @@ std::vector<std::string> toVectorString(PyObject* py_object) if (PyTuple_Check(py_object)) { for (Py_ssize_t i = 0; i < PyTuple_Size(py_object); i++) { PyObject* value = PyTuple_GetItem(py_object, i); - result.push_back(PythonInterpreter::pyStrtoString(value)); + result.push_back(PyInterpreter::pyStrtoString(value)); } } else if (PyList_Check(py_object)) { for (Py_ssize_t i = 0; i < PyList_Size(py_object); i++) { PyObject* value = PyList_GetItem(py_object, i); - result.push_back(PythonInterpreter::pyStrtoString(value)); + result.push_back(PyInterpreter::pyStrtoString(value)); } } else { - throw std::runtime_error(PythonInterpreter::errorDescription( - "PythonInterpreter: Cannnot convert the given Python object " + throw std::runtime_error(PyInterpreter::errorDescription( + "PyInterpreter: Cannnot convert the given Python object " "to vector<string>.")); } @@ -120,20 +120,20 @@ inline void _realArray2DToDouble(PyArrayObject* const npArray_ptr, npy_intp rowI // see <https://docs.python.org/3/c-api/stable.html> // Python stable ABI -void PythonInterpreter::initialize() +void PyInterpreter::initialize() { if (!Py_IsInitialized()) Py_Initialize(); } // Python stable ABI -bool PythonInterpreter::isInitialized() +bool PyInterpreter::isInitialized() { return static_cast<bool>(Py_IsInitialized()); } // Python stable ABI -void PythonInterpreter::finalize() +void PyInterpreter::finalize() { // undo all initializations made by Py_Initialize() and subsequent use // of Python/C API functions, and destroy all sub-interpreters. @@ -142,11 +142,11 @@ void PythonInterpreter::finalize() } // Python stable ABI -bool PythonInterpreter::checkError() +bool PyInterpreter::checkError() { if (PyErr_Occurred()) { // print a standard traceback to sys.stderr and clear the error indicator - std::cerr << "---PythonInterpreter: Error in Python interpreter:\n"; + std::cerr << "---PyInterpreter: Error in Python interpreter:\n"; PyErr_Print(); std::cerr << "\n---\n"; return true; @@ -156,7 +156,7 @@ bool PythonInterpreter::checkError() } // Python stable ABI -void PythonInterpreter::addPythonPath(const std::string& path) +void PyInterpreter::addPythonPath(const std::string& path) { if (!path.empty()) { // add path to `PYTHONPATH` @@ -164,23 +164,22 @@ void PythonInterpreter::addPythonPath(const std::string& path) PyList_Append(sysPath, PyUnicode_FromString(path.c_str())); } - throw std::runtime_error( - "PythonInterpreter.addPythonPath: Cannot add to an empty Python path."); + throw std::runtime_error("PyInterpreter.addPythonPath: Cannot add to an empty Python path."); } // Python stable ABI -void PythonInterpreter::setPythonPath(const std::string& path) +void PyInterpreter::setPythonPath(const std::string& path) { // returns 0 on success, -1 on error const int result = PySys_SetObject((char*)"path", PyUnicode_FromString(path.c_str())); if (result != 0) { - PythonInterpreter::checkError(); - throw std::runtime_error("PythonInterpreter.setPythonPath: Cannot set the Python path."); + PyInterpreter::checkError(); + throw std::runtime_error("PyInterpreter.setPythonPath: Cannot set the Python path."); } } // Python stable ABI -PyObjectPtr PythonInterpreter::import(const std::string& pymodule_name, const std::string& path) +PyObjectPtr PyInterpreter::import(const std::string& pymodule_name, const std::string& path) { addPythonPath(path); @@ -188,7 +187,7 @@ PyObjectPtr PythonInterpreter::import(const std::string& pymodule_name, const st PyObject* pymodule = PyImport_ImportModule(pymodule_name.c_str()); if (!pymodule || !PyModule_Check(pymodule)) { checkError(); - throw std::runtime_error(errorDescription("PythonInterpreter: Cannot load Python module '" + throw std::runtime_error(errorDescription("PyInterpreter: Cannot load Python module '" + pymodule_name + "' (given path = '" + path + "')")); } @@ -198,12 +197,12 @@ PyObjectPtr PythonInterpreter::import(const std::string& pymodule_name, const st } // Python stable ABI -void PythonInterpreter::DecRef(PyObject* py_object) +void PyInterpreter::DecRef(PyObject* py_object) { Py_XDECREF(py_object); } -std::string PythonInterpreter::pyStrtoString(PyObject* py_object) +std::string PyInterpreter::pyStrtoString(PyObject* py_object) { std::string result; PyObject* pyStr = PyUnicode_AsEncodedString(py_object, "utf-8", "replace"); @@ -215,7 +214,7 @@ std::string PythonInterpreter::pyStrtoString(PyObject* py_object) return result; } -std::string PythonInterpreter::runtimeInfo() +std::string PyInterpreter::runtimeInfo() { std::string result; @@ -241,7 +240,7 @@ std::string PythonInterpreter::runtimeInfo() // Attempt to retrieve Python stack trace // Ref: <https://stackoverflow.com/q/1796510> -std::string PythonInterpreter::stackTrace() +std::string PyInterpreter::stackTrace() { std::string result; @@ -280,23 +279,23 @@ std::string PythonInterpreter::stackTrace() } result += "\n"; - result += PythonInterpreter::runtimeInfo(); + result += PyInterpreter::runtimeInfo(); result += "\n"; return result; } -std::string PythonInterpreter::errorDescription(const std::string& title) +std::string PyInterpreter::errorDescription(const std::string& title) { - std::string msg = title + "\n" + PythonInterpreter::stackTrace() + "\n"; + std::string msg = title + "\n" + PyInterpreter::stackTrace() + "\n"; return msg; } -int PythonInterpreter::Numpy::initialize() +int PyInterpreter::Numpy::initialize() { // initialize Python C API, if needed - PythonInterpreter::initialize(); + PyInterpreter::initialize(); int res; _init_numpy(res); @@ -305,7 +304,7 @@ int PythonInterpreter::Numpy::initialize() case 0: return res; case 1: - throw std::runtime_error(errorDescription("PythonInterpreter: Cannot initialize Numpy")); + throw std::runtime_error(errorDescription("PyInterpreter: Cannot initialize Numpy")); case 2: return res; } @@ -313,17 +312,16 @@ int PythonInterpreter::Numpy::initialize() return res; } -bool PythonInterpreter::Numpy::isInitialized() +bool PyInterpreter::Numpy::isInitialized() { return static_cast<bool>(PyArray_API); } -std::vector<double> PythonInterpreter::Numpy::createVectorFromArray2D(PyObject* pyobject_ptr) +std::vector<double> PyInterpreter::Numpy::createVectorFromArray2D(PyObject* pyobject_ptr) { if (!pyobject_ptr || !PyArray_Check(pyobject_ptr)) { - throw(std::runtime_error( - errorDescription("PythonInterpreter::Numpy: Cannot convert an invalid " - "Numpy array to a C-Array"))); + throw(std::runtime_error(errorDescription("PyInterpreter::Numpy: Cannot convert an invalid " + "Numpy array to a C-Array"))); } PyArrayObject* npArray_ptr{reinterpret_cast<PyArrayObject*>(pyobject_ptr)}; @@ -338,18 +336,17 @@ std::vector<double> PythonInterpreter::Numpy::createVectorFromArray2D(PyObject* // Numpy array type must be 2d if (npArray_ndim != 2) { - std::string msg = "PythonInterpreter::Numpy: Expected a Numpy 2d-array " + std::string msg = "PyInterpreter::Numpy: Expected a Numpy 2d-array " "(given number of dimensions is " + std::to_string(npArray_ndim) + ")"; - throw std::runtime_error( - errorDescription("PythonInterpreter::Numpy: Expected a Numpy 2d-array " - "(given number of dimensions is " - + std::to_string(npArray_ndim) + ")")); + throw std::runtime_error(errorDescription("PyInterpreter::Numpy: Expected a Numpy 2d-array " + "(given number of dimensions is " + + std::to_string(npArray_ndim) + ")")); } // Numpy array type must be numeric and real (eligible for casting to double) if (!PyDataType_ISNUMBER(npArray_descr) || PyDataType_ISCOMPLEX(npArray_descr)) { - std::string msg = "PythonInterpreter::Numpy: " + std::string msg = "PyInterpreter::Numpy: " "Expected a Numpy array of numeric type and real " "(given type '" + std::to_string(npArray_dtype) + "')"; @@ -409,7 +406,7 @@ std::vector<double> PythonInterpreter::Numpy::createVectorFromArray2D(PyObject* #endif // _WIN32 default: - throw std::runtime_error(errorDescription("PythonInterpreter::Numpy: " + throw std::runtime_error(errorDescription("PyInterpreter::Numpy: " "Conversion of Numpy array of dtype '" + std::to_string(npArray_dtype) + "' " @@ -419,17 +416,16 @@ std::vector<double> PythonInterpreter::Numpy::createVectorFromArray2D(PyObject* return data; } -PyObjectPtr PythonInterpreter::Numpy::createArray2DfromC(double* const c_array, - const np_size_t dims[2]) +PyObjectPtr PyInterpreter::Numpy::createArray2DfromC(double* const c_array, const np_size_t dims[2]) { if (!c_array) { - throw std::runtime_error("PythonInterpreter::Numpy: " + throw std::runtime_error("PyInterpreter::Numpy: " "Cannot create a Numpy 1D-array from a null data pointer"); } const np_size_t size = dims[0] * dims[1]; if (size < 1) { - throw std::runtime_error("PythonInterpreter::Numpy: " + throw std::runtime_error("PyInterpreter::Numpy: " "Cannot create a Numpy 1D-array from a data with size = 0"); } @@ -439,7 +435,7 @@ PyObjectPtr PythonInterpreter::Numpy::createArray2DfromC(double* const c_array, PyObject* npArray_ptr = PyArray_SimpleNew(/* n_dims */ 2, npDims, NPY_DOUBLE); if (!npArray_ptr) { checkError(); - throw std::runtime_error("PythonInterpreter::Numpy: " + throw std::runtime_error("PyInterpreter::Numpy: " "Cannot create a Numpy 1D-array from the " "given data (size = " + std::to_string(size) + ")"); @@ -457,17 +453,16 @@ PyObjectPtr PythonInterpreter::Numpy::createArray2DfromC(double* const c_array, return {npArray_ptr}; } -PyObjectPtr PythonInterpreter::Numpy::createArray1DfromC(double* const c_array, - const np_size_t size) +PyObjectPtr PyInterpreter::Numpy::createArray1DfromC(double* const c_array, const np_size_t size) { if (!c_array) { - throw std::runtime_error("PythonInterpreter::Numpy: " + throw std::runtime_error("PyInterpreter::Numpy: " "Cannot create a Numpy 1D-array from a null data pointer"); } if (size < 1) { throw std::runtime_error( - errorDescription("PythonInterpreter::Numpy: " + errorDescription("PyInterpreter::Numpy: " "Cannot create a Numpy 1D-array from a data with size = 0")); } @@ -477,11 +472,11 @@ PyObjectPtr PythonInterpreter::Numpy::createArray1DfromC(double* const c_array, PyObject* npArray_ptr = PyArray_SimpleNew(/* n_dims */ 1, npDims, NPY_DOUBLE); if (!npArray_ptr) { checkError(); - std::string msg = "PythonInterpreter::Numpy: " + std::string msg = "PyInterpreter::Numpy: " "Cannot create a Numpy 1D-array from the " "given data (size = " + std::to_string(size) + ")"; - throw std::runtime_error(errorDescription("PythonInterpreter::Numpy: " + throw std::runtime_error(errorDescription("PyInterpreter::Numpy: " "Cannot create a Numpy 1D-array from the " "given data (size = " + std::to_string(size) + ")")); @@ -500,18 +495,18 @@ PyObjectPtr PythonInterpreter::Numpy::createArray1DfromC(double* const c_array, } -PyObjectPtr PythonInterpreter::Numpy::CArrayAsNpy2D(double* const c_array, const np_size_t dims[2]) +PyObjectPtr PyInterpreter::Numpy::CArrayAsNpy2D(double* const c_array, const np_size_t dims[2]) { if (!c_array) { throw std::runtime_error( - errorDescription("PythonInterpreter::Numpy: " + errorDescription("PyInterpreter::Numpy: " "Cannot create a Numpy 2D-array from a null data pointer")); } const np_size_t size = dims[0] * dims[1]; if (size < 1) { throw std::runtime_error( - errorDescription("PythonInterpreter::Numpy: " + errorDescription("PyInterpreter::Numpy: " "Cannot create a Numpy 2D-array from a data with size = 0")); } @@ -522,17 +517,16 @@ PyObjectPtr PythonInterpreter::Numpy::CArrayAsNpy2D(double* const c_array, const /* n_dims */ 2, npDims, NPY_DOUBLE, reinterpret_cast<void*>(c_array)); if (!npArray_ptr || !PyArray_Check(npArray_ptr)) { - PythonInterpreter::checkError(); - throw std::runtime_error( - errorDescription("PythonInterpreter::Numpy: Cannot convert the given " - "C-Array to a Numpy 2D-array")); + PyInterpreter::checkError(); + throw std::runtime_error(errorDescription("PyInterpreter::Numpy: Cannot convert the given " + "C-Array to a Numpy 2D-array")); } // returns a _new_ reference; ie. caller is responsible for the ref-count return {npArray_ptr}; } -PyObjectPtr PythonInterpreter::Numpy::arrayND(std::vector<std::size_t>& dimensions) +PyObjectPtr PyInterpreter::Numpy::arrayND(std::vector<std::size_t>& dimensions) { // descriptors for the array dimensions const std::size_t n_dims = dimensions.size(); @@ -561,7 +555,7 @@ PyObjectPtr PythonInterpreter::Numpy::arrayND(std::vector<std::size_t>& dimensio if (!npyArray_ptr) { checkError(); - throw std::runtime_error(errorDescription("PythonInterpreter::Numpy: Cannot create a Numpy " + throw std::runtime_error(errorDescription("PyInterpreter::Numpy: Cannot create a Numpy " + std::to_string(n_dims) + "D-array from the given data")); } @@ -569,7 +563,7 @@ PyObjectPtr PythonInterpreter::Numpy::arrayND(std::vector<std::size_t>& dimensio return {npyArray_ptr}; } -double* PythonInterpreter::Numpy::getDataPtr(PyObject* pyobject_ptr) +double* PyInterpreter::Numpy::getDataPtr(PyObject* pyobject_ptr) { PyArrayObject* npArray_ptr{reinterpret_cast<PyArrayObject*>(pyobject_ptr)}; @@ -579,7 +573,7 @@ double* PythonInterpreter::Numpy::getDataPtr(PyObject* pyobject_ptr) if (!data_ptr) { checkError(); - throw(std::runtime_error(errorDescription("PythonInterpreter::Numpy: " + throw(std::runtime_error(errorDescription("PyInterpreter::Numpy: " "Numpy array has invalid data pointer"))); } @@ -587,9 +581,9 @@ double* PythonInterpreter::Numpy::getDataPtr(PyObject* pyobject_ptr) } -PyObjectPtr PythonInterpreter::BornAgain::import(const std::string& path) +PyObjectPtr PyInterpreter::BornAgain::import(const std::string& path) { - PythonInterpreter::addPythonPath(path); + PyInterpreter::addPythonPath(path); #ifndef _WIN32 // Stores signal handler before numpy's mess it up. @@ -604,7 +598,7 @@ PyObjectPtr PythonInterpreter::BornAgain::import(const std::string& path) if (!ba_pymodule || !PyModule_Check(ba_pymodule)) { checkError(); throw std::runtime_error( - errorDescription("PythonInterpreter: Cannot load 'bornagain' Python module " + errorDescription("PyInterpreter: Cannot load 'bornagain' Python module " "(given path = '" + path + "')")); } @@ -617,10 +611,10 @@ PyObjectPtr PythonInterpreter::BornAgain::import(const std::string& path) return {ba_pymodule}; } -PyObjectPtr PythonInterpreter::BornAgain::importScript(const std::string& script, - const std::string& path) +PyObjectPtr PyInterpreter::BornAgain::importScript(const std::string& script, + const std::string& path) { - PyObjectPtr ba_pymodule{PythonInterpreter::BornAgain::import(path)}; + PyObjectPtr ba_pymodule{PyInterpreter::BornAgain::import(path)}; // TODO: Check ba module PyObject* pCompiledFn = Py_CompileString(script.c_str(), "", Py_file_input); @@ -641,15 +635,15 @@ PyObjectPtr PythonInterpreter::BornAgain::importScript(const std::string& script } -std::vector<std::string> PythonInterpreter::BornAgain::listOfFunctions(const std::string& script, - const std::string& path) +std::vector<std::string> PyInterpreter::BornAgain::listOfFunctions(const std::string& script, + const std::string& path) { - PyObjectPtr tmpModule{PythonInterpreter::BornAgain::importScript(script, path)}; + PyObjectPtr tmpModule{PyInterpreter::BornAgain::importScript(script, path)}; PyObject* pDict = PyModule_GetDict(tmpModule.get()); if (!pDict) { checkError(); - throw std::runtime_error("PythonInterpreter::BornAgain: " + throw std::runtime_error("PyInterpreter::BornAgain: " "Cannot obtain the dictionary from the script module"); } @@ -658,7 +652,7 @@ std::vector<std::string> PythonInterpreter::BornAgain::listOfFunctions(const std std::vector<std::string> fn_names; while (PyDict_Next(pDict, &pos, &key, &value)) { if (PyCallable_Check(value)) { - std::string func_name{PythonInterpreter::pyStrtoString(key)}; + std::string func_name{PyInterpreter::pyStrtoString(key)}; if (func_name.find("__") == std::string::npos) fn_names.push_back(func_name); } @@ -670,34 +664,34 @@ std::vector<std::string> PythonInterpreter::BornAgain::listOfFunctions(const std } -PyObjectPtr PythonInterpreter::Fabio::import() +PyObjectPtr PyInterpreter::Fabio::import() { - return {PythonInterpreter::import("fabio")}; + return {PyInterpreter::import("fabio")}; } -PyObjectPtr PythonInterpreter::Fabio::open(const std::string& filename, PyObjectPtr& fabio_module) +PyObjectPtr PyInterpreter::Fabio::open(const std::string& filename, PyObjectPtr& fabio_module) { // load an image via calling `fabio.load` function which takes a // filename (Python string) and returns a Numpy array. if (!fabio_module.valid() || !PyModule_Check(fabio_module.get())) { - throw std::runtime_error(errorDescription("PythonInterpreter.fabio: Invalid Python module " + throw std::runtime_error(errorDescription("PyInterpreter.fabio: Invalid Python module " "(expected 'fabio' module)")); } PyObject* pFunc = PyObject_GetAttrString(fabio_module.get(), (char*)"open"); if (!pFunc || !PyCallable_Check(pFunc)) { - PythonInterpreter::checkError(); + PyInterpreter::checkError(); throw std::runtime_error( - errorDescription("PythonInterpreter.fabio: The function 'fabio.open' is not callable")); + errorDescription("PyInterpreter.fabio: The function 'fabio.open' is not callable")); } // convert the filename to a Python unicode string PyObject* pFilename = PyUnicode_FromString(filename.c_str()); if (!pFilename) { - PythonInterpreter::checkError(); + PyInterpreter::checkError(); throw std::runtime_error( - errorDescription("PythonInterpreter.fabio: Filename '" + filename + errorDescription("PyInterpreter.fabio: Filename '" + filename + "' cannot be converted to Python unicode string")); } @@ -705,8 +699,8 @@ PyObjectPtr PythonInterpreter::Fabio::open(const std::string& filename, PyObject PyObject* pResult_open = PyObject_CallFunctionObjArgs(pFunc, pFilename, NULL); Py_DecRef(pFunc); if (!pResult_open) { - PythonInterpreter::checkError(); - std::runtime_error(errorDescription("PythonInterpreter.fabio: " + PyInterpreter::checkError(); + std::runtime_error(errorDescription("PyInterpreter.fabio: " "Invalid return value from calling the function " "'fabio.open(\"" + filename + "\")'")); @@ -716,8 +710,8 @@ PyObjectPtr PythonInterpreter::Fabio::open(const std::string& filename, PyObject PyObject* npyArray_ptr = PyObject_GetAttrString(pResult_open, (char*)"data"); Py_DecRef(pResult_open); if (!npyArray_ptr || !PyArray_Check(npyArray_ptr)) { - PythonInterpreter::checkError(); - std::runtime_error(errorDescription("PythonInterpreter.fabio: Invalid return value from " + PyInterpreter::checkError(); + std::runtime_error(errorDescription("PyInterpreter.fabio: Invalid return value from " "calling the function 'fabio.open(\"" + filename + "\")' (expected a Numpy array)")); } diff --git a/PyCore/Embed/PythonInterpreter.h b/PyCore/Embed/PyInterpreter.h similarity index 93% rename from PyCore/Embed/PythonInterpreter.h rename to PyCore/Embed/PyInterpreter.h index 627d92040505429085c054194c96cf90f1e48f48..c3c3725f1b215567bee77cad0fac53bdae358292 100644 --- a/PyCore/Embed/PythonInterpreter.h +++ b/PyCore/Embed/PyInterpreter.h @@ -2,7 +2,7 @@ // // BornAgain: simulate and fit reflection and scattering // -//! @file PyCore/Embed/PythonInterpreter.h +//! @file PyCore/Embed/PyInterpreter.h //! @brief Declares functions to expose Python-interpreter functionality to C++. //! //! @homepage http://www.bornagainproject.org @@ -12,14 +12,14 @@ // // ************************************************************************************************ -#ifndef BORNAGAIN_PYCORE_EMBED_PYTHONINTERPRETER_H -#define BORNAGAIN_PYCORE_EMBED_PYTHONINTERPRETER_H +#ifndef BORNAGAIN_PYCORE_EMBED_PYINTERPRETER_H +#define BORNAGAIN_PYCORE_EMBED_PYINTERPRETER_H #include "PyCore/Embed/PyObjectPtr.h" #include <string> #include <vector> -namespace PythonInterpreter { +namespace PyInterpreter { // Python stable ABI void initialize(); @@ -111,6 +111,6 @@ PyObjectPtr open(const std::string& filename, PyObjectPtr& fabio_module); } // namespace Fabio -} // namespace PythonInterpreter +} // namespace PyInterpreter -#endif // BORNAGAIN_PYCORE_EMBED_PYTHONINTERPRETER_H +#endif // BORNAGAIN_PYCORE_EMBED_PYINTERPRETER_H diff --git a/PyCore/Embed/PyObjectPtr.cpp b/PyCore/Embed/PyObjectPtr.cpp index 0999b33bcd66508ecd18bb86860b96b697a31641..93e9530bd2bcb98af1225b7635f66f4230c044ee 100644 --- a/PyCore/Embed/PyObjectPtr.cpp +++ b/PyCore/Embed/PyObjectPtr.cpp @@ -13,7 +13,7 @@ // ************************************************************************************************ #include "PyObjectPtr.h" -#include "PythonInterpreter.h" +#include "PyInterpreter.h" #include <stdexcept> // runtime_error @@ -53,13 +53,13 @@ void PyObjectPtr::reset() void PyObjectPtr::discard() { - if (!PythonInterpreter::isInitialized()) { + if (!PyInterpreter::isInitialized()) { throw(std::runtime_error("Decrementing Python reference-count without " "Python initialized leads to memory access violation " "(segmentation fault)")); } - PythonInterpreter::DecRef(m_ptr); + PyInterpreter::DecRef(m_ptr); reset(); } diff --git a/PyCore/Sample/ImportMultiLayer.cpp b/PyCore/Sample/ImportMultiLayer.cpp index 57e15a633bd2a82357c9271ecb6ff08cea055509..c538008ee88e4596213680c4ffcf411be6af453c 100644 --- a/PyCore/Sample/ImportMultiLayer.cpp +++ b/PyCore/Sample/ImportMultiLayer.cpp @@ -2,7 +2,7 @@ #include "ImportMultiLayer.h" #include "PyCore/Embed/PyCore.h" -#include "PyCore/Embed/PythonInterpreter.h" // BornAgain::importScript +#include "PyCore/Embed/PyInterpreter.h" // BornAgain::importScript // SWIG runtime access for creating a MultiLayer instance from Python #include "auto/Wrap/swig_runtime.h" @@ -10,17 +10,17 @@ #include <stdexcept> // runtime_error -namespace PythonInterpreter { +namespace PyInterpreter { PyObjectPtr createMultiLayerFromPython(void*& multilayer_ptr, const std::string& script, const std::string& functionName, const std::string& path) { - PyObjectPtr tmpModule{PythonInterpreter::BornAgain::importScript(script, path)}; + PyObjectPtr tmpModule{PyInterpreter::BornAgain::importScript(script, path)}; // locate the `get_simulation` function (it is an attribute of the module) PyObject* pAddFn = PyObject_GetAttrString(tmpModule.get(), functionName.c_str()); if (!pAddFn) { - throw std::runtime_error(errorDescription("PythonInterpreter::BornAgain: " + throw std::runtime_error(errorDescription("PyInterpreter::BornAgain: " "Cannot locate the compiled function '" + functionName + "'")); } @@ -33,7 +33,7 @@ PyObjectPtr createMultiLayerFromPython(void*& multilayer_ptr, const std::string& if (!instance) { throw std::runtime_error(errorDescription( - "PythonInterpreter::BornAgain: Cannot call the function '" + functionName + "'")); + "PyInterpreter::BornAgain: Cannot call the function '" + functionName + "'")); } // Construct a C++ object from the Python object @@ -48,7 +48,7 @@ PyObjectPtr createMultiLayerFromPython(void*& multilayer_ptr, const std::string& if (!SWIG_IsOK(res)) { Py_DecRef(instance); throw std::runtime_error( - errorDescription("PythonInterpreter::BornAgain: SWIG failed to extract a " + errorDescription("PyInterpreter::BornAgain: SWIG failed to extract a " "'MultiLayer' instance via calling the function '" + functionName + "'")); } @@ -57,6 +57,6 @@ PyObjectPtr createMultiLayerFromPython(void*& multilayer_ptr, const std::string& return {instance}; } -} // namespace PythonInterpreter +} // namespace PyInterpreter #endif // BORNAGAIN_PYTHON diff --git a/PyCore/Sample/ImportMultiLayer.h b/PyCore/Sample/ImportMultiLayer.h index 97aa9127fd7a0ce2da256e11306688c8b3781fb5..bac5057da4875f160580842c1f9ac683115f4710 100644 --- a/PyCore/Sample/ImportMultiLayer.h +++ b/PyCore/Sample/ImportMultiLayer.h @@ -24,7 +24,7 @@ class MultiLayer; -namespace PythonInterpreter { +namespace PyInterpreter { //! Creates a multi layer by running python code in embedded interpreter, given //! The Python-script filename and the name of the function which produces a `MultiLayer`. @@ -37,6 +37,6 @@ PyObjectPtr createMultiLayerFromPython(void*& multilayer_ptr, const std::string& const std::string& functionName, const std::string& path = ""); -} // namespace PythonInterpreter +} // namespace PyInterpreter #endif // BORNAGAIN_PYCORE_SAMPLE_IMPORTMULTILAYER_H