frozen.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Frozen modules initializer
  2. *
  3. * Frozen modules are written to header files by Programs/_freeze_module.
  4. * These files are typically put in Python/frozen_modules/. Each holds
  5. * an array of bytes named "_Py_M__<module>", which is used below.
  6. *
  7. * These files must be regenerated any time the corresponding .pyc
  8. * file would change (including with changes to the compiler, bytecode
  9. * format, marshal format). This can be done with "make regen-frozen".
  10. * That make target just runs Tools/build/freeze_modules.py.
  11. *
  12. * The freeze_modules.py script also determines which modules get
  13. * frozen. Update the list at the top of the script to add, remove,
  14. * or modify the target modules. Then run the script
  15. * (or run "make regen-frozen").
  16. *
  17. * The script does the following:
  18. *
  19. * 1. run Programs/_freeze_module on the target modules
  20. * 2. update the includes and _PyImport_FrozenModules[] in this file
  21. * 3. update the FROZEN_FILES variable in Makefile.pre.in
  22. * 4. update the per-module targets in Makefile.pre.in
  23. * 5. update the lists of modules in PCbuild/_freeze_module.vcxproj and
  24. * PCbuild/_freeze_module.vcxproj.filters
  25. *
  26. * (Note that most of the data in this file is auto-generated by the script.)
  27. *
  28. * Those steps can also be done manually, though this is not recommended.
  29. * Expect such manual changes to be removed the next time
  30. * freeze_modules.py runs.
  31. * */
  32. /* In order to test the support for frozen modules, by default we
  33. define some simple frozen modules: __hello__, __phello__ (a package),
  34. and __phello__.spam. Loading any will print some famous words... */
  35. #include "Python.h"
  36. #include "pycore_import.h"
  37. #include <stdbool.h>
  38. /* Includes for frozen modules: */
  39. #include "frozen_modules/importlib._bootstrap.h"
  40. #include "frozen_modules/importlib._bootstrap_external.h"
  41. #include "frozen_modules/zipimport.h"
  42. #include "frozen_modules/abc.h"
  43. #include "frozen_modules/codecs.h"
  44. #include "frozen_modules/io.h"
  45. #include "frozen_modules/_collections_abc.h"
  46. #include "frozen_modules/_sitebuiltins.h"
  47. #include "frozen_modules/genericpath.h"
  48. #include "frozen_modules/ntpath.h"
  49. #include "frozen_modules/posixpath.h"
  50. #include "frozen_modules/os.h"
  51. #include "frozen_modules/site.h"
  52. #include "frozen_modules/stat.h"
  53. #include "frozen_modules/importlib.util.h"
  54. #include "frozen_modules/importlib.machinery.h"
  55. #include "frozen_modules/runpy.h"
  56. #include "frozen_modules/__hello__.h"
  57. #include "frozen_modules/__phello__.h"
  58. #include "frozen_modules/__phello__.ham.h"
  59. #include "frozen_modules/__phello__.ham.eggs.h"
  60. #include "frozen_modules/__phello__.spam.h"
  61. #include "frozen_modules/frozen_only.h"
  62. /* End includes */
  63. #define GET_CODE(name) _Py_get_##name##_toplevel
  64. /* Start extern declarations */
  65. extern PyObject *_Py_get_importlib__bootstrap_toplevel(void);
  66. extern PyObject *_Py_get_importlib__bootstrap_external_toplevel(void);
  67. extern PyObject *_Py_get_zipimport_toplevel(void);
  68. extern PyObject *_Py_get_abc_toplevel(void);
  69. extern PyObject *_Py_get_codecs_toplevel(void);
  70. extern PyObject *_Py_get_io_toplevel(void);
  71. extern PyObject *_Py_get__collections_abc_toplevel(void);
  72. extern PyObject *_Py_get__sitebuiltins_toplevel(void);
  73. extern PyObject *_Py_get_genericpath_toplevel(void);
  74. extern PyObject *_Py_get_ntpath_toplevel(void);
  75. extern PyObject *_Py_get_posixpath_toplevel(void);
  76. extern PyObject *_Py_get_os_toplevel(void);
  77. extern PyObject *_Py_get_site_toplevel(void);
  78. extern PyObject *_Py_get_stat_toplevel(void);
  79. extern PyObject *_Py_get_importlib_util_toplevel(void);
  80. extern PyObject *_Py_get_importlib_machinery_toplevel(void);
  81. extern PyObject *_Py_get_runpy_toplevel(void);
  82. extern PyObject *_Py_get___hello___toplevel(void);
  83. extern PyObject *_Py_get___phello___toplevel(void);
  84. extern PyObject *_Py_get___phello___ham_toplevel(void);
  85. extern PyObject *_Py_get___phello___ham_eggs_toplevel(void);
  86. extern PyObject *_Py_get___phello___spam_toplevel(void);
  87. extern PyObject *_Py_get_frozen_only_toplevel(void);
  88. /* End extern declarations */
  89. static const struct _frozen bootstrap_modules[] = {
  90. {"_frozen_importlib", _Py_M__importlib__bootstrap, (int)sizeof(_Py_M__importlib__bootstrap), false, GET_CODE(importlib__bootstrap)},
  91. {"_frozen_importlib_external", _Py_M__importlib__bootstrap_external, (int)sizeof(_Py_M__importlib__bootstrap_external), false, GET_CODE(importlib__bootstrap_external)},
  92. {"zipimport", _Py_M__zipimport, (int)sizeof(_Py_M__zipimport), false, GET_CODE(zipimport)},
  93. {0, 0, 0} /* bootstrap sentinel */
  94. };
  95. static const struct _frozen stdlib_modules[] = {
  96. /* stdlib - startup, without site (python -S) */
  97. {"abc", _Py_M__abc, (int)sizeof(_Py_M__abc), false, GET_CODE(abc)},
  98. {"codecs", _Py_M__codecs, (int)sizeof(_Py_M__codecs), false, GET_CODE(codecs)},
  99. {"io", _Py_M__io, (int)sizeof(_Py_M__io), false, GET_CODE(io)},
  100. /* stdlib - startup, with site */
  101. {"_collections_abc", _Py_M___collections_abc, (int)sizeof(_Py_M___collections_abc), false, GET_CODE(_collections_abc)},
  102. {"_sitebuiltins", _Py_M___sitebuiltins, (int)sizeof(_Py_M___sitebuiltins), false, GET_CODE(_sitebuiltins)},
  103. {"genericpath", _Py_M__genericpath, (int)sizeof(_Py_M__genericpath), false, GET_CODE(genericpath)},
  104. {"ntpath", _Py_M__ntpath, (int)sizeof(_Py_M__ntpath), false, GET_CODE(ntpath)},
  105. {"posixpath", _Py_M__posixpath, (int)sizeof(_Py_M__posixpath), false, GET_CODE(posixpath)},
  106. {"os.path", _Py_M__posixpath, (int)sizeof(_Py_M__posixpath), false, GET_CODE(posixpath)},
  107. {"os", _Py_M__os, (int)sizeof(_Py_M__os), false, GET_CODE(os)},
  108. {"site", _Py_M__site, (int)sizeof(_Py_M__site), false, GET_CODE(site)},
  109. {"stat", _Py_M__stat, (int)sizeof(_Py_M__stat), false, GET_CODE(stat)},
  110. /* runpy - run module with -m */
  111. {"importlib.util", _Py_M__importlib_util, (int)sizeof(_Py_M__importlib_util), false, GET_CODE(importlib_util)},
  112. {"importlib.machinery", _Py_M__importlib_machinery, (int)sizeof(_Py_M__importlib_machinery), false, GET_CODE(importlib_machinery)},
  113. {"runpy", _Py_M__runpy, (int)sizeof(_Py_M__runpy), false, GET_CODE(runpy)},
  114. {0, 0, 0} /* stdlib sentinel */
  115. };
  116. static const struct _frozen test_modules[] = {
  117. {"__hello__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false, GET_CODE(__hello__)},
  118. {"__hello_alias__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false, GET_CODE(__hello__)},
  119. {"__phello_alias__", _Py_M____hello__, (int)sizeof(_Py_M____hello__), true, GET_CODE(__hello__)},
  120. {"__phello_alias__.spam", _Py_M____hello__, (int)sizeof(_Py_M____hello__), false, GET_CODE(__hello__)},
  121. {"__phello__", _Py_M____phello__, (int)sizeof(_Py_M____phello__), true, GET_CODE(__phello__)},
  122. {"__phello__.__init__", _Py_M____phello__, (int)sizeof(_Py_M____phello__), false, GET_CODE(__phello__)},
  123. {"__phello__.ham", _Py_M____phello___ham, (int)sizeof(_Py_M____phello___ham), true, GET_CODE(__phello___ham)},
  124. {"__phello__.ham.__init__", _Py_M____phello___ham, (int)sizeof(_Py_M____phello___ham), false, GET_CODE(__phello___ham)},
  125. {"__phello__.ham.eggs", _Py_M____phello___ham_eggs, (int)sizeof(_Py_M____phello___ham_eggs), false, GET_CODE(__phello___ham_eggs)},
  126. {"__phello__.spam", _Py_M____phello___spam, (int)sizeof(_Py_M____phello___spam), false, GET_CODE(__phello___spam)},
  127. {"__hello_only__", _Py_M__frozen_only, (int)sizeof(_Py_M__frozen_only), false, GET_CODE(frozen_only)},
  128. {0, 0, 0} /* test sentinel */
  129. };
  130. const struct _frozen *_PyImport_FrozenBootstrap = bootstrap_modules;
  131. const struct _frozen *_PyImport_FrozenStdlib = stdlib_modules;
  132. const struct _frozen *_PyImport_FrozenTest = test_modules;
  133. static const struct _module_alias aliases[] = {
  134. {"_frozen_importlib", "importlib._bootstrap"},
  135. {"_frozen_importlib_external", "importlib._bootstrap_external"},
  136. {"os.path", "posixpath"},
  137. {"__hello_alias__", "__hello__"},
  138. {"__phello_alias__", "__hello__"},
  139. {"__phello_alias__.spam", "__hello__"},
  140. {"__phello__.__init__", "<__phello__"},
  141. {"__phello__.ham.__init__", "<__phello__.ham"},
  142. {"__hello_only__", NULL},
  143. {0, 0} /* aliases sentinel */
  144. };
  145. const struct _module_alias *_PyImport_FrozenAliases = aliases;
  146. /* Embedding apps may change this pointer to point to their favorite
  147. collection of frozen modules: */
  148. const struct _frozen *PyImport_FrozenModules = NULL;