MPQC 3.0.0-alpha
Loading...
Searching...
No Matches
python.hpp
1#ifndef PYTHON_HPP
2#define PYTHON_HPP
3
4#ifdef HAVE_PYTHON
5
6//#include <Python.h>
7#include <stdio.h>
8#include <boost/python.hpp>
9#include <boost/noncopyable.hpp>
10
11// template<typename T>
12// void operator%(const std::string &fmt, const T &t) {
13// }
14
15struct Python {
16private:
17
18 struct initializer {
19 initializer() {
20 using namespace boost::python;
21 if (!Py_IsInitialized()) {
22 Py_InitializeEx(0);
23 boost::python::object main = boost::python::import("__main__");
24 const char* startup = getenv("PYTHONSTARTUP");
25 if (startup) {
26 FILE *fp = fopen(startup, "r");
27 if (fp) PyRun_SimpleFile(fp, startup);
28 fclose(fp);
29 }
30
31 // disable catching Ctrl-C
32 PyRun_SimpleString("import signal\n"
33 "signal.signal(signal.SIGINT, signal.SIG_DFL)\n");
34
35 // PyRun_SimpleString("import signal\n"
36 // "import sys\n"
37 // "def sigint(signal, frame): sys.exit(0)\n"
38 // "signal.signal(signal.SIGINT, sigint)\n");
39
40 PyImport_AddModule("mpqc");
41 PyRun_SimpleString("import mpqc");
42 }
43 }
44 // must only call once to avoid crashes with boost::python
45 ~initializer() {
46 //std::cout << "Py_Finalize" << std::endl;
47 Py_Finalize();
48 }
49 };
50
51public:
52
53 Python() {
54 static initializer init;
55 }
56
57 struct tuple : boost::python::tuple {
58 template<typename T>
59 tuple(T t)
60 : boost::python::tuple(boost::python::make_tuple(t)) {}
61 template<typename T0, typename T1>
62 tuple(T0 t0, T1 t1)
63 : boost::python::tuple(boost::python::make_tuple(t0,t1)) {}
64 template<typename T0, typename T1, typename T2, typename T3>
65 tuple(T0 t0, T1 t1, T2 t2, T3 t3)
66 : boost::python::tuple(boost::python::make_tuple(t0,t1,t2,t3)) {}
67 };
68
69 void error() {
70 PyErr_Print();
71 };
72
73 void exec(const std::string &cmd) {
74 std::cout << cmd << std::endl;
75 PyRun_SimpleString(cmd.c_str());
76 }
77
78 void exec(const boost::python::object &cmd) {
79 exec(boost::python::extract<std::string>(cmd));
80 }
81
82 void interactive() {
83 namespace py = boost::python;
84 //update(this->main_module, this->main);
85 PyRun_InteractiveLoop(stdin, "<stdin>");
86 }
87
88 // void update(boost::python::object ns, boost::python::dict kv) {
89 // namespace py = boost::python;
90 // py::list keys = py::list(kv.iterkeys());
91 // for (size_t i = 0; i < py::len(keys); ++i) {
92 // ns.attr(keys[i]) = kv[keys[i]];
93 // }
94 // }
95
96 // try {
97 // py::object main =
98 // py::object(py::handle<>(py::borrowed(PyImport_AddModule("__main__"))));
99 // py::object ns = main.attr("__dict__");
100 // ns["value"] = "fuck";
101 // PyRun_SimpleString("import readline\n");
102 // // boost::python::exec("import readline\n");
103 // PyRun_InteractiveLoop(stdin, "<stdin>");
104 // }
105 // catch (...) {
106 // PyErr_Print();
107 // }
108 // }
109
110};
111
112
113#endif // HAVE_PYTHON
114
115#endif /* PYTHON_HPP */

Generated at Wed Sep 25 2024 02:45:30 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.12.0.