Cantera  3.1.0
Loading...
Searching...
No Matches
utils_utils.h
1// This file is part of Cantera. See License.txt in the top-level directory or
2// at https://cantera.org/license.txt for license and copyright information.
3
4#ifndef CT_PY_UTILS_UTILS_H
5#define CT_PY_UTILS_UTILS_H
6
8#include "sundials/sundials_config.h"
9
10#include "Python.h"
11
12// Warning types supported by the Python C-API.
13// See https://docs.python.org/3/c-api/exceptions.html#issuing-warnings
14static std::map<std::string, PyObject*> mapped_PyWarnings = {
15 {"", PyExc_Warning},
16 {"Bytes", PyExc_BytesWarning},
17 {"Cantera", PyExc_UserWarning}, // pre-existing warning
18 {"Deprecation", PyExc_DeprecationWarning},
19 {"Future", PyExc_FutureWarning},
20 {"Import", PyExc_ImportWarning},
21 {"PendingDeprecation", PyExc_PendingDeprecationWarning},
22 {"Resource", PyExc_ResourceWarning},
23 {"Runtime", PyExc_RuntimeWarning},
24 {"Syntax", PyExc_SyntaxWarning},
25 {"Unicode", PyExc_UnicodeWarning},
26 {"User", PyExc_UserWarning}
27};
28
29// Cantera version for Python module compilation
30inline std::string get_cantera_version_py()
31{
32 return CANTERA_VERSION;
33}
34
35// Git commit for Python module compilation
36inline std::string get_cantera_git_commit_py()
37{
38#ifdef GIT_COMMIT
39 return GIT_COMMIT;
40#else
41 return "unknown";
42#endif
43}
44
45// Wrappers for preprocessor defines
46inline std::string get_sundials_version()
47{
48 return SUNDIALS_VERSION;
49}
50
52{
53public:
54 void write(const std::string& s) override {
55 // 1000 bytes is the maximum size permitted by PySys_WriteStdout
56 static const size_t N = 999;
57 for (size_t i = 0; i < s.size(); i+=N) {
58 PySys_WriteStdout("%s", s.substr(i, N).c_str());
59 }
60 std::cout.flush();
61 }
62
63 void writeendl() override {
64 PySys_WriteStdout("%s", "\n");
65 std::cout.flush();
66 }
67
68 void warn(const std::string& warning, const std::string& msg) override {
69 if (mapped_PyWarnings.find(warning) != mapped_PyWarnings.end()) {
70 PyErr_WarnEx(mapped_PyWarnings[warning], msg.c_str(), 1);
71 } else {
72 // issue generic warning
73 PyErr_WarnEx(PyExc_Warning, msg.c_str(), 1);
74 }
75 }
76
77 void error(const std::string& msg) override {
78 PyErr_SetString(PyExc_RuntimeError, msg.c_str());
79 }
80};
81
82#endif
Base class for 'loggers' that write text messages to log files.
Definition logger.h:40
void writeendl() override
Write an end of line character and flush output.
Definition utils_utils.h:63
void error(const std::string &msg) override
Write an error message and quit.
Definition utils_utils.h:77
void write(const std::string &s) override
Write a log message.
Definition utils_utils.h:54
void warn(const std::string &warning, const std::string &msg) override
Write a warning message.
Definition utils_utils.h:68
Header for Base class for 'loggers' that write text messages to log files (see Logging and class Logg...