Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
python_compile.h File Reference
#include "cs_python.h"
#include "hashtable.h"
#include <string>
#include <compile.h>

Go to the source code of this file.

Classes

class  PythonBasicType
 
class  BasicPointer< T >
 

Functions

void InterpretPython (const std::string &filename)
 
PyCodeObject * CompilePython (const std::string &filename)
 
void CompileRunPython (const std::string &filename)
 
PyObject * CreateTuple (const std::vector< PythonBasicType > &values)
 

Variables

Hashtable< std::string,
PyCodeObject, 1023 > 
compiled_python
 

Function Documentation

PyCodeObject* CompilePython ( const std::string &  filename)

Definition at line 49 of file python_compile.cpp.

References fprintf, Hashtable< KEY, VALUE, SIZ >::Get(), getCompilingName(), LoadString(), and Hashtable< KEY, VALUE, SIZ >::Put().

Referenced by CompileRunPython().

50 {
51  Python::reseterrors();
52  PyCodeObject *retval = compiled_python.Get( name );
53  Python::reseterrors();
54  if (retval)
55  return retval;
56  char *str = LoadString( name.c_str() );
57  if (str) {
58  fprintf( stdout, "Compiling python module %s\n", name.c_str() );
59 
60  std::string compiling_name = getCompilingName( name ).c_str();
61  char *temp = strdup( compiling_name.c_str() );
62 
63  retval = (PyCodeObject*) Py_CompileString( str, temp, Py_file_input );
64  if (retval)
65  compiled_python.Put( name, retval );
66  free( temp );
67  free( str );
68  }
69  return retval;
70 }
void CompileRunPython ( const std::string &  filename)

Definition at line 72 of file python_compile.cpp.

References CompilePython(), d, VegaConfig::getVariable(), InterpretPython(), XMLSupport::parse_bool(), and vs_config.

Referenced by ChooseNavPoint(), DockedScript(), PythonAI< SuperClass >::Factory(), PythonClass< PythonMissionBaseClass >::Factory(), RunPython(), RunPythonPhysicsFrame(), RunPythonPress(), RunPythonRelease(), and RunPythonToggle().

73 {
74  static bool ndebug_libs = XMLSupport::parse_bool( vs_config->getVariable( "AI", "compile_python", "true" ) );
75  if (ndebug_libs) {
76  Python::reseterrors();
77  PyCodeObject *CompiledProgram = CompilePython( filename );
78  Python::reseterrors();
79  if (CompiledProgram) {
80  PyObject *m, *d;
81  static char main_str[16] = "__main__"; //by chuck_starchaser, to squash a warning
82  if ( ( m = PyImport_AddModule( main_str ) ) != NULL ) {
83  PyObject *localdict = PyDict_New();
84  if ( ( d = PyModule_GetDict( m ) ) != NULL ) {
85  PyObject *exe = PyEval_EvalCode( CompiledProgram, d, localdict );
86  Py_XDECREF( exe );
87  //unref exe?
88  }
89  Py_XDECREF( localdict );
90  }
91  }
92  } else {
93  Python::reseterrors();
94  InterpretPython( filename );
95  Python::reseterrors();
96  }
97 }
PyObject* CreateTuple ( const std::vector< PythonBasicType > &  values)

Definition at line 99 of file python_compile.cpp.

References i.

100 {
101  PyObject *retval = PyTuple_New( values.size() );
102  for (unsigned int i = 0; i < values.size(); i++) {
103  PyObject *val = values[i].NewObject();
104  if (val)
105  PyTuple_SET_ITEM( retval, i, val );
106  }
107  return retval;
108 }
void InterpretPython ( const std::string &  filename)

Definition at line 37 of file python_compile.cpp.

References getCompilingName(), VSFileSystem::vs_close(), and VSFileSystem::vs_open().

Referenced by CompileRunPython().

38 {
39  char *temp = strdup( getCompilingName( name ).c_str() );
40  FILE *fp = VSFileSystem::vs_open( name.c_str(), "r" );
41  if (fp) {
42  PyRun_SimpleFile( fp, temp );
43  Python::reseterrors();
45  }
46  free( temp );
47 }

Variable Documentation

Hashtable< std::string, PyCodeObject, 1023 > compiled_python

Definition at line 11 of file python_compile.cpp.