diff --git a/Base/Py/PyEmbeddedUtils.cpp b/Base/Py/PyUtils.cpp similarity index 79% rename from Base/Py/PyEmbeddedUtils.cpp rename to Base/Py/PyUtils.cpp index e4a2e6245f87f59fb5f1ec5bc624ff166c144610..aefead734d36763991c67764ef7b72154cf84f52 100644 --- a/Base/Py/PyEmbeddedUtils.cpp +++ b/Base/Py/PyUtils.cpp @@ -2,8 +2,8 @@ // // BornAgain: simulate and fit scattering at grazing incidence // -//! @file Base/Py/PyEmbeddedUtils.cpp -//! @brief IOmplements various functions from PyEmbeddedUtils namespace +//! @file Base/Py/PyUtils.cpp +//! @brief IOmplements various functions from PyUtils namespace //! //! @homepage http://www.bornagainproject.org //! @license GNU General Public License v3 or higher (see COPYING) @@ -14,14 +14,14 @@ #ifdef BORNAGAIN_PYTHON -#include "Base/Py/PyEmbeddedUtils.h" +#include "Base/Py/PyUtils.h" #include "Base/Py/PyCore.h" #include "Base/Utils/SysUtils.h" #include <iostream> #include <sstream> #include <stdexcept> -std::string PyEmbeddedUtils::toString(PyObject* obj) { +std::string PyUtils::toString(PyObject* obj) { std::string result; PyObject* pyStr = PyUnicode_AsEncodedString(obj, "utf-8", "Error ~"); result = std::string(PyBytes_AsString(pyStr)); @@ -29,7 +29,7 @@ std::string PyEmbeddedUtils::toString(PyObject* obj) { return result; } -std::vector<std::string> PyEmbeddedUtils::toVectorString(PyObject* obj) { +std::vector<std::string> PyUtils::toVectorString(PyObject* obj) { std::vector<std::string> result; if (PyTuple_Check(obj)) { @@ -45,26 +45,26 @@ std::vector<std::string> PyEmbeddedUtils::toVectorString(PyObject* obj) { } } else - throw std::runtime_error("PyEmbeddedUtils::toVectorString() -> Error. Unexpected object."); + throw std::runtime_error("PyUtils::toVectorString() -> Error. Unexpected object."); return result; } -std::string PyEmbeddedUtils::toString(char* c) { +std::string PyUtils::toString(char* c) { if (c) return c; else return ""; } -std::string PyEmbeddedUtils::toString(wchar_t* c) { +std::string PyUtils::toString(wchar_t* c) { if (!c) return ""; std::wstring wstr(c); return std::string(wstr.begin(), wstr.end()); } -void PyEmbeddedUtils::import_bornagain(const std::string& path) { +void PyUtils::import_bornagain(const std::string& path) { if (!Py_IsInitialized()) { Py_InitializeEx(0); @@ -92,7 +92,7 @@ void PyEmbeddedUtils::import_bornagain(const std::string& path) { } } -std::string PyEmbeddedUtils::pythonRuntimeInfo() { +std::string PyUtils::pythonRuntimeInfo() { Py_InitializeEx(0); std::stringstream result; @@ -104,15 +104,15 @@ std::string PyEmbeddedUtils::pythonRuntimeInfo() { result << "PYTHONHOME: " << SysUtils::getenv("PYTHONHOME") << "\n"; // Embedded Python details - result << "Py_GetProgramName(): " << PyEmbeddedUtils::toString(Py_GetProgramName()) << "\n"; - result << "Py_GetProgramFullPath(): " << PyEmbeddedUtils::toString(Py_GetProgramFullPath()) + result << "Py_GetProgramName(): " << PyUtils::toString(Py_GetProgramName()) << "\n"; + result << "Py_GetProgramFullPath(): " << PyUtils::toString(Py_GetProgramFullPath()) << "\n"; - result << "Py_GetPath(): " << PyEmbeddedUtils::toString(Py_GetPath()) << "\n"; - result << "Py_GetPythonHome(): " << PyEmbeddedUtils::toString(Py_GetPythonHome()) << "\n"; + result << "Py_GetPath(): " << PyUtils::toString(Py_GetPath()) << "\n"; + result << "Py_GetPythonHome(): " << PyUtils::toString(Py_GetPythonHome()) << "\n"; // Runtime Python's sys.path PyObject* sysPath = PySys_GetObject((char*)"path"); - auto content = PyEmbeddedUtils::toVectorString(sysPath); + auto content = PyUtils::toVectorString(sysPath); result << "sys.path: "; for (auto s : content) result << s << ","; @@ -124,7 +124,7 @@ std::string PyEmbeddedUtils::pythonRuntimeInfo() { // Attempt to retrieve Python stack trace // https://stackoverflow.com/questions/1796510/accessing-a-python-traceback-from-the-c-api -std::string PyEmbeddedUtils::pythonStackTrace() { +std::string PyUtils::pythonStackTrace() { std::stringstream result; if (PyErr_Occurred()) { diff --git a/Base/Py/PyEmbeddedUtils.h b/Base/Py/PyUtils.h similarity index 91% rename from Base/Py/PyEmbeddedUtils.h rename to Base/Py/PyUtils.h index a3fe229b91f9c06ca761c54b39077f2f0e7baa7d..8af78455d75e682ac2e1f2078a393490282a014a 100644 --- a/Base/Py/PyEmbeddedUtils.h +++ b/Base/Py/PyUtils.h @@ -2,8 +2,8 @@ // // BornAgain: simulate and fit scattering at grazing incidence // -//! @file Base/Py/PyEmbeddedUtils.h -//! @brief Defines PyEmbeddedUtils namespace +//! @file Base/Py/PyUtils.h +//! @brief Defines PyUtils namespace //! //! @homepage http://www.bornagainproject.org //! @license GNU General Public License v3 or higher (see COPYING) @@ -29,7 +29,7 @@ class MultiLayer; -namespace PyEmbeddedUtils { +namespace PyUtils { //! Converts PyObject into string, if possible, or throws exception. std::string toString(PyObject* obj); @@ -49,7 +49,7 @@ std::string pythonRuntimeInfo(); //! Returns string representing python stack trace. std::string pythonStackTrace(); -} // namespace PyEmbeddedUtils +} // namespace PyUtils #endif // BORNAGAIN_PYTHON diff --git a/Sample/Multilayer/PyImport.cpp b/Sample/Multilayer/PyImport.cpp index 37dd53209774b058329c16afe9eba886167d617e..077ae4493755659e1cf1e96eff799e3c2e9e6323 100644 --- a/Sample/Multilayer/PyImport.cpp +++ b/Sample/Multilayer/PyImport.cpp @@ -15,7 +15,7 @@ #ifdef BORNAGAIN_PYTHON #include "Sample/Multilayer/PyImport.h" -#include "Base/Py/PyEmbeddedUtils.h" +#include "Base/Py/PyUtils.h" #include "Base/Py/PyCore.h" #include "Sample/Multilayer/MultiLayer.h" @@ -24,7 +24,7 @@ namespace { std::string error_description(const std::string& title) { std::stringstream buf; buf << title << "\n"; - buf << PyEmbeddedUtils::pythonStackTrace() << "\n"; + buf << PyUtils::pythonStackTrace() << "\n"; return buf.str(); } @@ -33,7 +33,7 @@ std::string error_description(const std::string& title) { std::unique_ptr<MultiLayer> PyImport::createFromPython(const std::string& script, const std::string& functionName, const std::string& path) { - PyEmbeddedUtils::import_bornagain(path); + PyUtils::import_bornagain(path); PyObject* pCompiledFn = Py_CompileString(script.c_str(), "", Py_file_input); if (!pCompiledFn) @@ -83,7 +83,7 @@ std::unique_ptr<MultiLayer> PyImport::createFromPython(const std::string& script std::vector<std::string> PyImport::listOfFunctions(const std::string& script, const std::string& path) { - PyEmbeddedUtils::import_bornagain(path); + PyUtils::import_bornagain(path); PyObject* pCompiledFn = Py_CompileString(script.c_str(), "", Py_file_input); if (!pCompiledFn) @@ -105,7 +105,7 @@ std::vector<std::string> PyImport::listOfFunctions(const std::string& script, Py_ssize_t pos = 0; while (PyDict_Next(dict, &pos, &key, &value)) { if (PyCallable_Check(value)) { - std::string func_name = PyEmbeddedUtils::toString(key); + std::string func_name = PyUtils::toString(key); if (func_name.find("__") == std::string::npos) result.push_back(func_name); } diff --git a/Tests/Functional/Python/PyEmbedded/Tests.cpp b/Tests/Functional/Python/PyEmbedded/Tests.cpp index c48144942af62f71729ff92db24101b2eba1dd95..d33ce3f469615988fbe11743a533cc6bbc51f0d7 100644 --- a/Tests/Functional/Python/PyEmbedded/Tests.cpp +++ b/Tests/Functional/Python/PyEmbedded/Tests.cpp @@ -13,7 +13,7 @@ #include "BABuild.h" #include "BAVersion.h" -#include "Base/Py/PyEmbeddedUtils.h" +#include "Base/Py/PyUtils.h" #include "Base/Py/PyCore.h" #include "Base/Utils/SysUtils.h" #include "Core/Export/ExportToPython.h" @@ -44,7 +44,7 @@ TEST_F(PyEmbedded, SysPath) { std::cout << "buildLibDir(): " << BABuild::buildLibDir() << std::endl; // Runtime info - auto content = PyEmbeddedUtils::pythonRuntimeInfo(); + auto content = PyUtils::pythonRuntimeInfo(); EXPECT_TRUE(!content.empty()); } @@ -63,7 +63,7 @@ TEST_F(PyEmbedded, ImportNumpy) { if (!pvar) throw std::runtime_error("Can't get a variable"); - auto version_string = PyEmbeddedUtils::toString(pvar); + auto version_string = PyUtils::toString(pvar); Py_DecRef(pvar); std::cout << "numpy_version_string=" << version_string << std::endl; @@ -102,7 +102,7 @@ TEST_F(PyEmbedded, FunctionCall) { if (!result) throw std::runtime_error("Error while calling function"); - auto str = PyEmbeddedUtils::toString(result); + auto str = PyUtils::toString(result); Py_DecRef(result); Py_Finalize(); @@ -242,7 +242,7 @@ TEST_F(PyEmbedded, CompiledFunction) { // convert the result to a string PyObject* pResultRepr = PyObject_Repr(pResult); - std::string result = PyEmbeddedUtils::toString(pResultRepr); + std::string result = PyUtils::toString(pResultRepr); Py_DecRef(pResultRepr); Py_Finalize(); diff --git a/auto/Wrap/doxygenBase.i b/auto/Wrap/doxygenBase.i index c6c4dfe327ab8623d027aafefb20ac251e103044..9f3cf381456647347ec0bb16f98c7c6a756bfe0d 100644 --- a/auto/Wrap/doxygenBase.i +++ b/auto/Wrap/doxygenBase.i @@ -1404,36 +1404,36 @@ Returns the difference of the logarithm; input values are truncated at the minim // File: namespacePhysConsts.xml -// File: namespacePyEmbeddedUtils.xml -%feature("docstring") PyEmbeddedUtils::toString "std::string PyEmbeddedUtils::toString(PyObject *obj) +// File: namespacePyUtils.xml +%feature("docstring") PyUtils::toString "std::string PyUtils::toString(PyObject *obj) Converts PyObject into string, if possible, or throws exception. "; -%feature("docstring") PyEmbeddedUtils::toVectorString "std::vector< std::string > PyEmbeddedUtils::toVectorString(PyObject *obj) +%feature("docstring") PyUtils::toVectorString "std::vector< std::string > PyUtils::toVectorString(PyObject *obj) Converts PyObject into vector of strings, if possible, or throws exception. "; -%feature("docstring") PyEmbeddedUtils::toString "std::string PyEmbeddedUtils::toString(char *c) +%feature("docstring") PyUtils::toString "std::string PyUtils::toString(char *c) Converts char to string. In the case of nullptr will return an empty string. "; -%feature("docstring") PyEmbeddedUtils::toString "std::string PyEmbeddedUtils::toString(wchar_t *c) +%feature("docstring") PyUtils::toString "std::string PyUtils::toString(wchar_t *c) "; -%feature("docstring") PyEmbeddedUtils::import_bornagain "void PyEmbeddedUtils::import_bornagain(const std::string &path=\"\") +%feature("docstring") PyUtils::import_bornagain "void PyUtils::import_bornagain(const std::string &path=\"\") Imports BornAgain from given location. If path is empty, tries to rely on PYTHONPATH. "; -%feature("docstring") PyEmbeddedUtils::pythonRuntimeInfo "std::string PyEmbeddedUtils::pythonRuntimeInfo() +%feature("docstring") PyUtils::pythonRuntimeInfo "std::string PyUtils::pythonRuntimeInfo() Returns multi-line string representing PATH, PYTHONPATH, sys.path and other info. "; -%feature("docstring") PyEmbeddedUtils::pythonStackTrace "std::string PyEmbeddedUtils::pythonStackTrace() +%feature("docstring") PyUtils::pythonStackTrace "std::string PyUtils::pythonStackTrace() Returns string representing python stack trace. "; @@ -1651,13 +1651,13 @@ Template function to create an integrator object // File: PyCore_8h.xml -// File: PyEmbeddedUtils_8cpp.xml +// File: PyObject_8h.xml -// File: PyEmbeddedUtils_8h.xml +// File: PyUtils_8cpp.xml -// File: PyObject_8h.xml +// File: PyUtils_8h.xml // File: CloneableVector_8h.xml