1+ #include "Python.h"
2+ #include "pycore_import.h"
3+
4+ #include <string.h>
5+
6+ PyDoc_STRVAR (frozen_utils__doc__ ,
7+ "Expose the modules that were frozen together with the Python interpreter" );
8+
9+ static int
10+ frozen_utils_exec (PyObject * module )
11+ {
12+ return 0 ;
13+ }
14+
15+ PyDoc_STRVAR (frozen_utils_get_frozen_modules__doc__ ,
16+ "get_frozen_modules() -> List[str]\n\nReturn all of the Python modules & submodules that have \
17+ been frozen with the Python interpreter." );
18+
19+ int
20+ is_frozen_modules_end (const struct _frozen * current_frozen )
21+ {
22+ // The array end in a _frozen instance that is completely zeroed out.
23+ struct _frozen empty_frozen ;
24+ memset (& empty_frozen , 0 , sizeof (struct _frozen ));
25+
26+ return current_frozen == NULL || memcmp (& empty_frozen , current_frozen , sizeof (struct _frozen )) == 0 ;
27+ }
28+
29+ void
30+ add_modules (PyObject * frozen_list , const struct _frozen * frozen_modules_start )
31+ {
32+ const struct _frozen * current_frozen = NULL ;
33+ for (current_frozen = frozen_modules_start ; !is_frozen_modules_end (current_frozen ); current_frozen ++ )
34+ {
35+ PyObject * name_str = PyUnicode_FromString (current_frozen -> name );
36+ PyList_Append (frozen_list , name_str );
37+ }
38+ }
39+
40+ static PyObject *
41+ get_frozen_modules (PyObject * module , PyObject * const * args , Py_ssize_t nargs )
42+ {
43+ PyObject * frozen_modules_list = PyList_New (0 );
44+
45+ add_modules (frozen_modules_list , _PyImport_FrozenBootstrap );
46+ add_modules (frozen_modules_list , _PyImport_FrozenStdlib );
47+ add_modules (frozen_modules_list , PyImport_FrozenModules );
48+ add_modules (frozen_modules_list , _PyImport_FrozenTest );
49+
50+ return frozen_modules_list ;
51+ }
52+
53+ static PyMethodDef frozen_utils_methods [] = {
54+ {"get_frozen_modules" , _PyCFunction_CAST (get_frozen_modules ), METH_FASTCALL , frozen_utils_get_frozen_modules__doc__ },
55+ {NULL , NULL }
56+ };
57+
58+ static PyModuleDef_Slot frozen_utils_slots [] = {
59+ {Py_mod_exec , frozen_utils_exec },
60+ {Py_mod_multiple_interpreters , Py_MOD_PER_INTERPRETER_GIL_SUPPORTED },
61+ {0 , NULL }
62+ };
63+
64+ static struct PyModuleDef frozen_utils_module = {
65+ PyModuleDef_HEAD_INIT ,
66+ .m_name = "frozen_utils" ,
67+ .m_doc = frozen_utils__doc__ ,
68+ .m_size = 0 ,
69+ .m_methods = frozen_utils_methods ,
70+ .m_slots = frozen_utils_slots ,
71+ };
72+
73+ PyMODINIT_FUNC
74+ PyInit_frozen_utils (void )
75+ {
76+ return PyModuleDef_Init (& frozen_utils_module );
77+ }
0 commit comments