9 #include <boost/version.hpp>
10 #if BOOST_VERSION != 102800
11 #if defined (_MSC_VER) && _MSC_VER <= 1200
14 #include <boost/python.hpp>
15 #include <boost/python/converter/from_python.hpp>
16 #if defined (_MSC_VER) && _MSC_VER <= 1200
20 #include <boost/python/class_builder.hpp>
21 #include <boost/python/detail/extension_class.hpp>
30 #if defined (_WIN32) && !defined (__CYGWIN__)
35 #ifdef OLD_PYTHON_TEST
39 public: hello(
const std::string &country )
41 this->country = country;
43 virtual std::string greet()
const
45 return "Hello from "+country;
53 struct hello_callback : hello
56 hello_callback( PyObject *self_,
const std::string &
x )
61 hello_callback( PyObject *self_,
const hello &
x )
66 std::string greet() const
68 return boost::python::callback< std::string >::call_method(
self,
"greet" );
70 virtual ~hello_callback()
75 static std::string default_greet(
const hello &self_ )
77 return self_.hello::greet();
88 public: MyBaseClass(
int set )
92 virtual void foo(
int set )
100 virtual ~MyBaseClass() {}
103 class MyDerivedClass : MyBaseClass
106 public: MyDerivedClass(
int set ) : MyBaseClass( set )
110 virtual void foo(
int set )
118 virtual ~MyDerivedClass() {}
125 class PythonVarConfig
132 myattr( std::string nam, std::string val )
138 std::vector< myattr >myvar;
139 std::string MyGetVariable( std::string name,
int &loc )
const
141 std::vector< myattr >::const_iterator iter = myvar.begin();
142 std::string value =
"";
144 while ( iter != myvar.end() ) {
145 if ( (*iter).name == name ) {
146 value = (*iter).value;
153 void setVariable(
const std::string &name,
const std::string &value )
155 printf(
"variable %s set to %s.\n", name.c_str(), value.c_str() );
157 std::string newval = MyGetVariable( name, loc );
158 if ( newval.empty() )
159 myvar.push_back( myattr( name, value ) );
161 myvar[loc].value = value;
163 std::string
getVariable(
const std::string &name )
const
166 std::string value = MyGetVariable( name, loc );
168 value =
"<UNDEFINED>";
169 printf(
"variable %s is %s\n", name.c_str(), value.c_str() );
177 static std::strstream
buffer;
178 static void write( PythonIOString &
self,
string data )
212 printf(
"CommAI\n" );
217 BOOST_PYTHON_MODULE_INIT( Vegastrike )
220 boost::python::module_builder vs(
"VS" );
238 boost::python::class_builder< MyFA >FA( vs,
"CommAI" );
239 FA.def( &MyFA::Execute,
"Execute" );
240 FA.def( boost::python::constructor< > () );
241 boost::python::class_builder< PythonIOString >
243 IO.def( boost::python::constructor< > () );
247 IO.def( PythonIOString::write,
"write" );
264 void Python::initpaths()
285 std::string modpaths(
"" );
302 std::string changepath(
"import sys\nprint sys.path\nsys.path = ["+modpaths+
"] + sys.path\n" );
311 char *temppython = strdup( changepath.c_str() );
312 PyRun_SimpleString( temppython );
313 Python::reseterrors();
317 void Python::reseterrors()
319 if ( PyErr_Occurred() ) {
346 #if BOOST_VERSION != 102800
347 static void * Vector_convertible( PyObject *p )
349 return PyTuple_Check( p ) ? p : 0;
353 static void Vector_construct( PyObject *source, boost::python::converter::rvalue_from_python_stage1_data *data )
355 void*
const storage = ( (boost::python::converter::rvalue_from_python_storage< Vector >*)data )->storage.bytes;
356 new (storage)
Vector( 0, 0, 0 );
360 static char fff[4] =
"fff";
361 PyArg_ParseTuple( source, fff, &vec->i, &vec->j, &vec->k );
362 data->convertible = storage;
365 static void QVector_construct( PyObject *source, boost::python::converter::rvalue_from_python_stage1_data *data )
367 void*
const storage = ( (boost::python::converter::rvalue_from_python_storage< QVector >*)data )->storage.bytes;
368 new (storage)
QVector( 0, 0, 0 );
372 static char ddd[4] =
"ddd";
373 PyArg_ParseTuple( source, ddd, &vec->i, &vec->j, &vec->k );
374 data->convertible = storage;
380 static bool isinit =
false;
388 #if BOOST_VERSION != 102800
389 boost::python::converter::registry::insert( Vector_convertible, QVector_construct, boost::python::type_id< QVector > () );
390 boost::python::converter::registry::insert( Vector_convertible, Vector_construct, boost::python::type_id< Vector > () );
395 std::string changepath(
"import sys\nprint sys.path\n" );
397 char *temppython = strdup( changepath.c_str() );
398 PyRun_SimpleString( temppython );
399 Python::reseterrors();
439 #if 0 //defined(WIN32)
440 FILE *fp = VSFileSystem::OpenFile(
"config.py",
"r" );
442 freopen(
"stderr",
"w", stderr );
443 freopen(
"stdout",
"w", stdout );
445 FILE *fp1 = VSFileSystem::OpenFile(
"config.py",
"r" );
450 PyRun_SimpleFile( fp,
"config.py" );
451 VSFileSystem::Close( fp1 );
454 #ifdef OLD_PYTHON_TEST
464 "sys.stderr.write('asdf')\n"
471 "class MyAI(VS.CommAI):\n"
472 " def Execute(self):\n"
473 " sys.stdout.write('MyAI\\n')\n"
475 "hi1 = VS.CommAI()\n"
476 "print hi1.Execute()\n"
477 "print hi2.Execute()\n"