PageRenderTime 57ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/TestSuite/TestEmbedPython/main.cpp

https://github.com/ned14/tnfox
C++ | 158 lines | 129 code | 8 blank | 21 comment | 3 complexity | 88643e93f7b903b2a434af5153173f96 MD5 | raw file
Possible License(s): GPL-2.0
  1. /********************************************************************************
  2. * *
  3. * Test of embedding Python *
  4. * *
  5. *********************************************************************************
  6. * Copyright (C) 2003-2007 by Niall Douglas. All Rights Reserved. *
  7. * NOTE THAT I DO NOT PERMIT ANY OF MY CODE TO BE PROMOTED TO THE GPL *
  8. *********************************************************************************
  9. * This code is free software; you can redistribute it and/or modify it under *
  10. * the terms of the GNU Library General Public License v2.1 as published by the *
  11. * Free Software Foundation EXCEPT that clause 3 does not apply ie; you may not *
  12. * "upgrade" this code to the GPL without my prior written permission. *
  13. * Please consult the file "License_Addendum2.txt" accompanying this file. *
  14. * *
  15. * This code is distributed in the hope that it will be useful, *
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
  18. *********************************************************************************
  19. * $Id: *
  20. ********************************************************************************/
  21. #include "fx.h"
  22. #include "FXPython.h"
  23. using namespace boost::python;
  24. static void doThreads1(FXPythonInterp *interp, int ino)
  25. {
  26. FXPython_CtxHold ctxhold(interp);
  27. py( "class TestThread(QThread):\n"
  28. " def __init__(self, interpno, threadno):\n"
  29. " QThread.__init__(self)\n"
  30. " self.interpno=interpno\n"
  31. " self.threadno=threadno\n"
  32. " def run(self):\n"
  33. " while not self.checkForTerminate():\n"
  34. " print 'I am TestThread no',self.threadno,'in interpreter',self.interpno\n"
  35. " #QThread.msleep(1)\n"
  36. " def cleanup(self):\n"
  37. " return 0\n");
  38. py(FXString("a=TestThread(%1, 1); b=TestThread(%2, 2)\n").arg(ino).arg(ino));;
  39. py( "a.start(); b.start();");
  40. }
  41. static void doThreads2(FXPythonInterp *interp)
  42. {
  43. FXPython_CtxHold ctxhold(interp);
  44. handle<> ah=pyeval("a"), bh=pyeval("b");
  45. py( "a.requestTermination(); b.requestTermination();\n"
  46. "a.wait(); b.wait();\n"
  47. "del a; del b\n"
  48. "print 'All threads have ended in this interpreter'\n"
  49. );
  50. assert(ah.get()->ob_refcnt==1); assert(bh.get()->ob_refcnt==1);
  51. }
  52. namespace FX {
  53. extern void *testfunc();
  54. }
  55. int main(int argc, char *argv[])
  56. {
  57. FXProcess myprocess(argc, argv);
  58. fxmessage("TnFOX Python test:\n"
  59. "-=-=-=-=-=-=-=-=-=\n");
  60. try
  61. {
  62. fxmessage("Working with Python %s\n", FXPython::version().text());
  63. FXPythonInterp mainpython(argc, argv, "main interpreter");
  64. py("print 'If you can read this, python prints!'");
  65. void *ret=testfunc();
  66. printf("Typeinfo of pointer returned from DLL is %p,%p (%s)\n", &typeid(*(FXException *) ret), typeid(*(FXException *) ret).name(), typeid(*(FXException *) ret).name());
  67. printf("Typeinfo of FXException in EXE is %p,%p (%s)\n", &typeid(FXException), typeid(FXException).name(), typeid(FXException).name());
  68. printf("Typeinfo of FXPythonException in EXE is %p,%p (%s)\n", &typeid(FXPythonException), typeid(FXPythonException).name(), typeid(FXPythonException).name());
  69. {
  70. FXProcess::MappedFileInfoList list=FXProcess::mappedFiles();
  71. for(FXProcess::MappedFileInfoList::iterator it=list.begin(); it!=list.end(); ++it)
  72. {
  73. FXString temp2(" %1 to %2 %3%4%5%6 %7\n");
  74. temp2.arg((FXulong) (*it).startaddr,-8,16).arg((FXulong) (*it).endaddr,-8,16);
  75. temp2.arg(((*it).read) ? 'r' : '-').arg(((*it).write) ? 'w' : '-').arg(((*it).execute) ? 'x' : '-').arg(((*it).copyonwrite) ? 'c' : '-');
  76. temp2.arg((*it).path);
  77. fxmessage(temp2.text());
  78. }
  79. }
  80. #if 1 // Doesn't work on POSIX
  81. FXERRH_TRY
  82. {
  83. py( "print '\\nThrowing a python exception ...'\n"
  84. "raise IOError, 'A parameter to a python exception'");
  85. FXPythonException f("Test", 0);
  86. FXERRH_THROW(f);
  87. }
  88. FXERRH_CATCH(FXException &e)
  89. {
  90. fxmessage("C++ exception caught: %s\n", e.report().text());
  91. }
  92. FXERRH_ENDTRY
  93. #endif
  94. py("print '\\nCalculating some fibonacci numbers ...'");
  95. py( "def fibGen():\n"
  96. " a=1; b=1; yield a\n"
  97. " while True:\n"
  98. " c=b; b+=a; a=c\n"
  99. " yield a\n"
  100. "def calcFibonacci(no):\n"
  101. " it=fibGen()\n"
  102. " return [it.next() for n in range(0,no)]\n"
  103. "print 'First ten fibonacci numbers are',calcFibonacci(10)");
  104. {
  105. fxmessage("Invoking fibonacci number calculation via BPL ...\n");
  106. object calcFibonacci(pyeval("calcFibonacci"));
  107. list result(calcFibonacci(10));
  108. const char *resultstr=extract<const char *>(str(result));
  109. fxmessage("List from C++ is: %s\n", resultstr);
  110. list slice(result.slice(_, 3));
  111. resultstr=extract<const char *>(str(slice));
  112. fxmessage("Slice [:3] from C++ is: %s\n", resultstr);
  113. object item7(result[7]);
  114. resultstr=extract<const char *>(str(item7));
  115. fxmessage("Item [7] from C++ is: %s\n", resultstr);
  116. }
  117. {
  118. py("def printNum(no): print no");
  119. object printNum(pyeval("printNum"));
  120. long_ bignum((FXulong)-1);
  121. fxmessage("\nThe biggest number C++ can handle: "); printNum(bignum);
  122. bignum*=bignum;
  123. fxmessage("That number multiplied by itself: "); printNum(bignum);
  124. }
  125. fxmessage("\nStarting multiple threads in multiple interpreters ...\n");
  126. FXPythonInterp ointerp;
  127. ointerp.unsetContext();
  128. doThreads1(&mainpython, 0);
  129. doThreads1(&ointerp, 1);
  130. fxmessage("Press Return to end\n");
  131. mainpython.unsetContext();
  132. if(myprocess.isAutomatedTest())
  133. QThread::msleep(10000);
  134. else
  135. getchar();
  136. //mainpython.setContext();
  137. doThreads2(&ointerp);
  138. doThreads2(&mainpython);
  139. }
  140. catch(const FXException &e)
  141. {
  142. fxmessage("Exception thrown: %s\n", e.report().text());
  143. }
  144. fxmessage("All Done!\n");
  145. #ifdef _MSC_VER
  146. if(!myprocess.isAutomatedTest())
  147. getchar();
  148. #endif
  149. return 0;
  150. }