|
1 | 1 |
|
2 | 2 | /* Testing module for single-phase initialization of extension modules
|
3 |
| - */ |
| 3 | +
|
| 4 | +This file contains 5 distinct modules, meaning each as its own name |
| 5 | +and its own init function (PyInit_...). The default import system will |
| 6 | +only find the one matching the filename: _testsinglephase. To load the |
| 7 | +others you must do so manually. For example: |
| 8 | +
|
| 9 | +```python |
| 10 | +name = '_testsinglephase_base_wrapper' |
| 11 | +filename = _testsinglephase.__file__ |
| 12 | +loader = importlib.machinery.ExtensionFileLoader(name, filename) |
| 13 | +spec = importlib.util.spec_from_file_location(name, filename, loader=loader) |
| 14 | +mod = importlib._bootstrap._load(spec) |
| 15 | +``` |
| 16 | +
|
| 17 | +Here are the 5 modules: |
| 18 | +
|
| 19 | +* _testsinglephase |
| 20 | + * def: _testsinglephase_basic, |
| 21 | + * m_name: "_testsinglephase" |
| 22 | + * m_size: -1 |
| 23 | + * state |
| 24 | + * process-global |
| 25 | + * <int> initialized_count (default to -1; will never be 0) |
| 26 | + * <module_state> module (see module state below) |
| 27 | + * module state: no |
| 28 | + * initial __dict__: see common initial __dict__ below |
| 29 | + * init function |
| 30 | + 1. create module |
| 31 | + 2. clear <global>.module |
| 32 | + 3. initialize <global>.module: see module state below |
| 33 | + 4. initialize module: set initial __dict__ |
| 34 | + 5. increment <global>.initialized_count |
| 35 | + * functions |
| 36 | + * (3 common, see below) |
| 37 | + * initialized_count() - return <global>.module.initialized_count |
| 38 | + * import system |
| 39 | + * caches |
| 40 | + * global extensions cache: yes |
| 41 | + * def.m_base.m_copy: yes |
| 42 | + * def.m_base.m_init: no |
| 43 | + * per-interpreter cache: yes (all single-phase init modules) |
| 44 | + * load in main interpreter |
| 45 | + * initial (not already in global cache) |
| 46 | + 1. get init function from shared object file |
| 47 | + 2. run init function |
| 48 | + 3. copy __dict__ into def.m_base.m_copy |
| 49 | + 4. set entry in global cache |
| 50 | + 5. set entry in per-interpreter cache |
| 51 | + 6. set entry in sys.modules |
| 52 | + * reload (already in sys.modules) |
| 53 | + 1. get def from global cache |
| 54 | + 2. get module from sys.modules |
| 55 | + 3. update module with contents of def.m_base.m_copy |
| 56 | + * already loaded in other interpreter (already in global cache) |
| 57 | + * same as reload, but create new module and update *it* |
| 58 | + * not in any sys.modules, still in global cache |
| 59 | + * same as already loaded |
| 60 | + * load in legacy (non-isolated) interpreter |
| 61 | + * same as main interpreter |
| 62 | + * unload: never (all single-phase init modules) |
| 63 | +* _testsinglephase_basic_wrapper |
| 64 | + * identical to _testsinglephase except module name |
| 65 | +* _testsinglephase_basic_copy |
| 66 | + * def: static local variable in init function |
| 67 | + * m_name: "_testsinglephase_basic_copy" |
| 68 | + * m_size: -1 |
| 69 | + * state: same as _testsinglephase |
| 70 | + * init function: same as _testsinglephase |
| 71 | + * functions: same as _testsinglephase |
| 72 | + * import system: same as _testsinglephase |
| 73 | +* _testsinglephase_with_reinit |
| 74 | + * def: _testsinglephase_with_reinit, |
| 75 | + * m_name: "_testsinglephase_with_reinit" |
| 76 | + * m_size: 0 |
| 77 | + * state |
| 78 | + * process-global state: no |
| 79 | + * module state: no |
| 80 | + * initial __dict__: see common initial __dict__ below |
| 81 | + * init function |
| 82 | + 1. create module |
| 83 | + 2. initialize temporary module state (local var): see module state below |
| 84 | + 3. initialize module: set initial __dict__ |
| 85 | + * functions: see common functions below |
| 86 | + * import system |
| 87 | + * caches |
| 88 | + * global extensions cache: only if loaded in main interpreter |
| 89 | + * def.m_base.m_copy: no |
| 90 | + * def.m_base.m_init: only if loaded in the main interpreter |
| 91 | + * per-interpreter cache: yes (all single-phase init modules) |
| 92 | + * load in main interpreter |
| 93 | + * initial (not already in global cache) |
| 94 | + * (same as _testsinglephase except step 3) |
| 95 | + 1. get init function from shared object file |
| 96 | + 2. run init function |
| 97 | + 3. set def.m_base.m_init to the init function |
| 98 | + 4. set entry in global cache |
| 99 | + 5. set entry in per-interpreter cache |
| 100 | + 6. set entry in sys.modules |
| 101 | + * reload (already in sys.modules) |
| 102 | + 1. get def from global cache |
| 103 | + 2. call def->m_base.m_init to get a new module object |
| 104 | + 3. replace the existing module in sys.modules |
| 105 | + * already loaded in other interpreter (already in global cache) |
| 106 | + * same as reload (since will only be in cache for main interp) |
| 107 | + * not in any sys.modules, still in global cache |
| 108 | + * same as already loaded |
| 109 | + * load in legacy (non-isolated) interpreter |
| 110 | + * initial (not already in global cache) |
| 111 | + * (same as main interpreter except skip steps 3 & 4 there) |
| 112 | + 1. get init function from shared object file |
| 113 | + 2. run init function |
| 114 | + ... |
| 115 | + 5. set entry in per-interpreter cache |
| 116 | + 6. set entry in sys.modules |
| 117 | + * reload (already in sys.modules) |
| 118 | + * same as initial (load from scratch) |
| 119 | + * already loaded in other interpreter (already in global cache) |
| 120 | + * same as initial (load from scratch) |
| 121 | + * not in any sys.modules, still in global cache |
| 122 | + * same as initial (load from scratch) |
| 123 | + * unload: never (all single-phase init modules) |
| 124 | +* _testsinglephase_with_state |
| 125 | + * def: _testsinglephase_with_state, |
| 126 | + * m_name: "_testsinglephase_with_state" |
| 127 | + * m_size: sizeof(module_state) |
| 128 | + * state |
| 129 | + * process-global: no |
| 130 | + * module state: see module state below |
| 131 | + * initial __dict__: see common initial __dict__ below |
| 132 | + * init function |
| 133 | + 1. create module |
| 134 | + 3. initialize module state: see module state below |
| 135 | + 4. initialize module: set initial __dict__ |
| 136 | + 5. increment <global>.initialized_count |
| 137 | + * functions: see common functions below |
| 138 | + * import system: same as _testsinglephase_basic_copy |
| 139 | +
|
| 140 | +Module state: |
| 141 | +
|
| 142 | +* fields |
| 143 | + * <PyTime_t> initialized - when the module was first initialized |
| 144 | + * <PyObject> *error |
| 145 | + * <PyObject> *int_const |
| 146 | + * <PyObject> *str_const |
| 147 | +* initialization |
| 148 | + 1. set state.initialized to the current time |
| 149 | + 2. set state.error to a new exception class |
| 150 | + 3. set state->int_const to int(1969) |
| 151 | + 4. set state->str_const to "something different" |
| 152 | +
|
| 153 | +Common initial __dict__: |
| 154 | +
|
| 155 | +* error: state.error |
| 156 | +* int_const: state.int_const |
| 157 | +* str_const: state.str_const |
| 158 | +* _module_initialized: state.initialized |
| 159 | +
|
| 160 | +Common functions: |
| 161 | +
|
| 162 | +* look_up_self() - return the module from the per-interpreter "by-index" cache |
| 163 | +* sum() - return a + b |
| 164 | +* state_initialized() - return state->initialized (or None if m_size == 0) |
| 165 | +
|
| 166 | +See Python/import.c, especially the long comments, for more about |
| 167 | +single-phase init modules. |
| 168 | +*/ |
| 169 | + |
4 | 170 | #ifndef Py_BUILD_CORE_BUILTIN
|
5 | 171 | # define Py_BUILD_CORE_MODULE 1
|
6 | 172 | #endif
|
|
0 commit comments