_bootstrap_python.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Frozen modules bootstrap
  2. *
  3. * Limited and restricted Python interpreter to run
  4. * "Tools/build/deepfreeze.py" on systems with no or older Python
  5. * interpreter.
  6. */
  7. #include "Python.h"
  8. #include "pycore_import.h"
  9. /* Includes for frozen modules: */
  10. #include "Python/frozen_modules/importlib._bootstrap.h"
  11. #include "Python/frozen_modules/importlib._bootstrap_external.h"
  12. #include "Python/frozen_modules/zipimport.h"
  13. /* End includes */
  14. uint32_t _Py_next_func_version = 1;
  15. /* Empty initializer for deepfrozen modules */
  16. int _Py_Deepfreeze_Init(void)
  17. {
  18. return 0;
  19. }
  20. /* Empty finalizer for deepfrozen modules */
  21. void
  22. _Py_Deepfreeze_Fini(void)
  23. {
  24. }
  25. /* Note that a negative size indicates a package. */
  26. static const struct _frozen bootstrap_modules[] = {
  27. {"_frozen_importlib", _Py_M__importlib__bootstrap, (int)sizeof(_Py_M__importlib__bootstrap)},
  28. {"_frozen_importlib_external", _Py_M__importlib__bootstrap_external, (int)sizeof(_Py_M__importlib__bootstrap_external)},
  29. {"zipimport", _Py_M__zipimport, (int)sizeof(_Py_M__zipimport)},
  30. {0, 0, 0} /* bootstrap sentinel */
  31. };
  32. static const struct _frozen stdlib_modules[] = {
  33. {0, 0, 0} /* stdlib sentinel */
  34. };
  35. static const struct _frozen test_modules[] = {
  36. {0, 0, 0} /* test sentinel */
  37. };
  38. const struct _frozen *_PyImport_FrozenBootstrap = bootstrap_modules;
  39. const struct _frozen *_PyImport_FrozenStdlib = stdlib_modules;
  40. const struct _frozen *_PyImport_FrozenTest = test_modules;
  41. static const struct _module_alias aliases[] = {
  42. {"_frozen_importlib", "importlib._bootstrap"},
  43. {"_frozen_importlib_external", "importlib._bootstrap_external"},
  44. {0, 0} /* aliases sentinel */
  45. };
  46. const struct _module_alias *_PyImport_FrozenAliases = aliases;
  47. /* Embedding apps may change this pointer to point to their favorite
  48. collection of frozen modules: */
  49. const struct _frozen *PyImport_FrozenModules = NULL;
  50. int
  51. #ifdef MS_WINDOWS
  52. wmain(int argc, wchar_t **argv)
  53. #else
  54. main(int argc, char **argv)
  55. #endif
  56. {
  57. PyStatus status;
  58. PyConfig config;
  59. PyConfig_InitIsolatedConfig(&config);
  60. // don't warn, pybuilddir.txt does not exist yet
  61. config.pathconfig_warnings = 0;
  62. // parse arguments
  63. config.parse_argv = 1;
  64. // add current script dir to sys.path
  65. config.isolated = 0;
  66. config.safe_path = 0;
  67. #ifdef MS_WINDOWS
  68. status = PyConfig_SetArgv(&config, argc, argv);
  69. #else
  70. status = PyConfig_SetBytesArgv(&config, argc, argv);
  71. #endif
  72. if (PyStatus_Exception(status)) {
  73. goto error;
  74. }
  75. status = PyConfig_Read(&config);
  76. if (config.run_filename == NULL) {
  77. status = PyStatus_Error("Run filename expected");
  78. goto error;
  79. }
  80. #define CLEAR(ATTR) \
  81. do { \
  82. PyMem_RawFree(ATTR); \
  83. ATTR = NULL; \
  84. } while (0)
  85. // isolate from system Python
  86. CLEAR(config.base_prefix);
  87. CLEAR(config.prefix);
  88. CLEAR(config.base_exec_prefix);
  89. CLEAR(config.exec_prefix);
  90. status = Py_InitializeFromConfig(&config);
  91. if (PyStatus_Exception(status)) {
  92. goto error;
  93. }
  94. PyConfig_Clear(&config);
  95. return Py_RunMain();
  96. error:
  97. PyConfig_Clear(&config);
  98. if (PyStatus_IsExit(status)) {
  99. return status.exitcode;
  100. }
  101. Py_ExitStatusException(status);
  102. }