main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file main.c
  10. * @author rdb
  11. * @date 2024-11-03
  12. */
  13. /**
  14. * This script embeds the Python interpreter and runs the unit testing suite.
  15. * It is designed for use with a statically built Panda3D, where the Panda3D
  16. * modules are linked directly into the interpreter.
  17. */
  18. #include <Python.h>
  19. #include "pandabase.h"
  20. #ifdef LINK_ALL_STATIC
  21. extern PyObject *PyInit_core();
  22. #ifdef HAVE_DIRECT
  23. extern PyObject *PyInit_direct();
  24. #endif
  25. #ifdef HAVE_PHYSICS
  26. extern PyObject *PyInit_physics();
  27. #endif
  28. #ifdef HAVE_EGG
  29. extern PyObject *PyInit_egg();
  30. extern EXPCL_PANDAEGG void init_libpandaegg();
  31. #endif
  32. #ifdef HAVE_BULLET
  33. extern PyObject *PyInit_bullet();
  34. #endif
  35. extern EXPCL_PANDA_PNMIMAGETYPES void init_libpnmimagetypes();
  36. #endif // LINK_ALL_STATIC
  37. int main(int argc, char **argv) {
  38. PyStatus status;
  39. PyConfig config;
  40. PyConfig_InitPythonConfig(&config);
  41. PyConfig_SetBytesString(&config, &config.run_module, "pytest");
  42. config.parse_argv = 0;
  43. status = PyConfig_SetBytesArgv(&config, argc, argv);
  44. if (PyStatus_Exception(status)) {
  45. goto exception;
  46. }
  47. status = Py_InitializeFromConfig(&config);
  48. if (PyStatus_Exception(status)) {
  49. goto exception;
  50. }
  51. PyConfig_Clear(&config);
  52. #ifdef LINK_ALL_STATIC
  53. #ifdef HAVE_EGG
  54. init_libpandaegg();
  55. #endif
  56. init_libpnmimagetypes();
  57. {
  58. PyObject *panda3d_module = PyImport_ImportModule("panda3d");
  59. PyObject *panda3d_dict = PyModule_GetDict(panda3d_module);
  60. PyObject *sys_modules = PySys_GetObject("modules");
  61. PyObject *core_module = PyInit_core();
  62. PyDict_SetItemString(panda3d_dict, "core", core_module);
  63. PyDict_SetItemString(sys_modules, "panda3d.core", core_module);
  64. #ifdef HAVE_DIRECT
  65. PyObject *direct_module = PyInit_direct();
  66. PyDict_SetItemString(panda3d_dict, "direct", direct_module);
  67. PyDict_SetItemString(sys_modules, "panda3d.direct", direct_module);
  68. #endif
  69. #ifdef HAVE_PHYSICS
  70. PyObject *physics_module = PyInit_physics();
  71. PyDict_SetItemString(panda3d_dict, "physics", physics_module);
  72. PyDict_SetItemString(sys_modules, "panda3d.physics", physics_module);
  73. #endif
  74. #ifdef HAVE_EGG
  75. PyObject *egg_module = PyInit_egg();
  76. PyDict_SetItemString(panda3d_dict, "egg", egg_module);
  77. PyDict_SetItemString(sys_modules, "panda3d.egg", egg_module);
  78. #endif
  79. #ifdef HAVE_BULLET
  80. PyObject *bullet_module = PyInit_bullet();
  81. PyDict_SetItemString(panda3d_dict, "bullet", bullet_module);
  82. PyDict_SetItemString(sys_modules, "panda3d.bullet", bullet_module);
  83. #endif
  84. }
  85. #endif // LINK_ALL_STATIC
  86. #ifdef __EMSCRIPTEN__
  87. // Default fd capturing doesn't work on emscripten
  88. PyRun_SimpleString("import sys; sys.argv.insert(1, '--capture=sys')");
  89. #endif
  90. #ifdef ANDROID
  91. // No caching on Android
  92. PyRun_SimpleString("import sys; sys.argv.insert(1, '-o cache_dir=/dev/null')");
  93. #endif
  94. return Py_RunMain();
  95. exception:
  96. PyConfig_Clear(&config);
  97. if (PyStatus_IsExit(status)) {
  98. return status.exitcode;
  99. }
  100. Py_ExitStatusException(status);
  101. }