1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (2008) Alexander Stukowski
//
// This file is part of OVITO (Open Visualization Tool).
//
// OVITO is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// OVITO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
// Include the Boost.Python library.
#include <boost/python.hpp>
#include <boost/python/to_python_converter.hpp>
#include <boost/python/object.hpp>
#include <boost/python/handle.hpp>
#if WIN32
#define DLL_EXPORT_DECL __declspec(dllexport)
#else
#define DLL_EXPORT_DECL
#endif
using namespace boost::python;
/*
class HelloWorldClass {
public:
void print() {
std::cout << "Hello World!" << std::endl;
}
};
BOOST_PYTHON_MODULE(HelloWorld)
{
class_<HelloWorldClass>("HelloWorldClass")
.def("SayHello", &HelloWorldClass::print)
;
}
*/
DLL_EXPORT_DECL void RunScript()
{
std::cout << "Python Program Name: " << Py_GetProgramName() << std::endl;
std::cout << "Python Program Full Path: " << Py_GetProgramFullPath() << std::endl;
std::cout << "Python Prefix: " << Py_GetPrefix() << std::endl;
std::cout << "Python Exec Prefix: " << Py_GetExecPrefix() << std::endl;
std::cout << "Python Path: " << Py_GetPath() << std::endl;
Py_Initialize();
using namespace boost::python;
try {
object main_module = object(handle< >(borrowed(PyImport_AddModule("__main__"))));
object main_namespace = object(handle< >(borrowed(PyModule_GetDict(main_module.ptr()))));
object sys_module = object(handle< >(PyImport_ImportModule("sys")));
object sys_namespace = object(handle< >(borrowed(PyModule_GetDict(sys_module.ptr()))));
// Make a copy of the initial main namespace. The original namespace dictionary
// is not touched by the script.
object main_namespace_copy(handle<>(PyDict_Copy(main_namespace.ptr())));
// Run the user script.
handle<> ignored2((PyRun_String(
"print \"Running python script\"\n"
"import math\n"
"print \"Cos = %f\" % math.cos(1)\n"
//"import HelloWorld\n"
//"obj = HelloWorld.HelloWorldClass()\n"
//"obj.SayHello()"
,Py_file_input,
main_namespace_copy.ptr(), main_namespace_copy.ptr())));
}
catch(error_already_set) {
PyErr_Print();
std::cerr << "Python interpreter has exited with an error." << std::endl;
}
catch(...) {
std::cerr << "Unhandled exception thrown by Python interpreter." << std::endl;
}
Py_Finalize();
}
class Initializer {
public:
Initializer() { RunScript(); }
};
static Initializer init;
|