Mercurial > cpython-lru_cache-weakref
comparison Python/pythonrun.c @ 76303:2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
importlib._bootstrap is now frozen into Python/importlib.h and stored
as _frozen_importlib in sys.modules. Py_Initialize() loads the frozen
code along with sys and imp and then uses _frozen_importlib._install()
to set builtins.__import__() w/ _frozen_importlib.__import__().
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Sat, 14 Apr 2012 14:10:13 -0400 |
| parents | ff7587546a1d |
| children | c7bf93802f5d |
comparison
equal
deleted
inserted
replaced
| 76302:e730da0cd489 | 76303:2dd046be2c88 |
|---|---|
| 188 PyErr_SetNone(PyExc_NotImplementedError); | 188 PyErr_SetNone(PyExc_NotImplementedError); |
| 189 return NULL; | 189 return NULL; |
| 190 #endif | 190 #endif |
| 191 } | 191 } |
| 192 | 192 |
| 193 static void | |
| 194 import_init(PyInterpreterState *interp, PyObject *sysmod) | |
| 195 { | |
| 196 PyObject *importlib; | |
| 197 PyObject *impmod; | |
| 198 PyObject *sys_modules; | |
| 199 PyObject *value; | |
| 200 | |
| 201 /* Import _importlib through its frozen version, _frozen_importlib. */ | |
| 202 /* XXX(bcannon): The file path for _frozen_importlib is completely off | |
| 203 */ | |
| 204 if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) { | |
| 205 Py_FatalError("Py_Initialize: can't import _frozen_importlib"); | |
| 206 } | |
| 207 else if (Py_VerboseFlag) { | |
| 208 PySys_FormatStderr("import _frozen_importlib # frozen\n"); | |
| 209 } | |
| 210 importlib = PyImport_AddModule("_frozen_importlib"); | |
| 211 if (importlib == NULL) { | |
| 212 Py_FatalError("Py_Initialize: couldn't get _frozen_importlib from " | |
| 213 "sys.modules"); | |
| 214 } | |
| 215 interp->importlib = importlib; | |
| 216 Py_INCREF(interp->importlib); | |
| 217 | |
| 218 /* Install _importlib as __import__ */ | |
| 219 impmod = PyInit_imp(); | |
| 220 if (impmod == NULL) { | |
| 221 Py_FatalError("Py_Initialize: can't import imp"); | |
| 222 } | |
| 223 else if (Py_VerboseFlag) { | |
| 224 PySys_FormatStderr("import imp # builtin\n"); | |
| 225 } | |
| 226 sys_modules = PyImport_GetModuleDict(); | |
| 227 if (Py_VerboseFlag) { | |
| 228 PySys_FormatStderr("import sys # builtin\n"); | |
| 229 } | |
| 230 if (PyDict_SetItemString(sys_modules, "imp", impmod) < 0) { | |
| 231 Py_FatalError("Py_Initialize: can't save imp to sys.modules"); | |
| 232 } | |
| 233 | |
| 234 value = PyObject_CallMethod(importlib, "_setup", "OO", sysmod, impmod); | |
| 235 if (value == NULL) { | |
| 236 PyErr_Print(); | |
| 237 Py_FatalError("Py_Initialize: importlib install failed"); | |
| 238 } | |
| 239 Py_DECREF(value); | |
| 240 | |
| 241 _PyImportZip_Init(); | |
| 242 } | |
| 243 | |
| 244 | |
| 193 void | 245 void |
| 194 Py_InitializeEx(int install_sigs) | 246 Py_InitializeEx(int install_sigs) |
| 195 { | 247 { |
| 196 PyInterpreterState *interp; | 248 PyInterpreterState *interp; |
| 197 PyThreadState *tstate; | 249 PyThreadState *tstate; |
| 279 if (interp->builtins == NULL) | 331 if (interp->builtins == NULL) |
| 280 Py_FatalError("Py_Initialize: can't initialize builtins dict"); | 332 Py_FatalError("Py_Initialize: can't initialize builtins dict"); |
| 281 Py_INCREF(interp->builtins); | 333 Py_INCREF(interp->builtins); |
| 282 | 334 |
| 283 /* initialize builtin exceptions */ | 335 /* initialize builtin exceptions */ |
| 284 _PyExc_Init(); | 336 _PyExc_Init(bimod); |
| 285 | 337 |
| 286 sysmod = _PySys_Init(); | 338 sysmod = _PySys_Init(); |
| 287 if (sysmod == NULL) | 339 if (sysmod == NULL) |
| 288 Py_FatalError("Py_Initialize: can't initialize sys"); | 340 Py_FatalError("Py_Initialize: can't initialize sys"); |
| 289 interp->sysdict = PyModule_GetDict(sysmod); | 341 interp->sysdict = PyModule_GetDict(sysmod); |
| 312 if (_PyFaulthandler_Init()) | 364 if (_PyFaulthandler_Init()) |
| 313 Py_FatalError("Py_Initialize: can't initialize faulthandler"); | 365 Py_FatalError("Py_Initialize: can't initialize faulthandler"); |
| 314 | 366 |
| 315 /* Initialize _warnings. */ | 367 /* Initialize _warnings. */ |
| 316 _PyWarnings_Init(); | 368 _PyWarnings_Init(); |
| 369 | |
| 370 import_init(interp, sysmod); | |
| 317 | 371 |
| 318 _PyTime_Init(); | 372 _PyTime_Init(); |
| 319 | 373 |
| 320 if (initfsencoding(interp) < 0) | 374 if (initfsencoding(interp) < 0) |
| 321 Py_FatalError("Py_Initialize: unable to load the file system codec"); | 375 Py_FatalError("Py_Initialize: unable to load the file system codec"); |
| 636 goto handle_error; | 690 goto handle_error; |
| 637 Py_INCREF(interp->builtins); | 691 Py_INCREF(interp->builtins); |
| 638 } | 692 } |
| 639 | 693 |
| 640 /* initialize builtin exceptions */ | 694 /* initialize builtin exceptions */ |
| 641 _PyExc_Init(); | 695 _PyExc_Init(bimod); |
| 642 | 696 |
| 643 sysmod = _PyImport_FindBuiltin("sys"); | 697 sysmod = _PyImport_FindBuiltin("sys"); |
| 644 if (bimod != NULL && sysmod != NULL) { | 698 if (bimod != NULL && sysmod != NULL) { |
| 645 PyObject *pstderr; | 699 PyObject *pstderr; |
| 700 | |
| 646 interp->sysdict = PyModule_GetDict(sysmod); | 701 interp->sysdict = PyModule_GetDict(sysmod); |
| 647 if (interp->sysdict == NULL) | 702 if (interp->sysdict == NULL) |
| 648 goto handle_error; | 703 goto handle_error; |
| 649 Py_INCREF(interp->sysdict); | 704 Py_INCREF(interp->sysdict); |
| 650 PySys_SetPath(Py_GetPath()); | 705 PySys_SetPath(Py_GetPath()); |
| 658 PySys_SetObject("stderr", pstderr); | 713 PySys_SetObject("stderr", pstderr); |
| 659 PySys_SetObject("__stderr__", pstderr); | 714 PySys_SetObject("__stderr__", pstderr); |
| 660 Py_DECREF(pstderr); | 715 Py_DECREF(pstderr); |
| 661 | 716 |
| 662 _PyImportHooks_Init(); | 717 _PyImportHooks_Init(); |
| 718 | |
| 719 import_init(interp, sysmod); | |
| 663 | 720 |
| 664 if (initfsencoding(interp) < 0) | 721 if (initfsencoding(interp) < 0) |
| 665 goto handle_error; | 722 goto handle_error; |
| 666 | 723 |
| 667 if (initstdio() < 0) | 724 if (initstdio() < 0) |
