[Stackless-checkins] CVS: slpdev/src/2.2/src/Stackless/testbench testbench.cpp, 1.3, 1.4

Christian Tismer tismer at centera.de
Fri Apr 16 00:31:44 CEST 2004


Update of /home/cvs/slpdev/src/2.2/src/Stackless/testbench
In directory centera.de:/tmp/cvs-serv27791/src/2.3/dev/Stackless/testbench

Modified Files:
	testbench.cpp 
Log Message:
a current, very wrong approach to embedding. Doesn't work, need rewrite, sorry.

Index: testbench.cpp
===================================================================
RCS file: /home/cvs/slpdev/src/2.2/src/Stackless/testbench/testbench.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** testbench.cpp	6 Jan 2004 02:40:23 -0000	1.3
--- testbench.cpp	15 Apr 2004 22:31:42 -0000	1.4
***************
*** 7,25 ****
  
  
! // Dummy class 
! class rFoo
! {
! public:
! 	rFoo() {}
! 
! };
! 
  
  typedef struct {
  	PyObject_HEAD
! 	rFoo *m_foo;
  } PyPythonScriptObject;
  
! static PyObject *PyPythonScript_NewScript(rFoo *script);
  static void PyPythonScript_DeallocScript(PyPythonScriptObject *pso);
  static PyObject *PyPythonScript_GetAttr(PyPythonScriptObject *self, char *name);
--- 7,18 ----
  
  
! class rTasklet;
  
  typedef struct {
  	PyObject_HEAD
! 	rTasklet *m_foo;
  } PyPythonScriptObject;
  
! static PyObject *PyPythonScript_NewScript(rTasklet *script);
  static void PyPythonScript_DeallocScript(PyPythonScriptObject *pso);
  static PyObject *PyPythonScript_GetAttr(PyPythonScriptObject *self, char *name);
***************
*** 48,52 ****
  
  static PyObject *
