diff options
author | John Wiegley <johnw@newartisans.com> | 2004-09-14 04:47:41 -0400 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2004-09-14 04:47:41 -0400 |
commit | a013b520ba151b9da3adec97d124676a96741b04 (patch) | |
tree | 4ee4d75800f9b9dd08c95283212fbdb7c71c1aee /python.h | |
parent | 6c66d1e0ef09f113bca11cfb5ef0f35eebcc61a3 (diff) | |
download | fork-ledger-a013b520ba151b9da3adec97d124676a96741b04.tar.gz fork-ledger-a013b520ba151b9da3adec97d124676a96741b04.tar.bz2 fork-ledger-a013b520ba151b9da3adec97d124676a96741b04.zip |
python integrated both ways (see sample.dat), and initialized on-demand
Diffstat (limited to 'python.h')
-rw-r--r-- | python.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/python.h b/python.h new file mode 100644 index 00000000..7faefa0d --- /dev/null +++ b/python.h @@ -0,0 +1,55 @@ +#ifndef _PYTHON_H +#define _PYTHON_H + +#include <boost/python.hpp> + +using namespace boost::python; + +namespace ledger { + +struct python_support +{ + handle<> main_module; + dict main_namespace; + + python_support() + : main_module(borrowed(PyImport_AddModule("__main__"))), + main_namespace(handle<>(borrowed(PyModule_GetDict(main_module.get())))) + {} + ~python_support() { + } +}; + +extern python_support * python_interpretor; + +void init_python(); + +inline void python_eval(std::istream& in) +{ + if (! python_interpretor) + init_python(); + + std::string buffer; + buffer.reserve(4096); + while (! in.eof()) { + char buf[256]; + in.getline(buf, 255); + if (buf[0] == '!') + break; + buffer += buf; + buffer += "\n"; + } + + try { + handle<>(borrowed(PyRun_String(buffer.c_str(), Py_file_input, + python_interpretor->main_namespace.ptr(), + python_interpretor->main_namespace.ptr()))); + } + catch(const boost::python::error_already_set&) { + PyErr_Print(); + } +} + +} // namespace ledger + +#endif // _PYTHON_H |