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