! PyPythonScript_NewScript(rFoo *foo)
  {
    PyPythonScriptObject *pso;
--- 41,45 ----
  
  static PyObject *
! PyPythonScript_NewScript(rTasklet *foo)
  {
    PyPythonScriptObject *pso;
***************
*** 140,144 ****
  /* note: to switch back to windows mode, change
   * /subsystem:console   back to
!  * /subsystem:windows   in the cofiguration window.
   */
  
--- 133,137 ----
  /* note: to switch back to windows mode, change
   * /subsystem:console   back to
!  * /subsystem:windows   in the configuration window.
   */
  
***************
*** 157,207 ****
  }
  
! #if 0
! int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
! #else
! int
! main(int argc, char **argv)
! #endif
  {
!   char *msg = NULL;
!   // 1. Init python and stackless 
!   Py_Initialize();
!   int code = Py_IsInitialized();
!   /* no threads needed */
  
!   // 2. Init execution wrapper
!   PySW_InitWrapper();
  
!   // 3. Store this instances threadstate
!   PyThreadState *m_interp = PyEval_SaveThread();
  
!   // 4. Each script will have its own interpreter
!   PyEval_AcquireLock();
!   m_interp = Py_NewInterpreter();
!   PyThreadState_Swap(m_interp);
  
!   // 5. Add extension module to test script execution
!   rFoo foobar;
!   PyPythonScript_Type.ob_type = &PyType_Type;
!   PyObject *iface_class = PyPythonScript_NewScript(&foobar);
!   PyImport_AddModule("simscript");
!   Py_InitModule4("simscript", PyPythonScript_methods, NULL, iface_class, PYTHON_API_VERSION);
  
!   // 6. Use main
!   PyObject *pName, *pModule, *pDict;
!   pName = PyString_FromString("__main__");
!   pModule = PyImport_Import(pName);
  
!   if (pModule != NULL)
!   {
  
-     PySW_SetPath("C:\\codebase\\main\\development\\3rdparty\\stackless_wrapper\\testbench");
  
!     PyObject *stacklessmodule = PyImport_AddModule("stackless");
!     pDict = PyModule_GetDict(pModule);
  
!     // 7. Compile code (below)
  
!     char sourcecode[] = "           \n\
                                      \n\
  import simscript                    \n\
--- 150,279 ----
  }
  
! 
! // Wrapper tasklet class 
! class rTasklet
  {
! public:
! 	PyThreadState       *m_interp;
! 	PyObject            *m_script_code;
! 	PyObject            *m_tasklet;
  
! public:
! 	rTasklet() 
! 	{
! 	  m_interp = Py_NewInterpreter();
! 	  PyThreadState_Swap(m_interp);
  
!       PyPythonScript_Type.ob_type = &PyType_Type;
!       PyObject *iface_class = PyPythonScript_NewScript(this);
!       PyImport_AddModule("simscript");
!       Py_InitModule4("simscript", PyPythonScript_methods, NULL, iface_class, PYTHON_API_VERSION);
  
!       PyObject *pName, *pModule, *pDict;
!       pName = PyString_FromString("__main__");
!       pModule = PyImport_Import(pName);
  
!       PySW_SetPath("C:\\codebase\\main\\development\\3rdparty\\stackless_wrapper\\testbench");
!       PyObject *stacklessmodule = PyImport_AddModule("stackless");
!       pDict = PyModule_GetDict(pModule);
! 	}
  
! 	void CompileAndExecuteMain( char* code )
! 	{
! 	  PyThreadState_Swap(m_interp);
!       m_script_code = Py_CompileString(code, "pythoncode", Py_file_input);
! 	  Py_INCREF(m_script_code);
  
! 	  PyObject *mainobj = PyImport_ExecCodeModule("__main__", m_script_code);
! 	  if( !mainobj )
! 		PyErr_Print();
! 	}
  
  
!     PyObject *CreateTask(char* func_name )
! 	{
! 	  PyThreadState_Swap(m_interp);
!       PyObject *pName, *pModule, *pDict;
!       pName = PyString_FromString("__main__");
!       pModule = PyImport_Import(pName);
!       pDict = PyModule_GetDict(pModule);
  
! 	  m_tasklet = NULL;
!       PyObject *func = PyDict_GetItemString(pDict, func_name);
!       if (func && PyCallable_Check(func))
!         m_tasklet = PySW_CreateTasklet( func );
!       Py_XINCREF(m_tasklet);
! 	  return m_tasklet;
!     }
  
! 	void RunTask( char* msg )
! 	{
! 	  PyThreadState_Swap(m_interp);
!       CPCE(PySW_RunTasklet(m_tasklet), msg);
! 	}
! 
! 	void ContinueTask(char* msg )
! 	{
! 	  PyThreadState_Swap(m_interp);
!       CPCE(PySW_ContinueTasklet(m_tasklet), msg);
! 	}
! 
! 	void ResetTask(char* msg )
! 	{
! 	  PyThreadState_Swap(m_interp);
!       CPCE(PySW_ResetTasklet(m_tasklet), msg);
! 	}
! 
! 
! 	void DeleteTask( char* msg )
! 	{
! 	  if( m_tasklet )
! 	  {
! 		PyThreadState_Swap(m_interp);
! 		CPCE(PySW_CleanupTasklet( m_tasklet ), msg);
! 		// Py_XDECREF(m_tasklet);
! 		m_tasklet = NULL;
! 	  }
! 	}
! 
!     PyObject *GetTask()
! 	{
!       return m_tasklet;
! 	}
! 
!     PyObject *GetFunction(char *func_name)
! 	{
! 	  PyThreadState_Swap(m_interp);
!       PyObject *pName, *pModule, *pDict;
!       pName = PyString_FromString("__main__");
!       pModule = PyImport_Import(pName);
!       pDict = PyModule_GetDict(pModule);
!       return PyDict_GetItemString(pDict, func_name);
! 	}
! 
! 
! 	void Destroy( char* msg )
! 	{
! 
!       PyThreadState *old_state = PyThreadState_Swap(m_interp);
! 
!       PyThreadState_Clear(m_interp);
!       // Interpreter deletion deos not work yet! And it leakes now memory ...
! 	  // PyThreadState_Swap(NULL); // Should not be required ... nobody has tested these python calls together
!       // PyThreadState_Delete(m_interp);
! 	  // PyThreadState_Swap(NULL);
! 	  // PyThreadState_Swap(m_interp);
!       // CPCE( true, msg);
! 	  Py_EndInterpreter( m_interp );
! 
!     }	
!  
! };
! 
! 
! 
! 
! 
! char sourcecode[] = "           \n\
                                      \n\
  import simscript                    \n\
***************
*** 233,321 ****
  ";
  
-     PyObject *m_script_code = Py_CompileString(sourcecode, "pythoncode", Py_file_input);
- 	Py_INCREF(m_script_code);
-     
- 	if( m_script_code )
-     { int *argll = (int*)_alloca(10000);
- 
- 	  for(int i=0; i<10; i++)
- 	  {
  
  
!       // 8. And reload it as current main module for execution
! 	  PyObject *mainobj = PyImport_ExecCodeModule("__main__", m_script_code);
! 	  if( !mainobj )
! 		PyErr_Print();
! 
! 	  // 9. Init main tasklet
! 	  /* this is done automatically */
  
! 	  // 10. Extract tasklet 1 method from script and create tasklet
! 	  PyObject *task1 = NULL;
!       PyObject *func = PyDict_GetItemString(pDict, "run1");
!       if (func && PyCallable_Check(func))
!         task1 = PySW_CreateTasklet( func );
  
!       // 11. Extract tasklet 2 method from script and create tasklet
! 	  PyObject *task2 = NULL;
!       func = PyDict_GetItemString(pDict, "run2");
!       if (func && PyCallable_Check(func))
!         task2 = PySW_CreateTasklet( func );
  
! 	  // 12. Execute tasklet 1 & 2 code till defined execution points
! 	  if( task1 && task2 )
! 	  {
! 	    msg = "13. Execute tasklet 1 till point \"A\"";
! 		CPCE(PySW_RunTasklet(task1), msg);
  
! 		msg = "14. Execute tasklet 2 till point \"C\"";
! 		CPCE(PySW_RunTasklet(task2), msg);
  
!         msg = "15. Execute tasklet 1 till point \"B\"";
! 		CPCE(PySW_ContinueTasklet( task1 ), msg);
!          
! 		msg = "16. Emulate interpreter switch";
! 		/* CT dropped */
  
! 	    msg = "17. Call condition func in a middle of tasklet 1 execution";
!         PyObject *condf = PyDict_GetItemString(pDict, "condition_func");
!         printf("%s returns %d\n", msg, TestConditionFunction( condf ));
!         
!         msg = "18. Execute tasklet 2 till point \"D\"";
! 		CPCE(PySW_ContinueTasklet( task2 ), msg);
  
! 		msg = "19. Execute tasklet 1 till end";
! 		CPCE(PySW_ContinueTasklet( task1 ), msg);
  
! 		msg = "20. Reset tasklet 1 execution";
! 		CPCE(PySW_ResetTasklet( task1 ), msg);
  
! 		msg = "21. Cleanup tasklet 1 execution";
! 		CPCE(PySW_CleanupTasklet( task1 ), msg);
  
! 		msg = "22. Cleanup tasklet 2 execution (suspended)";
! 		CPCE(PySW_CleanupTasklet( task2 ), msg);
  
!       }
  
! 	    Py_XDECREF(task1);
! 		Py_XDECREF(task2);
  
- 	  }
  
  
!     }
! 	else
! 	{
! 	  // 21. Syntax error at script code
! 	  PyErr_Print();
! 	}
  
    }
  
-   // 22. Release execution context
-   /* CT dropped */
- 
-   Py_Finalize();
    return 0;
  }
