annotate cos/python/Python/import.c @ 210:67b0feafe5ae

Added missing file
author Windel Bouwman
date Sat, 29 Jun 2013 10:09:50 +0200
parents 7f74363f4c82
children
rev   line source
27
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2 /* Module definition and import implementation */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4 #include "Python.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
5
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
6 #include "Python-ast.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
7 #undef Yield /* undefine macro conflicting with winbase.h */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
8 #include "errcode.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
9 #include "marshal.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
10 #include "code.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
11 #include "osdefs.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
12 #include "importdl.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
13
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
14 #ifdef HAVE_FCNTL_H
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
15 #include <fcntl.h>
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
16 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
17
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
18
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
19 /* Magic word to reject .pyc files generated by other Python versions.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
20 It should change for each incompatible change to the bytecode.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
21
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
22 The value of CR and LF is incorporated so if you ever read or write
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
23 a .pyc file in text mode the magic number will be wrong; also, the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
24 Apple MPW compiler swaps their values, botching string constants.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
25
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
26 The magic numbers must be spaced apart at least 2 values, as the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
27 -U interpeter flag will cause MAGIC+1 being used. They have been
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
28 odd numbers for some time now.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
29
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
30 There were a variety of old schemes for setting the magic number.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
31 The current working scheme is to increment the previous value by
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
32 10.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
33
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
34 Starting with the adoption of PEP 3147 in Python 3.2, every bump in magic
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
35 number also includes a new "magic tag", i.e. a human readable string used
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
36 to represent the magic number in __pycache__ directories. When you change
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
37 the magic number, you must also set a new unique magic tag. Generally this
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
38 can be named after the Python major version of the magic number bump, but
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
39 it can really be anything, as long as it's different than anything else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
40 that's come before. The tags are included in the following table, starting
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
41 with Python 3.2a0.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
42
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
43 Known values:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
44 Python 1.5: 20121
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
45 Python 1.5.1: 20121
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
46 Python 1.5.2: 20121
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
47 Python 1.6: 50428
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
48 Python 2.0: 50823
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
49 Python 2.0.1: 50823
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
50 Python 2.1: 60202
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
51 Python 2.1.1: 60202
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
52 Python 2.1.2: 60202
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
53 Python 2.2: 60717
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
54 Python 2.3a0: 62011
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
55 Python 2.3a0: 62021
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
56 Python 2.3a0: 62011 (!)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
57 Python 2.4a0: 62041
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
58 Python 2.4a3: 62051
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
59 Python 2.4b1: 62061
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
60 Python 2.5a0: 62071
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
61 Python 2.5a0: 62081 (ast-branch)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
62 Python 2.5a0: 62091 (with)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
63 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
64 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
65 Python 2.5b3: 62111 (fix wrong code: x += yield)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
66 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
67 storing constants that should have been removed)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
68 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
69 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
70 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
71 Python 3000: 3000
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
72 3010 (removed UNARY_CONVERT)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
73 3020 (added BUILD_SET)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
74 3030 (added keyword-only parameters)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
75 3040 (added signature annotations)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
76 3050 (print becomes a function)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
77 3060 (PEP 3115 metaclass syntax)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
78 3061 (string literals become unicode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
79 3071 (PEP 3109 raise changes)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
80 3081 (PEP 3137 make __file__ and __name__ unicode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
81 3091 (kill str8 interning)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
82 3101 (merge from 2.6a0, see 62151)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
83 3103 (__file__ points to source file)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
84 Python 3.0a4: 3111 (WITH_CLEANUP optimization).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
85 Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
86 Python 3.1a0: 3141 (optimize list, set and dict comprehensions:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
87 change LIST_APPEND and SET_ADD, add MAP_ADD)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
88 Python 3.1a0: 3151 (optimize conditional branches:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
89 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
90 Python 3.2a0: 3160 (add SETUP_WITH)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
91 tag: cpython-32
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
92 Python 3.2a1: 3170 (add DUP_TOP_TWO, remove DUP_TOPX and ROT_FOUR)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
93 tag: cpython-32
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
94 Python 3.2a2 3180 (add DELETE_DEREF)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
95 Python 3.3a0 3190 __class__ super closure changed
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
96 Python 3.3a0 3200 (__qualname__ added)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
97 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
98
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
99 /* MAGIC must change whenever the bytecode emitted by the compiler may no
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
100 longer be understood by older implementations of the eval loop (usually
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
101 due to the addition of new opcodes)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
102 TAG must change for each major Python release. The magic number will take
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
103 care of any bytecode changes that occur during development.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
104 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
105 #define QUOTE(arg) #arg
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
106 #define STRIFY(name) QUOTE(name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
107 #define MAJOR STRIFY(PY_MAJOR_VERSION)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
108 #define MINOR STRIFY(PY_MINOR_VERSION)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
109 #define MAGIC (3200 | ((long)'\r'<<16) | ((long)'\n'<<24))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
110 #define TAG "cpython-" MAJOR MINOR;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
111 #define CACHEDIR "__pycache__"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
112 /* Current magic word and string tag as globals. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
113 static long pyc_magic = MAGIC;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
114 static const char *pyc_tag = TAG;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
115 #undef QUOTE
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
116 #undef STRIFY
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
117 #undef MAJOR
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
118 #undef MINOR
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
119
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
120 /* See _PyImport_FixupExtensionObject() below */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
121 static PyObject *extensions = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
122
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
123 /* Function from Parser/tokenizer.c */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
124 extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
125
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
126 /* This table is defined in config.c: */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
127 extern struct _inittab _PyImport_Inittab[];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
128
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
129 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
130
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
131 /* these tables define the module suffixes that Python recognizes */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
132 struct filedescr * _PyImport_Filetab = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
133
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
134 static const struct filedescr _PyImport_StandardFiletab[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
135 {".py", "U", PY_SOURCE},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
136 {".pyc", "rb", PY_COMPILED},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
137 {0, 0}
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
138 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
139
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
140 static PyObject *initstr = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
141 _Py_IDENTIFIER(__path__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
142
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
143 /* Initialize things */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
144
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
145 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
146 _PyImport_Init(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
147 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
148 const struct filedescr *scan;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
149 struct filedescr *filetab;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
150 int countD = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
151 int countS = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
152
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
153 initstr = PyUnicode_InternFromString("__init__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
154 if (initstr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
155 Py_FatalError("Can't initialize import variables");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
156
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
157 /* prepare _PyImport_Filetab: copy entries from
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
158 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
159 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
160 #ifdef HAVE_DYNAMIC_LOADING
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
161 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
162 ++countD;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
163 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
164 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
165 ++countS;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
166 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
167 if (filetab == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
168 Py_FatalError("Can't initialize import file table.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
169 #ifdef HAVE_DYNAMIC_LOADING
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
170 memcpy(filetab, _PyImport_DynLoadFiletab,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
171 countD * sizeof(struct filedescr));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
172 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
173 memcpy(filetab + countD, _PyImport_StandardFiletab,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
174 countS * sizeof(struct filedescr));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
175 filetab[countD + countS].suffix = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
176
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
177 _PyImport_Filetab = filetab;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
178
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
179 if (Py_OptimizeFlag) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
180 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
181 for (; filetab->suffix != NULL; filetab++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
182 if (strcmp(filetab->suffix, ".pyc") == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
183 filetab->suffix = ".pyo";
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
184 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
185 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
186 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
187
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
188 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
189 _PyImportHooks_Init(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
190 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
191 PyObject *v, *path_hooks = NULL, *zimpimport;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
192 int err = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
193
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
194 /* adding sys.path_hooks and sys.path_importer_cache, setting up
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
195 zipimport */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
196 if (PyType_Ready(&PyNullImporter_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
197 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
198
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
199 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
200 PySys_WriteStderr("# installing zipimport hook\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
201
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
202 v = PyList_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
203 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
204 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
205 err = PySys_SetObject("meta_path", v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
206 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
207 if (err)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
208 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
209 v = PyDict_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
210 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
211 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
212 err = PySys_SetObject("path_importer_cache", v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
213 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
214 if (err)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
215 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
216 path_hooks = PyList_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
217 if (path_hooks == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
218 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
219 err = PySys_SetObject("path_hooks", path_hooks);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
220 if (err) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
221 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
222 PyErr_Print();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
223 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
224 "path_importer_cache, or NullImporter failed"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
225 );
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
226 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
227
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
228 zimpimport = PyImport_ImportModule("zipimport");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
229 if (zimpimport == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
230 PyErr_Clear(); /* No zip import module -- okay */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
231 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
232 PySys_WriteStderr("# can't import zipimport\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
233 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
234 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
235 _Py_IDENTIFIER(zipimporter);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
236 PyObject *zipimporter = _PyObject_GetAttrId(zimpimport,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
237 &PyId_zipimporter);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
238 Py_DECREF(zimpimport);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
239 if (zipimporter == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
240 PyErr_Clear(); /* No zipimporter object -- okay */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
241 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
242 PySys_WriteStderr(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
243 "# can't import zipimport.zipimporter\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
244 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
245 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
246 /* sys.path_hooks.append(zipimporter) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
247 err = PyList_Append(path_hooks, zipimporter);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
248 Py_DECREF(zipimporter);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
249 if (err)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
250 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
251 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
252 PySys_WriteStderr(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
253 "# installed zipimport hook\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
254 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
255 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
256 Py_DECREF(path_hooks);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
257 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
258
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
259 /* Locking primitives to prevent parallel imports of the same module
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
260 in different threads to return with a partially loaded module.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
261 These calls are serialized by the global interpreter lock. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
262
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
263 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
264
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
265 #include "pythread.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
266
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
267 static PyThread_type_lock import_lock = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
268 static long import_lock_thread = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
269 static int import_lock_level = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
270
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
271 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
272 _PyImport_AcquireLock(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
273 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
274 long me = PyThread_get_thread_ident();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
275 if (me == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
276 return; /* Too bad */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
277 if (import_lock == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
278 import_lock = PyThread_allocate_lock();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
279 if (import_lock == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
280 return; /* Nothing much we can do. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
281 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
282 if (import_lock_thread == me) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
283 import_lock_level++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
284 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
285 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
286 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
287 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
288 PyThreadState *tstate = PyEval_SaveThread();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
289 PyThread_acquire_lock(import_lock, 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
290 PyEval_RestoreThread(tstate);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
291 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
292 import_lock_thread = me;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
293 import_lock_level = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
294 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
295
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
296 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
297 _PyImport_ReleaseLock(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
298 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
299 long me = PyThread_get_thread_ident();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
300 if (me == -1 || import_lock == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
301 return 0; /* Too bad */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
302 if (import_lock_thread != me)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
303 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
304 import_lock_level--;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
305 if (import_lock_level == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
306 import_lock_thread = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
307 PyThread_release_lock(import_lock);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
308 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
309 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
310 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
311
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
312 /* This function is called from PyOS_AfterFork to ensure that newly
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
313 created child processes do not share locks with the parent.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
314 We now acquire the import lock around fork() calls but on some platforms
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
315 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
316
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
317 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
318 _PyImport_ReInitLock(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
319 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
320 if (import_lock != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
321 import_lock = PyThread_allocate_lock();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
322 if (import_lock_level > 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
323 /* Forked as a side effect of import */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
324 long me = PyThread_get_thread_ident();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
325 PyThread_acquire_lock(import_lock, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
326 /* XXX: can the previous line fail? */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
327 import_lock_thread = me;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
328 import_lock_level--;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
329 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
330 import_lock_thread = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
331 import_lock_level = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
332 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
333 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
334
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
335 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
336
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
337 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
338 imp_lock_held(PyObject *self, PyObject *noargs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
339 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
340 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
341 return PyBool_FromLong(import_lock_thread != -1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
342 #else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
343 return PyBool_FromLong(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
344 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
345 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
346
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
347 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
348 imp_acquire_lock(PyObject *self, PyObject *noargs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
349 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
350 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
351 _PyImport_AcquireLock();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
352 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
353 Py_INCREF(Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
354 return Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
355 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
356
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
357 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
358 imp_release_lock(PyObject *self, PyObject *noargs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
359 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
360 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
361 if (_PyImport_ReleaseLock() < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
362 PyErr_SetString(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
363 "not holding the import lock");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
364 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
365 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
366 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
367 Py_INCREF(Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
368 return Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
369 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
370
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
371 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
372 _PyImport_Fini(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
373 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
374 Py_XDECREF(extensions);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
375 extensions = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
376 PyMem_DEL(_PyImport_Filetab);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
377 _PyImport_Filetab = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
378 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
379 if (import_lock != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
380 PyThread_free_lock(import_lock);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
381 import_lock = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
382 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
383 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
384 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
385
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
386 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
387 imp_modules_reloading_clear(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
388 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
389 PyInterpreterState *interp = PyThreadState_Get()->interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
390 if (interp->modules_reloading != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
391 PyDict_Clear(interp->modules_reloading);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
392 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
393
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
394 /* Helper for sys */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
395
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
396 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
397 PyImport_GetModuleDict(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
398 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
399 PyInterpreterState *interp = PyThreadState_GET()->interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
400 if (interp->modules == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
401 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
402 return interp->modules;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
403 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
404
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
405
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
406 /* List of names to clear in sys */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
407 static char* sys_deletes[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
408 "path", "argv", "ps1", "ps2",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
409 "last_type", "last_value", "last_traceback",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
410 "path_hooks", "path_importer_cache", "meta_path",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
411 /* misc stuff */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
412 "flags", "float_info",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
413 NULL
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
414 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
415
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
416 static char* sys_files[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
417 "stdin", "__stdin__",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
418 "stdout", "__stdout__",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
419 "stderr", "__stderr__",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
420 NULL
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
421 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
422
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
423
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
424 /* Un-initialize things, as good as we can */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
425
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
426 void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
427 PyImport_Cleanup(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
428 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
429 Py_ssize_t pos, ndone;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
430 PyObject *key, *value, *dict;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
431 PyInterpreterState *interp = PyThreadState_GET()->interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
432 PyObject *modules = interp->modules;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
433
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
434 if (modules == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
435 return; /* Already done */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
436
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
437 /* Delete some special variables first. These are common
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
438 places where user values hide and people complain when their
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
439 destructors fail. Since the modules containing them are
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
440 deleted *last* of all, they would come too late in the normal
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
441 destruction order. Sigh. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
442
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
443 value = PyDict_GetItemString(modules, "builtins");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
444 if (value != NULL && PyModule_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
445 dict = PyModule_GetDict(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
446 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
447 PySys_WriteStderr("# clear builtins._\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
448 PyDict_SetItemString(dict, "_", Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
449 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
450 value = PyDict_GetItemString(modules, "sys");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
451 if (value != NULL && PyModule_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
452 char **p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
453 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
454 dict = PyModule_GetDict(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
455 for (p = sys_deletes; *p != NULL; p++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
456 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
457 PySys_WriteStderr("# clear sys.%s\n", *p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
458 PyDict_SetItemString(dict, *p, Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
459 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
460 for (p = sys_files; *p != NULL; p+=2) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
461 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
462 PySys_WriteStderr("# restore sys.%s\n", *p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
463 v = PyDict_GetItemString(dict, *(p+1));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
464 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
465 v = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
466 PyDict_SetItemString(dict, *p, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
467 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
468 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
469
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
470 /* First, delete __main__ */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
471 value = PyDict_GetItemString(modules, "__main__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
472 if (value != NULL && PyModule_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
473 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
474 PySys_WriteStderr("# cleanup __main__\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
475 _PyModule_Clear(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
476 PyDict_SetItemString(modules, "__main__", Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
477 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
478
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
479 /* The special treatment of "builtins" here is because even
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
480 when it's not referenced as a module, its dictionary is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
481 referenced by almost every module's __builtins__. Since
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
482 deleting a module clears its dictionary (even if there are
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
483 references left to it), we need to delete the "builtins"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
484 module last. Likewise, we don't delete sys until the very
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
485 end because it is implicitly referenced (e.g. by print).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
486
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
487 Also note that we 'delete' modules by replacing their entry
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
488 in the modules dict with None, rather than really deleting
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
489 them; this avoids a rehash of the modules dictionary and
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
490 also marks them as "non existent" so they won't be
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
491 re-imported. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
492
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
493 /* Next, repeatedly delete modules with a reference count of
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
494 one (skipping builtins and sys) and delete them */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
495 do {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
496 ndone = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
497 pos = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
498 while (PyDict_Next(modules, &pos, &key, &value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
499 if (value->ob_refcnt != 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
500 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
501 if (PyUnicode_Check(key) && PyModule_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
502 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
503 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
504 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
505 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
506 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
507 PySys_FormatStderr(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
508 "# cleanup[1] %U\n", key);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
509 _PyModule_Clear(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
510 PyDict_SetItem(modules, key, Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
511 ndone++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
512 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
513 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
514 } while (ndone > 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
515
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
516 /* Next, delete all modules (still skipping builtins and sys) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
517 pos = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
518 while (PyDict_Next(modules, &pos, &key, &value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
519 if (PyUnicode_Check(key) && PyModule_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
520 if (PyUnicode_CompareWithASCIIString(key, "builtins") == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
521 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
522 if (PyUnicode_CompareWithASCIIString(key, "sys") == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
523 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
524 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
525 PySys_FormatStderr("# cleanup[2] %U\n", key);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
526 _PyModule_Clear(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
527 PyDict_SetItem(modules, key, Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
528 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
529 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
530
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
531 /* Next, delete sys and builtins (in that order) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
532 value = PyDict_GetItemString(modules, "sys");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
533 if (value != NULL && PyModule_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
534 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
535 PySys_WriteStderr("# cleanup sys\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
536 _PyModule_Clear(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
537 PyDict_SetItemString(modules, "sys", Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
538 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
539 value = PyDict_GetItemString(modules, "builtins");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
540 if (value != NULL && PyModule_Check(value)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
541 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
542 PySys_WriteStderr("# cleanup builtins\n");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
543 _PyModule_Clear(value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
544 PyDict_SetItemString(modules, "builtins", Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
545 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
546
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
547 /* Finally, clear and delete the modules directory */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
548 PyDict_Clear(modules);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
549 interp->modules = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
550 Py_DECREF(modules);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
551 Py_CLEAR(interp->modules_reloading);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
552 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
553
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
554
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
555 /* Helper for pythonrun.c -- return magic number and tag. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
556
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
557 long
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
558 PyImport_GetMagicNumber(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
559 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
560 return pyc_magic;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
561 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
562
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
563
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
564 const char *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
565 PyImport_GetMagicTag(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
566 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
567 return pyc_tag;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
568 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
569
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
570 /* Magic for extension modules (built-in as well as dynamically
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
571 loaded). To prevent initializing an extension module more than
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
572 once, we keep a static dictionary 'extensions' keyed by module name
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
573 (for built-in modules) or by filename (for dynamically loaded
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
574 modules), containing these modules. A copy of the module's
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
575 dictionary is stored by calling _PyImport_FixupExtensionObject()
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
576 immediately after the module initialization function succeeds. A
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
577 copy can be retrieved from there by calling
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
578 _PyImport_FindExtensionObject().
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
579
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
580 Modules which do support multiple initialization set their m_size
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
581 field to a non-negative number (indicating the size of the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
582 module-specific state). They are still recorded in the extensions
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
583 dictionary, to avoid loading shared libraries twice.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
584 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
585
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
586 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
587 _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
588 PyObject *filename)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
589 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
590 PyObject *modules, *dict;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
591 struct PyModuleDef *def;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
592 if (extensions == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
593 extensions = PyDict_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
594 if (extensions == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
595 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
596 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
597 if (mod == NULL || !PyModule_Check(mod)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
598 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
599 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
600 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
601 def = PyModule_GetDef(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
602 if (!def) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
603 PyErr_BadInternalCall();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
604 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
605 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
606 modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
607 if (PyDict_SetItem(modules, name, mod) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
608 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
609 if (_PyState_AddModule(mod, def) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
610 PyDict_DelItem(modules, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
611 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
612 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
613 if (def->m_size == -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
614 if (def->m_base.m_copy) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
615 /* Somebody already imported the module,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
616 likely under a different name.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
617 XXX this should really not happen. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
618 Py_DECREF(def->m_base.m_copy);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
619 def->m_base.m_copy = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
620 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
621 dict = PyModule_GetDict(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
622 if (dict == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
623 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
624 def->m_base.m_copy = PyDict_Copy(dict);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
625 if (def->m_base.m_copy == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
626 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
627 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
628 PyDict_SetItem(extensions, filename, (PyObject*)def);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
629 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
630 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
631
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
632 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
633 _PyImport_FixupBuiltin(PyObject *mod, char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
634 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
635 int res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
636 PyObject *nameobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
637 nameobj = PyUnicode_InternFromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
638 if (nameobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
639 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
640 res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
641 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
642 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
643 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
644
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
645 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
646 _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
647 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
648 PyObject *mod, *mdict;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
649 PyModuleDef* def;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
650 if (extensions == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
651 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
652 def = (PyModuleDef*)PyDict_GetItem(extensions, filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
653 if (def == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
654 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
655 if (def->m_size == -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
656 /* Module does not support repeated initialization */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
657 if (def->m_base.m_copy == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
658 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
659 mod = PyImport_AddModuleObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
660 if (mod == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
661 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
662 mdict = PyModule_GetDict(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
663 if (mdict == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
664 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
665 if (PyDict_Update(mdict, def->m_base.m_copy))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
666 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
667 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
668 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
669 if (def->m_base.m_init == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
670 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
671 mod = def->m_base.m_init();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
672 if (mod == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
673 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
674 PyDict_SetItem(PyImport_GetModuleDict(), name, mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
675 Py_DECREF(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
676 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
677 if (_PyState_AddModule(mod, def) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
678 PyDict_DelItem(PyImport_GetModuleDict(), name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
679 Py_DECREF(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
680 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
681 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
682 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
683 PySys_FormatStderr("import %U # previously loaded (%R)\n",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
684 name, filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
685 return mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
686
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
687 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
688
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
689 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
690 _PyImport_FindBuiltin(const char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
691 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
692 PyObject *res, *nameobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
693 nameobj = PyUnicode_InternFromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
694 if (nameobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
695 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
696 res = _PyImport_FindExtensionObject(nameobj, nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
697 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
698 return res;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
699 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
700
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
701 /* Get the module object corresponding to a module name.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
702 First check the modules dictionary if there's one there,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
703 if not, create a new one and insert it in the modules dictionary.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
704 Because the former action is most common, THIS DOES NOT RETURN A
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
705 'NEW' REFERENCE! */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
706
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
707 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
708 PyImport_AddModuleObject(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
709 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
710 PyObject *modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
711 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
712
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
713 if ((m = PyDict_GetItem(modules, name)) != NULL &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
714 PyModule_Check(m))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
715 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
716 m = PyModule_NewObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
717 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
718 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
719 if (PyDict_SetItem(modules, name, m) != 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
720 Py_DECREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
721 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
722 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
723 Py_DECREF(m); /* Yes, it still exists, in modules! */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
724
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
725 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
726 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
727
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
728 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
729 PyImport_AddModule(const char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
730 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
731 PyObject *nameobj, *module;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
732 nameobj = PyUnicode_FromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
733 if (nameobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
734 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
735 module = PyImport_AddModuleObject(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
736 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
737 return module;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
738 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
739
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
740
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
741 /* Remove name from sys.modules, if it's there. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
742 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
743 remove_module(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
744 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
745 PyObject *modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
746 if (PyDict_GetItem(modules, name) == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
747 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
748 if (PyDict_DelItem(modules, name) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
749 Py_FatalError("import: deleting existing key in"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
750 "sys.modules failed");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
751 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
752
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
753 static PyObject * get_sourcefile(PyObject *filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
754 static PyObject *make_source_pathname(PyObject *pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
755 static PyObject* make_compiled_pathname(PyObject *pathname, int debug);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
756
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
757 /* Execute a code object in a module and return the module object
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
758 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
759 * removed from sys.modules, to avoid leaving damaged module objects
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
760 * in sys.modules. The caller may wish to restore the original
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
761 * module object (if any) in this case; PyImport_ReloadModule is an
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
762 * example.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
763 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
764 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
765 * interface. The other two exist primarily for backward compatibility.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
766 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
767 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
768 PyImport_ExecCodeModule(char *name, PyObject *co)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
769 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
770 return PyImport_ExecCodeModuleWithPathnames(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
771 name, co, (char *)NULL, (char *)NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
772 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
773
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
774 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
775 PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
776 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
777 return PyImport_ExecCodeModuleWithPathnames(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
778 name, co, pathname, (char *)NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
779 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
780
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
781 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
782 PyImport_ExecCodeModuleWithPathnames(char *name, PyObject *co, char *pathname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
783 char *cpathname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
784 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
785 PyObject *m = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
786 PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
787
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
788 nameobj = PyUnicode_FromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
789 if (nameobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
790 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
791
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
792 if (pathname != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
793 pathobj = PyUnicode_DecodeFSDefault(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
794 if (pathobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
795 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
796 } else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
797 pathobj = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
798 if (cpathname != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
799 cpathobj = PyUnicode_DecodeFSDefault(cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
800 if (cpathobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
801 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
802 } else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
803 cpathobj = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
804 m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
805 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
806 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
807 Py_XDECREF(pathobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
808 Py_XDECREF(cpathobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
809 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
810 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
811
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
812 PyObject*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
813 PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
814 PyObject *cpathname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
815 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
816 PyObject *modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
817 PyObject *m, *d, *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
818
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
819 m = PyImport_AddModuleObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
820 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
821 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
822 /* If the module is being reloaded, we get the old module back
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
823 and re-use its dict to exec the new code. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
824 d = PyModule_GetDict(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
825 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
826 if (PyDict_SetItemString(d, "__builtins__",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
827 PyEval_GetBuiltins()) != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
828 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
829 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
830 /* Remember the filename as the __file__ attribute */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
831 if (pathname != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
832 v = get_sourcefile(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
833 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
834 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
835 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
836 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
837 v = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
838 if (v == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
839 v = ((PyCodeObject *)co)->co_filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
840 Py_INCREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
841 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
842 if (PyDict_SetItemString(d, "__file__", v) != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
843 PyErr_Clear(); /* Not important enough to report */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
844 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
845
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
846 /* Remember the pyc path name as the __cached__ attribute. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
847 if (cpathname != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
848 v = cpathname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
849 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
850 v = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
851 if (PyDict_SetItemString(d, "__cached__", v) != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
852 PyErr_Clear(); /* Not important enough to report */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
853
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
854 v = PyEval_EvalCode(co, d, d);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
855 if (v == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
856 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
857 Py_DECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
858
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
859 if ((m = PyDict_GetItem(modules, name)) == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
860 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
861 "Loaded module %R not found in sys.modules",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
862 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
863 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
864 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
865
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
866 Py_INCREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
867
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
868 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
869
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
870 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
871 remove_module(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
872 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
873 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
874
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
875
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
876 /* Like strrchr(string, '/') but searches for the rightmost of either SEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
877 or ALTSEP, if the latter is defined.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
878 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
879 static Py_UCS4*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
880 rightmost_sep(Py_UCS4 *s)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
881 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
882 Py_UCS4 *found, c;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
883 for (found = NULL; (c = *s); s++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
884 if (c == SEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
885 #ifdef ALTSEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
886 || c == ALTSEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
887 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
888 )
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
889 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
890 found = s;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
891 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
892 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
893 return found;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
894 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
895
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
896 /* Like rightmost_sep, but operate on unicode objects. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
897 static Py_ssize_t
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
898 rightmost_sep_obj(PyObject* o, Py_ssize_t start, Py_ssize_t end)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
899 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
900 Py_ssize_t found, i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
901 Py_UCS4 c;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
902 for (found = -1, i = start; i < end; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
903 c = PyUnicode_READ_CHAR(o, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
904 if (c == SEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
905 #ifdef ALTSEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
906 || c == ALTSEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
907 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
908 )
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
909 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
910 found = i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
911 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
912 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
913 return found;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
914 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
915
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
916 /* Given a pathname for a Python source file, fill a buffer with the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
917 pathname for the corresponding compiled file. Return the pathname
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
918 for the compiled file, or NULL if there's no space in the buffer.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
919 Doesn't set an exception.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
920
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
921 foo.py -> __pycache__/foo.<tag>.pyc
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
922
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
923 pathstr is assumed to be "ready".
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
924 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
925
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
926 static PyObject*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
927 make_compiled_pathname(PyObject *pathstr, int debug)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
928 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
929 PyObject *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
930 Py_ssize_t fname, ext, len, i, pos, taglen;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
931 Py_ssize_t pycache_len = sizeof(CACHEDIR) - 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
932 int kind;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
933 void *data;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
934 Py_UCS4 lastsep;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
935
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
936 /* Compute the output string size. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
937 len = PyUnicode_GET_LENGTH(pathstr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
938 /* If there is no separator, this returns -1, so
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
939 fname will be 0. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
940 fname = rightmost_sep_obj(pathstr, 0, len) + 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
941 /* Windows: re-use the last separator character (/ or \\) when
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
942 appending the __pycache__ path. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
943 if (fname > 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
944 lastsep = PyUnicode_READ_CHAR(pathstr, fname -1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
945 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
946 lastsep = SEP;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
947 ext = fname - 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
948 for(i = fname; i < len; i++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
949 if (PyUnicode_READ_CHAR(pathstr, i) == '.')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
950 ext = i + 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
951 if (ext < fname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
952 /* No dot in filename; use entire filename */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
953 ext = len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
954
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
955 /* result = pathstr[:fname] + "__pycache__" + SEP +
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
956 pathstr[fname:ext] + tag + ".py[co]" */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
957 taglen = strlen(pyc_tag);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
958 result = PyUnicode_New(ext + pycache_len + 1 + taglen + 4,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
959 PyUnicode_MAX_CHAR_VALUE(pathstr));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
960 if (!result)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
961 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
962 kind = PyUnicode_KIND(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
963 data = PyUnicode_DATA(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
964 PyUnicode_CopyCharacters(result, 0, pathstr, 0, fname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
965 pos = fname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
966 for (i = 0; i < pycache_len; i++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
967 PyUnicode_WRITE(kind, data, pos++, CACHEDIR[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
968 PyUnicode_WRITE(kind, data, pos++, lastsep);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
969 PyUnicode_CopyCharacters(result, pos, pathstr,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
970 fname, ext - fname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
971 pos += ext - fname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
972 for (i = 0; pyc_tag[i]; i++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
973 PyUnicode_WRITE(kind, data, pos++, pyc_tag[i]);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
974 PyUnicode_WRITE(kind, data, pos++, '.');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
975 PyUnicode_WRITE(kind, data, pos++, 'p');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
976 PyUnicode_WRITE(kind, data, pos++, 'y');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
977 PyUnicode_WRITE(kind, data, pos++, debug ? 'c' : 'o');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
978 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
979 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
980
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
981
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
982 /* Given a pathname to a Python byte compiled file, return the path to the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
983 source file, if the path matches the PEP 3147 format. This does not check
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
984 for any file existence, however, if the pyc file name does not match PEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
985 3147 style, NULL is returned. buf must be at least as big as pathname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
986 the resulting path will always be shorter.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
987
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
988 (...)/__pycache__/foo.<tag>.pyc -> (...)/foo.py */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
989
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
990 static PyObject*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
991 make_source_pathname(PyObject *path)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
992 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
993 Py_ssize_t left, right, dot0, dot1, len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
994 Py_ssize_t i, j;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
995 PyObject *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
996 int kind;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
997 void *data;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
998
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
999 len = PyUnicode_GET_LENGTH(path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1000 if (len > MAXPATHLEN)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1001 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1002
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1003 /* Look back two slashes from the end. In between these two slashes
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1004 must be the string __pycache__ or this is not a PEP 3147 style
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1005 path. It's possible for there to be only one slash.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1006 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1007 right = rightmost_sep_obj(path, 0, len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1008 if (right == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1009 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1010 left = rightmost_sep_obj(path, 0, right);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1011 if (left == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1012 left = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1013 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1014 left++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1015 if (right-left != sizeof(CACHEDIR)-1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1016 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1017 for (i = 0; i < sizeof(CACHEDIR)-1; i++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1018 if (PyUnicode_READ_CHAR(path, left+i) != CACHEDIR[i])
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1019 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1020
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1021 /* Now verify that the path component to the right of the last slash
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1022 has two dots in it.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1023 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1024 dot0 = PyUnicode_FindChar(path, '.', right+1, len, 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1025 if (dot0 < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1026 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1027 dot1 = PyUnicode_FindChar(path, '.', dot0+1, len, 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1028 if (dot1 < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1029 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1030 /* Too many dots? */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1031 if (PyUnicode_FindChar(path, '.', dot1+1, len, 1) != -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1032 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1033
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1034 /* This is a PEP 3147 path. Start by copying everything from the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1035 start of pathname up to and including the leftmost slash. Then
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1036 copy the file's basename, removing the magic tag and adding a .py
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1037 suffix.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1038 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1039 result = PyUnicode_New(left + (dot0-right) + 2,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1040 PyUnicode_MAX_CHAR_VALUE(path));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1041 if (!result)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1042 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1043 kind = PyUnicode_KIND(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1044 data = PyUnicode_DATA(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1045 PyUnicode_CopyCharacters(result, 0, path, 0, (i = left));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1046 PyUnicode_CopyCharacters(result, left, path, right+1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1047 (j = dot0-right));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1048 PyUnicode_WRITE(kind, data, i+j, 'p');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1049 PyUnicode_WRITE(kind, data, i+j+1, 'y');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1050 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1051 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1052
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1053 /* Given a pathname for a Python source file, its time of last
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1054 modification, and a pathname for a compiled file, check whether the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1055 compiled file represents the same version of the source. If so,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1056 return a FILE pointer for the compiled file, positioned just after
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1057 the header; if not, return NULL.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1058 Doesn't set an exception. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1059
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1060 static FILE *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1061 check_compiled_module(PyObject *pathname, time_t mtime, PyObject *cpathname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1062 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1063 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1064 long magic;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1065 long pyc_mtime;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1066
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1067 fp = _Py_fopen(cpathname, "rb");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1068 if (fp == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1069 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1070 magic = PyMarshal_ReadLongFromFile(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1071 if (magic != pyc_magic) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1072 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1073 PySys_FormatStderr("# %R has bad magic\n", cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1074 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1075 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1076 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1077 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1078 if (pyc_mtime != mtime) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1079 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1080 PySys_FormatStderr("# %R has bad mtime\n", cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1081 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1082 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1083 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1084 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1085 PySys_FormatStderr("# %R matches %R\n", cpathname, pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1086 return fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1087 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1088
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1089
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1090 /* Read a code object from a file and check it for validity */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1091
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1092 static PyCodeObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1093 read_compiled_module(PyObject *cpathname, FILE *fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1094 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1095 PyObject *co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1096
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1097 co = PyMarshal_ReadLastObjectFromFile(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1098 if (co == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1099 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1100 if (!PyCode_Check(co)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1101 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1102 "Non-code object in %R", cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1103 Py_DECREF(co);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1104 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1105 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1106 return (PyCodeObject *)co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1107 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1108
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1109
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1110 /* Load a module from a compiled file, execute it, and return its
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1111 module object WITH INCREMENTED REFERENCE COUNT */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1112
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1113 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1114 load_compiled_module(PyObject *name, PyObject *cpathname, FILE *fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1115 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1116 long magic;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1117 PyCodeObject *co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1118 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1119
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1120 magic = PyMarshal_ReadLongFromFile(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1121 if (magic != pyc_magic) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1122 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1123 "Bad magic number in %R", cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1124 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1125 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1126 (void) PyMarshal_ReadLongFromFile(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1127 co = read_compiled_module(cpathname, fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1128 if (co == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1129 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1130 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1131 PySys_FormatStderr("import %U # precompiled from %R\n",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1132 name, cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1133 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1134 cpathname, cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1135 Py_DECREF(co);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1136
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1137 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1138 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1139
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1140 /* Parse a source file and return the corresponding code object */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1141
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1142 static PyCodeObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1143 parse_source_module(PyObject *pathname, FILE *fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1144 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1145 PyCodeObject *co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1146 PyObject *pathbytes;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1147 mod_ty mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1148 PyCompilerFlags flags;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1149 PyArena *arena;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1150
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1151 pathbytes = PyUnicode_EncodeFSDefault(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1152 if (pathbytes == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1153 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1154
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1155 arena = PyArena_New();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1156 if (arena == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1157 Py_DECREF(pathbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1158 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1159 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1160
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1161 flags.cf_flags = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1162 mod = PyParser_ASTFromFile(fp, PyBytes_AS_STRING(pathbytes), NULL,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1163 Py_file_input, 0, 0, &flags,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1164 NULL, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1165 if (mod != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1166 co = PyAST_Compile(mod, PyBytes_AS_STRING(pathbytes), NULL, arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1167 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1168 co = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1169 Py_DECREF(pathbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1170 PyArena_Free(arena);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1171 return co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1172 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1173
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1174 /* Write a compiled module to a file, placing the time of last
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1175 modification of its source into the header.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1176 Errors are ignored, if a write error occurs an attempt is made to
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1177 remove the file. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1178
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1179 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1180 write_compiled_module(PyCodeObject *co, PyObject *cpathname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1181 struct stat *srcstat)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1182 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1183 Py_UCS4 *cpathname_ucs4;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1184 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1185 time_t mtime = srcstat->st_mtime;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1186 PyObject *cpathname_tmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1187 mode_t dirmode = (srcstat->st_mode |
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1188 S_IXUSR | S_IXGRP | S_IXOTH |
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1189 S_IWUSR | S_IWGRP | S_IWOTH);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1190 PyObject *dirbytes;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1191 PyObject *cpathbytes, *cpathbytes_tmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1192 int fd;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1193 PyObject *dirname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1194 Py_UCS4 *dirsep;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1195 int res, ok;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1196
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1197 /* Ensure that the __pycache__ directory exists. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1198 cpathname_ucs4 = PyUnicode_AsUCS4Copy(cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1199 if (!cpathname_ucs4)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1200 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1201 dirsep = rightmost_sep(cpathname_ucs4);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1202 if (dirsep == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1203 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1204 PySys_FormatStderr("# no %s path found %R\n", CACHEDIR, cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1205 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1206 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1207 dirname = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1208 cpathname_ucs4,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1209 dirsep - cpathname_ucs4);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1210 PyMem_Free(cpathname_ucs4);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1211 if (dirname == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1212 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1213 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1214 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1215
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1216 dirbytes = PyUnicode_EncodeFSDefault(dirname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1217 if (dirbytes == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1218 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1219 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1220 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1221 res = mkdir(PyBytes_AS_STRING(dirbytes), dirmode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1222 Py_DECREF(dirbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1223 if (0 <= res)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1224 ok = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1225 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1226 ok = (errno == EEXIST);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1227 if (!ok) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1228 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1229 PySys_FormatStderr("# cannot create cache dir %R\n", dirname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1230 Py_DECREF(dirname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1231 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1232 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1233 Py_DECREF(dirname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1234
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1235 /* We first write to a tmp file and then take advantage
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1236 of atomic renaming (which *should* be true even under Windows).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1237 As in importlib, we use id(something) to generate a pseudo-random
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1238 filename. mkstemp() can't be used since it doesn't allow specifying
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1239 the file access permissions.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1240 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1241 cpathname_tmp = PyUnicode_FromFormat("%U.%zd",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1242 cpathname, (Py_ssize_t) co);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1243 if (cpathname_tmp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1244 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1245 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1246 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1247 cpathbytes_tmp = PyUnicode_EncodeFSDefault(cpathname_tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1248 Py_DECREF(cpathname_tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1249 if (cpathbytes_tmp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1250 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1251 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1252 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1253 cpathbytes = PyUnicode_EncodeFSDefault(cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1254 if (cpathbytes == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1255 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1256 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1257 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1258 fd = open(PyBytes_AS_STRING(cpathbytes_tmp),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1259 O_CREAT | O_EXCL | O_WRONLY, 0666);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1260 if (0 <= fd)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1261 fp = fdopen(fd, "wb");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1262 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1263 fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1264 if (fp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1265 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1266 PySys_FormatStderr(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1267 "# can't create %R\n", cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1268 Py_DECREF(cpathbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1269 Py_DECREF(cpathbytes_tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1270 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1271 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1272 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1273 /* First write a 0 for mtime */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1274 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1275 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1276 fflush(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1277 /* Now write the true mtime */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1278 fseek(fp, 4L, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1279 assert(mtime < LONG_MAX);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1280 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1281 if (fflush(fp) != 0 || ferror(fp)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1282 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1283 PySys_FormatStderr("# can't write %R\n", cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1284 /* Don't keep partial file */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1285 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1286 (void) unlink(PyBytes_AS_STRING(cpathbytes_tmp));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1287 Py_DECREF(cpathbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1288 Py_DECREF(cpathbytes_tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1289 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1290 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1291 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1292 /* Do a (hopefully) atomic rename */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1293 if (rename(PyBytes_AS_STRING(cpathbytes_tmp),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1294 PyBytes_AS_STRING(cpathbytes))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1295 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1296 PySys_FormatStderr("# can't write %R\n", cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1297 /* Don't keep tmp file */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1298 unlink(PyBytes_AS_STRING(cpathbytes_tmp));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1299 Py_DECREF(cpathbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1300 Py_DECREF(cpathbytes_tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1301 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1302 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1303 Py_DECREF(cpathbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1304 Py_DECREF(cpathbytes_tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1305 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1306 PySys_FormatStderr("# wrote %R\n", cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1307 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1308
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1309 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1310 update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1311 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1312 PyObject *constants, *tmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1313 Py_ssize_t i, n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1314
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1315 if (PyUnicode_Compare(co->co_filename, oldname))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1316 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1317
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1318 tmp = co->co_filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1319 co->co_filename = newname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1320 Py_INCREF(co->co_filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1321 Py_DECREF(tmp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1322
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1323 constants = co->co_consts;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1324 n = PyTuple_GET_SIZE(constants);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1325 for (i = 0; i < n; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1326 tmp = PyTuple_GET_ITEM(constants, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1327 if (PyCode_Check(tmp))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1328 update_code_filenames((PyCodeObject *)tmp,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1329 oldname, newname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1330 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1331 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1332
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1333 static void
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1334 update_compiled_module(PyCodeObject *co, PyObject *newname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1335 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1336 PyObject *oldname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1337
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1338 if (PyUnicode_Compare(co->co_filename, newname) == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1339 return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1340
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1341 oldname = co->co_filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1342 Py_INCREF(oldname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1343 update_code_filenames(co, oldname, newname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1344 Py_DECREF(oldname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1345 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1346
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1347 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1348 imp_fix_co_filename(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1349 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1350 PyObject *co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1351 PyObject *file_path;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1352
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1353 if (!PyArg_ParseTuple(args, "OO:_fix_co_filename", &co, &file_path))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1354 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1355
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1356 if (!PyCode_Check(co)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1357 PyErr_SetString(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1358 "first argument must be a code object");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1359 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1360 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1361
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1362 if (!PyUnicode_Check(file_path)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1363 PyErr_SetString(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1364 "second argument must be a string");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1365 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1366 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1367
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1368 update_compiled_module((PyCodeObject*)co, file_path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1369
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1370 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1371 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1372
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1373 /* Load a source module from a given file and return its module
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1374 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1375 byte-compiled file, use that instead. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1376
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1377 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1378 load_source_module(PyObject *name, PyObject *pathname, FILE *fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1379 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1380 struct stat st;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1381 FILE *fpc;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1382 PyObject *cpathname = NULL, *cpathbytes = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1383 PyCodeObject *co;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1384 PyObject *m = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1385
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1386 if (fstat(fileno(fp), &st) != 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1387 PyErr_Format(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1388 "unable to get file status from %R",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1389 pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1390 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1391 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1392 #if SIZEOF_TIME_T > 4
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1393 /* Python's .pyc timestamp handling presumes that the timestamp fits
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1394 in 4 bytes. This will be fine until sometime in the year 2038,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1395 when a 4-byte signed time_t will overflow.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1396 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1397 if (st.st_mtime >> 32) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1398 PyErr_SetString(PyExc_OverflowError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1399 "modification time overflows a 4 byte field");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1400 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1401 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1402 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1403 if (PyUnicode_READY(pathname) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1404 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1405 cpathname = make_compiled_pathname(pathname, !Py_OptimizeFlag);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1406
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1407 if (cpathname != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1408 fpc = check_compiled_module(pathname, st.st_mtime, cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1409 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1410 fpc = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1411
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1412 if (fpc) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1413 co = read_compiled_module(cpathname, fpc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1414 fclose(fpc);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1415 if (co == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1416 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1417 update_compiled_module(co, pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1418 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1419 PySys_FormatStderr("import %U # precompiled from %R\n",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1420 name, cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1421 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1422 cpathname, cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1423 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1424 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1425 co = parse_source_module(pathname, fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1426 if (co == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1427 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1428 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1429 PySys_FormatStderr("import %U # from %R\n",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1430 name, pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1431 if (cpathname != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1432 PyObject *ro = PySys_GetObject("dont_write_bytecode");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1433 if (ro == NULL || !PyObject_IsTrue(ro))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1434 write_compiled_module(co, cpathname, &st);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1435 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1436 m = PyImport_ExecCodeModuleObject(name, (PyObject *)co,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1437 pathname, cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1438 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1439 Py_DECREF(co);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1440
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1441 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1442 Py_XDECREF(cpathbytes);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1443 Py_XDECREF(cpathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1444 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1445 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1446
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1447 /* Get source file -> unicode or None
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1448 * Returns the path to the py file if available, else the given path
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1449 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1450 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1451 get_sourcefile(PyObject *filename)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1452 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1453 Py_ssize_t len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1454 Py_UCS4 *fileuni;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1455 PyObject *py;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1456 struct stat statbuf;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1457
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1458 len = PyUnicode_GET_LENGTH(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1459 if (len == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1460 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1461
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1462 /* don't match *.pyc or *.pyo? */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1463 fileuni = PyUnicode_AsUCS4Copy(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1464 if (!fileuni)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1465 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1466 if (len < 5
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1467 || fileuni[len-4] != '.'
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1468 || (fileuni[len-3] != 'p' && fileuni[len-3] != 'P')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1469 || (fileuni[len-2] != 'y' && fileuni[len-2] != 'Y'))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1470 goto unchanged;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1471
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1472 /* Start by trying to turn PEP 3147 path into source path. If that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1473 * fails, just chop off the trailing character, i.e. legacy pyc path
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1474 * to py.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1475 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1476 py = make_source_pathname(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1477 if (py == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1478 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1479 py = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, fileuni, len - 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1480 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1481 if (py == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1482 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1483
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1484 if (_Py_stat(py, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1485 PyMem_Free(fileuni);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1486 return py;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1487 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1488 Py_DECREF(py);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1489 goto unchanged;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1490
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1491 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1492 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1493 unchanged:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1494 PyMem_Free(fileuni);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1495 Py_INCREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1496 return filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1497 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1498
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1499 /* Forward */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1500 static PyObject *load_module(PyObject *, FILE *, PyObject *, int, PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1501 static struct filedescr *find_module(PyObject *, PyObject *, PyObject *,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1502 PyObject **, FILE **, PyObject **);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1503 static struct _frozen * find_frozen(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1504
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1505 /* Load a package and return its module object WITH INCREMENTED
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1506 REFERENCE COUNT */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1507
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1508 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1509 load_package(PyObject *name, PyObject *pathname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1510 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1511 PyObject *m, *d, *bufobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1512 PyObject *file = NULL, *path_list = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1513 int err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1514 FILE *fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1515 struct filedescr *fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1516
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1517 m = PyImport_AddModuleObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1518 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1519 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1520 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1521 PySys_FormatStderr("import %U # directory %R\n",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1522 name, pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1523 file = get_sourcefile(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1524 if (file == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1525 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1526 path_list = Py_BuildValue("[O]", file);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1527 if (path_list == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1528 Py_DECREF(file);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1529 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1530 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1531 d = PyModule_GetDict(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1532 err = PyDict_SetItemString(d, "__file__", file);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1533 Py_DECREF(file);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1534 if (err == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1535 err = PyDict_SetItemString(d, "__path__", path_list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1536 if (err != 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1537 Py_DECREF(path_list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1538 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1539 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1540 fdp = find_module(name, initstr, path_list,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1541 &bufobj, &fp, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1542 Py_DECREF(path_list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1543 if (fdp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1544 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1545 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1546 Py_INCREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1547 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1548 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1549 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1550 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1551 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1552 m = load_module(name, fp, bufobj, fdp->type, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1553 Py_XDECREF(bufobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1554 if (fp != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1555 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1556 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1557 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1558
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1559
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1560 /* Helper to test for built-in module */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1561
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1562 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1563 is_builtin(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1564 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1565 int i, cmp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1566 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1567 cmp = PyUnicode_CompareWithASCIIString(name, PyImport_Inittab[i].name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1568 if (cmp == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1569 if (PyImport_Inittab[i].initfunc == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1570 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1571 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1572 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1573 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1574 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1575 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1576 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1577
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1578
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1579 /* Return an importer object for a sys.path/pkg.__path__ item 'p',
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1580 possibly by fetching it from the path_importer_cache dict. If it
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1581 wasn't yet cached, traverse path_hooks until a hook is found
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1582 that can handle the path item. Return None if no hook could;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1583 this tells our caller it should fall back to the builtin
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1584 import mechanism. Cache the result in path_importer_cache.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1585 Returns a borrowed reference. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1586
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1587 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1588 get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1589 PyObject *p)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1590 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1591 PyObject *importer;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1592 Py_ssize_t j, nhooks;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1593
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1594 /* These conditions are the caller's responsibility: */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1595 assert(PyList_Check(path_hooks));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1596 assert(PyDict_Check(path_importer_cache));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1597
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1598 nhooks = PyList_Size(path_hooks);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1599 if (nhooks < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1600 return NULL; /* Shouldn't happen */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1601
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1602 importer = PyDict_GetItem(path_importer_cache, p);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1603 if (importer != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1604 return importer;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1605
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1606 /* set path_importer_cache[p] to None to avoid recursion */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1607 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1608 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1609
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1610 for (j = 0; j < nhooks; j++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1611 PyObject *hook = PyList_GetItem(path_hooks, j);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1612 if (hook == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1613 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1614 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1615 if (importer != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1616 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1617
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1618 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1619 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1620 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1621 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1622 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1623 if (importer == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1624 importer = PyObject_CallFunctionObjArgs(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1625 (PyObject *)&PyNullImporter_Type, p, NULL
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1626 );
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1627 if (importer == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1628 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1629 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1630 return Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1631 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1632 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1633 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1634 if (importer != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1635 int err = PyDict_SetItem(path_importer_cache, p, importer);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1636 Py_DECREF(importer);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1637 if (err != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1638 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1639 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1640 return importer;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1641 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1642
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1643 PyAPI_FUNC(PyObject *)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1644 PyImport_GetImporter(PyObject *path) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1645 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1646
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1647 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1648 if ((path_hooks = PySys_GetObject("path_hooks"))) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1649 importer = get_path_importer(path_importer_cache,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1650 path_hooks, path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1651 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1652 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1653 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1654 return importer;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1655 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1656
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1657 /* Search the path (default sys.path) for a module. Return the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1658 corresponding filedescr struct, and (via return arguments) the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1659 pathname and an open file. Return NULL if the module is not found. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1660
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1661 #ifdef MS_COREDLL
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1662 extern FILE *_PyWin_FindRegisteredModule(PyObject *, struct filedescr **,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1663 PyObject **p_path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1664 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1665
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1666 /* Forward */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1667 static int case_ok(PyObject *, Py_ssize_t, PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1668 static int find_init_module(PyObject *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1669 static struct filedescr importhookdescr = {"", "", IMP_HOOK};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1670
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1671 /* Get the path of a module: get its importer and call importer.find_module()
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1672 hook, or check if the module if a package (if path/__init__.py exists).
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1673
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1674 -1: error: a Python error occurred
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1675 0: ignore: an error occurred because of invalid data, but the error is not
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1676 important enough to be reported.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1677 1: get path: module not found, but *buf contains its path
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1678 2: found: *p_fd is the file descriptor (IMP_HOOK or PKG_DIRECTORY)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1679 and *buf is the path */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1680
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1681 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1682 find_module_path(PyObject *fullname, PyObject *name, PyObject *path,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1683 PyObject *path_hooks, PyObject *path_importer_cache,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1684 PyObject **p_path, PyObject **p_loader, struct filedescr **p_fd)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1685 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1686 PyObject *path_unicode, *filename = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1687 Py_ssize_t len, pos;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1688 struct stat statbuf;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1689 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1690 int result, addsep;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1691
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1692 if (PyUnicode_Check(path)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1693 Py_INCREF(path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1694 path_unicode = path;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1695 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1696 else if (PyBytes_Check(path)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1697 path_unicode = PyUnicode_DecodeFSDefaultAndSize(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1698 PyBytes_AS_STRING(path), PyBytes_GET_SIZE(path));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1699 if (path_unicode == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1700 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1701 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1702 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1703 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1704
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1705 if (PyUnicode_READY(path_unicode))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1706 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1707
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1708 len = PyUnicode_GET_LENGTH(path_unicode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1709 if (PyUnicode_FindChar(path_unicode, 0, 0, len, 1) != -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1710 result = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1711 goto out; /* path contains '\0' */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1712 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1713
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1714 /* sys.path_hooks import hook */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1715 if (p_loader != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1716 _Py_IDENTIFIER(find_module);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1717 PyObject *importer;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1718
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1719 importer = get_path_importer(path_importer_cache,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1720 path_hooks, path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1721 if (importer == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1722 result = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1723 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1724 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1725 /* Note: importer is a borrowed reference */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1726 if (importer != Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1727 PyObject *loader;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1728 loader = _PyObject_CallMethodId(importer,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1729 &PyId_find_module, "O", fullname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1730 if (loader == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1731 result = -1; /* error */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1732 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1733 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1734 if (loader != Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1735 /* a loader was found */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1736 *p_loader = loader;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1737 *p_fd = &importhookdescr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1738 result = 2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1739 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1740 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1741 Py_DECREF(loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1742 result = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1743 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1744 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1745 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1746 /* no hook was found, use builtin import */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1747
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1748 addsep = 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1749 if (len > 0 && PyUnicode_READ_CHAR(path_unicode, len-1) != SEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1750 #ifdef ALTSEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1751 && PyUnicode_READ_CHAR(path_unicode, len-1) != ALTSEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1752 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1753 )
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1754 addsep = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1755 filename = PyUnicode_New(len + PyUnicode_GET_LENGTH(name) + addsep,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1756 Py_MAX(PyUnicode_MAX_CHAR_VALUE(path_unicode),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1757 PyUnicode_MAX_CHAR_VALUE(name)));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1758 if (filename == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1759 result = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1760 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1761 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1762 PyUnicode_CopyCharacters(filename, 0, path_unicode, 0, len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1763 pos = len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1764 if (addsep)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1765 PyUnicode_WRITE(PyUnicode_KIND(filename),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1766 PyUnicode_DATA(filename),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1767 pos++, SEP);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1768 PyUnicode_CopyCharacters(filename, pos, name, 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1769 PyUnicode_GET_LENGTH(name));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1770
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1771 /* Check for package import (buf holds a directory name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1772 and there's an __init__ module in that directory */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1773 #ifdef HAVE_STAT
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1774 if (_Py_stat(filename, &statbuf) == 0 && /* it exists */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1775 S_ISDIR(statbuf.st_mode)) /* it's a directory */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1776 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1777 int match;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1778
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1779 match = case_ok(filename, 0, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1780 if (match < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1781 result = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1782 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1783 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1784 if (match) { /* case matches */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1785 if (find_init_module(filename)) { /* and has __init__.py */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1786 *p_path = filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1787 filename = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1788 *p_fd = &fd_package;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1789 result = 2;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1790 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1791 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1792 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1793 int err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1794 err = PyErr_WarnFormat(PyExc_ImportWarning, 1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1795 "Not importing directory %R: missing __init__.py",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1796 filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1797 if (err) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1798 result = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1799 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1800 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1801 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1802 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1803 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1804 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1805 *p_path = filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1806 filename = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1807 result = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1808 out:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1809 Py_DECREF(path_unicode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1810 Py_XDECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1811 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1812 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1813
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1814 /* Find a module in search_path_list. For each path, try
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1815 find_module_path() or try each _PyImport_Filetab suffix.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1816
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1817 If the module is found, return a file descriptor, write the path in
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1818 *p_filename, write the pointer to the file object into *p_fp, and (if
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1819 p_loader is not NULL) the loader into *p_loader.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1820
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1821 Otherwise, raise an exception and return NULL. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1822
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1823 static struct filedescr*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1824 find_module_path_list(PyObject *fullname, PyObject *name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1825 PyObject *search_path_list, PyObject *path_hooks,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1826 PyObject *path_importer_cache,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1827 PyObject **p_path, FILE **p_fp, PyObject **p_loader)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1828 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1829 Py_ssize_t i, npath;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1830 struct filedescr *fdp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1831 char *filemode;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1832 FILE *fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1833 PyObject *prefix, *filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1834 int match;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1835
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1836 npath = PyList_Size(search_path_list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1837 for (i = 0; i < npath; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1838 PyObject *path;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1839 int ok;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1840
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1841 path = PyList_GetItem(search_path_list, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1842 if (path == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1843 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1844
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1845 prefix = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1846 ok = find_module_path(fullname, name, path,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1847 path_hooks, path_importer_cache,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1848 &prefix, p_loader, &fdp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1849 if (ok < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1850 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1851 if (ok == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1852 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1853 if (ok == 2) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1854 *p_path = prefix;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1855 return fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1856 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1857
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1858 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1859 struct stat statbuf;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1860
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1861 filemode = fdp->mode;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1862 if (filemode[0] == 'U')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1863 filemode = "r" PY_STDIOTEXTMODE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1864
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1865 filename = PyUnicode_FromFormat("%U%s", prefix, fdp->suffix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1866 if (filename == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1867 Py_DECREF(prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1868 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1869 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1870
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1871 if (Py_VerboseFlag > 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1872 PySys_FormatStderr("# trying %R\n", filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1873
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1874 if (_Py_stat(filename, &statbuf) != 0 || S_ISDIR(statbuf.st_mode))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1875 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1876 /* it doesn't exist, or it's a directory */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1877 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1878 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1879 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1880
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1881 fp = _Py_fopen(filename, filemode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1882 if (fp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1883 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1884 if (PyErr_Occurred()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1885 Py_DECREF(prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1886 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1887 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1888 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1889 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1890 match = case_ok(filename, -(Py_ssize_t)strlen(fdp->suffix), name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1891 if (match < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1892 Py_DECREF(prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1893 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1894 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1895 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1896 if (match) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1897 Py_DECREF(prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1898 *p_path = filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1899 *p_fp = fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1900 return fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1901 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1902 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1903
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1904 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1905 fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1906 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1907 Py_DECREF(prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1908 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1909 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1910 "No module named %R", name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1911 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1912 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1913
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1914 /* Find a module:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1915
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1916 - try find_module() of each sys.meta_path hook
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1917 - try find_frozen()
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1918 - try is_builtin()
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1919 - try _PyWin_FindRegisteredModule() (Windows only)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1920 - otherwise, call find_module_path_list() with search_path_list (if not
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1921 NULL) or sys.path
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1922
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1923 fullname can be NULL, but only if p_loader is NULL.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1924
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1925 Return:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1926
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1927 - &fd_builtin (C_BUILTIN) if it is a builtin
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1928 - &fd_frozen (PY_FROZEN) if it is frozen
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1929 - &fd_package (PKG_DIRECTORY) and write the filename into *p_path
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1930 if it is a package
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1931 - &importhookdescr (IMP_HOOK) and write the loader into *p_loader if a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1932 importer loader was found
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1933 - a file descriptor (PY_SOURCE, PY_COMPILED, C_EXTENSION, PY_RESOURCE or
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1934 PY_CODERESOURCE: see _PyImport_Filetab), write the filename into
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1935 *p_path and the pointer to the open file into *p_fp
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1936 - NULL on error
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1937
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1938 By default, *p_path, *p_fp and *p_loader (if set) are set to NULL.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1939 Eg. *p_path is set to NULL for a builtin package.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1940 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1941
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1942 static struct filedescr *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1943 find_module(PyObject *fullname, PyObject *name, PyObject *search_path_list,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1944 PyObject **p_path, FILE **p_fp, PyObject **p_loader)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1945 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1946 Py_ssize_t i, npath;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1947 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1948 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1949 PyObject *path_hooks, *path_importer_cache;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1950
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1951 *p_path = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1952 *p_fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1953 if (p_loader != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1954 *p_loader = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1955
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1956 if (PyUnicode_GET_LENGTH(name) > MAXPATHLEN) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1957 PyErr_SetString(PyExc_OverflowError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1958 "module name is too long");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1959 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1960 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1961
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1962 /* sys.meta_path import hook */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1963 if (p_loader != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1964 _Py_IDENTIFIER(find_module);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1965 PyObject *meta_path;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1966
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1967 meta_path = PySys_GetObject("meta_path");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1968 if (meta_path == NULL || !PyList_Check(meta_path)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1969 PyErr_SetString(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1970 "sys.meta_path must be a list of "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1971 "import hooks");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1972 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1973 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1974 Py_INCREF(meta_path); /* zap guard */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1975 npath = PyList_Size(meta_path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1976 for (i = 0; i < npath; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1977 PyObject *loader;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1978 PyObject *hook = PyList_GetItem(meta_path, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1979 loader = _PyObject_CallMethodId(hook, &PyId_find_module,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1980 "OO", fullname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1981 search_path_list != NULL ?
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1982 search_path_list : Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1983 if (loader == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1984 Py_DECREF(meta_path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1985 return NULL; /* true error */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1986 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1987 if (loader != Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1988 /* a loader was found */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1989 *p_loader = loader;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1990 Py_DECREF(meta_path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1991 return &importhookdescr;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1992 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1993 Py_DECREF(loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1994 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1995 Py_DECREF(meta_path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1996 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1997
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1998 if (find_frozen(fullname) != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1999 return &fd_frozen;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2000
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2001 if (search_path_list == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2002 #ifdef MS_COREDLL
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2003 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2004 struct filedescr *fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2005 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2006 if (is_builtin(name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2007 return &fd_builtin;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2008 #ifdef MS_COREDLL
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2009 fp = _PyWin_FindRegisteredModule(name, &fdp, p_path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2010 if (fp != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2011 *p_fp = fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2012 return fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2013 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2014 else if (PyErr_Occurred())
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2015 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2016 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2017 search_path_list = PySys_GetObject("path");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2018 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2019
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2020 if (search_path_list == NULL || !PyList_Check(search_path_list)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2021 PyErr_SetString(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2022 "sys.path must be a list of directory names");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2023 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2024 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2025
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2026 path_hooks = PySys_GetObject("path_hooks");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2027 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2028 PyErr_SetString(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2029 "sys.path_hooks must be a list of "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2030 "import hooks");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2031 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2032 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2033 path_importer_cache = PySys_GetObject("path_importer_cache");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2034 if (path_importer_cache == NULL ||
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2035 !PyDict_Check(path_importer_cache)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2036 PyErr_SetString(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2037 "sys.path_importer_cache must be a dict");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2038 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2039 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2040
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2041 return find_module_path_list(fullname, name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2042 search_path_list, path_hooks,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2043 path_importer_cache,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2044 p_path, p_fp, p_loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2045 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2046
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2047 /* case_bytes(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2048 * The arguments here are tricky, best shown by example:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2049 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2050 * ^ ^ ^ ^
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2051 * |--------------------- buf ---------------------|
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2052 * |------------------- len ------------------|
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2053 * |------ name -------|
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2054 * |----- namelen -----|
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2055 * buf is the full path, but len only counts up to (& exclusive of) the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2056 * extension. name is the module name, also exclusive of extension.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2057 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2058 * We've already done a successful stat() or fopen() on buf, so know that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2059 * there's some match, possibly case-insensitive.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2060 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2061 * case_bytes() is to return 1 if there's a case-sensitive match for
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2062 * name, else 0. case_bytes() is also to return 1 if envar PYTHONCASEOK
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2063 * exists.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2064 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2065 * case_bytes() is used to implement case-sensitive import semantics even
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2066 * on platforms with case-insensitive filesystems. It's trivial to implement
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2067 * for case-sensitive filesystems. It's pretty much a cross-platform
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2068 * nightmare for systems with case-insensitive filesystems.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2069 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2070
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2071 /* First we may need a pile of platform-specific header files; the sequence
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2072 * of #if's here should match the sequence in the body of case_bytes().
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2073 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2074 #if defined(MS_WINDOWS)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2075 #include <windows.h>
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2076
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2077 #elif defined(DJGPP)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2078 #include <dir.h>
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2079
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2080 #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2081 #include <sys/types.h>
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2082 #include <dirent.h>
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2083
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2084 #elif defined(PYOS_OS2)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2085 #define INCL_DOS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2086 #define INCL_DOSERRORS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2087 #define INCL_NOPMAPI
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2088 #include <os2.h>
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2089 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2090
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2091 #if defined(DJGPP) \
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2092 || ((defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) \
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2093 && defined(HAVE_DIRENT_H)) \
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2094 || defined(PYOS_OS2)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2095 # define USE_CASE_OK_BYTES
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2096 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2097
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2098
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2099 #ifdef USE_CASE_OK_BYTES
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2100 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2101 case_bytes(char *buf, Py_ssize_t len, Py_ssize_t namelen, const char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2102 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2103 /* Pick a platform-specific implementation; the sequence of #if's here should
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2104 * match the sequence just above.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2105 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2106
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2107 /* DJGPP */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2108 #if defined(DJGPP)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2109 struct ffblk ffblk;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2110 int done;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2111
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2112 if (Py_GETENV("PYTHONCASEOK") != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2113 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2114
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2115 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2116 if (done) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2117 PyErr_Format(PyExc_NameError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2118 "Can't find file for module %.100s\n(filename %.300s)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2119 name, buf);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2120 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2121 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2122 return strncmp(ffblk.ff_name, name, namelen) == 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2123
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2124 /* new-fangled macintosh (macosx) or Cygwin */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2125 #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2126 DIR *dirp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2127 struct dirent *dp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2128 char dirname[MAXPATHLEN + 1];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2129 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2130
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2131 if (Py_GETENV("PYTHONCASEOK") != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2132 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2133
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2134 /* Copy the dir component into dirname; substitute "." if empty */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2135 if (dirlen <= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2136 dirname[0] = '.';
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2137 dirname[1] = '\0';
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2138 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2139 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2140 assert(dirlen <= MAXPATHLEN);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2141 memcpy(dirname, buf, dirlen);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2142 dirname[dirlen] = '\0';
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2143 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2144 /* Open the directory and search the entries for an exact match. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2145 dirp = opendir(dirname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2146 if (dirp) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2147 char *nameWithExt = buf + len - namelen;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2148 while ((dp = readdir(dirp)) != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2149 const int thislen =
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2150 #ifdef _DIRENT_HAVE_D_NAMELEN
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2151 dp->d_namlen;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2152 #else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2153 strlen(dp->d_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2154 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2155 if (thislen >= namelen &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2156 strcmp(dp->d_name, nameWithExt) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2157 (void)closedir(dirp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2158 return 1; /* Found */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2159 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2160 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2161 (void)closedir(dirp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2162 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2163 return 0 ; /* Not found */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2164
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2165 /* OS/2 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2166 #elif defined(PYOS_OS2)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2167 HDIR hdir = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2168 ULONG srchcnt = 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2169 FILEFINDBUF3 ffbuf;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2170 APIRET rc;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2171
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2172 if (Py_GETENV("PYTHONCASEOK") != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2173 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2174
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2175 rc = DosFindFirst(buf,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2176 &hdir,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2177 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2178 &ffbuf, sizeof(ffbuf),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2179 &srchcnt,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2180 FIL_STANDARD);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2181 if (rc != NO_ERROR)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2182 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2183 return strncmp(ffbuf.achName, name, namelen) == 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2184
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2185 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2186 #else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2187 # error "USE_CASE_OK_BYTES is not correctly defined"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2188 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2189 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2190 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2191
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2192 /*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2193 * Check if a filename case matchs the name case. We've already done a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2194 * successful stat() or fopen() on buf, so know that there's some match,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2195 * possibly case-insensitive.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2196 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2197 * case_ok() is to return 1 if there's a case-sensitive match for name, 0 if it
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2198 * the filename doesn't match, or -1 on error. case_ok() is also to return 1
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2199 * if envar PYTHONCASEOK exists.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2200 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2201 * case_ok() is used to implement case-sensitive import semantics even
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2202 * on platforms with case-insensitive filesystems. It's trivial to implement
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2203 * for case-sensitive filesystems. It's pretty much a cross-platform
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2204 * nightmare for systems with case-insensitive filesystems.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2205 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2206
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2207 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2208 case_ok(PyObject *filename, Py_ssize_t prefix_delta, PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2209 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2210 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2211 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2212
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2213 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2214
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2215 #ifdef HAVE_STAT
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2216
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2217 /* Helper to look for __init__.py or __init__.py[co] in potential package.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2218 Return 1 if __init__ was found, 0 if not, or -1 on error. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2219 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2220 find_init_module(PyObject *directory)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2221 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2222 struct stat statbuf;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2223 PyObject *filename;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2224 int match;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2225
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2226 filename = PyUnicode_FromFormat("%U%c__init__.py", directory, SEP);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2227 if (filename == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2228 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2229 if (_Py_stat(filename, &statbuf) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2230 /* 3=len(".py") */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2231 match = case_ok(filename, -3, initstr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2232 if (match < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2233 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2234 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2235 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2236 if (match) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2237 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2238 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2239 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2240 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2241 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2242
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2243 filename = PyUnicode_FromFormat("%U%c__init__.py%c",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2244 directory, SEP, Py_OptimizeFlag ? 'o' : 'c');
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2245 if (filename == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2246 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2247 if (_Py_stat(filename, &statbuf) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2248 /* 4=len(".pyc") */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2249 match = case_ok(filename, -4, initstr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2250 if (match < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2251 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2252 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2253 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2254 if (match) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2255 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2256 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2257 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2258 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2259 Py_DECREF(filename);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2260 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2261 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2262
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2263 #endif /* HAVE_STAT */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2264
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2265
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2266 static int init_builtin(PyObject *); /* Forward */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2267
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2268 static PyObject*
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2269 load_builtin(PyObject *name, int type)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2270 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2271 PyObject *m, *modules;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2272 int err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2273
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2274 if (type == C_BUILTIN)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2275 err = init_builtin(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2276 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2277 err = PyImport_ImportFrozenModuleObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2278 if (err < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2279 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2280 if (err == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2281 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2282 "Purported %s module %R not found",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2283 type == C_BUILTIN ? "builtin" : "frozen",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2284 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2285 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2286 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2287
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2288 modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2289 m = PyDict_GetItem(modules, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2290 if (m == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2291 PyErr_Format(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2292 PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2293 "%s module %R not properly initialized",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2294 type == C_BUILTIN ? "builtin" : "frozen",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2295 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2296 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2297 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2298 Py_INCREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2299 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2300 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2301
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2302 /* Load an external module using the default search path and return
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2303 its module object WITH INCREMENTED REFERENCE COUNT */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2304
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2305 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2306 load_module(PyObject *name, FILE *fp, PyObject *pathname, int type, PyObject *loader)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2307 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2308 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2309
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2310 /* First check that there's an open file (if we need one) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2311 switch (type) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2312 case PY_SOURCE:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2313 case PY_COMPILED:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2314 if (fp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2315 PyErr_Format(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2316 "file object required for import (type code %d)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2317 type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2318 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2319 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2320 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2321
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2322 switch (type) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2323
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2324 case PY_SOURCE:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2325 m = load_source_module(name, pathname, fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2326 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2327
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2328 case PY_COMPILED:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2329 m = load_compiled_module(name, pathname, fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2330 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2331
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2332 #ifdef HAVE_DYNAMIC_LOADING
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2333 case C_EXTENSION:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2334 m = _PyImport_LoadDynamicModule(name, pathname, fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2335 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2336 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2337
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2338 case PKG_DIRECTORY:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2339 m = load_package(name, pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2340 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2341
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2342 case C_BUILTIN:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2343 case PY_FROZEN:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2344 m = load_builtin(name, type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2345 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2346
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2347 case IMP_HOOK: {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2348 _Py_IDENTIFIER(load_module);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2349 if (loader == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2350 PyErr_SetString(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2351 "import hook without loader");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2352 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2353 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2354 m = _PyObject_CallMethodId(loader, &PyId_load_module, "O", name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2355 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2356 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2357
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2358 default:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2359 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2360 "Don't know how to import %R (type code %d)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2361 name, type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2362 m = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2363
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2364 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2365
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2366 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2367 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2368
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2369
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2370 /* Initialize a built-in module.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2371 Return 1 for success, 0 if the module is not found, and -1 with
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2372 an exception set if the initialization failed. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2373
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2374 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2375 init_builtin(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2376 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2377 struct _inittab *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2378
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2379 if (_PyImport_FindExtensionObject(name, name) != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2380 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2381
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2382 for (p = PyImport_Inittab; p->name != NULL; p++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2383 PyObject *mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2384 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2385 if (p->initfunc == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2386 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2387 "Cannot re-init internal module %R",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2388 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2389 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2390 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2391 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2392 PySys_FormatStderr("import %U # builtin\n", name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2393 mod = (*p->initfunc)();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2394 if (mod == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2395 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2396 if (_PyImport_FixupExtensionObject(mod, name, name) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2397 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2398 /* FixupExtension has put the module into sys.modules,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2399 so we can release our own reference. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2400 Py_DECREF(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2401 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2402 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2403 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2404 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2405 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2406
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2407
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2408 /* Frozen modules */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2409
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2410 static struct _frozen *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2411 find_frozen(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2412 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2413 struct _frozen *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2414
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2415 if (name == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2416 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2417
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2418 for (p = PyImport_FrozenModules; ; p++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2419 if (p->name == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2420 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2421 if (PyUnicode_CompareWithASCIIString(name, p->name) == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2422 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2423 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2424 return p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2425 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2426
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2427 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2428 get_frozen_object(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2429 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2430 struct _frozen *p = find_frozen(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2431 int size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2432
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2433 if (p == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2434 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2435 "No such frozen object named %R",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2436 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2437 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2438 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2439 if (p->code == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2440 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2441 "Excluded frozen object named %R",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2442 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2443 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2444 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2445 size = p->size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2446 if (size < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2447 size = -size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2448 return PyMarshal_ReadObjectFromString((char *)p->code, size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2449 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2450
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2451 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2452 is_frozen_package(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2453 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2454 struct _frozen *p = find_frozen(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2455 int size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2456
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2457 if (p == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2458 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2459 "No such frozen object named %R",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2460 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2461 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2462 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2463
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2464 size = p->size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2465
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2466 if (size < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2467 Py_RETURN_TRUE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2468 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2469 Py_RETURN_FALSE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2470 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2471
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2472
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2473 /* Initialize a frozen module.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2474 Return 1 for success, 0 if the module is not found, and -1 with
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2475 an exception set if the initialization failed.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2476 This function is also used from frozenmain.c */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2477
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2478 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2479 PyImport_ImportFrozenModuleObject(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2480 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2481 struct _frozen *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2482 PyObject *co, *m, *path;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2483 int ispackage;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2484 int size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2485
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2486 p = find_frozen(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2487
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2488 if (p == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2489 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2490 if (p->code == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2491 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2492 "Excluded frozen object named %R",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2493 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2494 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2495 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2496 size = p->size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2497 ispackage = (size < 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2498 if (ispackage)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2499 size = -size;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2500 if (Py_VerboseFlag)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2501 PySys_FormatStderr("import %U # frozen%s\n",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2502 name, ispackage ? " package" : "");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2503 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2504 if (co == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2505 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2506 if (!PyCode_Check(co)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2507 PyErr_Format(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2508 "frozen object %R is not a code object",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2509 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2510 goto err_return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2511 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2512 if (ispackage) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2513 /* Set __path__ to the package name */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2514 PyObject *d, *l;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2515 int err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2516 m = PyImport_AddModuleObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2517 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2518 goto err_return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2519 d = PyModule_GetDict(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2520 l = PyList_New(1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2521 if (l == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2522 goto err_return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2523 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2524 Py_INCREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2525 PyList_SET_ITEM(l, 0, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2526 err = PyDict_SetItemString(d, "__path__", l);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2527 Py_DECREF(l);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2528 if (err != 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2529 goto err_return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2530 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2531 path = PyUnicode_FromString("<frozen>");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2532 if (path == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2533 goto err_return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2534 m = PyImport_ExecCodeModuleObject(name, co, path, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2535 Py_DECREF(path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2536 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2537 goto err_return;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2538 Py_DECREF(co);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2539 Py_DECREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2540 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2541 err_return:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2542 Py_DECREF(co);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2543 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2544 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2545
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2546 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2547 PyImport_ImportFrozenModule(char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2548 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2549 PyObject *nameobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2550 int ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2551 nameobj = PyUnicode_InternFromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2552 if (nameobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2553 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2554 ret = PyImport_ImportFrozenModuleObject(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2555 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2556 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2557 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2558
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2559
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2560 /* Import a module, either built-in, frozen, or external, and return
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2561 its module object WITH INCREMENTED REFERENCE COUNT */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2562
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2563 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2564 PyImport_ImportModule(const char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2565 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2566 PyObject *pname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2567 PyObject *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2568
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2569 pname = PyUnicode_FromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2570 if (pname == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2571 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2572 result = PyImport_Import(pname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2573 Py_DECREF(pname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2574 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2575 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2576
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2577 /* Import a module without blocking
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2578 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2579 * At first it tries to fetch the module from sys.modules. If the module was
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2580 * never loaded before it loads it with PyImport_ImportModule() unless another
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2581 * thread holds the import lock. In the latter case the function raises an
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2582 * ImportError instead of blocking.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2583 *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2584 * Returns the module object with incremented ref count.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2585 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2586 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2587 PyImport_ImportModuleNoBlock(const char *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2588 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2589 PyObject *nameobj, *modules, *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2590 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2591 long me;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2592 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2593
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2594 /* Try to get the module from sys.modules[name] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2595 modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2596 if (modules == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2597 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2598
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2599 nameobj = PyUnicode_FromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2600 if (nameobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2601 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2602 result = PyDict_GetItem(modules, nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2603 if (result != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2604 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2605 Py_INCREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2606 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2607 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2608 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2609 #ifdef WITH_THREAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2610 /* check the import lock
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2611 * me might be -1 but I ignore the error here, the lock function
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2612 * takes care of the problem */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2613 me = PyThread_get_thread_ident();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2614 if (import_lock_thread == -1 || import_lock_thread == me) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2615 /* no thread or me is holding the lock */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2616 result = PyImport_Import(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2617 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2618 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2619 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2620 "Failed to import %R because the import lock"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2621 "is held by another thread.",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2622 nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2623 result = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2624 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2625 #else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2626 result = PyImport_Import(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2627 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2628 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2629 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2630 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2631
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2632 /* Forward declarations for helper routines */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2633 static PyObject *get_parent(PyObject *globals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2634 PyObject **p_name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2635 int level);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2636 static PyObject *load_next(PyObject *mod, PyObject *altmod,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2637 PyObject *inputname, PyObject **p_outputname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2638 PyObject **p_prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2639 static int mark_miss(PyObject *name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2640 static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2641 PyObject *buf, int recursive);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2642 static PyObject * import_submodule(PyObject *mod, PyObject *name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2643 PyObject *fullname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2644
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2645 /* The Magnum Opus of dotted-name import :-) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2646
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2647 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2648 import_module_level(PyObject *name, PyObject *globals, PyObject *locals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2649 PyObject *fromlist, int level)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2650 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2651 PyObject *parent, *next, *inputname, *outputname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2652 PyObject *head = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2653 PyObject *tail = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2654 PyObject *prefix = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2655 PyObject *result = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2656 Py_ssize_t sep, altsep;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2657
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2658 if (PyUnicode_READY(name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2659 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2660
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2661 sep = PyUnicode_FindChar(name, SEP, 0, PyUnicode_GET_LENGTH(name), 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2662 if (sep == -2)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2663 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2664 #ifdef ALTSEP
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2665 altsep = PyUnicode_FindChar(name, ALTSEP, 0, PyUnicode_GET_LENGTH(name), 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2666 if (altsep == -2)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2667 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2668 #else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2669 altsep = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2670 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2671 if (sep != -1 || altsep != -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2672 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2673 PyErr_SetString(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2674 "Import by filename is not supported.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2675 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2676 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2677
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2678 parent = get_parent(globals, &prefix, level);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2679 if (parent == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2680 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2681 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2682
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2683 if (PyUnicode_READY(prefix))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2684 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2685
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2686 head = load_next(parent, level < 0 ? Py_None : parent, name, &outputname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2687 &prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2688 if (head == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2689 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2690
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2691 tail = head;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2692 Py_INCREF(tail);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2693
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2694 if (outputname != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2695 while (1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2696 inputname = outputname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2697 next = load_next(tail, tail, inputname, &outputname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2698 &prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2699 Py_CLEAR(tail);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2700 Py_CLEAR(inputname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2701 if (next == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2702 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2703 tail = next;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2704
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2705 if (outputname == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2706 break;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2707 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2708 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2709 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2710 if (tail == Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2711 /* If tail is Py_None, both get_parent and load_next found
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2712 an empty module name: someone called __import__("") or
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2713 doctored faulty bytecode */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2714 PyErr_SetString(PyExc_ValueError, "Empty module name");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2715 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2716 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2717
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2718 if (fromlist != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2719 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2720 fromlist = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2721 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2722
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2723 if (fromlist == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2724 result = head;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2725 Py_INCREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2726 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2727 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2728
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2729 if (!ensure_fromlist(tail, fromlist, prefix, 0))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2730 goto out;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2731
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2732 result = tail;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2733 Py_INCREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2734 out:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2735 Py_XDECREF(head);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2736 Py_XDECREF(tail);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2737 Py_XDECREF(prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2738 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2739 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2740
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2741 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2742 PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2743 PyObject *locals, PyObject *fromlist,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2744 int level)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2745 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2746 PyObject *mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2747 _PyImport_AcquireLock();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2748 mod = import_module_level(name, globals, locals, fromlist, level);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2749 if (_PyImport_ReleaseLock() < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2750 Py_XDECREF(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2751 PyErr_SetString(PyExc_RuntimeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2752 "not holding the import lock");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2753 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2754 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2755 return mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2756 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2757
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2758 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2759 PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2760 PyObject *fromlist, int level)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2761 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2762 PyObject *nameobj, *mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2763 nameobj = PyUnicode_FromString(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2764 if (nameobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2765 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2766 mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2767 fromlist, level);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2768 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2769 return mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2770 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2771
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2772
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2773 /* Return the package that an import is being performed in. If globals comes
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2774 from the module foo.bar.bat (not itself a package), this returns the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2775 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2776 the package's entry in sys.modules is returned, as a borrowed reference.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2777
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2778 The name of the returned package is returned in *p_name.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2779
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2780 If globals doesn't come from a package or a module in a package, or a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2781 corresponding entry is not found in sys.modules, Py_None is returned.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2782 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2783 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2784 get_parent(PyObject *globals, PyObject **p_name, int level)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2785 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2786 PyObject *nameobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2787
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2788 static PyObject *namestr = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2789 static PyObject *pathstr = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2790 static PyObject *pkgstr = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2791 PyObject *pkgname, *modname, *modpath, *modules, *parent;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2792 int orig_level = level;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2793
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2794 if (globals == NULL || !PyDict_Check(globals) || !level)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2795 goto return_none;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2796
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2797 if (namestr == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2798 namestr = PyUnicode_InternFromString("__name__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2799 if (namestr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2800 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2801 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2802 if (pathstr == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2803 pathstr = PyUnicode_InternFromString("__path__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2804 if (pathstr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2805 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2806 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2807 if (pkgstr == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2808 pkgstr = PyUnicode_InternFromString("__package__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2809 if (pkgstr == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2810 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2811 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2812
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2813 pkgname = PyDict_GetItem(globals, pkgstr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2814
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2815 if ((pkgname != NULL) && (pkgname != Py_None)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2816 /* __package__ is set, so use it */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2817 if (!PyUnicode_Check(pkgname)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2818 PyErr_SetString(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2819 "__package__ set to non-string");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2820 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2821 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2822 if (PyUnicode_GET_LENGTH(pkgname) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2823 if (level > 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2824 PyErr_SetString(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2825 "Attempted relative import in non-package");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2826 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2827 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2828 goto return_none;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2829 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2830 Py_INCREF(pkgname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2831 nameobj = pkgname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2832 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2833 /* __package__ not set, so figure it out and set it */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2834 modname = PyDict_GetItem(globals, namestr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2835 if (modname == NULL || !PyUnicode_Check(modname))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2836 goto return_none;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2837
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2838 modpath = PyDict_GetItem(globals, pathstr);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2839 if (modpath != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2840 /* __path__ is set, so modname is already the package name */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2841 int error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2842
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2843 error = PyDict_SetItem(globals, pkgstr, modname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2844 if (error) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2845 PyErr_SetString(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2846 "Could not set __package__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2847 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2848 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2849 Py_INCREF(modname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2850 nameobj = modname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2851 } else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2852 /* Normal module, so work out the package name if any */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2853 Py_ssize_t len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2854 len = PyUnicode_FindChar(modname, '.',
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2855 0, PyUnicode_GET_LENGTH(modname), -1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2856 if (len == -2)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2857 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2858 if (len < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2859 if (level > 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2860 PyErr_SetString(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2861 "Attempted relative import in non-package");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2862 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2863 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2864 if (PyDict_SetItem(globals, pkgstr, Py_None)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2865 PyErr_SetString(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2866 "Could not set __package__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2867 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2868 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2869 goto return_none;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2870 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2871 pkgname = PyUnicode_Substring(modname, 0, len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2872 if (pkgname == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2873 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2874 if (PyDict_SetItem(globals, pkgstr, pkgname)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2875 Py_DECREF(pkgname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2876 PyErr_SetString(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2877 "Could not set __package__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2878 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2879 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2880 nameobj = pkgname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2881 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2882 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2883 if (level > 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2884 Py_ssize_t dot, end = PyUnicode_GET_LENGTH(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2885 PyObject *newname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2886 while (--level > 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2887 dot = PyUnicode_FindChar(nameobj, '.', 0, end, -1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2888 if (dot == -2) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2889 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2890 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2891 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2892 if (dot < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2893 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2894 PyErr_SetString(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2895 "Attempted relative import beyond "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2896 "toplevel package");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2897 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2898 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2899 end = dot;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2900 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2901 newname = PyUnicode_Substring(nameobj, 0, end);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2902 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2903 if (newname == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2904 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2905 nameobj = newname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2906 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2907
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2908 modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2909 parent = PyDict_GetItem(modules, nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2910 if (parent == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2911 int err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2912
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2913 if (orig_level >= 1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2914 PyErr_Format(PyExc_SystemError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2915 "Parent module %R not loaded, "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2916 "cannot perform relative import", nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2917 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2918 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2919 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2920
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2921 err = PyErr_WarnFormat(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2922 PyExc_RuntimeWarning, 1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2923 "Parent module %R not found while handling absolute import",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2924 nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2925 Py_DECREF(nameobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2926 if (err)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2927 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2928
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2929 goto return_none;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2930 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2931 *p_name = nameobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2932 return parent;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2933 /* We expect, but can't guarantee, if parent != None, that:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2934 - parent.__name__ == name
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2935 - parent.__dict__ is globals
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2936 If this is violated... Who cares? */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2937
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2938 return_none:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2939 nameobj = PyUnicode_New(0, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2940 if (nameobj == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2941 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2942 *p_name = nameobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2943 return Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2944 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2945
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2946 /* altmod is either None or same as mod */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2947 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2948 load_next(PyObject *mod, PyObject *altmod,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2949 PyObject *inputname, PyObject **p_outputname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2950 PyObject **p_prefix)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2951 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2952 Py_ssize_t dot;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2953 Py_ssize_t len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2954 PyObject *fullname, *name = NULL, *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2955
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2956 *p_outputname = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2957
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2958 len = PyUnicode_GET_LENGTH(inputname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2959 if (len == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2960 /* completely empty module name should only happen in
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2961 'from . import' (or '__import__("")')*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2962 Py_INCREF(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2963 return mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2964 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2965
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2966
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2967 dot = PyUnicode_FindChar(inputname, '.', 0, len, 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2968 if (dot >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2969 len = dot;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2970 if (len == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2971 PyErr_SetString(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2972 "Empty module name");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2973 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2974 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2975 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2976
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2977 /* name = inputname[:len] */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2978 name = PyUnicode_Substring(inputname, 0, len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2979 if (name == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2980 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2981
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2982 if (PyUnicode_GET_LENGTH(*p_prefix)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2983 /* fullname = prefix + "." + name */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2984 fullname = PyUnicode_FromFormat("%U.%U", *p_prefix, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2985 if (fullname == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2986 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2987 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2988 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2989 fullname = name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2990 Py_INCREF(fullname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2991 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2992
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2993 result = import_submodule(mod, name, fullname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2994 Py_DECREF(*p_prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2995 /* Transfer reference. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2996 *p_prefix = fullname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2997 if (result == Py_None && altmod != mod) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2998 Py_DECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2999 /* Here, altmod must be None and mod must not be None */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3000 result = import_submodule(altmod, name, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3001 if (result != NULL && result != Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3002 if (mark_miss(*p_prefix) != 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3003 Py_DECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3004 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3005 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3006 Py_DECREF(*p_prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3007 *p_prefix = name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3008 Py_INCREF(*p_prefix);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3009 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3010 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3011 if (result == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3012 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3013
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3014 if (result == Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3015 Py_DECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3016 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3017 "No module named %R", inputname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3018 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3019 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3020
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3021 if (dot >= 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3022 *p_outputname = PyUnicode_Substring(inputname, dot+1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3023 PyUnicode_GET_LENGTH(inputname));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3024 if (*p_outputname == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3025 Py_DECREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3026 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3027 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3028 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3029
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3030 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3031 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3032
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3033 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3034 Py_XDECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3035 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3036 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3037
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3038 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3039 mark_miss(PyObject *name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3040 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3041 PyObject *modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3042 return PyDict_SetItem(modules, name, Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3043 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3044
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3045 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3046 ensure_fromlist(PyObject *mod, PyObject *fromlist, PyObject *name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3047 int recursive)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3048 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3049 int i;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3050 PyObject *fullname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3051 Py_ssize_t fromlist_len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3052
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3053 if (!_PyObject_HasAttrId(mod, &PyId___path__))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3054 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3055
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3056 fromlist_len = PySequence_Size(fromlist);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3057
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3058 for (i = 0; i < fromlist_len; i++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3059 PyObject *item = PySequence_GetItem(fromlist, i);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3060 int hasit;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3061 if (item == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3062 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3063 if (!PyUnicode_Check(item)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3064 PyErr_SetString(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3065 "Item in ``from list'' not a string");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3066 Py_DECREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3067 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3068 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3069 if (PyUnicode_READ_CHAR(item, 0) == '*') {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3070 PyObject *all;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3071 _Py_IDENTIFIER(__all__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3072 Py_DECREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3073 /* See if the package defines __all__ */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3074 if (recursive)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3075 continue; /* Avoid endless recursion */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3076 all = _PyObject_GetAttrId(mod, &PyId___all__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3077 if (all == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3078 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3079 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3080 int ret = ensure_fromlist(mod, all, name, 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3081 Py_DECREF(all);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3082 if (!ret)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3083 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3084 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3085 continue;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3086 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3087 hasit = PyObject_HasAttr(mod, item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3088 if (!hasit) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3089 PyObject *submod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3090 fullname = PyUnicode_FromFormat("%U.%U", name, item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3091 if (fullname != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3092 submod = import_submodule(mod, item, fullname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3093 Py_DECREF(fullname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3094 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3095 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3096 submod = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3097 Py_XDECREF(submod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3098 if (submod == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3099 Py_DECREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3100 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3101 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3102 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3103 Py_DECREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3104 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3105
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3106 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3107 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3108
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3109 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3110 add_submodule(PyObject *mod, PyObject *submod, PyObject *fullname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3111 PyObject *subname, PyObject *modules)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3112 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3113 if (mod == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3114 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3115 /* Irrespective of the success of this load, make a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3116 reference to it in the parent package module. A copy gets
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3117 saved in the modules dictionary under the full name, so get a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3118 reference from there, if need be. (The exception is when the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3119 load failed with a SyntaxError -- then there's no trace in
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3120 sys.modules. In that case, of course, do nothing extra.) */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3121 if (submod == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3122 submod = PyDict_GetItem(modules, fullname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3123 if (submod == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3124 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3125 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3126 if (PyModule_Check(mod)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3127 /* We can't use setattr here since it can give a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3128 * spurious warning if the submodule name shadows a
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3129 * builtin name */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3130 PyObject *dict = PyModule_GetDict(mod);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3131 if (!dict)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3132 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3133 if (PyDict_SetItem(dict, subname, submod) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3134 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3135 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3136 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3137 if (PyObject_SetAttr(mod, subname, submod) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3138 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3139 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3140 return 1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3141 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3142
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3143 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3144 import_submodule(PyObject *mod, PyObject *subname, PyObject *fullname)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3145 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3146 PyObject *modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3147 PyObject *m = NULL, *bufobj, *path_list, *loader;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3148 struct filedescr *fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3149 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3150
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3151 /* Require:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3152 if mod == None: subname == fullname
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3153 else: mod.__name__ + "." + subname == fullname
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3154 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3155
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3156 if ((m = PyDict_GetItem(modules, fullname)) != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3157 Py_INCREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3158 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3159 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3160
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3161 if (mod == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3162 path_list = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3163 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3164 path_list = _PyObject_GetAttrId(mod, &PyId___path__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3165 if (path_list == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3166 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3167 Py_INCREF(Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3168 return Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3169 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3170 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3171
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3172 fdp = find_module(fullname, subname, path_list,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3173 &bufobj, &fp, &loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3174 Py_XDECREF(path_list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3175 if (fdp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3176 if (!PyErr_ExceptionMatches(PyExc_ImportError))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3177 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3178 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3179 Py_INCREF(Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3180 return Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3181 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3182 m = load_module(fullname, fp, bufobj, fdp->type, loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3183 Py_XDECREF(bufobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3184 Py_XDECREF(loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3185 if (fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3186 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3187 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3188 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3189 if (!add_submodule(mod, m, fullname, subname, modules)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3190 Py_XDECREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3191 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3192 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3193 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3194 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3195
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3196
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3197 /* Re-import a module of any kind and return its module object, WITH
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3198 INCREMENTED REFERENCE COUNT */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3199
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3200 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3201 PyImport_ReloadModule(PyObject *m)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3202 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3203 PyInterpreterState *interp = PyThreadState_Get()->interp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3204 PyObject *modules_reloading = interp->modules_reloading;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3205 PyObject *modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3206 PyObject *path_list = NULL, *loader = NULL, *existing_m = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3207 PyObject *name, *bufobj, *subname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3208 Py_ssize_t subname_start;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3209 struct filedescr *fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3210 FILE *fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3211 PyObject *newm = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3212
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3213 if (modules_reloading == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3214 Py_FatalError("PyImport_ReloadModule: "
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3215 "no modules_reloading dictionary!");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3216 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3217 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3218
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3219 if (m == NULL || !PyModule_Check(m)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3220 PyErr_SetString(PyExc_TypeError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3221 "reload() argument must be module");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3222 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3223 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3224 name = PyModule_GetNameObject(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3225 if (name == NULL || PyUnicode_READY(name) == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3226 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3227 if (m != PyDict_GetItem(modules, name)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3228 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3229 "reload(): module %R not in sys.modules",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3230 name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3231 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3232 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3233 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3234 existing_m = PyDict_GetItem(modules_reloading, name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3235 if (existing_m != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3236 /* Due to a recursive reload, this module is already
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3237 being reloaded. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3238 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3239 Py_INCREF(existing_m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3240 return existing_m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3241 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3242 if (PyDict_SetItem(modules_reloading, name, m) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3243 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3244 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3245 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3246
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3247 subname_start = PyUnicode_FindChar(name, '.', 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3248 PyUnicode_GET_LENGTH(name), -1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3249 if (subname_start == -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3250 Py_INCREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3251 subname = name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3252 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3253 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3254 PyObject *parentname, *parent;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3255 Py_ssize_t len;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3256 parentname = PyUnicode_Substring(name, 0, subname_start);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3257 if (parentname == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3258 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3259 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3260 parent = PyDict_GetItem(modules, parentname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3261 if (parent == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3262 PyErr_Format(PyExc_ImportError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3263 "reload(): parent %R not in sys.modules",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3264 parentname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3265 Py_DECREF(parentname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3266 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3267 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3268 Py_DECREF(parentname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3269 path_list = _PyObject_GetAttrId(parent, &PyId___path__);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3270 if (path_list == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3271 PyErr_Clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3272 subname_start++;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3273 len = PyUnicode_GET_LENGTH(name) - (subname_start + 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3274 subname = PyUnicode_Substring(name, subname_start, len);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3275 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3276 if (subname == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3277 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3278 fdp = find_module(name, subname, path_list,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3279 &bufobj, &fp, &loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3280 Py_DECREF(subname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3281 Py_XDECREF(path_list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3282
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3283 if (fdp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3284 Py_XDECREF(loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3285 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3286 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3287
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3288 newm = load_module(name, fp, bufobj, fdp->type, loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3289 Py_XDECREF(bufobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3290 Py_XDECREF(loader);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3291
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3292 if (fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3293 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3294 if (newm == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3295 /* load_module probably removed name from modules because of
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3296 * the error. Put back the original module object. We're
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3297 * going to return NULL in this case regardless of whether
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3298 * replacing name succeeds, so the return value is ignored.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3299 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3300 PyDict_SetItem(modules, name, m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3301 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3302
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3303 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3304 imp_modules_reloading_clear();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3305 Py_DECREF(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3306 return newm;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3307 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3308
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3309
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3310 /* Higher-level import emulator which emulates the "import" statement
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3311 more accurately -- it invokes the __import__() function from the
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3312 builtins of the current globals. This means that the import is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3313 done using whatever import hooks are installed in the current
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3314 environment.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3315 A dummy list ["__doc__"] is passed as the 4th argument so that
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3316 e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3317 will return <module "gencache"> instead of <module "win32com">. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3318
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3319 PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3320 PyImport_Import(PyObject *module_name)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3321 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3322 static PyObject *silly_list = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3323 static PyObject *builtins_str = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3324 static PyObject *import_str = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3325 PyObject *globals = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3326 PyObject *import = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3327 PyObject *builtins = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3328 PyObject *modules = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3329 PyObject *r = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3330
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3331 /* Initialize constant string objects */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3332 if (silly_list == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3333 import_str = PyUnicode_InternFromString("__import__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3334 if (import_str == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3335 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3336 builtins_str = PyUnicode_InternFromString("__builtins__");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3337 if (builtins_str == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3338 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3339 silly_list = PyList_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3340 if (silly_list == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3341 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3342 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3343
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3344 /* Get the builtins from current globals */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3345 globals = PyEval_GetGlobals();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3346 if (globals != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3347 Py_INCREF(globals);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3348 builtins = PyObject_GetItem(globals, builtins_str);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3349 if (builtins == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3350 goto err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3351 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3352 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3353 /* No globals -- use standard builtins, and fake globals */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3354 builtins = PyImport_ImportModuleLevel("builtins",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3355 NULL, NULL, NULL, 0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3356 if (builtins == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3357 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3358 globals = Py_BuildValue("{OO}", builtins_str, builtins);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3359 if (globals == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3360 goto err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3361 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3362
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3363 /* Get the __import__ function from the builtins */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3364 if (PyDict_Check(builtins)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3365 import = PyObject_GetItem(builtins, import_str);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3366 if (import == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3367 PyErr_SetObject(PyExc_KeyError, import_str);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3368 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3369 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3370 import = PyObject_GetAttr(builtins, import_str);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3371 if (import == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3372 goto err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3373
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3374 /* Call the __import__ function with the proper argument list
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3375 Always use absolute import here.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3376 Calling for side-effect of import. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3377 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3378 globals, silly_list, 0, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3379 if (r == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3380 goto err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3381 Py_DECREF(r);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3382
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3383 modules = PyImport_GetModuleDict();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3384 r = PyDict_GetItem(modules, module_name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3385 if (r != NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3386 Py_INCREF(r);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3387
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3388 err:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3389 Py_XDECREF(globals);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3390 Py_XDECREF(builtins);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3391 Py_XDECREF(import);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3392
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3393 return r;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3394 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3395
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3396
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3397 /* Module 'imp' provides Python access to the primitives used for
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3398 importing modules.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3399 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3400
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3401 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3402 imp_make_magic(long magic)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3403 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3404 char buf[4];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3405
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3406 buf[0] = (char) ((magic >> 0) & 0xff);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3407 buf[1] = (char) ((magic >> 8) & 0xff);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3408 buf[2] = (char) ((magic >> 16) & 0xff);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3409 buf[3] = (char) ((magic >> 24) & 0xff);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3410
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3411 return PyBytes_FromStringAndSize(buf, 4);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3412 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3413
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3414 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3415 imp_get_magic(PyObject *self, PyObject *noargs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3416 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3417 return imp_make_magic(pyc_magic);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3418 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3419
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3420 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3421 imp_get_tag(PyObject *self, PyObject *noargs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3422 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3423 return PyUnicode_FromString(pyc_tag);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3424 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3425
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3426 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3427 imp_get_suffixes(PyObject *self, PyObject *noargs)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3428 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3429 PyObject *list;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3430 struct filedescr *fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3431
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3432 list = PyList_New(0);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3433 if (list == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3434 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3435 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3436 PyObject *item = Py_BuildValue("ssi",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3437 fdp->suffix, fdp->mode, fdp->type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3438 if (item == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3439 Py_DECREF(list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3440 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3441 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3442 if (PyList_Append(list, item) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3443 Py_DECREF(list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3444 Py_DECREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3445 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3446 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3447 Py_DECREF(item);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3448 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3449 return list;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3450 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3451
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3452 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3453 call_find_module(PyObject *name, PyObject *path_list)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3454 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3455 extern int fclose(FILE *);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3456 PyObject *fob, *ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3457 PyObject *pathobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3458 struct filedescr *fdp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3459 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3460 int fd = -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3461 char *found_encoding = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3462 char *encoding = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3463
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3464 if (path_list == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3465 path_list = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3466 fdp = find_module(NULL, name, path_list,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3467 &pathobj, &fp, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3468 if (fdp == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3469 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3470 if (fp != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3471 fd = fileno(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3472 if (fd != -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3473 fd = dup(fd);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3474 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3475 if (fd == -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3476 PyErr_SetFromErrno(PyExc_OSError);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3477 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3478 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3479 fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3480 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3481 if (fd != -1) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3482 if (strchr(fdp->mode, 'b') == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3483 /* PyTokenizer_FindEncodingFilename() returns PyMem_MALLOC'ed
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3484 memory. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3485 found_encoding = PyTokenizer_FindEncodingFilename(fd, pathobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3486 lseek(fd, 0, 0); /* Reset position */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3487 if (found_encoding == NULL && PyErr_Occurred()) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3488 Py_XDECREF(pathobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3489 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3490 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3491 encoding = (found_encoding != NULL) ? found_encoding :
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3492 (char*)PyUnicode_GetDefaultEncoding();
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3493 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3494 fob = PyFile_FromFd(fd, NULL, fdp->mode, -1,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3495 (char*)encoding, NULL, NULL, 1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3496 if (fob == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3497 Py_XDECREF(pathobj);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3498 close(fd);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3499 PyMem_FREE(found_encoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3500 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3501 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3502 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3503 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3504 fob = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3505 Py_INCREF(fob);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3506 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3507 if (pathobj == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3508 Py_INCREF(Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3509 pathobj = Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3510 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3511 ret = Py_BuildValue("NN(ssi)",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3512 fob, pathobj, fdp->suffix, fdp->mode, fdp->type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3513 PyMem_FREE(found_encoding);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3514
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3515 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3516 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3517
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3518 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3519 imp_find_module(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3520 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3521 PyObject *name, *path_list = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3522 if (!PyArg_ParseTuple(args, "U|O:find_module",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3523 &name, &path_list))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3524 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3525 return call_find_module(name, path_list);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3526 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3527
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3528 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3529 imp_init_builtin(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3530 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3531 PyObject *name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3532 int ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3533 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3534 if (!PyArg_ParseTuple(args, "U:init_builtin", &name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3535 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3536 ret = init_builtin(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3537 if (ret < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3538 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3539 if (ret == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3540 Py_INCREF(Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3541 return Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3542 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3543 m = PyImport_AddModuleObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3544 Py_XINCREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3545 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3546 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3547
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3548 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3549 imp_init_frozen(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3550 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3551 PyObject *name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3552 int ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3553 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3554 if (!PyArg_ParseTuple(args, "U:init_frozen", &name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3555 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3556 ret = PyImport_ImportFrozenModuleObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3557 if (ret < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3558 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3559 if (ret == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3560 Py_INCREF(Py_None);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3561 return Py_None;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3562 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3563 m = PyImport_AddModuleObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3564 Py_XINCREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3565 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3566 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3567
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3568 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3569 imp_get_frozen_object(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3570 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3571 PyObject *name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3572
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3573 if (!PyArg_ParseTuple(args, "U:get_frozen_object", &name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3574 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3575 return get_frozen_object(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3576 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3577
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3578 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3579 imp_is_frozen_package(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3580 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3581 PyObject *name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3582
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3583 if (!PyArg_ParseTuple(args, "U:is_frozen_package", &name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3584 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3585 return is_frozen_package(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3586 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3587
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3588 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3589 imp_is_builtin(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3590 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3591 PyObject *name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3592 if (!PyArg_ParseTuple(args, "U:is_builtin", &name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3593 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3594 return PyLong_FromLong(is_builtin(name));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3595 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3596
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3597 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3598 imp_is_frozen(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3599 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3600 PyObject *name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3601 struct _frozen *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3602 if (!PyArg_ParseTuple(args, "U:is_frozen", &name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3603 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3604 p = find_frozen(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3605 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3606 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3607
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3608 static FILE *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3609 get_file(PyObject *pathname, PyObject *fob, char *mode)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3610 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3611 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3612 if (mode[0] == 'U')
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3613 mode = "r" PY_STDIOTEXTMODE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3614 if (fob == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3615 fp = _Py_fopen(pathname, mode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3616 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3617 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3618 int fd = PyObject_AsFileDescriptor(fob);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3619 if (fd == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3620 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3621 if (!_PyVerify_fd(fd))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3622 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3623 /* the FILE struct gets a new fd, so that it can be closed
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3624 * independently of the file descriptor given
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3625 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3626 fd = dup(fd);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3627 if (fd == -1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3628 goto error;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3629 fp = fdopen(fd, mode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3630 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3631 if (fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3632 return fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3633 error:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3634 PyErr_SetFromErrno(PyExc_IOError);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3635 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3636 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3637
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3638 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3639 imp_load_compiled(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3640 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3641 PyObject *name, *pathname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3642 PyObject *fob = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3643 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3644 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3645 if (!PyArg_ParseTuple(args, "UO&|O:load_compiled",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3646 &name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3647 PyUnicode_FSDecoder, &pathname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3648 &fob))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3649 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3650 fp = get_file(pathname, fob, "rb");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3651 if (fp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3652 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3653 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3654 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3655 m = load_compiled_module(name, pathname, fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3656 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3657 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3658 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3659 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3660
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3661 #ifdef HAVE_DYNAMIC_LOADING
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3662
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3663 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3664 imp_load_dynamic(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3665 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3666 PyObject *name, *pathname, *fob = NULL, *mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3667 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3668
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3669 if (!PyArg_ParseTuple(args, "UO&|O:load_dynamic",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3670 &name, PyUnicode_FSDecoder, &pathname, &fob))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3671 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3672 if (fob != NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3673 fp = get_file(NULL, fob, "r");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3674 if (fp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3675 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3676 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3677 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3678 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3679 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3680 fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3681 mod = _PyImport_LoadDynamicModule(name, pathname, fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3682 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3683 if (fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3684 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3685 return mod;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3686 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3687
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3688 #endif /* HAVE_DYNAMIC_LOADING */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3689
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3690 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3691 imp_load_source(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3692 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3693 PyObject *name, *pathname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3694 PyObject *fob = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3695 PyObject *m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3696 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3697 if (!PyArg_ParseTuple(args, "UO&|O:load_source",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3698 &name,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3699 PyUnicode_FSDecoder, &pathname,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3700 &fob))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3701 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3702 fp = get_file(pathname, fob, "r");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3703 if (fp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3704 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3705 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3706 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3707 m = load_source_module(name, pathname, fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3708 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3709 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3710 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3711 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3712
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3713 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3714 imp_load_module(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3715 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3716 PyObject *name, *fob, *pathname, *pathname_obj, *ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3717 char *suffix; /* Unused */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3718 char *mode;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3719 int type;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3720 FILE *fp;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3721
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3722 if (!PyArg_ParseTuple(args, "UOO(ssi):load_module",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3723 &name, &fob, &pathname_obj, &suffix, &mode, &type))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3724 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3725 if (pathname_obj != Py_None) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3726 if (!PyUnicode_FSDecoder(pathname_obj, &pathname))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3727 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3728 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3729 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3730 pathname = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3731
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3732 if (*mode) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3733 /* Mode must start with 'r' or 'U' and must not contain '+'.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3734 Implicit in this test is the assumption that the mode
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3735 may contain other modifiers like 'b' or 't'. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3736
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3737 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3738 PyErr_Format(PyExc_ValueError,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3739 "invalid file open mode %.200s", mode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3740 Py_XDECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3741 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3742 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3743 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3744 if (fob == Py_None)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3745 fp = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3746 else {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3747 fp = get_file(NULL, fob, mode);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3748 if (fp == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3749 Py_XDECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3750 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3751 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3752 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3753 ret = load_module(name, fp, pathname, type, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3754 Py_XDECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3755 if (fp)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3756 fclose(fp);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3757 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3758 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3759
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3760 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3761 imp_load_package(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3762 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3763 PyObject *name, *pathname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3764 PyObject * ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3765 if (!PyArg_ParseTuple(args, "UO&:load_package",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3766 &name, PyUnicode_FSDecoder, &pathname))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3767 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3768 ret = load_package(name, pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3769 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3770 return ret;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3771 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3772
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3773 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3774 imp_new_module(PyObject *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3775 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3776 PyObject *name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3777 if (!PyArg_ParseTuple(args, "U:new_module", &name))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3778 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3779 return PyModule_NewObject(name);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3780 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3781
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3782 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3783 imp_reload(PyObject *self, PyObject *v)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3784 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3785 return PyImport_ReloadModule(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3786 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3787
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3788 PyDoc_STRVAR(doc_reload,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3789 "reload(module) -> module\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3790 \n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3791 Reload the module. The module must have been successfully imported before.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3792
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3793 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3794 imp_cache_from_source(PyObject *self, PyObject *args, PyObject *kws)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3795 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3796 static char *kwlist[] = {"path", "debug_override", NULL};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3797
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3798 PyObject *pathname, *cpathname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3799 PyObject *debug_override = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3800 int debug = !Py_OptimizeFlag;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3801
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3802 if (!PyArg_ParseTupleAndKeywords(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3803 args, kws, "O&|O", kwlist,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3804 PyUnicode_FSDecoder, &pathname, &debug_override))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3805 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3806
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3807 if (debug_override != NULL &&
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3808 (debug = PyObject_IsTrue(debug_override)) < 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3809 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3810 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3811 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3812
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3813 if (PyUnicode_READY(pathname) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3814 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3815
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3816 cpathname = make_compiled_pathname(pathname, debug);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3817 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3818
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3819 if (cpathname == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3820 PyErr_Format(PyExc_SystemError, "path buffer too short");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3821 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3822 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3823 return cpathname;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3824 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3825
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3826 PyDoc_STRVAR(doc_cache_from_source,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3827 "cache_from_source(path, [debug_override]) -> path\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3828 Given the path to a .py file, return the path to its .pyc/.pyo file.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3829 \n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3830 The .py file does not need to exist; this simply returns the path to the\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3831 .pyc/.pyo file calculated as if the .py file were imported. The extension\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3832 will be .pyc unless __debug__ is not defined, then it will be .pyo.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3833 \n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3834 If debug_override is not None, then it must be a boolean and is taken as\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3835 the value of __debug__ instead.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3836
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3837 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3838 imp_source_from_cache(PyObject *self, PyObject *args, PyObject *kws)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3839 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3840 static char *kwlist[] = {"path", NULL};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3841 PyObject *pathname, *source;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3842
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3843 if (!PyArg_ParseTupleAndKeywords(
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3844 args, kws, "O&", kwlist,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3845 PyUnicode_FSDecoder, &pathname))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3846 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3847
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3848 source = make_source_pathname(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3849 if (source == NULL) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3850 PyErr_Format(PyExc_ValueError, "Not a PEP 3147 pyc path: %R",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3851 pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3852 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3853 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3854 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3855 Py_DECREF(pathname);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3856 return source;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3857 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3858
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3859 PyDoc_STRVAR(doc_source_from_cache,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3860 "source_from_cache(path) -> path\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3861 Given the path to a .pyc./.pyo file, return the path to its .py file.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3862 \n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3863 The .pyc/.pyo file does not need to exist; this simply returns the path to\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3864 the .py file calculated to correspond to the .pyc/.pyo file. If path\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3865 does not conform to PEP 3147 format, ValueError will be raised.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3866
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3867 /* Doc strings */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3868
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3869 PyDoc_STRVAR(doc_imp,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3870 "This module provides the components needed to build your own\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3871 __import__ function. Undocumented functions are obsolete.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3872
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3873 PyDoc_STRVAR(doc_find_module,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3874 "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3875 Search for a module. If path is omitted or None, search for a\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3876 built-in, frozen or special module and continue search in sys.path.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3877 The module name cannot contain '.'; to search for a submodule of a\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3878 package, pass the submodule name and the package's __path__.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3879
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3880 PyDoc_STRVAR(doc_load_module,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3881 "load_module(name, file, filename, (suffix, mode, type)) -> module\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3882 Load a module, given information returned by find_module().\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3883 The module name must include the full package name, if any.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3884
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3885 PyDoc_STRVAR(doc_get_magic,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3886 "get_magic() -> string\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3887 Return the magic number for .pyc or .pyo files.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3888
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3889 PyDoc_STRVAR(doc_get_tag,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3890 "get_tag() -> string\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3891 Return the magic tag for .pyc or .pyo files.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3892
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3893 PyDoc_STRVAR(doc_get_suffixes,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3894 "get_suffixes() -> [(suffix, mode, type), ...]\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3895 Return a list of (suffix, mode, type) tuples describing the files\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3896 that find_module() looks for.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3897
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3898 PyDoc_STRVAR(doc_new_module,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3899 "new_module(name) -> module\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3900 Create a new module. Do not enter it in sys.modules.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3901 The module name must include the full package name, if any.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3902
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3903 PyDoc_STRVAR(doc_lock_held,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3904 "lock_held() -> boolean\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3905 Return True if the import lock is currently held, else False.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3906 On platforms without threads, return False.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3907
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3908 PyDoc_STRVAR(doc_acquire_lock,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3909 "acquire_lock() -> None\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3910 Acquires the interpreter's import lock for the current thread.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3911 This lock should be used by import hooks to ensure thread-safety\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3912 when importing modules.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3913 On platforms without threads, this function does nothing.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3914
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3915 PyDoc_STRVAR(doc_release_lock,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3916 "release_lock() -> None\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3917 Release the interpreter's import lock.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3918 On platforms without threads, this function does nothing.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3919
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3920 static PyMethodDef imp_methods[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3921 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3922 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3923 {"get_tag", imp_get_tag, METH_NOARGS, doc_get_tag},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3924 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3925 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3926 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3927 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3928 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3929 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3930 {"reload", imp_reload, METH_O, doc_reload},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3931 {"cache_from_source", (PyCFunction)imp_cache_from_source,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3932 METH_VARARGS | METH_KEYWORDS, doc_cache_from_source},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3933 {"source_from_cache", (PyCFunction)imp_source_from_cache,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3934 METH_VARARGS | METH_KEYWORDS, doc_source_from_cache},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3935 /* The rest are obsolete */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3936 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3937 {"is_frozen_package", imp_is_frozen_package, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3938 {"init_builtin", imp_init_builtin, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3939 {"init_frozen", imp_init_frozen, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3940 {"is_builtin", imp_is_builtin, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3941 {"is_frozen", imp_is_frozen, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3942 {"load_compiled", imp_load_compiled, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3943 #ifdef HAVE_DYNAMIC_LOADING
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3944 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3945 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3946 {"load_package", imp_load_package, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3947 {"load_source", imp_load_source, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3948 {"_fix_co_filename", imp_fix_co_filename, METH_VARARGS},
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3949 {NULL, NULL} /* sentinel */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3950 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3951
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3952 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3953 setint(PyObject *d, char *name, int value)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3954 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3955 PyObject *v;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3956 int err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3957
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3958 v = PyLong_FromLong((long)value);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3959 err = PyDict_SetItemString(d, name, v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3960 Py_XDECREF(v);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3961 return err;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3962 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3963
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3964 typedef struct {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3965 PyObject_HEAD
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3966 } NullImporter;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3967
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3968 static int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3969 NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3970 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3971 #ifndef MS_WINDOWS
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3972 PyObject *path;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3973 struct stat statbuf;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3974 int rv;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3975
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3976 if (!_PyArg_NoKeywords("NullImporter()", kwds))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3977 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3978
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3979 if (!PyArg_ParseTuple(args, "O&:NullImporter",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3980 PyUnicode_FSConverter, &path))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3981 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3982
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3983 if (PyBytes_GET_SIZE(path) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3984 Py_DECREF(path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3985 PyErr_SetString(PyExc_ImportError, "empty pathname");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3986 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3987 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3988
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3989 rv = stat(PyBytes_AS_STRING(path), &statbuf);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3990 Py_DECREF(path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3991 if (rv == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3992 /* it exists */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3993 if (S_ISDIR(statbuf.st_mode)) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3994 /* it's a directory */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3995 PyErr_SetString(PyExc_ImportError, "existing directory");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3996 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3997 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3998 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3999 #else /* MS_WINDOWS */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4000 PyObject *pathobj;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4001 DWORD rv;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4002 wchar_t *path;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4003
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4004 if (!_PyArg_NoKeywords("NullImporter()", kwds))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4005 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4006
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4007 if (!PyArg_ParseTuple(args, "U:NullImporter",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4008 &pathobj))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4009 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4010
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4011 if (PyUnicode_GET_LENGTH(pathobj) == 0) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4012 PyErr_SetString(PyExc_ImportError, "empty pathname");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4013 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4014 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4015
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4016 path = PyUnicode_AsWideCharString(pathobj, NULL);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4017 if (path == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4018 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4019 /* see issue1293 and issue3677:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4020 * stat() on Windows doesn't recognise paths like
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4021 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4022 */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4023 rv = GetFileAttributesW(path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4024 PyMem_Free(path);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4025 if (rv != INVALID_FILE_ATTRIBUTES) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4026 /* it exists */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4027 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4028 /* it's a directory */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4029 PyErr_SetString(PyExc_ImportError, "existing directory");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4030 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4031 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4032 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4033 #endif
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4034 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4035 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4036
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4037 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4038 NullImporter_find_module(NullImporter *self, PyObject *args)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4039 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4040 Py_RETURN_NONE;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4041 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4042
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4043 static PyMethodDef NullImporter_methods[] = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4044 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4045 "Always return None"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4046 },
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4047 {NULL} /* Sentinel */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4048 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4049
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4050
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4051 PyTypeObject PyNullImporter_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4052 PyVarObject_HEAD_INIT(NULL, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4053 "imp.NullImporter", /*tp_name*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4054 sizeof(NullImporter), /*tp_basicsize*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4055 0, /*tp_itemsize*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4056 0, /*tp_dealloc*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4057 0, /*tp_print*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4058 0, /*tp_getattr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4059 0, /*tp_setattr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4060 0, /*tp_reserved*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4061 0, /*tp_repr*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4062 0, /*tp_as_number*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4063 0, /*tp_as_sequence*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4064 0, /*tp_as_mapping*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4065 0, /*tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4066 0, /*tp_call*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4067 0, /*tp_str*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4068 0, /*tp_getattro*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4069 0, /*tp_setattro*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4070 0, /*tp_as_buffer*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4071 Py_TPFLAGS_DEFAULT, /*tp_flags*/
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4072 "Null importer object", /* tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4073 0, /* tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4074 0, /* tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4075 0, /* tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4076 0, /* tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4077 0, /* tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4078 0, /* tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4079 NullImporter_methods, /* tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4080 0, /* tp_members */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4081 0, /* tp_getset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4082 0, /* tp_base */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4083 0, /* tp_dict */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4084 0, /* tp_descr_get */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4085 0, /* tp_descr_set */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4086 0, /* tp_dictoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4087 (initproc)NullImporter_init, /* tp_init */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4088 0, /* tp_alloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4089 PyType_GenericNew /* tp_new */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4090 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4091
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4092 static struct PyModuleDef impmodule = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4093 PyModuleDef_HEAD_INIT,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4094 "imp",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4095 doc_imp,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4096 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4097 imp_methods,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4098 NULL,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4099 NULL,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4100 NULL,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4101 NULL
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4102 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4103
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4104 PyMODINIT_FUNC
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4105 PyInit_imp(void)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4106 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4107 PyObject *m, *d;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4108
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4109 if (PyType_Ready(&PyNullImporter_Type) < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4110 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4111
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4112 m = PyModule_Create(&impmodule);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4113 if (m == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4114 goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4115 d = PyModule_GetDict(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4116 if (d == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4117 goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4118
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4119 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4120 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4121 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4122 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4123 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4124 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4125 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4126 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4127 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4128 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4129
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4130 Py_INCREF(&PyNullImporter_Type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4131 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4132 return m;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4133 failure:
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4134 Py_XDECREF(m);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4135 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4136 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4137
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4138
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4139 /* API for embedding applications that want to add their own entries
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4140 to the table of built-in modules. This should normally be called
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4141 *before* Py_Initialize(). When the table resize fails, -1 is
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4142 returned and the existing table is unchanged.
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4143
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4144 After a similar function by Just van Rossum. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4145
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4146 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4147 PyImport_ExtendInittab(struct _inittab *newtab)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4148 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4149 static struct _inittab *our_copy = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4150 struct _inittab *p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4151 int i, n;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4152
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4153 /* Count the number of entries in both tables */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4154 for (n = 0; newtab[n].name != NULL; n++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4155 ;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4156 if (n == 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4157 return 0; /* Nothing to do */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4158 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4159 ;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4160
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4161 /* Allocate new memory for the combined table */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4162 p = our_copy;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4163 PyMem_RESIZE(p, struct _inittab, i+n+1);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4164 if (p == NULL)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4165 return -1;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4166
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4167 /* Copy the tables into the new memory */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4168 if (our_copy != PyImport_Inittab)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4169 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4170 PyImport_Inittab = our_copy = p;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4171 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4172
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4173 return 0;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4174 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4175
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4176 /* Shorthand to add a single entry given a name and a function */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4177
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4178 int
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4179 PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4180 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4181 struct _inittab newtab[2];
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4182
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4183 memset(newtab, '\0', sizeof newtab);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4184
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4185 newtab[0].name = (char *)name;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4186 newtab[0].initfunc = initfunc;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4187
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4188 return PyImport_ExtendInittab(newtab);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4189 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4190