annotate Python/import.c @ 160:afa48a96a444 legacy-trunk

Create code string with initial size of 1000 instead of 0.
author Guido van Rossum <guido@python.org>
date Wed, 03 Apr 1991 19:00:23 +0000
parents 853b736a6a9e
children 28923e44254c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
1 /***********************************************************
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
3 Netherlands.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
6
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
8 documentation for any purpose and without fee is hereby granted,
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
9 provided that the above copyright notice appear in all copies and that
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
10 both that copyright notice and this permission notice appear in
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
13 distribution of the software without specific, written prior permission.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
14
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
22
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
23 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 141
diff changeset
24
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 /* Module definition and import implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
27 #include "allobjects.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 #include "node.h"
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 #include "token.h"
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 #include "graminit.h"
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 #include "import.h"
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 #include "errcode.h"
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include "sysmodule.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
35 #include "pythonrun.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
37 /* Define pathname separator used in file names */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 #ifdef THINK_C
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 #define SEP ':'
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 #endif
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 #ifndef SEP
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 #define SEP '/'
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 #endif
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
47 static object *modules;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
48
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
49 /* Initialization */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 void
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 initimport()
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
54 if ((modules = newdictobject()) == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
55 fatal("no mem for dictionary of modules");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
59 get_modules()
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
61 return modules;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
64 object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
65 add_module(name)
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
66 char *name;
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
67 {
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
68 object *m;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
69 if ((m = dictlookup(modules, name)) != NULL && is_moduleobject(m))
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
70 return m;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
71 m = newmoduleobject(name);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
72 if (m == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
74 if (dictinsert(modules, name, m) != 0) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
75 DECREF(m);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
76 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
78 DECREF(m); /* Yes, it still exists, in modules! */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
79 return m;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 static FILE *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
83 open_module(name, suffix, namebuf)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 char *suffix;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
86 char *namebuf; /* XXX No buffer overflow checks! */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 object *path;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 path = sysget("path");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if (path == NULL || !is_listobject(path)) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 strcpy(namebuf, name);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 strcat(namebuf, suffix);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 fp = fopen(namebuf, "r");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 else {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 int npath = getlistsize(path);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 fp = NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 for (i = 0; i < npath; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 object *v = getlistitem(path, i);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 int len;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 if (!is_stringobject(v))
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 continue;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 strcpy(namebuf, getstringvalue(v));
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 len = getstringsize(v);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 if (len > 0 && namebuf[len-1] != SEP)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 namebuf[len++] = SEP;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
110 strcpy(namebuf+len, name);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
111 strcat(namebuf, suffix);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 fp = fopen(namebuf, "r");
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 if (fp != NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 break;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 return fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 static object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
121 get_module(m, name, m_ret)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
122 /*module*/object *m;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 char *name;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
124 object **m_ret;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
126 object *d;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 FILE *fp;
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
128 node *n;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 int err;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
130 char namebuf[256];
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
132 fp = open_module(name, ".py", namebuf);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
133 if (fp == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
134 if (m == NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
135 err_setstr(NameError, name);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
136 else
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
137 err_setstr(RuntimeError, "no module source file");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
140 err = parse_file(fp, namebuf, file_input, &n);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 fclose(fp);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 if (err != E_DONE) {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
143 err_input(err);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
146 if (m == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
147 m = add_module(name);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
148 if (m == NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
149 freetree(n);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
150 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
151 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
152 *m_ret = m;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
153 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
154 d = getmoduledict(m);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
155 return run_node(n, namebuf, d, d);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
156 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
157
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
158 static object *
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
159 load_module(name)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
160 char *name;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
161 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
162 object *m, *v;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
163 v = get_module((object *)NULL, name, &m);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
164 if (v == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 return NULL;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
166 DECREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 return m;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
171 import_module(name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 char *name;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 object *m;
141
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
175 if ((m = dictlookup(modules, name)) == NULL) {
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
176 if (init_builtin(name)) {
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
177 if ((m = dictlookup(modules, name)) == NULL)
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
178 err_setstr(SystemError, "builtin module missing");
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
179 }
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
180 else {
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
181 m = load_module(name);
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
182 }
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
183 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 return m;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 }
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
186
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
187 object *
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
188 reload_module(m)
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
189 object *m;
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
190 {
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
191 if (m == NULL || !is_moduleobject(m)) {
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
192 err_setstr(TypeError, "reload() argument must be module");
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
193 return NULL;
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
194 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
195 /* XXX Ought to check for builtin modules -- can't reload these... */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
196 return get_module(m, getmodulename(m), (object **)NULL);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
197 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
198
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
199 static void
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
200 cleardict(d)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
201 object *d;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
202 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
203 int i;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
204 for (i = getdictsize(d); --i >= 0; ) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
205 char *k;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
206 k = getdictkey(d, i);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
207 if (k != NULL)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
208 (void) dictremove(d, k);
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
209 }
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
210 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
211
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
212 void
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
213 doneimport()
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
214 {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
215 if (modules != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
216 int i;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
217 /* Explicitly erase all modules; this is the safest way
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
218 to get rid of at least *some* circular dependencies */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
219 for (i = getdictsize(modules); --i >= 0; ) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
220 char *k;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
221 k = getdictkey(modules, i);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
222 if (k != NULL) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
223 object *m;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
224 m = dictlookup(modules, k);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
225 if (m != NULL && is_moduleobject(m)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
226 object *d;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
227 d = getmoduledict(m);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
228 if (d != NULL && is_dictobject(d)) {
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
229 cleardict(d);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
230 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
231 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
232 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
233 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
234 cleardict(modules);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
235 }
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
236 DECREF(modules);
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
237 }
141
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
238
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
239
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
240 /* Initialize built-in modules when first imported */
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
241
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
242 extern struct {
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
243 char *name;
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
244 void (*initfunc)();
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
245 } inittab[];
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
246
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
247 static int
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
248 init_builtin(name)
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
249 char *name;
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
250 {
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
251 int i;
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
252 for (i = 0; inittab[i].name != NULL; i++) {
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
253 if (strcmp(name, inittab[i].name) == 0) {
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
254 (*inittab[i].initfunc)();
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
255 return 1;
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
256 }
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
257 }
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
258 return 0;
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
259 }