--- 305,380 ----
  ";
  
  
  
! #if 0
! int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
! #else
! int
! main(int argc, char **argv)
! #endif
! {
!   char *msg = NULL;
!   // 1. Init python and stackless 
!   PyEval_InitThreads();
!   Py_Initialize();
!   int code = Py_IsInitialized();
!   /* no threads needed */
  
!   // 2. Init execution wrapper
!   PySW_InitWrapper();
  
! /*
  
!   // 3. Store this instances threadstate
!   PyThreadState *m_interp = PyEval_SaveThread();
  
!   // 4. Each script will have its own interpreter
!   PyEval_AcquireLock();
!   m_interp = Py_NewInterpreter();
!   PyThreadState_Swap(m_interp);
  
! */
  
!   for( int loops = 0; loops<100; loops++)
!   {
!     // 5. Create two task objects (both contain independent interpreter and tasklet defs)
!     rTasklet *task1 = new rTasklet();
!     rTasklet *task2 = new rTasklet();
  
!     // 6. Assign source code and execute main level for both tasklets objects
!     task1->CompileAndExecuteMain( sourcecode );
!     task2->CompileAndExecuteMain( sourcecode );
  
!     // 7. Create new tasklet for both tasklet objects
!     task1->CreateTask( "run1" );
!     task2->CreateTask( "run2" );
  
!     // 8. Start executing tasks in sequence
!     task1->RunTask("8. Execute tasklet 1 till point \"A\"");
!     task2->RunTask("9. Execute tasklet 2 till point \"C\"");
!     task1->ContinueTask("10. Continue tasklet 1 till point \"B\"");
  
!     /* CT dropped */
  
!     msg = "11. Call condition func in a middle of tasklet 1 execution";
!     PyObject *condf = task1->GetFunction("condition_func");
!     printf("%s returns %d\n", msg, TestConditionFunction( condf ));
!         
!     task2->ContinueTask("12. Continue tasklet 2 till point \"D\"");
!     task1->ContinueTask("13. Continue tasklet 1 till end");
  
!     task1->ResetTask("14. Reset tasklet 1");
!     task1->DeleteTask("15. Delete tasklet 1");
  
  
+     task2->DeleteTask("16. Delete tasklet 2 (suspended)");
  
!     task1->Destroy( "17. Destroy tasklet 1 internals (interpreter etc.)");
!     task2->Destroy( "18. Destroy tasklet 2 internals (interpreter etc.)");
  
+ 	delete task1;
+     delete task2;
    }
  
    return 0;
  }


_______________________________________________
Stackless-checkins mailing list
Stackless-checkins at stackless.com
http://www.stackless.com/mailman/listinfo/stackless-checkins



More information about the Stackless-checkins mailing list