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