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