| 1 | n/a | |
|---|
| 2 | n/a | /* Module definition and import implementation */ |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | #include "Python.h" |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | #include "Python-ast.h" |
|---|
| 7 | n/a | #undef Yield /* undefine macro conflicting with winbase.h */ |
|---|
| 8 | n/a | #include "errcode.h" |
|---|
| 9 | n/a | #include "marshal.h" |
|---|
| 10 | n/a | #include "code.h" |
|---|
| 11 | n/a | #include "frameobject.h" |
|---|
| 12 | n/a | #include "osdefs.h" |
|---|
| 13 | n/a | #include "importdl.h" |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | #ifdef HAVE_FCNTL_H |
|---|
| 16 | n/a | #include <fcntl.h> |
|---|
| 17 | n/a | #endif |
|---|
| 18 | n/a | #ifdef __cplusplus |
|---|
| 19 | n/a | extern "C" { |
|---|
| 20 | n/a | #endif |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | #define CACHEDIR "__pycache__" |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | /* See _PyImport_FixupExtensionObject() below */ |
|---|
| 25 | n/a | static PyObject *extensions = NULL; |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | /* This table is defined in config.c: */ |
|---|
| 28 | n/a | extern struct _inittab _PyImport_Inittab[]; |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | struct _inittab *PyImport_Inittab = _PyImport_Inittab; |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | static PyObject *initstr = NULL; |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | /*[clinic input] |
|---|
| 35 | n/a | module _imp |
|---|
| 36 | n/a | [clinic start generated code]*/ |
|---|
| 37 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/ |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | #include "clinic/import.c.h" |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | /* Initialize things */ |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | void |
|---|
| 44 | n/a | _PyImport_Init(void) |
|---|
| 45 | n/a | { |
|---|
| 46 | n/a | PyInterpreterState *interp = PyThreadState_Get()->interp; |
|---|
| 47 | n/a | initstr = PyUnicode_InternFromString("__init__"); |
|---|
| 48 | n/a | if (initstr == NULL) |
|---|
| 49 | n/a | Py_FatalError("Can't initialize import variables"); |
|---|
| 50 | n/a | interp->builtins_copy = PyDict_Copy(interp->builtins); |
|---|
| 51 | n/a | if (interp->builtins_copy == NULL) |
|---|
| 52 | n/a | Py_FatalError("Can't backup builtins dict"); |
|---|
| 53 | n/a | } |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | void |
|---|
| 56 | n/a | _PyImportHooks_Init(void) |
|---|
| 57 | n/a | { |
|---|
| 58 | n/a | PyObject *v, *path_hooks = NULL; |
|---|
| 59 | n/a | int err = 0; |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | /* adding sys.path_hooks and sys.path_importer_cache */ |
|---|
| 62 | n/a | v = PyList_New(0); |
|---|
| 63 | n/a | if (v == NULL) |
|---|
| 64 | n/a | goto error; |
|---|
| 65 | n/a | err = PySys_SetObject("meta_path", v); |
|---|
| 66 | n/a | Py_DECREF(v); |
|---|
| 67 | n/a | if (err) |
|---|
| 68 | n/a | goto error; |
|---|
| 69 | n/a | v = PyDict_New(); |
|---|
| 70 | n/a | if (v == NULL) |
|---|
| 71 | n/a | goto error; |
|---|
| 72 | n/a | err = PySys_SetObject("path_importer_cache", v); |
|---|
| 73 | n/a | Py_DECREF(v); |
|---|
| 74 | n/a | if (err) |
|---|
| 75 | n/a | goto error; |
|---|
| 76 | n/a | path_hooks = PyList_New(0); |
|---|
| 77 | n/a | if (path_hooks == NULL) |
|---|
| 78 | n/a | goto error; |
|---|
| 79 | n/a | err = PySys_SetObject("path_hooks", path_hooks); |
|---|
| 80 | n/a | if (err) { |
|---|
| 81 | n/a | error: |
|---|
| 82 | n/a | PyErr_Print(); |
|---|
| 83 | n/a | Py_FatalError("initializing sys.meta_path, sys.path_hooks, " |
|---|
| 84 | n/a | "or path_importer_cache failed"); |
|---|
| 85 | n/a | } |
|---|
| 86 | n/a | Py_DECREF(path_hooks); |
|---|
| 87 | n/a | } |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | void |
|---|
| 90 | n/a | _PyImportZip_Init(void) |
|---|
| 91 | n/a | { |
|---|
| 92 | n/a | PyObject *path_hooks, *zimpimport; |
|---|
| 93 | n/a | int err = 0; |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | path_hooks = PySys_GetObject("path_hooks"); |
|---|
| 96 | n/a | if (path_hooks == NULL) { |
|---|
| 97 | n/a | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path_hooks"); |
|---|
| 98 | n/a | goto error; |
|---|
| 99 | n/a | } |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | if (Py_VerboseFlag) |
|---|
| 102 | n/a | PySys_WriteStderr("# installing zipimport hook\n"); |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | zimpimport = PyImport_ImportModule("zipimport"); |
|---|
| 105 | n/a | if (zimpimport == NULL) { |
|---|
| 106 | n/a | PyErr_Clear(); /* No zip import module -- okay */ |
|---|
| 107 | n/a | if (Py_VerboseFlag) |
|---|
| 108 | n/a | PySys_WriteStderr("# can't import zipimport\n"); |
|---|
| 109 | n/a | } |
|---|
| 110 | n/a | else { |
|---|
| 111 | n/a | _Py_IDENTIFIER(zipimporter); |
|---|
| 112 | n/a | PyObject *zipimporter = _PyObject_GetAttrId(zimpimport, |
|---|
| 113 | n/a | &PyId_zipimporter); |
|---|
| 114 | n/a | Py_DECREF(zimpimport); |
|---|
| 115 | n/a | if (zipimporter == NULL) { |
|---|
| 116 | n/a | PyErr_Clear(); /* No zipimporter object -- okay */ |
|---|
| 117 | n/a | if (Py_VerboseFlag) |
|---|
| 118 | n/a | PySys_WriteStderr( |
|---|
| 119 | n/a | "# can't import zipimport.zipimporter\n"); |
|---|
| 120 | n/a | } |
|---|
| 121 | n/a | else { |
|---|
| 122 | n/a | /* sys.path_hooks.insert(0, zipimporter) */ |
|---|
| 123 | n/a | err = PyList_Insert(path_hooks, 0, zipimporter); |
|---|
| 124 | n/a | Py_DECREF(zipimporter); |
|---|
| 125 | n/a | if (err < 0) { |
|---|
| 126 | n/a | goto error; |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | if (Py_VerboseFlag) |
|---|
| 129 | n/a | PySys_WriteStderr( |
|---|
| 130 | n/a | "# installed zipimport hook\n"); |
|---|
| 131 | n/a | } |
|---|
| 132 | n/a | } |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | return; |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | error: |
|---|
| 137 | n/a | PyErr_Print(); |
|---|
| 138 | n/a | Py_FatalError("initializing zipimport failed"); |
|---|
| 139 | n/a | } |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | /* Locking primitives to prevent parallel imports of the same module |
|---|
| 142 | n/a | in different threads to return with a partially loaded module. |
|---|
| 143 | n/a | These calls are serialized by the global interpreter lock. */ |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | #ifdef WITH_THREAD |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | #include "pythread.h" |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | static PyThread_type_lock import_lock = 0; |
|---|
| 150 | n/a | static long import_lock_thread = -1; |
|---|
| 151 | n/a | static int import_lock_level = 0; |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | void |
|---|
| 154 | n/a | _PyImport_AcquireLock(void) |
|---|
| 155 | n/a | { |
|---|
| 156 | n/a | long me = PyThread_get_thread_ident(); |
|---|
| 157 | n/a | if (me == -1) |
|---|
| 158 | n/a | return; /* Too bad */ |
|---|
| 159 | n/a | if (import_lock == NULL) { |
|---|
| 160 | n/a | import_lock = PyThread_allocate_lock(); |
|---|
| 161 | n/a | if (import_lock == NULL) |
|---|
| 162 | n/a | return; /* Nothing much we can do. */ |
|---|
| 163 | n/a | } |
|---|
| 164 | n/a | if (import_lock_thread == me) { |
|---|
| 165 | n/a | import_lock_level++; |
|---|
| 166 | n/a | return; |
|---|
| 167 | n/a | } |
|---|
| 168 | n/a | if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0)) |
|---|
| 169 | n/a | { |
|---|
| 170 | n/a | PyThreadState *tstate = PyEval_SaveThread(); |
|---|
| 171 | n/a | PyThread_acquire_lock(import_lock, 1); |
|---|
| 172 | n/a | PyEval_RestoreThread(tstate); |
|---|
| 173 | n/a | } |
|---|
| 174 | n/a | assert(import_lock_level == 0); |
|---|
| 175 | n/a | import_lock_thread = me; |
|---|
| 176 | n/a | import_lock_level = 1; |
|---|
| 177 | n/a | } |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | int |
|---|
| 180 | n/a | _PyImport_ReleaseLock(void) |
|---|
| 181 | n/a | { |
|---|
| 182 | n/a | long me = PyThread_get_thread_ident(); |
|---|
| 183 | n/a | if (me == -1 || import_lock == NULL) |
|---|
| 184 | n/a | return 0; /* Too bad */ |
|---|
| 185 | n/a | if (import_lock_thread != me) |
|---|
| 186 | n/a | return -1; |
|---|
| 187 | n/a | import_lock_level--; |
|---|
| 188 | n/a | assert(import_lock_level >= 0); |
|---|
| 189 | n/a | if (import_lock_level == 0) { |
|---|
| 190 | n/a | import_lock_thread = -1; |
|---|
| 191 | n/a | PyThread_release_lock(import_lock); |
|---|
| 192 | n/a | } |
|---|
| 193 | n/a | return 1; |
|---|
| 194 | n/a | } |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | /* This function is called from PyOS_AfterFork to ensure that newly |
|---|
| 197 | n/a | created child processes do not share locks with the parent. |
|---|
| 198 | n/a | We now acquire the import lock around fork() calls but on some platforms |
|---|
| 199 | n/a | (Solaris 9 and earlier? see isue7242) that still left us with problems. */ |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | void |
|---|
| 202 | n/a | _PyImport_ReInitLock(void) |
|---|
| 203 | n/a | { |
|---|
| 204 | n/a | if (import_lock != NULL) { |
|---|
| 205 | n/a | import_lock = PyThread_allocate_lock(); |
|---|
| 206 | n/a | if (import_lock == NULL) { |
|---|
| 207 | n/a | Py_FatalError("PyImport_ReInitLock failed to create a new lock"); |
|---|
| 208 | n/a | } |
|---|
| 209 | n/a | } |
|---|
| 210 | n/a | if (import_lock_level > 1) { |
|---|
| 211 | n/a | /* Forked as a side effect of import */ |
|---|
| 212 | n/a | long me = PyThread_get_thread_ident(); |
|---|
| 213 | n/a | /* The following could fail if the lock is already held, but forking as |
|---|
| 214 | n/a | a side-effect of an import is a) rare, b) nuts, and c) difficult to |
|---|
| 215 | n/a | do thanks to the lock only being held when doing individual module |
|---|
| 216 | n/a | locks per import. */ |
|---|
| 217 | n/a | PyThread_acquire_lock(import_lock, NOWAIT_LOCK); |
|---|
| 218 | n/a | import_lock_thread = me; |
|---|
| 219 | n/a | import_lock_level--; |
|---|
| 220 | n/a | } else { |
|---|
| 221 | n/a | import_lock_thread = -1; |
|---|
| 222 | n/a | import_lock_level = 0; |
|---|
| 223 | n/a | } |
|---|
| 224 | n/a | } |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | #endif |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | /*[clinic input] |
|---|
| 229 | n/a | _imp.lock_held |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | Return True if the import lock is currently held, else False. |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | On platforms without threads, return False. |
|---|
| 234 | n/a | [clinic start generated code]*/ |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | static PyObject * |
|---|
| 237 | n/a | _imp_lock_held_impl(PyObject *module) |
|---|
| 238 | n/a | /*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/ |
|---|
| 239 | n/a | { |
|---|
| 240 | n/a | #ifdef WITH_THREAD |
|---|
| 241 | n/a | return PyBool_FromLong(import_lock_thread != -1); |
|---|
| 242 | n/a | #else |
|---|
| 243 | n/a | return PyBool_FromLong(0); |
|---|
| 244 | n/a | #endif |
|---|
| 245 | n/a | } |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | /*[clinic input] |
|---|
| 248 | n/a | _imp.acquire_lock |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | Acquires the interpreter's import lock for the current thread. |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | This lock should be used by import hooks to ensure thread-safety when importing |
|---|
| 253 | n/a | modules. On platforms without threads, this function does nothing. |
|---|
| 254 | n/a | [clinic start generated code]*/ |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | static PyObject * |
|---|
| 257 | n/a | _imp_acquire_lock_impl(PyObject *module) |
|---|
| 258 | n/a | /*[clinic end generated code: output=1aff58cb0ee1b026 input=4a2d4381866d5fdc]*/ |
|---|
| 259 | n/a | { |
|---|
| 260 | n/a | #ifdef WITH_THREAD |
|---|
| 261 | n/a | _PyImport_AcquireLock(); |
|---|
| 262 | n/a | #endif |
|---|
| 263 | n/a | Py_RETURN_NONE; |
|---|
| 264 | n/a | } |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | /*[clinic input] |
|---|
| 267 | n/a | _imp.release_lock |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | Release the interpreter's import lock. |
|---|
| 270 | n/a | |
|---|
| 271 | n/a | On platforms without threads, this function does nothing. |
|---|
| 272 | n/a | [clinic start generated code]*/ |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | static PyObject * |
|---|
| 275 | n/a | _imp_release_lock_impl(PyObject *module) |
|---|
| 276 | n/a | /*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/ |
|---|
| 277 | n/a | { |
|---|
| 278 | n/a | #ifdef WITH_THREAD |
|---|
| 279 | n/a | if (_PyImport_ReleaseLock() < 0) { |
|---|
| 280 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 281 | n/a | "not holding the import lock"); |
|---|
| 282 | n/a | return NULL; |
|---|
| 283 | n/a | } |
|---|
| 284 | n/a | #endif |
|---|
| 285 | n/a | Py_RETURN_NONE; |
|---|
| 286 | n/a | } |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | void |
|---|
| 289 | n/a | _PyImport_Fini(void) |
|---|
| 290 | n/a | { |
|---|
| 291 | n/a | Py_CLEAR(extensions); |
|---|
| 292 | n/a | #ifdef WITH_THREAD |
|---|
| 293 | n/a | if (import_lock != NULL) { |
|---|
| 294 | n/a | PyThread_free_lock(import_lock); |
|---|
| 295 | n/a | import_lock = NULL; |
|---|
| 296 | n/a | } |
|---|
| 297 | n/a | #endif |
|---|
| 298 | n/a | } |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | /* Helper for sys */ |
|---|
| 301 | n/a | |
|---|
| 302 | n/a | PyObject * |
|---|
| 303 | n/a | PyImport_GetModuleDict(void) |
|---|
| 304 | n/a | { |
|---|
| 305 | n/a | PyInterpreterState *interp = PyThreadState_GET()->interp; |
|---|
| 306 | n/a | if (interp->modules == NULL) |
|---|
| 307 | n/a | Py_FatalError("PyImport_GetModuleDict: no module dictionary!"); |
|---|
| 308 | n/a | return interp->modules; |
|---|
| 309 | n/a | } |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | |
|---|
| 312 | n/a | /* List of names to clear in sys */ |
|---|
| 313 | n/a | static const char * const sys_deletes[] = { |
|---|
| 314 | n/a | "path", "argv", "ps1", "ps2", |
|---|
| 315 | n/a | "last_type", "last_value", "last_traceback", |
|---|
| 316 | n/a | "path_hooks", "path_importer_cache", "meta_path", |
|---|
| 317 | n/a | "__interactivehook__", |
|---|
| 318 | n/a | /* misc stuff */ |
|---|
| 319 | n/a | "flags", "float_info", |
|---|
| 320 | n/a | NULL |
|---|
| 321 | n/a | }; |
|---|
| 322 | n/a | |
|---|
| 323 | n/a | static const char * const sys_files[] = { |
|---|
| 324 | n/a | "stdin", "__stdin__", |
|---|
| 325 | n/a | "stdout", "__stdout__", |
|---|
| 326 | n/a | "stderr", "__stderr__", |
|---|
| 327 | n/a | NULL |
|---|
| 328 | n/a | }; |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | /* Un-initialize things, as good as we can */ |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | void |
|---|
| 333 | n/a | PyImport_Cleanup(void) |
|---|
| 334 | n/a | { |
|---|
| 335 | n/a | Py_ssize_t pos; |
|---|
| 336 | n/a | PyObject *key, *value, *dict; |
|---|
| 337 | n/a | PyInterpreterState *interp = PyThreadState_GET()->interp; |
|---|
| 338 | n/a | PyObject *modules = interp->modules; |
|---|
| 339 | n/a | PyObject *weaklist = NULL; |
|---|
| 340 | n/a | const char * const *p; |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | if (modules == NULL) |
|---|
| 343 | n/a | return; /* Already done */ |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | /* Delete some special variables first. These are common |
|---|
| 346 | n/a | places where user values hide and people complain when their |
|---|
| 347 | n/a | destructors fail. Since the modules containing them are |
|---|
| 348 | n/a | deleted *last* of all, they would come too late in the normal |
|---|
| 349 | n/a | destruction order. Sigh. */ |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | /* XXX Perhaps these precautions are obsolete. Who knows? */ |
|---|
| 352 | n/a | |
|---|
| 353 | n/a | if (Py_VerboseFlag) |
|---|
| 354 | n/a | PySys_WriteStderr("# clear builtins._\n"); |
|---|
| 355 | n/a | PyDict_SetItemString(interp->builtins, "_", Py_None); |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | for (p = sys_deletes; *p != NULL; p++) { |
|---|
| 358 | n/a | if (Py_VerboseFlag) |
|---|
| 359 | n/a | PySys_WriteStderr("# clear sys.%s\n", *p); |
|---|
| 360 | n/a | PyDict_SetItemString(interp->sysdict, *p, Py_None); |
|---|
| 361 | n/a | } |
|---|
| 362 | n/a | for (p = sys_files; *p != NULL; p+=2) { |
|---|
| 363 | n/a | if (Py_VerboseFlag) |
|---|
| 364 | n/a | PySys_WriteStderr("# restore sys.%s\n", *p); |
|---|
| 365 | n/a | value = PyDict_GetItemString(interp->sysdict, *(p+1)); |
|---|
| 366 | n/a | if (value == NULL) |
|---|
| 367 | n/a | value = Py_None; |
|---|
| 368 | n/a | PyDict_SetItemString(interp->sysdict, *p, value); |
|---|
| 369 | n/a | } |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | /* We prepare a list which will receive (name, weakref) tuples of |
|---|
| 372 | n/a | modules when they are removed from sys.modules. The name is used |
|---|
| 373 | n/a | for diagnosis messages (in verbose mode), while the weakref helps |
|---|
| 374 | n/a | detect those modules which have been held alive. */ |
|---|
| 375 | n/a | weaklist = PyList_New(0); |
|---|
| 376 | n/a | if (weaklist == NULL) |
|---|
| 377 | n/a | PyErr_Clear(); |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | #define STORE_MODULE_WEAKREF(name, mod) \ |
|---|
| 380 | n/a | if (weaklist != NULL) { \ |
|---|
| 381 | n/a | PyObject *wr = PyWeakref_NewRef(mod, NULL); \ |
|---|
| 382 | n/a | if (name && wr) { \ |
|---|
| 383 | n/a | PyObject *tup = PyTuple_Pack(2, name, wr); \ |
|---|
| 384 | n/a | PyList_Append(weaklist, tup); \ |
|---|
| 385 | n/a | Py_XDECREF(tup); \ |
|---|
| 386 | n/a | } \ |
|---|
| 387 | n/a | Py_XDECREF(wr); \ |
|---|
| 388 | n/a | if (PyErr_Occurred()) \ |
|---|
| 389 | n/a | PyErr_Clear(); \ |
|---|
| 390 | n/a | } |
|---|
| 391 | n/a | |
|---|
| 392 | n/a | /* Remove all modules from sys.modules, hoping that garbage collection |
|---|
| 393 | n/a | can reclaim most of them. */ |
|---|
| 394 | n/a | pos = 0; |
|---|
| 395 | n/a | while (PyDict_Next(modules, &pos, &key, &value)) { |
|---|
| 396 | n/a | if (PyModule_Check(value)) { |
|---|
| 397 | n/a | if (Py_VerboseFlag && PyUnicode_Check(key)) |
|---|
| 398 | n/a | PySys_FormatStderr("# cleanup[2] removing %U\n", key); |
|---|
| 399 | n/a | STORE_MODULE_WEAKREF(key, value); |
|---|
| 400 | n/a | PyDict_SetItem(modules, key, Py_None); |
|---|
| 401 | n/a | } |
|---|
| 402 | n/a | } |
|---|
| 403 | n/a | |
|---|
| 404 | n/a | /* Clear the modules dict. */ |
|---|
| 405 | n/a | PyDict_Clear(modules); |
|---|
| 406 | n/a | /* Restore the original builtins dict, to ensure that any |
|---|
| 407 | n/a | user data gets cleared. */ |
|---|
| 408 | n/a | dict = PyDict_Copy(interp->builtins); |
|---|
| 409 | n/a | if (dict == NULL) |
|---|
| 410 | n/a | PyErr_Clear(); |
|---|
| 411 | n/a | PyDict_Clear(interp->builtins); |
|---|
| 412 | n/a | if (PyDict_Update(interp->builtins, interp->builtins_copy)) |
|---|
| 413 | n/a | PyErr_Clear(); |
|---|
| 414 | n/a | Py_XDECREF(dict); |
|---|
| 415 | n/a | /* Clear module dict copies stored in the interpreter state */ |
|---|
| 416 | n/a | _PyState_ClearModules(); |
|---|
| 417 | n/a | /* Collect references */ |
|---|
| 418 | n/a | _PyGC_CollectNoFail(); |
|---|
| 419 | n/a | /* Dump GC stats before it's too late, since it uses the warnings |
|---|
| 420 | n/a | machinery. */ |
|---|
| 421 | n/a | _PyGC_DumpShutdownStats(); |
|---|
| 422 | n/a | |
|---|
| 423 | n/a | /* Now, if there are any modules left alive, clear their globals to |
|---|
| 424 | n/a | minimize potential leaks. All C extension modules actually end |
|---|
| 425 | n/a | up here, since they are kept alive in the interpreter state. |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | The special treatment of "builtins" here is because even |
|---|
| 428 | n/a | when it's not referenced as a module, its dictionary is |
|---|
| 429 | n/a | referenced by almost every module's __builtins__. Since |
|---|
| 430 | n/a | deleting a module clears its dictionary (even if there are |
|---|
| 431 | n/a | references left to it), we need to delete the "builtins" |
|---|
| 432 | n/a | module last. Likewise, we don't delete sys until the very |
|---|
| 433 | n/a | end because it is implicitly referenced (e.g. by print). */ |
|---|
| 434 | n/a | if (weaklist != NULL) { |
|---|
| 435 | n/a | Py_ssize_t i, n; |
|---|
| 436 | n/a | n = PyList_GET_SIZE(weaklist); |
|---|
| 437 | n/a | for (i = 0; i < n; i++) { |
|---|
| 438 | n/a | PyObject *tup = PyList_GET_ITEM(weaklist, i); |
|---|
| 439 | n/a | PyObject *name = PyTuple_GET_ITEM(tup, 0); |
|---|
| 440 | n/a | PyObject *mod = PyWeakref_GET_OBJECT(PyTuple_GET_ITEM(tup, 1)); |
|---|
| 441 | n/a | if (mod == Py_None) |
|---|
| 442 | n/a | continue; |
|---|
| 443 | n/a | assert(PyModule_Check(mod)); |
|---|
| 444 | n/a | dict = PyModule_GetDict(mod); |
|---|
| 445 | n/a | if (dict == interp->builtins || dict == interp->sysdict) |
|---|
| 446 | n/a | continue; |
|---|
| 447 | n/a | Py_INCREF(mod); |
|---|
| 448 | n/a | if (Py_VerboseFlag && PyUnicode_Check(name)) |
|---|
| 449 | n/a | PySys_FormatStderr("# cleanup[3] wiping %U\n", name); |
|---|
| 450 | n/a | _PyModule_Clear(mod); |
|---|
| 451 | n/a | Py_DECREF(mod); |
|---|
| 452 | n/a | } |
|---|
| 453 | n/a | Py_DECREF(weaklist); |
|---|
| 454 | n/a | } |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | /* Next, delete sys and builtins (in that order) */ |
|---|
| 457 | n/a | if (Py_VerboseFlag) |
|---|
| 458 | n/a | PySys_FormatStderr("# cleanup[3] wiping sys\n"); |
|---|
| 459 | n/a | _PyModule_ClearDict(interp->sysdict); |
|---|
| 460 | n/a | if (Py_VerboseFlag) |
|---|
| 461 | n/a | PySys_FormatStderr("# cleanup[3] wiping builtins\n"); |
|---|
| 462 | n/a | _PyModule_ClearDict(interp->builtins); |
|---|
| 463 | n/a | |
|---|
| 464 | n/a | /* Clear and delete the modules directory. Actual modules will |
|---|
| 465 | n/a | still be there only if imported during the execution of some |
|---|
| 466 | n/a | destructor. */ |
|---|
| 467 | n/a | interp->modules = NULL; |
|---|
| 468 | n/a | Py_DECREF(modules); |
|---|
| 469 | n/a | |
|---|
| 470 | n/a | /* Once more */ |
|---|
| 471 | n/a | _PyGC_CollectNoFail(); |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | #undef STORE_MODULE_WEAKREF |
|---|
| 474 | n/a | } |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | /* Helper for pythonrun.c -- return magic number and tag. */ |
|---|
| 478 | n/a | |
|---|
| 479 | n/a | long |
|---|
| 480 | n/a | PyImport_GetMagicNumber(void) |
|---|
| 481 | n/a | { |
|---|
| 482 | n/a | long res; |
|---|
| 483 | n/a | PyInterpreterState *interp = PyThreadState_Get()->interp; |
|---|
| 484 | n/a | PyObject *external, *pyc_magic; |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); |
|---|
| 487 | n/a | if (external == NULL) |
|---|
| 488 | n/a | return -1; |
|---|
| 489 | n/a | pyc_magic = PyObject_GetAttrString(external, "_RAW_MAGIC_NUMBER"); |
|---|
| 490 | n/a | Py_DECREF(external); |
|---|
| 491 | n/a | if (pyc_magic == NULL) |
|---|
| 492 | n/a | return -1; |
|---|
| 493 | n/a | res = PyLong_AsLong(pyc_magic); |
|---|
| 494 | n/a | Py_DECREF(pyc_magic); |
|---|
| 495 | n/a | return res; |
|---|
| 496 | n/a | } |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | |
|---|
| 499 | n/a | extern const char * _PySys_ImplCacheTag; |
|---|
| 500 | n/a | |
|---|
| 501 | n/a | const char * |
|---|
| 502 | n/a | PyImport_GetMagicTag(void) |
|---|
| 503 | n/a | { |
|---|
| 504 | n/a | return _PySys_ImplCacheTag; |
|---|
| 505 | n/a | } |
|---|
| 506 | n/a | |
|---|
| 507 | n/a | |
|---|
| 508 | n/a | /* Magic for extension modules (built-in as well as dynamically |
|---|
| 509 | n/a | loaded). To prevent initializing an extension module more than |
|---|
| 510 | n/a | once, we keep a static dictionary 'extensions' keyed by the tuple |
|---|
| 511 | n/a | (module name, module name) (for built-in modules) or by |
|---|
| 512 | n/a | (filename, module name) (for dynamically loaded modules), containing these |
|---|
| 513 | n/a | modules. A copy of the module's dictionary is stored by calling |
|---|
| 514 | n/a | _PyImport_FixupExtensionObject() immediately after the module initialization |
|---|
| 515 | n/a | function succeeds. A copy can be retrieved from there by calling |
|---|
| 516 | n/a | _PyImport_FindExtensionObject(). |
|---|
| 517 | n/a | |
|---|
| 518 | n/a | Modules which do support multiple initialization set their m_size |
|---|
| 519 | n/a | field to a non-negative number (indicating the size of the |
|---|
| 520 | n/a | module-specific state). They are still recorded in the extensions |
|---|
| 521 | n/a | dictionary, to avoid loading shared libraries twice. |
|---|
| 522 | n/a | */ |
|---|
| 523 | n/a | |
|---|
| 524 | n/a | int |
|---|
| 525 | n/a | _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name, |
|---|
| 526 | n/a | PyObject *filename) |
|---|
| 527 | n/a | { |
|---|
| 528 | n/a | PyObject *modules, *dict, *key; |
|---|
| 529 | n/a | struct PyModuleDef *def; |
|---|
| 530 | n/a | int res; |
|---|
| 531 | n/a | if (extensions == NULL) { |
|---|
| 532 | n/a | extensions = PyDict_New(); |
|---|
| 533 | n/a | if (extensions == NULL) |
|---|
| 534 | n/a | return -1; |
|---|
| 535 | n/a | } |
|---|
| 536 | n/a | if (mod == NULL || !PyModule_Check(mod)) { |
|---|
| 537 | n/a | PyErr_BadInternalCall(); |
|---|
| 538 | n/a | return -1; |
|---|
| 539 | n/a | } |
|---|
| 540 | n/a | def = PyModule_GetDef(mod); |
|---|
| 541 | n/a | if (!def) { |
|---|
| 542 | n/a | PyErr_BadInternalCall(); |
|---|
| 543 | n/a | return -1; |
|---|
| 544 | n/a | } |
|---|
| 545 | n/a | modules = PyImport_GetModuleDict(); |
|---|
| 546 | n/a | if (PyDict_SetItem(modules, name, mod) < 0) |
|---|
| 547 | n/a | return -1; |
|---|
| 548 | n/a | if (_PyState_AddModule(mod, def) < 0) { |
|---|
| 549 | n/a | PyDict_DelItem(modules, name); |
|---|
| 550 | n/a | return -1; |
|---|
| 551 | n/a | } |
|---|
| 552 | n/a | if (def->m_size == -1) { |
|---|
| 553 | n/a | if (def->m_base.m_copy) { |
|---|
| 554 | n/a | /* Somebody already imported the module, |
|---|
| 555 | n/a | likely under a different name. |
|---|
| 556 | n/a | XXX this should really not happen. */ |
|---|
| 557 | n/a | Py_CLEAR(def->m_base.m_copy); |
|---|
| 558 | n/a | } |
|---|
| 559 | n/a | dict = PyModule_GetDict(mod); |
|---|
| 560 | n/a | if (dict == NULL) |
|---|
| 561 | n/a | return -1; |
|---|
| 562 | n/a | def->m_base.m_copy = PyDict_Copy(dict); |
|---|
| 563 | n/a | if (def->m_base.m_copy == NULL) |
|---|
| 564 | n/a | return -1; |
|---|
| 565 | n/a | } |
|---|
| 566 | n/a | key = PyTuple_Pack(2, filename, name); |
|---|
| 567 | n/a | if (key == NULL) |
|---|
| 568 | n/a | return -1; |
|---|
| 569 | n/a | res = PyDict_SetItem(extensions, key, (PyObject *)def); |
|---|
| 570 | n/a | Py_DECREF(key); |
|---|
| 571 | n/a | if (res < 0) |
|---|
| 572 | n/a | return -1; |
|---|
| 573 | n/a | return 0; |
|---|
| 574 | n/a | } |
|---|
| 575 | n/a | |
|---|
| 576 | n/a | int |
|---|
| 577 | n/a | _PyImport_FixupBuiltin(PyObject *mod, const char *name) |
|---|
| 578 | n/a | { |
|---|
| 579 | n/a | int res; |
|---|
| 580 | n/a | PyObject *nameobj; |
|---|
| 581 | n/a | nameobj = PyUnicode_InternFromString(name); |
|---|
| 582 | n/a | if (nameobj == NULL) |
|---|
| 583 | n/a | return -1; |
|---|
| 584 | n/a | res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj); |
|---|
| 585 | n/a | Py_DECREF(nameobj); |
|---|
| 586 | n/a | return res; |
|---|
| 587 | n/a | } |
|---|
| 588 | n/a | |
|---|
| 589 | n/a | PyObject * |
|---|
| 590 | n/a | _PyImport_FindExtensionObject(PyObject *name, PyObject *filename) |
|---|
| 591 | n/a | { |
|---|
| 592 | n/a | PyObject *mod, *mdict, *key; |
|---|
| 593 | n/a | PyModuleDef* def; |
|---|
| 594 | n/a | if (extensions == NULL) |
|---|
| 595 | n/a | return NULL; |
|---|
| 596 | n/a | key = PyTuple_Pack(2, filename, name); |
|---|
| 597 | n/a | if (key == NULL) |
|---|
| 598 | n/a | return NULL; |
|---|
| 599 | n/a | def = (PyModuleDef *)PyDict_GetItem(extensions, key); |
|---|
| 600 | n/a | Py_DECREF(key); |
|---|
| 601 | n/a | if (def == NULL) |
|---|
| 602 | n/a | return NULL; |
|---|
| 603 | n/a | if (def->m_size == -1) { |
|---|
| 604 | n/a | /* Module does not support repeated initialization */ |
|---|
| 605 | n/a | if (def->m_base.m_copy == NULL) |
|---|
| 606 | n/a | return NULL; |
|---|
| 607 | n/a | mod = PyImport_AddModuleObject(name); |
|---|
| 608 | n/a | if (mod == NULL) |
|---|
| 609 | n/a | return NULL; |
|---|
| 610 | n/a | mdict = PyModule_GetDict(mod); |
|---|
| 611 | n/a | if (mdict == NULL) |
|---|
| 612 | n/a | return NULL; |
|---|
| 613 | n/a | if (PyDict_Update(mdict, def->m_base.m_copy)) |
|---|
| 614 | n/a | return NULL; |
|---|
| 615 | n/a | } |
|---|
| 616 | n/a | else { |
|---|
| 617 | n/a | if (def->m_base.m_init == NULL) |
|---|
| 618 | n/a | return NULL; |
|---|
| 619 | n/a | mod = def->m_base.m_init(); |
|---|
| 620 | n/a | if (mod == NULL) |
|---|
| 621 | n/a | return NULL; |
|---|
| 622 | n/a | if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) { |
|---|
| 623 | n/a | Py_DECREF(mod); |
|---|
| 624 | n/a | return NULL; |
|---|
| 625 | n/a | } |
|---|
| 626 | n/a | Py_DECREF(mod); |
|---|
| 627 | n/a | } |
|---|
| 628 | n/a | if (_PyState_AddModule(mod, def) < 0) { |
|---|
| 629 | n/a | PyDict_DelItem(PyImport_GetModuleDict(), name); |
|---|
| 630 | n/a | Py_DECREF(mod); |
|---|
| 631 | n/a | return NULL; |
|---|
| 632 | n/a | } |
|---|
| 633 | n/a | if (Py_VerboseFlag) |
|---|
| 634 | n/a | PySys_FormatStderr("import %U # previously loaded (%R)\n", |
|---|
| 635 | n/a | name, filename); |
|---|
| 636 | n/a | return mod; |
|---|
| 637 | n/a | |
|---|
| 638 | n/a | } |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | PyObject * |
|---|
| 641 | n/a | _PyImport_FindBuiltin(const char *name) |
|---|
| 642 | n/a | { |
|---|
| 643 | n/a | PyObject *res, *nameobj; |
|---|
| 644 | n/a | nameobj = PyUnicode_InternFromString(name); |
|---|
| 645 | n/a | if (nameobj == NULL) |
|---|
| 646 | n/a | return NULL; |
|---|
| 647 | n/a | res = _PyImport_FindExtensionObject(nameobj, nameobj); |
|---|
| 648 | n/a | Py_DECREF(nameobj); |
|---|
| 649 | n/a | return res; |
|---|
| 650 | n/a | } |
|---|
| 651 | n/a | |
|---|
| 652 | n/a | /* Get the module object corresponding to a module name. |
|---|
| 653 | n/a | First check the modules dictionary if there's one there, |
|---|
| 654 | n/a | if not, create a new one and insert it in the modules dictionary. |
|---|
| 655 | n/a | Because the former action is most common, THIS DOES NOT RETURN A |
|---|
| 656 | n/a | 'NEW' REFERENCE! */ |
|---|
| 657 | n/a | |
|---|
| 658 | n/a | PyObject * |
|---|
| 659 | n/a | PyImport_AddModuleObject(PyObject *name) |
|---|
| 660 | n/a | { |
|---|
| 661 | n/a | PyObject *modules = PyImport_GetModuleDict(); |
|---|
| 662 | n/a | PyObject *m; |
|---|
| 663 | n/a | |
|---|
| 664 | n/a | if ((m = PyDict_GetItemWithError(modules, name)) != NULL && |
|---|
| 665 | n/a | PyModule_Check(m)) { |
|---|
| 666 | n/a | return m; |
|---|
| 667 | n/a | } |
|---|
| 668 | n/a | if (PyErr_Occurred()) { |
|---|
| 669 | n/a | return NULL; |
|---|
| 670 | n/a | } |
|---|
| 671 | n/a | m = PyModule_NewObject(name); |
|---|
| 672 | n/a | if (m == NULL) |
|---|
| 673 | n/a | return NULL; |
|---|
| 674 | n/a | if (PyDict_SetItem(modules, name, m) != 0) { |
|---|
| 675 | n/a | Py_DECREF(m); |
|---|
| 676 | n/a | return NULL; |
|---|
| 677 | n/a | } |
|---|
| 678 | n/a | Py_DECREF(m); /* Yes, it still exists, in modules! */ |
|---|
| 679 | n/a | |
|---|
| 680 | n/a | return m; |
|---|
| 681 | n/a | } |
|---|
| 682 | n/a | |
|---|
| 683 | n/a | PyObject * |
|---|
| 684 | n/a | PyImport_AddModule(const char *name) |
|---|
| 685 | n/a | { |
|---|
| 686 | n/a | PyObject *nameobj, *module; |
|---|
| 687 | n/a | nameobj = PyUnicode_FromString(name); |
|---|
| 688 | n/a | if (nameobj == NULL) |
|---|
| 689 | n/a | return NULL; |
|---|
| 690 | n/a | module = PyImport_AddModuleObject(nameobj); |
|---|
| 691 | n/a | Py_DECREF(nameobj); |
|---|
| 692 | n/a | return module; |
|---|
| 693 | n/a | } |
|---|
| 694 | n/a | |
|---|
| 695 | n/a | |
|---|
| 696 | n/a | /* Remove name from sys.modules, if it's there. */ |
|---|
| 697 | n/a | static void |
|---|
| 698 | n/a | remove_module(PyObject *name) |
|---|
| 699 | n/a | { |
|---|
| 700 | n/a | PyObject *modules = PyImport_GetModuleDict(); |
|---|
| 701 | n/a | if (PyDict_GetItem(modules, name) == NULL) |
|---|
| 702 | n/a | return; |
|---|
| 703 | n/a | if (PyDict_DelItem(modules, name) < 0) |
|---|
| 704 | n/a | Py_FatalError("import: deleting existing key in" |
|---|
| 705 | n/a | "sys.modules failed"); |
|---|
| 706 | n/a | } |
|---|
| 707 | n/a | |
|---|
| 708 | n/a | |
|---|
| 709 | n/a | /* Execute a code object in a module and return the module object |
|---|
| 710 | n/a | * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is |
|---|
| 711 | n/a | * removed from sys.modules, to avoid leaving damaged module objects |
|---|
| 712 | n/a | * in sys.modules. The caller may wish to restore the original |
|---|
| 713 | n/a | * module object (if any) in this case; PyImport_ReloadModule is an |
|---|
| 714 | n/a | * example. |
|---|
| 715 | n/a | * |
|---|
| 716 | n/a | * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer |
|---|
| 717 | n/a | * interface. The other two exist primarily for backward compatibility. |
|---|
| 718 | n/a | */ |
|---|
| 719 | n/a | PyObject * |
|---|
| 720 | n/a | PyImport_ExecCodeModule(const char *name, PyObject *co) |
|---|
| 721 | n/a | { |
|---|
| 722 | n/a | return PyImport_ExecCodeModuleWithPathnames( |
|---|
| 723 | n/a | name, co, (char *)NULL, (char *)NULL); |
|---|
| 724 | n/a | } |
|---|
| 725 | n/a | |
|---|
| 726 | n/a | PyObject * |
|---|
| 727 | n/a | PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname) |
|---|
| 728 | n/a | { |
|---|
| 729 | n/a | return PyImport_ExecCodeModuleWithPathnames( |
|---|
| 730 | n/a | name, co, pathname, (char *)NULL); |
|---|
| 731 | n/a | } |
|---|
| 732 | n/a | |
|---|
| 733 | n/a | PyObject * |
|---|
| 734 | n/a | PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, |
|---|
| 735 | n/a | const char *pathname, |
|---|
| 736 | n/a | const char *cpathname) |
|---|
| 737 | n/a | { |
|---|
| 738 | n/a | PyObject *m = NULL; |
|---|
| 739 | n/a | PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL; |
|---|
| 740 | n/a | |
|---|
| 741 | n/a | nameobj = PyUnicode_FromString(name); |
|---|
| 742 | n/a | if (nameobj == NULL) |
|---|
| 743 | n/a | return NULL; |
|---|
| 744 | n/a | |
|---|
| 745 | n/a | if (cpathname != NULL) { |
|---|
| 746 | n/a | cpathobj = PyUnicode_DecodeFSDefault(cpathname); |
|---|
| 747 | n/a | if (cpathobj == NULL) |
|---|
| 748 | n/a | goto error; |
|---|
| 749 | n/a | } |
|---|
| 750 | n/a | else |
|---|
| 751 | n/a | cpathobj = NULL; |
|---|
| 752 | n/a | |
|---|
| 753 | n/a | if (pathname != NULL) { |
|---|
| 754 | n/a | pathobj = PyUnicode_DecodeFSDefault(pathname); |
|---|
| 755 | n/a | if (pathobj == NULL) |
|---|
| 756 | n/a | goto error; |
|---|
| 757 | n/a | } |
|---|
| 758 | n/a | else if (cpathobj != NULL) { |
|---|
| 759 | n/a | PyInterpreterState *interp = PyThreadState_GET()->interp; |
|---|
| 760 | n/a | _Py_IDENTIFIER(_get_sourcefile); |
|---|
| 761 | n/a | |
|---|
| 762 | n/a | if (interp == NULL) { |
|---|
| 763 | n/a | Py_FatalError("PyImport_ExecCodeModuleWithPathnames: " |
|---|
| 764 | n/a | "no interpreter!"); |
|---|
| 765 | n/a | } |
|---|
| 766 | n/a | |
|---|
| 767 | n/a | external= PyObject_GetAttrString(interp->importlib, |
|---|
| 768 | n/a | "_bootstrap_external"); |
|---|
| 769 | n/a | if (external != NULL) { |
|---|
| 770 | n/a | pathobj = _PyObject_CallMethodIdObjArgs(external, |
|---|
| 771 | n/a | &PyId__get_sourcefile, cpathobj, |
|---|
| 772 | n/a | NULL); |
|---|
| 773 | n/a | Py_DECREF(external); |
|---|
| 774 | n/a | } |
|---|
| 775 | n/a | if (pathobj == NULL) |
|---|
| 776 | n/a | PyErr_Clear(); |
|---|
| 777 | n/a | } |
|---|
| 778 | n/a | else |
|---|
| 779 | n/a | pathobj = NULL; |
|---|
| 780 | n/a | |
|---|
| 781 | n/a | m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj); |
|---|
| 782 | n/a | error: |
|---|
| 783 | n/a | Py_DECREF(nameobj); |
|---|
| 784 | n/a | Py_XDECREF(pathobj); |
|---|
| 785 | n/a | Py_XDECREF(cpathobj); |
|---|
| 786 | n/a | return m; |
|---|
| 787 | n/a | } |
|---|
| 788 | n/a | |
|---|
| 789 | n/a | static PyObject * |
|---|
| 790 | n/a | module_dict_for_exec(PyObject *name) |
|---|
| 791 | n/a | { |
|---|
| 792 | n/a | PyObject *m, *d = NULL; |
|---|
| 793 | n/a | |
|---|
| 794 | n/a | m = PyImport_AddModuleObject(name); |
|---|
| 795 | n/a | if (m == NULL) |
|---|
| 796 | n/a | return NULL; |
|---|
| 797 | n/a | /* If the module is being reloaded, we get the old module back |
|---|
| 798 | n/a | and re-use its dict to exec the new code. */ |
|---|
| 799 | n/a | d = PyModule_GetDict(m); |
|---|
| 800 | n/a | if (PyDict_GetItemString(d, "__builtins__") == NULL) { |
|---|
| 801 | n/a | if (PyDict_SetItemString(d, "__builtins__", |
|---|
| 802 | n/a | PyEval_GetBuiltins()) != 0) { |
|---|
| 803 | n/a | remove_module(name); |
|---|
| 804 | n/a | return NULL; |
|---|
| 805 | n/a | } |
|---|
| 806 | n/a | } |
|---|
| 807 | n/a | |
|---|
| 808 | n/a | return d; /* Return a borrowed reference. */ |
|---|
| 809 | n/a | } |
|---|
| 810 | n/a | |
|---|
| 811 | n/a | static PyObject * |
|---|
| 812 | n/a | exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object) |
|---|
| 813 | n/a | { |
|---|
| 814 | n/a | PyObject *modules = PyImport_GetModuleDict(); |
|---|
| 815 | n/a | PyObject *v, *m; |
|---|
| 816 | n/a | |
|---|
| 817 | n/a | v = PyEval_EvalCode(code_object, module_dict, module_dict); |
|---|
| 818 | n/a | if (v == NULL) { |
|---|
| 819 | n/a | remove_module(name); |
|---|
| 820 | n/a | return NULL; |
|---|
| 821 | n/a | } |
|---|
| 822 | n/a | Py_DECREF(v); |
|---|
| 823 | n/a | |
|---|
| 824 | n/a | if ((m = PyDict_GetItem(modules, name)) == NULL) { |
|---|
| 825 | n/a | PyErr_Format(PyExc_ImportError, |
|---|
| 826 | n/a | "Loaded module %R not found in sys.modules", |
|---|
| 827 | n/a | name); |
|---|
| 828 | n/a | return NULL; |
|---|
| 829 | n/a | } |
|---|
| 830 | n/a | |
|---|
| 831 | n/a | Py_INCREF(m); |
|---|
| 832 | n/a | |
|---|
| 833 | n/a | return m; |
|---|
| 834 | n/a | } |
|---|
| 835 | n/a | |
|---|
| 836 | n/a | PyObject* |
|---|
| 837 | n/a | PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, |
|---|
| 838 | n/a | PyObject *cpathname) |
|---|
| 839 | n/a | { |
|---|
| 840 | n/a | PyObject *d, *external, *res; |
|---|
| 841 | n/a | PyInterpreterState *interp = PyThreadState_GET()->interp; |
|---|
| 842 | n/a | _Py_IDENTIFIER(_fix_up_module); |
|---|
| 843 | n/a | |
|---|
| 844 | n/a | d = module_dict_for_exec(name); |
|---|
| 845 | n/a | if (d == NULL) { |
|---|
| 846 | n/a | return NULL; |
|---|
| 847 | n/a | } |
|---|
| 848 | n/a | |
|---|
| 849 | n/a | if (pathname == NULL) { |
|---|
| 850 | n/a | pathname = ((PyCodeObject *)co)->co_filename; |
|---|
| 851 | n/a | } |
|---|
| 852 | n/a | external = PyObject_GetAttrString(interp->importlib, "_bootstrap_external"); |
|---|
| 853 | n/a | if (external == NULL) |
|---|
| 854 | n/a | return NULL; |
|---|
| 855 | n/a | res = _PyObject_CallMethodIdObjArgs(external, |
|---|
| 856 | n/a | &PyId__fix_up_module, |
|---|
| 857 | n/a | d, name, pathname, cpathname, NULL); |
|---|
| 858 | n/a | Py_DECREF(external); |
|---|
| 859 | n/a | if (res != NULL) { |
|---|
| 860 | n/a | Py_DECREF(res); |
|---|
| 861 | n/a | res = exec_code_in_module(name, d, co); |
|---|
| 862 | n/a | } |
|---|
| 863 | n/a | return res; |
|---|
| 864 | n/a | } |
|---|
| 865 | n/a | |
|---|
| 866 | n/a | |
|---|
| 867 | n/a | static void |
|---|
| 868 | n/a | update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname) |
|---|
| 869 | n/a | { |
|---|
| 870 | n/a | PyObject *constants, *tmp; |
|---|
| 871 | n/a | Py_ssize_t i, n; |
|---|
| 872 | n/a | |
|---|
| 873 | n/a | if (PyUnicode_Compare(co->co_filename, oldname)) |
|---|
| 874 | n/a | return; |
|---|
| 875 | n/a | |
|---|
| 876 | n/a | Py_INCREF(newname); |
|---|
| 877 | n/a | Py_XSETREF(co->co_filename, newname); |
|---|
| 878 | n/a | |
|---|
| 879 | n/a | constants = co->co_consts; |
|---|
| 880 | n/a | n = PyTuple_GET_SIZE(constants); |
|---|
| 881 | n/a | for (i = 0; i < n; i++) { |
|---|
| 882 | n/a | tmp = PyTuple_GET_ITEM(constants, i); |
|---|
| 883 | n/a | if (PyCode_Check(tmp)) |
|---|
| 884 | n/a | update_code_filenames((PyCodeObject *)tmp, |
|---|
| 885 | n/a | oldname, newname); |
|---|
| 886 | n/a | } |
|---|
| 887 | n/a | } |
|---|
| 888 | n/a | |
|---|
| 889 | n/a | static void |
|---|
| 890 | n/a | update_compiled_module(PyCodeObject *co, PyObject *newname) |
|---|
| 891 | n/a | { |
|---|
| 892 | n/a | PyObject *oldname; |
|---|
| 893 | n/a | |
|---|
| 894 | n/a | if (PyUnicode_Compare(co->co_filename, newname) == 0) |
|---|
| 895 | n/a | return; |
|---|
| 896 | n/a | |
|---|
| 897 | n/a | oldname = co->co_filename; |
|---|
| 898 | n/a | Py_INCREF(oldname); |
|---|
| 899 | n/a | update_code_filenames(co, oldname, newname); |
|---|
| 900 | n/a | Py_DECREF(oldname); |
|---|
| 901 | n/a | } |
|---|
| 902 | n/a | |
|---|
| 903 | n/a | /*[clinic input] |
|---|
| 904 | n/a | _imp._fix_co_filename |
|---|
| 905 | n/a | |
|---|
| 906 | n/a | code: object(type="PyCodeObject *", subclass_of="&PyCode_Type") |
|---|
| 907 | n/a | Code object to change. |
|---|
| 908 | n/a | |
|---|
| 909 | n/a | path: unicode |
|---|
| 910 | n/a | File path to use. |
|---|
| 911 | n/a | / |
|---|
| 912 | n/a | |
|---|
| 913 | n/a | Changes code.co_filename to specify the passed-in file path. |
|---|
| 914 | n/a | [clinic start generated code]*/ |
|---|
| 915 | n/a | |
|---|
| 916 | n/a | static PyObject * |
|---|
| 917 | n/a | _imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code, |
|---|
| 918 | n/a | PyObject *path) |
|---|
| 919 | n/a | /*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/ |
|---|
| 920 | n/a | |
|---|
| 921 | n/a | { |
|---|
| 922 | n/a | update_compiled_module(code, path); |
|---|
| 923 | n/a | |
|---|
| 924 | n/a | Py_RETURN_NONE; |
|---|
| 925 | n/a | } |
|---|
| 926 | n/a | |
|---|
| 927 | n/a | |
|---|
| 928 | n/a | /* Forward */ |
|---|
| 929 | n/a | static const struct _frozen * find_frozen(PyObject *); |
|---|
| 930 | n/a | |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | /* Helper to test for built-in module */ |
|---|
| 933 | n/a | |
|---|
| 934 | n/a | static int |
|---|
| 935 | n/a | is_builtin(PyObject *name) |
|---|
| 936 | n/a | { |
|---|
| 937 | n/a | int i; |
|---|
| 938 | n/a | for (i = 0; PyImport_Inittab[i].name != NULL; i++) { |
|---|
| 939 | n/a | if (_PyUnicode_EqualToASCIIString(name, PyImport_Inittab[i].name)) { |
|---|
| 940 | n/a | if (PyImport_Inittab[i].initfunc == NULL) |
|---|
| 941 | n/a | return -1; |
|---|
| 942 | n/a | else |
|---|
| 943 | n/a | return 1; |
|---|
| 944 | n/a | } |
|---|
| 945 | n/a | } |
|---|
| 946 | n/a | return 0; |
|---|
| 947 | n/a | } |
|---|
| 948 | n/a | |
|---|
| 949 | n/a | |
|---|
| 950 | n/a | /* Return a finder object for a sys.path/pkg.__path__ item 'p', |
|---|
| 951 | n/a | possibly by fetching it from the path_importer_cache dict. If it |
|---|
| 952 | n/a | wasn't yet cached, traverse path_hooks until a hook is found |
|---|
| 953 | n/a | that can handle the path item. Return None if no hook could; |
|---|
| 954 | n/a | this tells our caller that the path based finder could not find |
|---|
| 955 | n/a | a finder for this path item. Cache the result in |
|---|
| 956 | n/a | path_importer_cache. |
|---|
| 957 | n/a | Returns a borrowed reference. */ |
|---|
| 958 | n/a | |
|---|
| 959 | n/a | static PyObject * |
|---|
| 960 | n/a | get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks, |
|---|
| 961 | n/a | PyObject *p) |
|---|
| 962 | n/a | { |
|---|
| 963 | n/a | PyObject *importer; |
|---|
| 964 | n/a | Py_ssize_t j, nhooks; |
|---|
| 965 | n/a | |
|---|
| 966 | n/a | /* These conditions are the caller's responsibility: */ |
|---|
| 967 | n/a | assert(PyList_Check(path_hooks)); |
|---|
| 968 | n/a | assert(PyDict_Check(path_importer_cache)); |
|---|
| 969 | n/a | |
|---|
| 970 | n/a | nhooks = PyList_Size(path_hooks); |
|---|
| 971 | n/a | if (nhooks < 0) |
|---|
| 972 | n/a | return NULL; /* Shouldn't happen */ |
|---|
| 973 | n/a | |
|---|
| 974 | n/a | importer = PyDict_GetItem(path_importer_cache, p); |
|---|
| 975 | n/a | if (importer != NULL) |
|---|
| 976 | n/a | return importer; |
|---|
| 977 | n/a | |
|---|
| 978 | n/a | /* set path_importer_cache[p] to None to avoid recursion */ |
|---|
| 979 | n/a | if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0) |
|---|
| 980 | n/a | return NULL; |
|---|
| 981 | n/a | |
|---|
| 982 | n/a | for (j = 0; j < nhooks; j++) { |
|---|
| 983 | n/a | PyObject *hook = PyList_GetItem(path_hooks, j); |
|---|
| 984 | n/a | if (hook == NULL) |
|---|
| 985 | n/a | return NULL; |
|---|
| 986 | n/a | importer = PyObject_CallFunctionObjArgs(hook, p, NULL); |
|---|
| 987 | n/a | if (importer != NULL) |
|---|
| 988 | n/a | break; |
|---|
| 989 | n/a | |
|---|
| 990 | n/a | if (!PyErr_ExceptionMatches(PyExc_ImportError)) { |
|---|
| 991 | n/a | return NULL; |
|---|
| 992 | n/a | } |
|---|
| 993 | n/a | PyErr_Clear(); |
|---|
| 994 | n/a | } |
|---|
| 995 | n/a | if (importer == NULL) { |
|---|
| 996 | n/a | return Py_None; |
|---|
| 997 | n/a | } |
|---|
| 998 | n/a | if (importer != NULL) { |
|---|
| 999 | n/a | int err = PyDict_SetItem(path_importer_cache, p, importer); |
|---|
| 1000 | n/a | Py_DECREF(importer); |
|---|
| 1001 | n/a | if (err != 0) |
|---|
| 1002 | n/a | return NULL; |
|---|
| 1003 | n/a | } |
|---|
| 1004 | n/a | return importer; |
|---|
| 1005 | n/a | } |
|---|
| 1006 | n/a | |
|---|
| 1007 | n/a | PyAPI_FUNC(PyObject *) |
|---|
| 1008 | n/a | PyImport_GetImporter(PyObject *path) { |
|---|
| 1009 | n/a | PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL; |
|---|
| 1010 | n/a | |
|---|
| 1011 | n/a | path_importer_cache = PySys_GetObject("path_importer_cache"); |
|---|
| 1012 | n/a | path_hooks = PySys_GetObject("path_hooks"); |
|---|
| 1013 | n/a | if (path_importer_cache != NULL && path_hooks != NULL) { |
|---|
| 1014 | n/a | importer = get_path_importer(path_importer_cache, |
|---|
| 1015 | n/a | path_hooks, path); |
|---|
| 1016 | n/a | } |
|---|
| 1017 | n/a | Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */ |
|---|
| 1018 | n/a | return importer; |
|---|
| 1019 | n/a | } |
|---|
| 1020 | n/a | |
|---|
| 1021 | n/a | /*[clinic input] |
|---|
| 1022 | n/a | _imp.create_builtin |
|---|
| 1023 | n/a | |
|---|
| 1024 | n/a | spec: object |
|---|
| 1025 | n/a | / |
|---|
| 1026 | n/a | |
|---|
| 1027 | n/a | Create an extension module. |
|---|
| 1028 | n/a | [clinic start generated code]*/ |
|---|
| 1029 | n/a | |
|---|
| 1030 | n/a | static PyObject * |
|---|
| 1031 | n/a | _imp_create_builtin(PyObject *module, PyObject *spec) |
|---|
| 1032 | n/a | /*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/ |
|---|
| 1033 | n/a | { |
|---|
| 1034 | n/a | struct _inittab *p; |
|---|
| 1035 | n/a | PyObject *name; |
|---|
| 1036 | n/a | const char *namestr; |
|---|
| 1037 | n/a | PyObject *mod; |
|---|
| 1038 | n/a | |
|---|
| 1039 | n/a | name = PyObject_GetAttrString(spec, "name"); |
|---|
| 1040 | n/a | if (name == NULL) { |
|---|
| 1041 | n/a | return NULL; |
|---|
| 1042 | n/a | } |
|---|
| 1043 | n/a | |
|---|
| 1044 | n/a | mod = _PyImport_FindExtensionObject(name, name); |
|---|
| 1045 | n/a | if (mod || PyErr_Occurred()) { |
|---|
| 1046 | n/a | Py_DECREF(name); |
|---|
| 1047 | n/a | Py_XINCREF(mod); |
|---|
| 1048 | n/a | return mod; |
|---|
| 1049 | n/a | } |
|---|
| 1050 | n/a | |
|---|
| 1051 | n/a | namestr = PyUnicode_AsUTF8(name); |
|---|
| 1052 | n/a | if (namestr == NULL) { |
|---|
| 1053 | n/a | Py_DECREF(name); |
|---|
| 1054 | n/a | return NULL; |
|---|
| 1055 | n/a | } |
|---|
| 1056 | n/a | |
|---|
| 1057 | n/a | for (p = PyImport_Inittab; p->name != NULL; p++) { |
|---|
| 1058 | n/a | PyModuleDef *def; |
|---|
| 1059 | n/a | if (_PyUnicode_EqualToASCIIString(name, p->name)) { |
|---|
| 1060 | n/a | if (p->initfunc == NULL) { |
|---|
| 1061 | n/a | /* Cannot re-init internal module ("sys" or "builtins") */ |
|---|
| 1062 | n/a | mod = PyImport_AddModule(namestr); |
|---|
| 1063 | n/a | Py_DECREF(name); |
|---|
| 1064 | n/a | return mod; |
|---|
| 1065 | n/a | } |
|---|
| 1066 | n/a | mod = (*p->initfunc)(); |
|---|
| 1067 | n/a | if (mod == NULL) { |
|---|
| 1068 | n/a | Py_DECREF(name); |
|---|
| 1069 | n/a | return NULL; |
|---|
| 1070 | n/a | } |
|---|
| 1071 | n/a | if (PyObject_TypeCheck(mod, &PyModuleDef_Type)) { |
|---|
| 1072 | n/a | Py_DECREF(name); |
|---|
| 1073 | n/a | return PyModule_FromDefAndSpec((PyModuleDef*)mod, spec); |
|---|
| 1074 | n/a | } else { |
|---|
| 1075 | n/a | /* Remember pointer to module init function. */ |
|---|
| 1076 | n/a | def = PyModule_GetDef(mod); |
|---|
| 1077 | n/a | if (def == NULL) { |
|---|
| 1078 | n/a | Py_DECREF(name); |
|---|
| 1079 | n/a | return NULL; |
|---|
| 1080 | n/a | } |
|---|
| 1081 | n/a | def->m_base.m_init = p->initfunc; |
|---|
| 1082 | n/a | if (_PyImport_FixupExtensionObject(mod, name, name) < 0) { |
|---|
| 1083 | n/a | Py_DECREF(name); |
|---|
| 1084 | n/a | return NULL; |
|---|
| 1085 | n/a | } |
|---|
| 1086 | n/a | Py_DECREF(name); |
|---|
| 1087 | n/a | return mod; |
|---|
| 1088 | n/a | } |
|---|
| 1089 | n/a | } |
|---|
| 1090 | n/a | } |
|---|
| 1091 | n/a | Py_DECREF(name); |
|---|
| 1092 | n/a | Py_RETURN_NONE; |
|---|
| 1093 | n/a | } |
|---|
| 1094 | n/a | |
|---|
| 1095 | n/a | |
|---|
| 1096 | n/a | /* Frozen modules */ |
|---|
| 1097 | n/a | |
|---|
| 1098 | n/a | static const struct _frozen * |
|---|
| 1099 | n/a | find_frozen(PyObject *name) |
|---|
| 1100 | n/a | { |
|---|
| 1101 | n/a | const struct _frozen *p; |
|---|
| 1102 | n/a | |
|---|
| 1103 | n/a | if (name == NULL) |
|---|
| 1104 | n/a | return NULL; |
|---|
| 1105 | n/a | |
|---|
| 1106 | n/a | for (p = PyImport_FrozenModules; ; p++) { |
|---|
| 1107 | n/a | if (p->name == NULL) |
|---|
| 1108 | n/a | return NULL; |
|---|
| 1109 | n/a | if (_PyUnicode_EqualToASCIIString(name, p->name)) |
|---|
| 1110 | n/a | break; |
|---|
| 1111 | n/a | } |
|---|
| 1112 | n/a | return p; |
|---|
| 1113 | n/a | } |
|---|
| 1114 | n/a | |
|---|
| 1115 | n/a | static PyObject * |
|---|
| 1116 | n/a | get_frozen_object(PyObject *name) |
|---|
| 1117 | n/a | { |
|---|
| 1118 | n/a | const struct _frozen *p = find_frozen(name); |
|---|
| 1119 | n/a | int size; |
|---|
| 1120 | n/a | |
|---|
| 1121 | n/a | if (p == NULL) { |
|---|
| 1122 | n/a | PyErr_Format(PyExc_ImportError, |
|---|
| 1123 | n/a | "No such frozen object named %R", |
|---|
| 1124 | n/a | name); |
|---|
| 1125 | n/a | return NULL; |
|---|
| 1126 | n/a | } |
|---|
| 1127 | n/a | if (p->code == NULL) { |
|---|
| 1128 | n/a | PyErr_Format(PyExc_ImportError, |
|---|
| 1129 | n/a | "Excluded frozen object named %R", |
|---|
| 1130 | n/a | name); |
|---|
| 1131 | n/a | return NULL; |
|---|
| 1132 | n/a | } |
|---|
| 1133 | n/a | size = p->size; |
|---|
| 1134 | n/a | if (size < 0) |
|---|
| 1135 | n/a | size = -size; |
|---|
| 1136 | n/a | return PyMarshal_ReadObjectFromString((const char *)p->code, size); |
|---|
| 1137 | n/a | } |
|---|
| 1138 | n/a | |
|---|
| 1139 | n/a | static PyObject * |
|---|
| 1140 | n/a | is_frozen_package(PyObject *name) |
|---|
| 1141 | n/a | { |
|---|
| 1142 | n/a | const struct _frozen *p = find_frozen(name); |
|---|
| 1143 | n/a | int size; |
|---|
| 1144 | n/a | |
|---|
| 1145 | n/a | if (p == NULL) { |
|---|
| 1146 | n/a | PyErr_Format(PyExc_ImportError, |
|---|
| 1147 | n/a | "No such frozen object named %R", |
|---|
| 1148 | n/a | name); |
|---|
| 1149 | n/a | return NULL; |
|---|
| 1150 | n/a | } |
|---|
| 1151 | n/a | |
|---|
| 1152 | n/a | size = p->size; |
|---|
| 1153 | n/a | |
|---|
| 1154 | n/a | if (size < 0) |
|---|
| 1155 | n/a | Py_RETURN_TRUE; |
|---|
| 1156 | n/a | else |
|---|
| 1157 | n/a | Py_RETURN_FALSE; |
|---|
| 1158 | n/a | } |
|---|
| 1159 | n/a | |
|---|
| 1160 | n/a | |
|---|
| 1161 | n/a | /* Initialize a frozen module. |
|---|
| 1162 | n/a | Return 1 for success, 0 if the module is not found, and -1 with |
|---|
| 1163 | n/a | an exception set if the initialization failed. |
|---|
| 1164 | n/a | This function is also used from frozenmain.c */ |
|---|
| 1165 | n/a | |
|---|
| 1166 | n/a | int |
|---|
| 1167 | n/a | PyImport_ImportFrozenModuleObject(PyObject *name) |
|---|
| 1168 | n/a | { |
|---|
| 1169 | n/a | const struct _frozen *p; |
|---|
| 1170 | n/a | PyObject *co, *m, *d; |
|---|
| 1171 | n/a | int ispackage; |
|---|
| 1172 | n/a | int size; |
|---|
| 1173 | n/a | |
|---|
| 1174 | n/a | p = find_frozen(name); |
|---|
| 1175 | n/a | |
|---|
| 1176 | n/a | if (p == NULL) |
|---|
| 1177 | n/a | return 0; |
|---|
| 1178 | n/a | if (p->code == NULL) { |
|---|
| 1179 | n/a | PyErr_Format(PyExc_ImportError, |
|---|
| 1180 | n/a | "Excluded frozen object named %R", |
|---|
| 1181 | n/a | name); |
|---|
| 1182 | n/a | return -1; |
|---|
| 1183 | n/a | } |
|---|
| 1184 | n/a | size = p->size; |
|---|
| 1185 | n/a | ispackage = (size < 0); |
|---|
| 1186 | n/a | if (ispackage) |
|---|
| 1187 | n/a | size = -size; |
|---|
| 1188 | n/a | co = PyMarshal_ReadObjectFromString((const char *)p->code, size); |
|---|
| 1189 | n/a | if (co == NULL) |
|---|
| 1190 | n/a | return -1; |
|---|
| 1191 | n/a | if (!PyCode_Check(co)) { |
|---|
| 1192 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1193 | n/a | "frozen object %R is not a code object", |
|---|
| 1194 | n/a | name); |
|---|
| 1195 | n/a | goto err_return; |
|---|
| 1196 | n/a | } |
|---|
| 1197 | n/a | if (ispackage) { |
|---|
| 1198 | n/a | /* Set __path__ to the empty list */ |
|---|
| 1199 | n/a | PyObject *l; |
|---|
| 1200 | n/a | int err; |
|---|
| 1201 | n/a | m = PyImport_AddModuleObject(name); |
|---|
| 1202 | n/a | if (m == NULL) |
|---|
| 1203 | n/a | goto err_return; |
|---|
| 1204 | n/a | d = PyModule_GetDict(m); |
|---|
| 1205 | n/a | l = PyList_New(0); |
|---|
| 1206 | n/a | if (l == NULL) { |
|---|
| 1207 | n/a | goto err_return; |
|---|
| 1208 | n/a | } |
|---|
| 1209 | n/a | err = PyDict_SetItemString(d, "__path__", l); |
|---|
| 1210 | n/a | Py_DECREF(l); |
|---|
| 1211 | n/a | if (err != 0) |
|---|
| 1212 | n/a | goto err_return; |
|---|
| 1213 | n/a | } |
|---|
| 1214 | n/a | d = module_dict_for_exec(name); |
|---|
| 1215 | n/a | if (d == NULL) { |
|---|
| 1216 | n/a | goto err_return; |
|---|
| 1217 | n/a | } |
|---|
| 1218 | n/a | m = exec_code_in_module(name, d, co); |
|---|
| 1219 | n/a | if (m == NULL) |
|---|
| 1220 | n/a | goto err_return; |
|---|
| 1221 | n/a | Py_DECREF(co); |
|---|
| 1222 | n/a | Py_DECREF(m); |
|---|
| 1223 | n/a | return 1; |
|---|
| 1224 | n/a | err_return: |
|---|
| 1225 | n/a | Py_DECREF(co); |
|---|
| 1226 | n/a | return -1; |
|---|
| 1227 | n/a | } |
|---|
| 1228 | n/a | |
|---|
| 1229 | n/a | int |
|---|
| 1230 | n/a | PyImport_ImportFrozenModule(const char *name) |
|---|
| 1231 | n/a | { |
|---|
| 1232 | n/a | PyObject *nameobj; |
|---|
| 1233 | n/a | int ret; |
|---|
| 1234 | n/a | nameobj = PyUnicode_InternFromString(name); |
|---|
| 1235 | n/a | if (nameobj == NULL) |
|---|
| 1236 | n/a | return -1; |
|---|
| 1237 | n/a | ret = PyImport_ImportFrozenModuleObject(nameobj); |
|---|
| 1238 | n/a | Py_DECREF(nameobj); |
|---|
| 1239 | n/a | return ret; |
|---|
| 1240 | n/a | } |
|---|
| 1241 | n/a | |
|---|
| 1242 | n/a | |
|---|
| 1243 | n/a | /* Import a module, either built-in, frozen, or external, and return |
|---|
| 1244 | n/a | its module object WITH INCREMENTED REFERENCE COUNT */ |
|---|
| 1245 | n/a | |
|---|
| 1246 | n/a | PyObject * |
|---|
| 1247 | n/a | PyImport_ImportModule(const char *name) |
|---|
| 1248 | n/a | { |
|---|
| 1249 | n/a | PyObject *pname; |
|---|
| 1250 | n/a | PyObject *result; |
|---|
| 1251 | n/a | |
|---|
| 1252 | n/a | pname = PyUnicode_FromString(name); |
|---|
| 1253 | n/a | if (pname == NULL) |
|---|
| 1254 | n/a | return NULL; |
|---|
| 1255 | n/a | result = PyImport_Import(pname); |
|---|
| 1256 | n/a | Py_DECREF(pname); |
|---|
| 1257 | n/a | return result; |
|---|
| 1258 | n/a | } |
|---|
| 1259 | n/a | |
|---|
| 1260 | n/a | /* Import a module without blocking |
|---|
| 1261 | n/a | * |
|---|
| 1262 | n/a | * At first it tries to fetch the module from sys.modules. If the module was |
|---|
| 1263 | n/a | * never loaded before it loads it with PyImport_ImportModule() unless another |
|---|
| 1264 | n/a | * thread holds the import lock. In the latter case the function raises an |
|---|
| 1265 | n/a | * ImportError instead of blocking. |
|---|
| 1266 | n/a | * |
|---|
| 1267 | n/a | * Returns the module object with incremented ref count. |
|---|
| 1268 | n/a | */ |
|---|
| 1269 | n/a | PyObject * |
|---|
| 1270 | n/a | PyImport_ImportModuleNoBlock(const char *name) |
|---|
| 1271 | n/a | { |
|---|
| 1272 | n/a | return PyImport_ImportModule(name); |
|---|
| 1273 | n/a | } |
|---|
| 1274 | n/a | |
|---|
| 1275 | n/a | |
|---|
| 1276 | n/a | /* Remove importlib frames from the traceback, |
|---|
| 1277 | n/a | * except in Verbose mode. */ |
|---|
| 1278 | n/a | static void |
|---|
| 1279 | n/a | remove_importlib_frames(void) |
|---|
| 1280 | n/a | { |
|---|
| 1281 | n/a | const char *importlib_filename = "<frozen importlib._bootstrap>"; |
|---|
| 1282 | n/a | const char *external_filename = "<frozen importlib._bootstrap_external>"; |
|---|
| 1283 | n/a | const char *remove_frames = "_call_with_frames_removed"; |
|---|
| 1284 | n/a | int always_trim = 0; |
|---|
| 1285 | n/a | int in_importlib = 0; |
|---|
| 1286 | n/a | PyObject *exception, *value, *base_tb, *tb; |
|---|
| 1287 | n/a | PyObject **prev_link, **outer_link = NULL; |
|---|
| 1288 | n/a | |
|---|
| 1289 | n/a | /* Synopsis: if it's an ImportError, we trim all importlib chunks |
|---|
| 1290 | n/a | from the traceback. We always trim chunks |
|---|
| 1291 | n/a | which end with a call to "_call_with_frames_removed". */ |
|---|
| 1292 | n/a | |
|---|
| 1293 | n/a | PyErr_Fetch(&exception, &value, &base_tb); |
|---|
| 1294 | n/a | if (!exception || Py_VerboseFlag) |
|---|
| 1295 | n/a | goto done; |
|---|
| 1296 | n/a | if (PyType_IsSubtype((PyTypeObject *) exception, |
|---|
| 1297 | n/a | (PyTypeObject *) PyExc_ImportError)) |
|---|
| 1298 | n/a | always_trim = 1; |
|---|
| 1299 | n/a | |
|---|
| 1300 | n/a | prev_link = &base_tb; |
|---|
| 1301 | n/a | tb = base_tb; |
|---|
| 1302 | n/a | while (tb != NULL) { |
|---|
| 1303 | n/a | PyTracebackObject *traceback = (PyTracebackObject *)tb; |
|---|
| 1304 | n/a | PyObject *next = (PyObject *) traceback->tb_next; |
|---|
| 1305 | n/a | PyFrameObject *frame = traceback->tb_frame; |
|---|
| 1306 | n/a | PyCodeObject *code = frame->f_code; |
|---|
| 1307 | n/a | int now_in_importlib; |
|---|
| 1308 | n/a | |
|---|
| 1309 | n/a | assert(PyTraceBack_Check(tb)); |
|---|
| 1310 | n/a | now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) || |
|---|
| 1311 | n/a | _PyUnicode_EqualToASCIIString(code->co_filename, external_filename); |
|---|
| 1312 | n/a | if (now_in_importlib && !in_importlib) { |
|---|
| 1313 | n/a | /* This is the link to this chunk of importlib tracebacks */ |
|---|
| 1314 | n/a | outer_link = prev_link; |
|---|
| 1315 | n/a | } |
|---|
| 1316 | n/a | in_importlib = now_in_importlib; |
|---|
| 1317 | n/a | |
|---|
| 1318 | n/a | if (in_importlib && |
|---|
| 1319 | n/a | (always_trim || |
|---|
| 1320 | n/a | _PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) { |
|---|
| 1321 | n/a | Py_XINCREF(next); |
|---|
| 1322 | n/a | Py_XSETREF(*outer_link, next); |
|---|
| 1323 | n/a | prev_link = outer_link; |
|---|
| 1324 | n/a | } |
|---|
| 1325 | n/a | else { |
|---|
| 1326 | n/a | prev_link = (PyObject **) &traceback->tb_next; |
|---|
| 1327 | n/a | } |
|---|
| 1328 | n/a | tb = next; |
|---|
| 1329 | n/a | } |
|---|
| 1330 | n/a | done: |
|---|
| 1331 | n/a | PyErr_Restore(exception, value, base_tb); |
|---|
| 1332 | n/a | } |
|---|
| 1333 | n/a | |
|---|
| 1334 | n/a | |
|---|
| 1335 | n/a | static PyObject * |
|---|
| 1336 | n/a | resolve_name(PyObject *name, PyObject *globals, int level) |
|---|
| 1337 | n/a | { |
|---|
| 1338 | n/a | _Py_IDENTIFIER(__spec__); |
|---|
| 1339 | n/a | _Py_IDENTIFIER(__package__); |
|---|
| 1340 | n/a | _Py_IDENTIFIER(__path__); |
|---|
| 1341 | n/a | _Py_IDENTIFIER(__name__); |
|---|
| 1342 | n/a | _Py_IDENTIFIER(parent); |
|---|
| 1343 | n/a | PyObject *abs_name; |
|---|
| 1344 | n/a | PyObject *package = NULL; |
|---|
| 1345 | n/a | PyObject *spec; |
|---|
| 1346 | n/a | PyInterpreterState *interp = PyThreadState_GET()->interp; |
|---|
| 1347 | n/a | Py_ssize_t last_dot; |
|---|
| 1348 | n/a | PyObject *base; |
|---|
| 1349 | n/a | int level_up; |
|---|
| 1350 | n/a | |
|---|
| 1351 | n/a | if (globals == NULL) { |
|---|
| 1352 | n/a | PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); |
|---|
| 1353 | n/a | goto error; |
|---|
| 1354 | n/a | } |
|---|
| 1355 | n/a | if (!PyDict_Check(globals)) { |
|---|
| 1356 | n/a | PyErr_SetString(PyExc_TypeError, "globals must be a dict"); |
|---|
| 1357 | n/a | goto error; |
|---|
| 1358 | n/a | } |
|---|
| 1359 | n/a | package = _PyDict_GetItemId(globals, &PyId___package__); |
|---|
| 1360 | n/a | if (package == Py_None) { |
|---|
| 1361 | n/a | package = NULL; |
|---|
| 1362 | n/a | } |
|---|
| 1363 | n/a | spec = _PyDict_GetItemId(globals, &PyId___spec__); |
|---|
| 1364 | n/a | |
|---|
| 1365 | n/a | if (package != NULL) { |
|---|
| 1366 | n/a | Py_INCREF(package); |
|---|
| 1367 | n/a | if (!PyUnicode_Check(package)) { |
|---|
| 1368 | n/a | PyErr_SetString(PyExc_TypeError, "package must be a string"); |
|---|
| 1369 | n/a | goto error; |
|---|
| 1370 | n/a | } |
|---|
| 1371 | n/a | else if (spec != NULL && spec != Py_None) { |
|---|
| 1372 | n/a | int equal; |
|---|
| 1373 | n/a | PyObject *parent = _PyObject_GetAttrId(spec, &PyId_parent); |
|---|
| 1374 | n/a | if (parent == NULL) { |
|---|
| 1375 | n/a | goto error; |
|---|
| 1376 | n/a | } |
|---|
| 1377 | n/a | |
|---|
| 1378 | n/a | equal = PyObject_RichCompareBool(package, parent, Py_EQ); |
|---|
| 1379 | n/a | Py_DECREF(parent); |
|---|
| 1380 | n/a | if (equal < 0) { |
|---|
| 1381 | n/a | goto error; |
|---|
| 1382 | n/a | } |
|---|
| 1383 | n/a | else if (equal == 0) { |
|---|
| 1384 | n/a | if (PyErr_WarnEx(PyExc_ImportWarning, |
|---|
| 1385 | n/a | "__package__ != __spec__.parent", 1) < 0) { |
|---|
| 1386 | n/a | goto error; |
|---|
| 1387 | n/a | } |
|---|
| 1388 | n/a | } |
|---|
| 1389 | n/a | } |
|---|
| 1390 | n/a | } |
|---|
| 1391 | n/a | else if (spec != NULL && spec != Py_None) { |
|---|
| 1392 | n/a | package = _PyObject_GetAttrId(spec, &PyId_parent); |
|---|
| 1393 | n/a | if (package == NULL) { |
|---|
| 1394 | n/a | goto error; |
|---|
| 1395 | n/a | } |
|---|
| 1396 | n/a | else if (!PyUnicode_Check(package)) { |
|---|
| 1397 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1398 | n/a | "__spec__.parent must be a string"); |
|---|
| 1399 | n/a | goto error; |
|---|
| 1400 | n/a | } |
|---|
| 1401 | n/a | } |
|---|
| 1402 | n/a | else { |
|---|
| 1403 | n/a | if (PyErr_WarnEx(PyExc_ImportWarning, |
|---|
| 1404 | n/a | "can't resolve package from __spec__ or __package__, " |
|---|
| 1405 | n/a | "falling back on __name__ and __path__", 1) < 0) { |
|---|
| 1406 | n/a | goto error; |
|---|
| 1407 | n/a | } |
|---|
| 1408 | n/a | |
|---|
| 1409 | n/a | package = _PyDict_GetItemId(globals, &PyId___name__); |
|---|
| 1410 | n/a | if (package == NULL) { |
|---|
| 1411 | n/a | PyErr_SetString(PyExc_KeyError, "'__name__' not in globals"); |
|---|
| 1412 | n/a | goto error; |
|---|
| 1413 | n/a | } |
|---|
| 1414 | n/a | |
|---|
| 1415 | n/a | Py_INCREF(package); |
|---|
| 1416 | n/a | if (!PyUnicode_Check(package)) { |
|---|
| 1417 | n/a | PyErr_SetString(PyExc_TypeError, "__name__ must be a string"); |
|---|
| 1418 | n/a | goto error; |
|---|
| 1419 | n/a | } |
|---|
| 1420 | n/a | |
|---|
| 1421 | n/a | if (_PyDict_GetItemId(globals, &PyId___path__) == NULL) { |
|---|
| 1422 | n/a | Py_ssize_t dot; |
|---|
| 1423 | n/a | |
|---|
| 1424 | n/a | if (PyUnicode_READY(package) < 0) { |
|---|
| 1425 | n/a | goto error; |
|---|
| 1426 | n/a | } |
|---|
| 1427 | n/a | |
|---|
| 1428 | n/a | dot = PyUnicode_FindChar(package, '.', |
|---|
| 1429 | n/a | 0, PyUnicode_GET_LENGTH(package), -1); |
|---|
| 1430 | n/a | if (dot == -2) { |
|---|
| 1431 | n/a | goto error; |
|---|
| 1432 | n/a | } |
|---|
| 1433 | n/a | |
|---|
| 1434 | n/a | if (dot >= 0) { |
|---|
| 1435 | n/a | PyObject *substr = PyUnicode_Substring(package, 0, dot); |
|---|
| 1436 | n/a | if (substr == NULL) { |
|---|
| 1437 | n/a | goto error; |
|---|
| 1438 | n/a | } |
|---|
| 1439 | n/a | Py_SETREF(package, substr); |
|---|
| 1440 | n/a | } |
|---|
| 1441 | n/a | } |
|---|
| 1442 | n/a | } |
|---|
| 1443 | n/a | |
|---|
| 1444 | n/a | last_dot = PyUnicode_GET_LENGTH(package); |
|---|
| 1445 | n/a | if (last_dot == 0) { |
|---|
| 1446 | n/a | PyErr_SetString(PyExc_ImportError, |
|---|
| 1447 | n/a | "attempted relative import with no known parent package"); |
|---|
| 1448 | n/a | goto error; |
|---|
| 1449 | n/a | } |
|---|
| 1450 | n/a | else if (PyDict_GetItem(interp->modules, package) == NULL) { |
|---|
| 1451 | n/a | PyErr_Format(PyExc_SystemError, |
|---|
| 1452 | n/a | "Parent module %R not loaded, cannot perform relative " |
|---|
| 1453 | n/a | "import", package); |
|---|
| 1454 | n/a | goto error; |
|---|
| 1455 | n/a | } |
|---|
| 1456 | n/a | |
|---|
| 1457 | n/a | for (level_up = 1; level_up < level; level_up += 1) { |
|---|
| 1458 | n/a | last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1); |
|---|
| 1459 | n/a | if (last_dot == -2) { |
|---|
| 1460 | n/a | goto error; |
|---|
| 1461 | n/a | } |
|---|
| 1462 | n/a | else if (last_dot == -1) { |
|---|
| 1463 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 1464 | n/a | "attempted relative import beyond top-level " |
|---|
| 1465 | n/a | "package"); |
|---|
| 1466 | n/a | goto error; |
|---|
| 1467 | n/a | } |
|---|
| 1468 | n/a | } |
|---|
| 1469 | n/a | |
|---|
| 1470 | n/a | base = PyUnicode_Substring(package, 0, last_dot); |
|---|
| 1471 | n/a | Py_DECREF(package); |
|---|
| 1472 | n/a | if (base == NULL || PyUnicode_GET_LENGTH(name) == 0) { |
|---|
| 1473 | n/a | return base; |
|---|
| 1474 | n/a | } |
|---|
| 1475 | n/a | |
|---|
| 1476 | n/a | abs_name = PyUnicode_FromFormat("%U.%U", base, name); |
|---|
| 1477 | n/a | Py_DECREF(base); |
|---|
| 1478 | n/a | return abs_name; |
|---|
| 1479 | n/a | |
|---|
| 1480 | n/a | error: |
|---|
| 1481 | n/a | Py_XDECREF(package); |
|---|
| 1482 | n/a | return NULL; |
|---|
| 1483 | n/a | } |
|---|
| 1484 | n/a | |
|---|
| 1485 | n/a | PyObject * |
|---|
| 1486 | n/a | PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, |
|---|
| 1487 | n/a | PyObject *locals, PyObject *fromlist, |
|---|
| 1488 | n/a | int level) |
|---|
| 1489 | n/a | { |
|---|
| 1490 | n/a | _Py_IDENTIFIER(_find_and_load); |
|---|
| 1491 | n/a | _Py_IDENTIFIER(_handle_fromlist); |
|---|
| 1492 | n/a | PyObject *abs_name = NULL; |
|---|
| 1493 | n/a | PyObject *final_mod = NULL; |
|---|
| 1494 | n/a | PyObject *mod = NULL; |
|---|
| 1495 | n/a | PyObject *package = NULL; |
|---|
| 1496 | n/a | PyInterpreterState *interp = PyThreadState_GET()->interp; |
|---|
| 1497 | n/a | int has_from; |
|---|
| 1498 | n/a | |
|---|
| 1499 | n/a | if (name == NULL) { |
|---|
| 1500 | n/a | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
|---|
| 1501 | n/a | goto error; |
|---|
| 1502 | n/a | } |
|---|
| 1503 | n/a | |
|---|
| 1504 | n/a | /* The below code is importlib.__import__() & _gcd_import(), ported to C |
|---|
| 1505 | n/a | for added performance. */ |
|---|
| 1506 | n/a | |
|---|
| 1507 | n/a | if (!PyUnicode_Check(name)) { |
|---|
| 1508 | n/a | PyErr_SetString(PyExc_TypeError, "module name must be a string"); |
|---|
| 1509 | n/a | goto error; |
|---|
| 1510 | n/a | } |
|---|
| 1511 | n/a | if (PyUnicode_READY(name) < 0) { |
|---|
| 1512 | n/a | goto error; |
|---|
| 1513 | n/a | } |
|---|
| 1514 | n/a | if (level < 0) { |
|---|
| 1515 | n/a | PyErr_SetString(PyExc_ValueError, "level must be >= 0"); |
|---|
| 1516 | n/a | goto error; |
|---|
| 1517 | n/a | } |
|---|
| 1518 | n/a | |
|---|
| 1519 | n/a | if (level > 0) { |
|---|
| 1520 | n/a | abs_name = resolve_name(name, globals, level); |
|---|
| 1521 | n/a | if (abs_name == NULL) |
|---|
| 1522 | n/a | goto error; |
|---|
| 1523 | n/a | } |
|---|
| 1524 | n/a | else { /* level == 0 */ |
|---|
| 1525 | n/a | if (PyUnicode_GET_LENGTH(name) == 0) { |
|---|
| 1526 | n/a | PyErr_SetString(PyExc_ValueError, "Empty module name"); |
|---|
| 1527 | n/a | goto error; |
|---|
| 1528 | n/a | } |
|---|
| 1529 | n/a | abs_name = name; |
|---|
| 1530 | n/a | Py_INCREF(abs_name); |
|---|
| 1531 | n/a | } |
|---|
| 1532 | n/a | |
|---|
| 1533 | n/a | mod = PyDict_GetItem(interp->modules, abs_name); |
|---|
| 1534 | n/a | if (mod == Py_None) { |
|---|
| 1535 | n/a | PyObject *msg = PyUnicode_FromFormat("import of %R halted; " |
|---|
| 1536 | n/a | "None in sys.modules", abs_name); |
|---|
| 1537 | n/a | if (msg != NULL) { |
|---|
| 1538 | n/a | PyErr_SetImportErrorSubclass(PyExc_ModuleNotFoundError, msg, |
|---|
| 1539 | n/a | abs_name, NULL); |
|---|
| 1540 | n/a | Py_DECREF(msg); |
|---|
| 1541 | n/a | } |
|---|
| 1542 | n/a | mod = NULL; |
|---|
| 1543 | n/a | goto error; |
|---|
| 1544 | n/a | } |
|---|
| 1545 | n/a | else if (mod != NULL) { |
|---|
| 1546 | n/a | _Py_IDENTIFIER(__spec__); |
|---|
| 1547 | n/a | _Py_IDENTIFIER(_initializing); |
|---|
| 1548 | n/a | _Py_IDENTIFIER(_lock_unlock_module); |
|---|
| 1549 | n/a | PyObject *value = NULL; |
|---|
| 1550 | n/a | PyObject *spec; |
|---|
| 1551 | n/a | int initializing = 0; |
|---|
| 1552 | n/a | |
|---|
| 1553 | n/a | Py_INCREF(mod); |
|---|
| 1554 | n/a | /* Optimization: only call _bootstrap._lock_unlock_module() if |
|---|
| 1555 | n/a | __spec__._initializing is true. |
|---|
| 1556 | n/a | NOTE: because of this, initializing must be set *before* |
|---|
| 1557 | n/a | stuffing the new module in sys.modules. |
|---|
| 1558 | n/a | */ |
|---|
| 1559 | n/a | spec = _PyObject_GetAttrId(mod, &PyId___spec__); |
|---|
| 1560 | n/a | if (spec != NULL) { |
|---|
| 1561 | n/a | value = _PyObject_GetAttrId(spec, &PyId__initializing); |
|---|
| 1562 | n/a | Py_DECREF(spec); |
|---|
| 1563 | n/a | } |
|---|
| 1564 | n/a | if (value == NULL) |
|---|
| 1565 | n/a | PyErr_Clear(); |
|---|
| 1566 | n/a | else { |
|---|
| 1567 | n/a | initializing = PyObject_IsTrue(value); |
|---|
| 1568 | n/a | Py_DECREF(value); |
|---|
| 1569 | n/a | if (initializing == -1) |
|---|
| 1570 | n/a | PyErr_Clear(); |
|---|
| 1571 | n/a | if (initializing > 0) { |
|---|
| 1572 | n/a | #ifdef WITH_THREAD |
|---|
| 1573 | n/a | _PyImport_AcquireLock(); |
|---|
| 1574 | n/a | #endif |
|---|
| 1575 | n/a | /* _bootstrap._lock_unlock_module() releases the import lock */ |
|---|
| 1576 | n/a | value = _PyObject_CallMethodIdObjArgs(interp->importlib, |
|---|
| 1577 | n/a | &PyId__lock_unlock_module, abs_name, |
|---|
| 1578 | n/a | NULL); |
|---|
| 1579 | n/a | if (value == NULL) |
|---|
| 1580 | n/a | goto error; |
|---|
| 1581 | n/a | Py_DECREF(value); |
|---|
| 1582 | n/a | } |
|---|
| 1583 | n/a | } |
|---|
| 1584 | n/a | } |
|---|
| 1585 | n/a | else { |
|---|
| 1586 | n/a | #ifdef WITH_THREAD |
|---|
| 1587 | n/a | _PyImport_AcquireLock(); |
|---|
| 1588 | n/a | #endif |
|---|
| 1589 | n/a | /* _bootstrap._find_and_load() releases the import lock */ |
|---|
| 1590 | n/a | mod = _PyObject_CallMethodIdObjArgs(interp->importlib, |
|---|
| 1591 | n/a | &PyId__find_and_load, abs_name, |
|---|
| 1592 | n/a | interp->import_func, NULL); |
|---|
| 1593 | n/a | if (mod == NULL) { |
|---|
| 1594 | n/a | goto error; |
|---|
| 1595 | n/a | } |
|---|
| 1596 | n/a | } |
|---|
| 1597 | n/a | |
|---|
| 1598 | n/a | has_from = 0; |
|---|
| 1599 | n/a | if (fromlist != NULL && fromlist != Py_None) { |
|---|
| 1600 | n/a | has_from = PyObject_IsTrue(fromlist); |
|---|
| 1601 | n/a | if (has_from < 0) |
|---|
| 1602 | n/a | goto error; |
|---|
| 1603 | n/a | } |
|---|
| 1604 | n/a | if (!has_from) { |
|---|
| 1605 | n/a | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
|---|
| 1606 | n/a | if (level == 0 || len > 0) { |
|---|
| 1607 | n/a | Py_ssize_t dot; |
|---|
| 1608 | n/a | |
|---|
| 1609 | n/a | dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
|---|
| 1610 | n/a | if (dot == -2) { |
|---|
| 1611 | n/a | goto error; |
|---|
| 1612 | n/a | } |
|---|
| 1613 | n/a | |
|---|
| 1614 | n/a | if (dot == -1) { |
|---|
| 1615 | n/a | /* No dot in module name, simple exit */ |
|---|
| 1616 | n/a | final_mod = mod; |
|---|
| 1617 | n/a | Py_INCREF(mod); |
|---|
| 1618 | n/a | goto error; |
|---|
| 1619 | n/a | } |
|---|
| 1620 | n/a | |
|---|
| 1621 | n/a | if (level == 0) { |
|---|
| 1622 | n/a | PyObject *front = PyUnicode_Substring(name, 0, dot); |
|---|
| 1623 | n/a | if (front == NULL) { |
|---|
| 1624 | n/a | goto error; |
|---|
| 1625 | n/a | } |
|---|
| 1626 | n/a | |
|---|
| 1627 | n/a | final_mod = PyImport_ImportModuleLevelObject(front, NULL, NULL, NULL, 0); |
|---|
| 1628 | n/a | Py_DECREF(front); |
|---|
| 1629 | n/a | } |
|---|
| 1630 | n/a | else { |
|---|
| 1631 | n/a | Py_ssize_t cut_off = len - dot; |
|---|
| 1632 | n/a | Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name); |
|---|
| 1633 | n/a | PyObject *to_return = PyUnicode_Substring(abs_name, 0, |
|---|
| 1634 | n/a | abs_name_len - cut_off); |
|---|
| 1635 | n/a | if (to_return == NULL) { |
|---|
| 1636 | n/a | goto error; |
|---|
| 1637 | n/a | } |
|---|
| 1638 | n/a | |
|---|
| 1639 | n/a | final_mod = PyDict_GetItem(interp->modules, to_return); |
|---|
| 1640 | n/a | Py_DECREF(to_return); |
|---|
| 1641 | n/a | if (final_mod == NULL) { |
|---|
| 1642 | n/a | PyErr_Format(PyExc_KeyError, |
|---|
| 1643 | n/a | "%R not in sys.modules as expected", |
|---|
| 1644 | n/a | to_return); |
|---|
| 1645 | n/a | goto error; |
|---|
| 1646 | n/a | } |
|---|
| 1647 | n/a | Py_INCREF(final_mod); |
|---|
| 1648 | n/a | } |
|---|
| 1649 | n/a | } |
|---|
| 1650 | n/a | else { |
|---|
| 1651 | n/a | final_mod = mod; |
|---|
| 1652 | n/a | Py_INCREF(mod); |
|---|
| 1653 | n/a | } |
|---|
| 1654 | n/a | } |
|---|
| 1655 | n/a | else { |
|---|
| 1656 | n/a | final_mod = _PyObject_CallMethodIdObjArgs(interp->importlib, |
|---|
| 1657 | n/a | &PyId__handle_fromlist, mod, |
|---|
| 1658 | n/a | fromlist, interp->import_func, |
|---|
| 1659 | n/a | NULL); |
|---|
| 1660 | n/a | } |
|---|
| 1661 | n/a | |
|---|
| 1662 | n/a | error: |
|---|
| 1663 | n/a | Py_XDECREF(abs_name); |
|---|
| 1664 | n/a | Py_XDECREF(mod); |
|---|
| 1665 | n/a | Py_XDECREF(package); |
|---|
| 1666 | n/a | if (final_mod == NULL) |
|---|
| 1667 | n/a | remove_importlib_frames(); |
|---|
| 1668 | n/a | return final_mod; |
|---|
| 1669 | n/a | } |
|---|
| 1670 | n/a | |
|---|
| 1671 | n/a | PyObject * |
|---|
| 1672 | n/a | PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, |
|---|
| 1673 | n/a | PyObject *fromlist, int level) |
|---|
| 1674 | n/a | { |
|---|
| 1675 | n/a | PyObject *nameobj, *mod; |
|---|
| 1676 | n/a | nameobj = PyUnicode_FromString(name); |
|---|
| 1677 | n/a | if (nameobj == NULL) |
|---|
| 1678 | n/a | return NULL; |
|---|
| 1679 | n/a | mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals, |
|---|
| 1680 | n/a | fromlist, level); |
|---|
| 1681 | n/a | Py_DECREF(nameobj); |
|---|
| 1682 | n/a | return mod; |
|---|
| 1683 | n/a | } |
|---|
| 1684 | n/a | |
|---|
| 1685 | n/a | |
|---|
| 1686 | n/a | /* Re-import a module of any kind and return its module object, WITH |
|---|
| 1687 | n/a | INCREMENTED REFERENCE COUNT */ |
|---|
| 1688 | n/a | |
|---|
| 1689 | n/a | PyObject * |
|---|
| 1690 | n/a | PyImport_ReloadModule(PyObject *m) |
|---|
| 1691 | n/a | { |
|---|
| 1692 | n/a | _Py_IDENTIFIER(reload); |
|---|
| 1693 | n/a | PyObject *reloaded_module = NULL; |
|---|
| 1694 | n/a | PyObject *modules = PyImport_GetModuleDict(); |
|---|
| 1695 | n/a | PyObject *imp = PyDict_GetItemString(modules, "imp"); |
|---|
| 1696 | n/a | if (imp == NULL) { |
|---|
| 1697 | n/a | imp = PyImport_ImportModule("imp"); |
|---|
| 1698 | n/a | if (imp == NULL) { |
|---|
| 1699 | n/a | return NULL; |
|---|
| 1700 | n/a | } |
|---|
| 1701 | n/a | } |
|---|
| 1702 | n/a | else { |
|---|
| 1703 | n/a | Py_INCREF(imp); |
|---|
| 1704 | n/a | } |
|---|
| 1705 | n/a | |
|---|
| 1706 | n/a | reloaded_module = _PyObject_CallMethodIdObjArgs(imp, &PyId_reload, m, NULL); |
|---|
| 1707 | n/a | Py_DECREF(imp); |
|---|
| 1708 | n/a | return reloaded_module; |
|---|
| 1709 | n/a | } |
|---|
| 1710 | n/a | |
|---|
| 1711 | n/a | |
|---|
| 1712 | n/a | /* Higher-level import emulator which emulates the "import" statement |
|---|
| 1713 | n/a | more accurately -- it invokes the __import__() function from the |
|---|
| 1714 | n/a | builtins of the current globals. This means that the import is |
|---|
| 1715 | n/a | done using whatever import hooks are installed in the current |
|---|
| 1716 | n/a | environment. |
|---|
| 1717 | n/a | A dummy list ["__doc__"] is passed as the 4th argument so that |
|---|
| 1718 | n/a | e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache")) |
|---|
| 1719 | n/a | will return <module "gencache"> instead of <module "win32com">. */ |
|---|
| 1720 | n/a | |
|---|
| 1721 | n/a | PyObject * |
|---|
| 1722 | n/a | PyImport_Import(PyObject *module_name) |
|---|
| 1723 | n/a | { |
|---|
| 1724 | n/a | static PyObject *silly_list = NULL; |
|---|
| 1725 | n/a | static PyObject *builtins_str = NULL; |
|---|
| 1726 | n/a | static PyObject *import_str = NULL; |
|---|
| 1727 | n/a | PyObject *globals = NULL; |
|---|
| 1728 | n/a | PyObject *import = NULL; |
|---|
| 1729 | n/a | PyObject *builtins = NULL; |
|---|
| 1730 | n/a | PyObject *modules = NULL; |
|---|
| 1731 | n/a | PyObject *r = NULL; |
|---|
| 1732 | n/a | |
|---|
| 1733 | n/a | /* Initialize constant string objects */ |
|---|
| 1734 | n/a | if (silly_list == NULL) { |
|---|
| 1735 | n/a | import_str = PyUnicode_InternFromString("__import__"); |
|---|
| 1736 | n/a | if (import_str == NULL) |
|---|
| 1737 | n/a | return NULL; |
|---|
| 1738 | n/a | builtins_str = PyUnicode_InternFromString("__builtins__"); |
|---|
| 1739 | n/a | if (builtins_str == NULL) |
|---|
| 1740 | n/a | return NULL; |
|---|
| 1741 | n/a | silly_list = PyList_New(0); |
|---|
| 1742 | n/a | if (silly_list == NULL) |
|---|
| 1743 | n/a | return NULL; |
|---|
| 1744 | n/a | } |
|---|
| 1745 | n/a | |
|---|
| 1746 | n/a | /* Get the builtins from current globals */ |
|---|
| 1747 | n/a | globals = PyEval_GetGlobals(); |
|---|
| 1748 | n/a | if (globals != NULL) { |
|---|
| 1749 | n/a | Py_INCREF(globals); |
|---|
| 1750 | n/a | builtins = PyObject_GetItem(globals, builtins_str); |
|---|
| 1751 | n/a | if (builtins == NULL) |
|---|
| 1752 | n/a | goto err; |
|---|
| 1753 | n/a | } |
|---|
| 1754 | n/a | else { |
|---|
| 1755 | n/a | /* No globals -- use standard builtins, and fake globals */ |
|---|
| 1756 | n/a | builtins = PyImport_ImportModuleLevel("builtins", |
|---|
| 1757 | n/a | NULL, NULL, NULL, 0); |
|---|
| 1758 | n/a | if (builtins == NULL) |
|---|
| 1759 | n/a | return NULL; |
|---|
| 1760 | n/a | globals = Py_BuildValue("{OO}", builtins_str, builtins); |
|---|
| 1761 | n/a | if (globals == NULL) |
|---|
| 1762 | n/a | goto err; |
|---|
| 1763 | n/a | } |
|---|
| 1764 | n/a | |
|---|
| 1765 | n/a | /* Get the __import__ function from the builtins */ |
|---|
| 1766 | n/a | if (PyDict_Check(builtins)) { |
|---|
| 1767 | n/a | import = PyObject_GetItem(builtins, import_str); |
|---|
| 1768 | n/a | if (import == NULL) |
|---|
| 1769 | n/a | PyErr_SetObject(PyExc_KeyError, import_str); |
|---|
| 1770 | n/a | } |
|---|
| 1771 | n/a | else |
|---|
| 1772 | n/a | import = PyObject_GetAttr(builtins, import_str); |
|---|
| 1773 | n/a | if (import == NULL) |
|---|
| 1774 | n/a | goto err; |
|---|
| 1775 | n/a | |
|---|
| 1776 | n/a | /* Call the __import__ function with the proper argument list |
|---|
| 1777 | n/a | Always use absolute import here. |
|---|
| 1778 | n/a | Calling for side-effect of import. */ |
|---|
| 1779 | n/a | r = PyObject_CallFunction(import, "OOOOi", module_name, globals, |
|---|
| 1780 | n/a | globals, silly_list, 0, NULL); |
|---|
| 1781 | n/a | if (r == NULL) |
|---|
| 1782 | n/a | goto err; |
|---|
| 1783 | n/a | Py_DECREF(r); |
|---|
| 1784 | n/a | |
|---|
| 1785 | n/a | modules = PyImport_GetModuleDict(); |
|---|
| 1786 | n/a | r = PyDict_GetItem(modules, module_name); |
|---|
| 1787 | n/a | if (r != NULL) |
|---|
| 1788 | n/a | Py_INCREF(r); |
|---|
| 1789 | n/a | |
|---|
| 1790 | n/a | err: |
|---|
| 1791 | n/a | Py_XDECREF(globals); |
|---|
| 1792 | n/a | Py_XDECREF(builtins); |
|---|
| 1793 | n/a | Py_XDECREF(import); |
|---|
| 1794 | n/a | |
|---|
| 1795 | n/a | return r; |
|---|
| 1796 | n/a | } |
|---|
| 1797 | n/a | |
|---|
| 1798 | n/a | /*[clinic input] |
|---|
| 1799 | n/a | _imp.extension_suffixes |
|---|
| 1800 | n/a | |
|---|
| 1801 | n/a | Returns the list of file suffixes used to identify extension modules. |
|---|
| 1802 | n/a | [clinic start generated code]*/ |
|---|
| 1803 | n/a | |
|---|
| 1804 | n/a | static PyObject * |
|---|
| 1805 | n/a | _imp_extension_suffixes_impl(PyObject *module) |
|---|
| 1806 | n/a | /*[clinic end generated code: output=0bf346e25a8f0cd3 input=ecdeeecfcb6f839e]*/ |
|---|
| 1807 | n/a | { |
|---|
| 1808 | n/a | PyObject *list; |
|---|
| 1809 | n/a | const char *suffix; |
|---|
| 1810 | n/a | unsigned int index = 0; |
|---|
| 1811 | n/a | |
|---|
| 1812 | n/a | list = PyList_New(0); |
|---|
| 1813 | n/a | if (list == NULL) |
|---|
| 1814 | n/a | return NULL; |
|---|
| 1815 | n/a | #ifdef HAVE_DYNAMIC_LOADING |
|---|
| 1816 | n/a | while ((suffix = _PyImport_DynLoadFiletab[index])) { |
|---|
| 1817 | n/a | PyObject *item = PyUnicode_FromString(suffix); |
|---|
| 1818 | n/a | if (item == NULL) { |
|---|
| 1819 | n/a | Py_DECREF(list); |
|---|
| 1820 | n/a | return NULL; |
|---|
| 1821 | n/a | } |
|---|
| 1822 | n/a | if (PyList_Append(list, item) < 0) { |
|---|
| 1823 | n/a | Py_DECREF(list); |
|---|
| 1824 | n/a | Py_DECREF(item); |
|---|
| 1825 | n/a | return NULL; |
|---|
| 1826 | n/a | } |
|---|
| 1827 | n/a | Py_DECREF(item); |
|---|
| 1828 | n/a | index += 1; |
|---|
| 1829 | n/a | } |
|---|
| 1830 | n/a | #endif |
|---|
| 1831 | n/a | return list; |
|---|
| 1832 | n/a | } |
|---|
| 1833 | n/a | |
|---|
| 1834 | n/a | /*[clinic input] |
|---|
| 1835 | n/a | _imp.init_frozen |
|---|
| 1836 | n/a | |
|---|
| 1837 | n/a | name: unicode |
|---|
| 1838 | n/a | / |
|---|
| 1839 | n/a | |
|---|
| 1840 | n/a | Initializes a frozen module. |
|---|
| 1841 | n/a | [clinic start generated code]*/ |
|---|
| 1842 | n/a | |
|---|
| 1843 | n/a | static PyObject * |
|---|
| 1844 | n/a | _imp_init_frozen_impl(PyObject *module, PyObject *name) |
|---|
| 1845 | n/a | /*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/ |
|---|
| 1846 | n/a | { |
|---|
| 1847 | n/a | int ret; |
|---|
| 1848 | n/a | PyObject *m; |
|---|
| 1849 | n/a | |
|---|
| 1850 | n/a | ret = PyImport_ImportFrozenModuleObject(name); |
|---|
| 1851 | n/a | if (ret < 0) |
|---|
| 1852 | n/a | return NULL; |
|---|
| 1853 | n/a | if (ret == 0) { |
|---|
| 1854 | n/a | Py_RETURN_NONE; |
|---|
| 1855 | n/a | } |
|---|
| 1856 | n/a | m = PyImport_AddModuleObject(name); |
|---|
| 1857 | n/a | Py_XINCREF(m); |
|---|
| 1858 | n/a | return m; |
|---|
| 1859 | n/a | } |
|---|
| 1860 | n/a | |
|---|
| 1861 | n/a | /*[clinic input] |
|---|
| 1862 | n/a | _imp.get_frozen_object |
|---|
| 1863 | n/a | |
|---|
| 1864 | n/a | name: unicode |
|---|
| 1865 | n/a | / |
|---|
| 1866 | n/a | |
|---|
| 1867 | n/a | Create a code object for a frozen module. |
|---|
| 1868 | n/a | [clinic start generated code]*/ |
|---|
| 1869 | n/a | |
|---|
| 1870 | n/a | static PyObject * |
|---|
| 1871 | n/a | _imp_get_frozen_object_impl(PyObject *module, PyObject *name) |
|---|
| 1872 | n/a | /*[clinic end generated code: output=2568cc5b7aa0da63 input=ed689bc05358fdbd]*/ |
|---|
| 1873 | n/a | { |
|---|
| 1874 | n/a | return get_frozen_object(name); |
|---|
| 1875 | n/a | } |
|---|
| 1876 | n/a | |
|---|
| 1877 | n/a | /*[clinic input] |
|---|
| 1878 | n/a | _imp.is_frozen_package |
|---|
| 1879 | n/a | |
|---|
| 1880 | n/a | name: unicode |
|---|
| 1881 | n/a | / |
|---|
| 1882 | n/a | |
|---|
| 1883 | n/a | Returns True if the module name is of a frozen package. |
|---|
| 1884 | n/a | [clinic start generated code]*/ |
|---|
| 1885 | n/a | |
|---|
| 1886 | n/a | static PyObject * |
|---|
| 1887 | n/a | _imp_is_frozen_package_impl(PyObject *module, PyObject *name) |
|---|
| 1888 | n/a | /*[clinic end generated code: output=e70cbdb45784a1c9 input=81b6cdecd080fbb8]*/ |
|---|
| 1889 | n/a | { |
|---|
| 1890 | n/a | return is_frozen_package(name); |
|---|
| 1891 | n/a | } |
|---|
| 1892 | n/a | |
|---|
| 1893 | n/a | /*[clinic input] |
|---|
| 1894 | n/a | _imp.is_builtin |
|---|
| 1895 | n/a | |
|---|
| 1896 | n/a | name: unicode |
|---|
| 1897 | n/a | / |
|---|
| 1898 | n/a | |
|---|
| 1899 | n/a | Returns True if the module name corresponds to a built-in module. |
|---|
| 1900 | n/a | [clinic start generated code]*/ |
|---|
| 1901 | n/a | |
|---|
| 1902 | n/a | static PyObject * |
|---|
| 1903 | n/a | _imp_is_builtin_impl(PyObject *module, PyObject *name) |
|---|
| 1904 | n/a | /*[clinic end generated code: output=3bfd1162e2d3be82 input=86befdac021dd1c7]*/ |
|---|
| 1905 | n/a | { |
|---|
| 1906 | n/a | return PyLong_FromLong(is_builtin(name)); |
|---|
| 1907 | n/a | } |
|---|
| 1908 | n/a | |
|---|
| 1909 | n/a | /*[clinic input] |
|---|
| 1910 | n/a | _imp.is_frozen |
|---|
| 1911 | n/a | |
|---|
| 1912 | n/a | name: unicode |
|---|
| 1913 | n/a | / |
|---|
| 1914 | n/a | |
|---|
| 1915 | n/a | Returns True if the module name corresponds to a frozen module. |
|---|
| 1916 | n/a | [clinic start generated code]*/ |
|---|
| 1917 | n/a | |
|---|
| 1918 | n/a | static PyObject * |
|---|
| 1919 | n/a | _imp_is_frozen_impl(PyObject *module, PyObject *name) |
|---|
| 1920 | n/a | /*[clinic end generated code: output=01f408f5ec0f2577 input=7301dbca1897d66b]*/ |
|---|
| 1921 | n/a | { |
|---|
| 1922 | n/a | const struct _frozen *p; |
|---|
| 1923 | n/a | |
|---|
| 1924 | n/a | p = find_frozen(name); |
|---|
| 1925 | n/a | return PyBool_FromLong((long) (p == NULL ? 0 : p->size)); |
|---|
| 1926 | n/a | } |
|---|
| 1927 | n/a | |
|---|
| 1928 | n/a | /* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */ |
|---|
| 1929 | n/a | static int |
|---|
| 1930 | n/a | exec_builtin_or_dynamic(PyObject *mod) { |
|---|
| 1931 | n/a | PyModuleDef *def; |
|---|
| 1932 | n/a | void *state; |
|---|
| 1933 | n/a | |
|---|
| 1934 | n/a | if (!PyModule_Check(mod)) { |
|---|
| 1935 | n/a | return 0; |
|---|
| 1936 | n/a | } |
|---|
| 1937 | n/a | |
|---|
| 1938 | n/a | def = PyModule_GetDef(mod); |
|---|
| 1939 | n/a | if (def == NULL) { |
|---|
| 1940 | n/a | return 0; |
|---|
| 1941 | n/a | } |
|---|
| 1942 | n/a | |
|---|
| 1943 | n/a | state = PyModule_GetState(mod); |
|---|
| 1944 | n/a | if (state) { |
|---|
| 1945 | n/a | /* Already initialized; skip reload */ |
|---|
| 1946 | n/a | return 0; |
|---|
| 1947 | n/a | } |
|---|
| 1948 | n/a | |
|---|
| 1949 | n/a | return PyModule_ExecDef(mod, def); |
|---|
| 1950 | n/a | } |
|---|
| 1951 | n/a | |
|---|
| 1952 | n/a | #ifdef HAVE_DYNAMIC_LOADING |
|---|
| 1953 | n/a | |
|---|
| 1954 | n/a | /*[clinic input] |
|---|
| 1955 | n/a | _imp.create_dynamic |
|---|
| 1956 | n/a | |
|---|
| 1957 | n/a | spec: object |
|---|
| 1958 | n/a | file: object = NULL |
|---|
| 1959 | n/a | / |
|---|
| 1960 | n/a | |
|---|
| 1961 | n/a | Create an extension module. |
|---|
| 1962 | n/a | [clinic start generated code]*/ |
|---|
| 1963 | n/a | |
|---|
| 1964 | n/a | static PyObject * |
|---|
| 1965 | n/a | _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file) |
|---|
| 1966 | n/a | /*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/ |
|---|
| 1967 | n/a | { |
|---|
| 1968 | n/a | PyObject *mod, *name, *path; |
|---|
| 1969 | n/a | FILE *fp; |
|---|
| 1970 | n/a | |
|---|
| 1971 | n/a | name = PyObject_GetAttrString(spec, "name"); |
|---|
| 1972 | n/a | if (name == NULL) { |
|---|
| 1973 | n/a | return NULL; |
|---|
| 1974 | n/a | } |
|---|
| 1975 | n/a | |
|---|
| 1976 | n/a | path = PyObject_GetAttrString(spec, "origin"); |
|---|
| 1977 | n/a | if (path == NULL) { |
|---|
| 1978 | n/a | Py_DECREF(name); |
|---|
| 1979 | n/a | return NULL; |
|---|
| 1980 | n/a | } |
|---|
| 1981 | n/a | |
|---|
| 1982 | n/a | mod = _PyImport_FindExtensionObject(name, path); |
|---|
| 1983 | n/a | if (mod != NULL) { |
|---|
| 1984 | n/a | Py_DECREF(name); |
|---|
| 1985 | n/a | Py_DECREF(path); |
|---|
| 1986 | n/a | Py_INCREF(mod); |
|---|
| 1987 | n/a | return mod; |
|---|
| 1988 | n/a | } |
|---|
| 1989 | n/a | |
|---|
| 1990 | n/a | if (file != NULL) { |
|---|
| 1991 | n/a | fp = _Py_fopen_obj(path, "r"); |
|---|
| 1992 | n/a | if (fp == NULL) { |
|---|
| 1993 | n/a | Py_DECREF(name); |
|---|
| 1994 | n/a | Py_DECREF(path); |
|---|
| 1995 | n/a | return NULL; |
|---|
| 1996 | n/a | } |
|---|
| 1997 | n/a | } |
|---|
| 1998 | n/a | else |
|---|
| 1999 | n/a | fp = NULL; |
|---|
| 2000 | n/a | |
|---|
| 2001 | n/a | mod = _PyImport_LoadDynamicModuleWithSpec(spec, fp); |
|---|
| 2002 | n/a | |
|---|
| 2003 | n/a | Py_DECREF(name); |
|---|
| 2004 | n/a | Py_DECREF(path); |
|---|
| 2005 | n/a | if (fp) |
|---|
| 2006 | n/a | fclose(fp); |
|---|
| 2007 | n/a | return mod; |
|---|
| 2008 | n/a | } |
|---|
| 2009 | n/a | |
|---|
| 2010 | n/a | /*[clinic input] |
|---|
| 2011 | n/a | _imp.exec_dynamic -> int |
|---|
| 2012 | n/a | |
|---|
| 2013 | n/a | mod: object |
|---|
| 2014 | n/a | / |
|---|
| 2015 | n/a | |
|---|
| 2016 | n/a | Initialize an extension module. |
|---|
| 2017 | n/a | [clinic start generated code]*/ |
|---|
| 2018 | n/a | |
|---|
| 2019 | n/a | static int |
|---|
| 2020 | n/a | _imp_exec_dynamic_impl(PyObject *module, PyObject *mod) |
|---|
| 2021 | n/a | /*[clinic end generated code: output=f5720ac7b465877d input=9fdbfcb250280d3a]*/ |
|---|
| 2022 | n/a | { |
|---|
| 2023 | n/a | return exec_builtin_or_dynamic(mod); |
|---|
| 2024 | n/a | } |
|---|
| 2025 | n/a | |
|---|
| 2026 | n/a | |
|---|
| 2027 | n/a | #endif /* HAVE_DYNAMIC_LOADING */ |
|---|
| 2028 | n/a | |
|---|
| 2029 | n/a | /*[clinic input] |
|---|
| 2030 | n/a | _imp.exec_builtin -> int |
|---|
| 2031 | n/a | |
|---|
| 2032 | n/a | mod: object |
|---|
| 2033 | n/a | / |
|---|
| 2034 | n/a | |
|---|
| 2035 | n/a | Initialize a built-in module. |
|---|
| 2036 | n/a | [clinic start generated code]*/ |
|---|
| 2037 | n/a | |
|---|
| 2038 | n/a | static int |
|---|
| 2039 | n/a | _imp_exec_builtin_impl(PyObject *module, PyObject *mod) |
|---|
| 2040 | n/a | /*[clinic end generated code: output=0262447b240c038e input=7beed5a2f12a60ca]*/ |
|---|
| 2041 | n/a | { |
|---|
| 2042 | n/a | return exec_builtin_or_dynamic(mod); |
|---|
| 2043 | n/a | } |
|---|
| 2044 | n/a | |
|---|
| 2045 | n/a | |
|---|
| 2046 | n/a | PyDoc_STRVAR(doc_imp, |
|---|
| 2047 | n/a | "(Extremely) low-level import machinery bits as used by importlib and imp."); |
|---|
| 2048 | n/a | |
|---|
| 2049 | n/a | static PyMethodDef imp_methods[] = { |
|---|
| 2050 | n/a | _IMP_EXTENSION_SUFFIXES_METHODDEF |
|---|
| 2051 | n/a | _IMP_LOCK_HELD_METHODDEF |
|---|
| 2052 | n/a | _IMP_ACQUIRE_LOCK_METHODDEF |
|---|
| 2053 | n/a | _IMP_RELEASE_LOCK_METHODDEF |
|---|
| 2054 | n/a | _IMP_GET_FROZEN_OBJECT_METHODDEF |
|---|
| 2055 | n/a | _IMP_IS_FROZEN_PACKAGE_METHODDEF |
|---|
| 2056 | n/a | _IMP_CREATE_BUILTIN_METHODDEF |
|---|
| 2057 | n/a | _IMP_INIT_FROZEN_METHODDEF |
|---|
| 2058 | n/a | _IMP_IS_BUILTIN_METHODDEF |
|---|
| 2059 | n/a | _IMP_IS_FROZEN_METHODDEF |
|---|
| 2060 | n/a | _IMP_CREATE_DYNAMIC_METHODDEF |
|---|
| 2061 | n/a | _IMP_EXEC_DYNAMIC_METHODDEF |
|---|
| 2062 | n/a | _IMP_EXEC_BUILTIN_METHODDEF |
|---|
| 2063 | n/a | _IMP__FIX_CO_FILENAME_METHODDEF |
|---|
| 2064 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 2065 | n/a | }; |
|---|
| 2066 | n/a | |
|---|
| 2067 | n/a | |
|---|
| 2068 | n/a | static struct PyModuleDef impmodule = { |
|---|
| 2069 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 2070 | n/a | "_imp", |
|---|
| 2071 | n/a | doc_imp, |
|---|
| 2072 | n/a | 0, |
|---|
| 2073 | n/a | imp_methods, |
|---|
| 2074 | n/a | NULL, |
|---|
| 2075 | n/a | NULL, |
|---|
| 2076 | n/a | NULL, |
|---|
| 2077 | n/a | NULL |
|---|
| 2078 | n/a | }; |
|---|
| 2079 | n/a | |
|---|
| 2080 | n/a | PyMODINIT_FUNC |
|---|
| 2081 | n/a | PyInit_imp(void) |
|---|
| 2082 | n/a | { |
|---|
| 2083 | n/a | PyObject *m, *d; |
|---|
| 2084 | n/a | |
|---|
| 2085 | n/a | m = PyModule_Create(&impmodule); |
|---|
| 2086 | n/a | if (m == NULL) |
|---|
| 2087 | n/a | goto failure; |
|---|
| 2088 | n/a | d = PyModule_GetDict(m); |
|---|
| 2089 | n/a | if (d == NULL) |
|---|
| 2090 | n/a | goto failure; |
|---|
| 2091 | n/a | |
|---|
| 2092 | n/a | return m; |
|---|
| 2093 | n/a | failure: |
|---|
| 2094 | n/a | Py_XDECREF(m); |
|---|
| 2095 | n/a | return NULL; |
|---|
| 2096 | n/a | } |
|---|
| 2097 | n/a | |
|---|
| 2098 | n/a | |
|---|
| 2099 | n/a | /* API for embedding applications that want to add their own entries |
|---|
| 2100 | n/a | to the table of built-in modules. This should normally be called |
|---|
| 2101 | n/a | *before* Py_Initialize(). When the table resize fails, -1 is |
|---|
| 2102 | n/a | returned and the existing table is unchanged. |
|---|
| 2103 | n/a | |
|---|
| 2104 | n/a | After a similar function by Just van Rossum. */ |
|---|
| 2105 | n/a | |
|---|
| 2106 | n/a | int |
|---|
| 2107 | n/a | PyImport_ExtendInittab(struct _inittab *newtab) |
|---|
| 2108 | n/a | { |
|---|
| 2109 | n/a | static struct _inittab *our_copy = NULL; |
|---|
| 2110 | n/a | struct _inittab *p; |
|---|
| 2111 | n/a | int i, n; |
|---|
| 2112 | n/a | |
|---|
| 2113 | n/a | /* Count the number of entries in both tables */ |
|---|
| 2114 | n/a | for (n = 0; newtab[n].name != NULL; n++) |
|---|
| 2115 | n/a | ; |
|---|
| 2116 | n/a | if (n == 0) |
|---|
| 2117 | n/a | return 0; /* Nothing to do */ |
|---|
| 2118 | n/a | for (i = 0; PyImport_Inittab[i].name != NULL; i++) |
|---|
| 2119 | n/a | ; |
|---|
| 2120 | n/a | |
|---|
| 2121 | n/a | /* Allocate new memory for the combined table */ |
|---|
| 2122 | n/a | p = our_copy; |
|---|
| 2123 | n/a | PyMem_RESIZE(p, struct _inittab, i+n+1); |
|---|
| 2124 | n/a | if (p == NULL) |
|---|
| 2125 | n/a | return -1; |
|---|
| 2126 | n/a | |
|---|
| 2127 | n/a | /* Copy the tables into the new memory */ |
|---|
| 2128 | n/a | if (our_copy != PyImport_Inittab) |
|---|
| 2129 | n/a | memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab)); |
|---|
| 2130 | n/a | PyImport_Inittab = our_copy = p; |
|---|
| 2131 | n/a | memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab)); |
|---|
| 2132 | n/a | |
|---|
| 2133 | n/a | return 0; |
|---|
| 2134 | n/a | } |
|---|
| 2135 | n/a | |
|---|
| 2136 | n/a | /* Shorthand to add a single entry given a name and a function */ |
|---|
| 2137 | n/a | |
|---|
| 2138 | n/a | int |
|---|
| 2139 | n/a | PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void)) |
|---|
| 2140 | n/a | { |
|---|
| 2141 | n/a | struct _inittab newtab[2]; |
|---|
| 2142 | n/a | |
|---|
| 2143 | n/a | memset(newtab, '\0', sizeof newtab); |
|---|
| 2144 | n/a | |
|---|
| 2145 | n/a | newtab[0].name = name; |
|---|
| 2146 | n/a | newtab[0].initfunc = initfunc; |
|---|
| 2147 | n/a | |
|---|
| 2148 | n/a | return PyImport_ExtendInittab(newtab); |
|---|
| 2149 | n/a | } |
|---|
| 2150 | n/a | |
|---|
| 2151 | n/a | #ifdef __cplusplus |
|---|
| 2152 | n/a | } |
|---|
| 2153 | n/a | #endif |
|---|