annotate Python/import.c @ 62743:2145593d108d legacy-trunk v2.7

move sections
author Benjamin Peterson <benjamin@python.org>
date Sat, 03 Jul 2010 13:57:30 +0000
parents 62c0e5f14138
children b36cb4602e21
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
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Module definition and import implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
4 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
6 #include "Python-ast.h"
41526
708c8c5fc173 Make pythoncore compile cleanly with VisualStudio 2005. Used an explicit typecast to get a 64 bit integer, and undefined the Yield macro that conflicts with winbase.h
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 41178
diff changeset
7 #undef Yield /* undefine macro conflicting with winbase.h */
36065
ea083efdc96c Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
8 #include "pyarena.h"
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
9 #include "pythonrun.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 #include "errcode.h"
263
28f0ffd9facf Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents: 228
diff changeset
11 #include "marshal.h"
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
12 #include "code.h"
263
28f0ffd9facf Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents: 228
diff changeset
13 #include "compile.h"
926
c156d4960c2b * Makefile: cosmetics
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
14 #include "eval.h"
675
5aff9f8fd299 Move SEP to osdefs.h. Use MAXPATHLEN from osdefs.h.
Guido van Rossum <guido@python.org>
parents: 630
diff changeset
15 #include "osdefs.h"
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
16 #include "importdl.h"
263
28f0ffd9facf Support ".pyc" files: cached compilation results.
Guido van Rossum <guido@python.org>
parents: 228
diff changeset
17
14708
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
18 #ifdef HAVE_FCNTL_H
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
19 #include <fcntl.h>
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
20 #endif
37655
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37488
diff changeset
21 #ifdef __cplusplus
61269
fff439f089fe Fix whitespace.
Brett Cannon <bcannon@gmail.com>
parents: 61268
diff changeset
22 extern "C" {
37655
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37488
diff changeset
23 #endif
14708
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
24
45492
97a1972b672e MS Windows doesn't have mode_t but stat.st_mode is defined as unsigned short.
Christian Heimes <christian@cheimes.de>
parents: 45470
diff changeset
25 #ifdef MS_WINDOWS
97a1972b672e MS Windows doesn't have mode_t but stat.st_mode is defined as unsigned short.
Christian Heimes <christian@cheimes.de>
parents: 45470
diff changeset
26 /* for stat.st_mode */
97a1972b672e MS Windows doesn't have mode_t but stat.st_mode is defined as unsigned short.
Christian Heimes <christian@cheimes.de>
parents: 45470
diff changeset
27 typedef unsigned short mode_t;
97a1972b672e MS Windows doesn't have mode_t but stat.st_mode is defined as unsigned short.
Christian Heimes <christian@cheimes.de>
parents: 45470
diff changeset
28 #endif
97a1972b672e MS Windows doesn't have mode_t but stat.st_mode is defined as unsigned short.
Christian Heimes <christian@cheimes.de>
parents: 45470
diff changeset
29
1351
e149c77d173c Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
30
31851
adb42a711696 Bump the magic number to avoid sharing bytecode between 2.3 and 2.4.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31824
diff changeset
31 /* Magic word to reject .pyc files generated by other Python versions.
adb42a711696 Bump the magic number to avoid sharing bytecode between 2.3 and 2.4.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31824
diff changeset
32 It should change for each incompatible change to the bytecode.
adb42a711696 Bump the magic number to avoid sharing bytecode between 2.3 and 2.4.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31824
diff changeset
33
adb42a711696 Bump the magic number to avoid sharing bytecode between 2.3 and 2.4.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31824
diff changeset
34 The value of CR and LF is incorporated so if you ever read or write
2812
fb3daf9b2456 new MAGIC; some changes to default files for imp.load_... functions
Guido van Rossum <guido@python.org>
parents: 2749
diff changeset
35 a .pyc file in text mode the magic number will be wrong; also, the
21010
4a9a9cc041de Since the MAGIC number scheme is going to break on January 1st, document
Tim Peters <tim.peters@gmail.com>
parents: 20768
diff changeset
36 Apple MPW compiler swaps their values, botching string constants.
21886
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
37
32422
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32294
diff changeset
38 The magic numbers must be spaced apart atleast 2 values, as the
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32294
diff changeset
39 -U interpeter flag will cause MAGIC+1 being used. They have been
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32294
diff changeset
40 odd numbers for some time now.
21886
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
41
31851
adb42a711696 Bump the magic number to avoid sharing bytecode between 2.3 and 2.4.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31824
diff changeset
42 There were a variety of old schemes for setting the magic number.
adb42a711696 Bump the magic number to avoid sharing bytecode between 2.3 and 2.4.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31824
diff changeset
43 The current working scheme is to increment the previous value by
adb42a711696 Bump the magic number to avoid sharing bytecode between 2.3 and 2.4.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 31824
diff changeset
44 10.
25085
5fe4d93df429 Because MWH changed the bytecode again, moved the magic number *back*
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
45
21886
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
46 Known values:
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
47 Python 1.5: 20121
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
48 Python 1.5.1: 20121
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
49 Python 1.5.2: 20121
37209
c844305aa8ed Found this in an old email message from Hartmut Goebel.
Skip Montanaro <skip@pobox.com>
parents: 37086
diff changeset
50 Python 1.6: 50428
21886
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
51 Python 2.0: 50823
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
52 Python 2.0.1: 50823
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
53 Python 2.1: 60202
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
54 Python 2.1.1: 60202
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
55 Python 2.1.2: 60202
abedb2418f62 Fix to the UTF-8 encoder: it failed on 0-length input strings.
Marc-André Lemburg <mal@egenix.com>
parents: 21712
diff changeset
56 Python 2.2: 60717
23851
d6ae544acd24 Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
57 Python 2.3a0: 62011
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24071
diff changeset
58 Python 2.3a0: 62021
25085
5fe4d93df429 Because MWH changed the bytecode again, moved the magic number *back*
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
59 Python 2.3a0: 62011 (!)
32422
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32294
diff changeset
60 Python 2.4a0: 62041
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 32882
diff changeset
61 Python 2.4a3: 62051
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33248
diff changeset
62 Python 2.4b1: 62061
35008
44e3f21f0525 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34961
diff changeset
63 Python 2.5a0: 62071
35757
eb15cdd4a2f1 the ast-branch changed the stack discipline of MAKE_CLOSURE, so we need to
Michael W. Hudson <mwh@python.net>
parents: 35742
diff changeset
64 Python 2.5a0: 62081 (ast-branch)
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36501
diff changeset
65 Python 2.5a0: 62091 (with)
36926
fc10ff322df1 Um, I thought I'd already checked this in.
Guido van Rossum <guido@python.org>
parents: 36866
diff changeset
66 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
39300
b745b3fd66a6 Patch #1531113: Fix augmented assignment with yield expressions.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39253
diff changeset
67 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
b745b3fd66a6 Patch #1531113: Fix augmented assignment with yield expressions.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39253
diff changeset
68 Python 2.5b3: 62111 (fix wrong code: x += yield)
39365
75bf77367da3 There were really two issues
Neal Norwitz <nnorwitz@gmail.com>
parents: 39363
diff changeset
69 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
70 storing constants that should have been removed)
39753
8dcb882ec3e1 Bug #1520864 (again): unpacking singleton tuples in list comprehensions and
Neal Norwitz <nnorwitz@gmail.com>
parents: 39468
diff changeset
71 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
44334
07aa19170a67 Speed-up dictionary constructor by about 10%.
Raymond Hettinger <python@rcn.com>
parents: 44116
diff changeset
72 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
45703
343597c7d682 Speed up with statements by storing the __exit__ method on the stack instead of in a temp variable (bumps the magic number for pyc files)
Nick Coghlan <ncoghlan@gmail.com>
parents: 45492
diff changeset
73 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
50204
94e79d78dff0 Issue #2183: Simplify and optimize bytecode for list comprehensions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48920
diff changeset
74 Python 2.7a0: 62171 (optimize list comprehensions/change LIST_APPEND)
51920
ef8fe9088696 Backport r69961 to trunk, replacing JUMP_IF_{TRUE,FALSE} with
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 50671
diff changeset
75 Python 2.7a0: 62181 (optimize conditional branches:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
76 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
54329
145376df3ad7 add a SETUP_WITH opcode
Benjamin Peterson <benjamin@python.org>
parents: 52720
diff changeset
77 Python 2.7a0 62191 (introduce SETUP_WITH)
58229
c2fdf25329ff Issue #2335: Backport set literals syntax from Python 3.x.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 56007
diff changeset
78 Python 2.7a0 62201 (introduce BUILD_SET)
58250
edfed0e32ced Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 58229
diff changeset
79 Python 2.7a0 62211 (introduce MAP_ADD and SET_ADD)
35757
eb15cdd4a2f1 the ast-branch changed the stack discipline of MAKE_CLOSURE, so we need to
Michael W. Hudson <mwh@python.net>
parents: 35742
diff changeset
80 .
21010
4a9a9cc041de Since the MAGIC number scheme is going to break on January 1st, document
Tim Peters <tim.peters@gmail.com>
parents: 20768
diff changeset
81 */
58250
edfed0e32ced Issue #2333: Backport set and dict comprehensions syntax.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 58229
diff changeset
82 #define MAGIC (62211 | ((long)'\r'<<16) | ((long)'\n'<<24))
487
77b80a91d357 New magic word; and check it.
Guido van Rossum <guido@python.org>
parents: 340
diff changeset
83
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
84 /* Magic word as global; note that _PyImport_Init() can change the
13181
6f4669814884 another typo caught by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
85 value of this global to accommodate for alterations of how the
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
86 compiler works which are enabled by command line switches. */
46138
a0ea3a0efc71 Build bots are working again - removing the hack
Christian Heimes <christian@cheimes.de>
parents: 46136
diff changeset
87 static long pyc_magic = MAGIC;
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
88
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
89 /* See _PyImport_FixupExtension() below */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
90 static PyObject *extensions = NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
91
6541
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
92 /* This table is defined in config.c: */
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
93 extern struct _inittab _PyImport_Inittab[];
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
94
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
95 struct _inittab *PyImport_Inittab = _PyImport_Inittab;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
96
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
97 /* these tables define the module suffixes that Python recognizes */
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
98 struct filedescr * _PyImport_Filetab = NULL;
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
99
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
100 #ifdef RISCOS
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
101 static const struct filedescr _PyImport_StandardFiletab[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
102 {"/py", "U", PY_SOURCE},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
103 {"/pyc", "rb", PY_COMPILED},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
104 {0, 0}
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
105 };
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
106 #else
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
107 static const struct filedescr _PyImport_StandardFiletab[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
108 {".py", "U", PY_SOURCE},
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23879
diff changeset
109 #ifdef MS_WINDOWS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
110 {".pyw", "U", PY_SOURCE},
21010
4a9a9cc041de Since the MAGIC number scheme is going to break on January 1st, document
Tim Peters <tim.peters@gmail.com>
parents: 20768
diff changeset
111 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
112 {".pyc", "rb", PY_COMPILED},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
113 {0, 0}
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
114 };
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
115 #endif
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
116
39253
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
117
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
118 /* Initialize things */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
119
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
120 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
121 _PyImport_Init(void)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
122 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
123 const struct filedescr *scan;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
124 struct filedescr *filetab;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
125 int countD = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
126 int countS = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
127
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
128 /* prepare _PyImport_Filetab: copy entries from
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
129 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
130 */
42734
9087337f3a3f Second half of #1752175: #ifdef out references to PyImport_DynLoadFiletab if HAVE_DYNAMIC_LOADING is not defined.
Georg Brandl <georg@python.org>
parents: 42688
diff changeset
131 #ifdef HAVE_DYNAMIC_LOADING
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
132 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
133 ++countD;
42734
9087337f3a3f Second half of #1752175: #ifdef out references to PyImport_DynLoadFiletab if HAVE_DYNAMIC_LOADING is not defined.
Georg Brandl <georg@python.org>
parents: 42688
diff changeset
134 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
135 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
136 ++countS;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
137 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
138 if (filetab == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
139 Py_FatalError("Can't initialize import file table.");
42734
9087337f3a3f Second half of #1752175: #ifdef out references to PyImport_DynLoadFiletab if HAVE_DYNAMIC_LOADING is not defined.
Georg Brandl <georg@python.org>
parents: 42688
diff changeset
140 #ifdef HAVE_DYNAMIC_LOADING
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
141 memcpy(filetab, _PyImport_DynLoadFiletab,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
142 countD * sizeof(struct filedescr));
42734
9087337f3a3f Second half of #1752175: #ifdef out references to PyImport_DynLoadFiletab if HAVE_DYNAMIC_LOADING is not defined.
Georg Brandl <georg@python.org>
parents: 42688
diff changeset
143 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
144 memcpy(filetab + countD, _PyImport_StandardFiletab,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
145 countS * sizeof(struct filedescr));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
146 filetab[countD + countS].suffix = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
147
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
148 _PyImport_Filetab = filetab;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
149
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
150 if (Py_OptimizeFlag) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
151 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
152 for (; filetab->suffix != NULL; filetab++) {
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
153 #ifndef RISCOS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
154 if (strcmp(filetab->suffix, ".pyc") == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
155 filetab->suffix = ".pyo";
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
156 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
157 if (strcmp(filetab->suffix, "/pyc") == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
158 filetab->suffix = "/pyo";
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
159 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
160 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
161 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
162
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
163 if (Py_UnicodeFlag) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
164 /* Fix the pyc_magic so that byte compiled code created
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
165 using the all-Unicode method doesn't interfere with
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
166 code created in normal operation mode. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
167 pyc_magic = MAGIC + 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
168 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
169 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
170
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
171 void
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
172 _PyImportHooks_Init(void)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
173 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
174 PyObject *v, *path_hooks = NULL, *zimpimport;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
175 int err = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
176
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
177 /* adding sys.path_hooks and sys.path_importer_cache, setting up
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
178 zipimport */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
179 if (PyType_Ready(&PyNullImporter_Type) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
180 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
181
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
182 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
183 PySys_WriteStderr("# installing zipimport hook\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
184
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
185 v = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
186 if (v == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
187 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
188 err = PySys_SetObject("meta_path", v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
189 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
190 if (err)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
191 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
192 v = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
193 if (v == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
194 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
195 err = PySys_SetObject("path_importer_cache", v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
196 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
197 if (err)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
198 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
199 path_hooks = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
200 if (path_hooks == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
201 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
202 err = PySys_SetObject("path_hooks", path_hooks);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
203 if (err) {
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
204 error:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
205 PyErr_Print();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
206 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
207 "path_importer_cache, or NullImporter failed"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
208 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
209 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
210
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
211 zimpimport = PyImport_ImportModule("zipimport");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
212 if (zimpimport == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
213 PyErr_Clear(); /* No zip import module -- okay */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
214 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
215 PySys_WriteStderr("# can't import zipimport\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
216 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
217 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
218 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
219 "zipimporter");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
220 Py_DECREF(zimpimport);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
221 if (zipimporter == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
222 PyErr_Clear(); /* No zipimporter object -- okay */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
223 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
224 PySys_WriteStderr(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
225 "# can't import zipimport.zipimporter\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
226 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
227 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
228 /* sys.path_hooks.append(zipimporter) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
229 err = PyList_Append(path_hooks, zipimporter);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
230 Py_DECREF(zipimporter);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
231 if (err)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
232 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
233 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
234 PySys_WriteStderr(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
235 "# installed zipimport hook\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
236 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
237 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
238 Py_DECREF(path_hooks);
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
239 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
240
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
241 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
242 _PyImport_Fini(void)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
243 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
244 Py_XDECREF(extensions);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
245 extensions = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
246 PyMem_DEL(_PyImport_Filetab);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
247 _PyImport_Filetab = NULL;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
248 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
249
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
250
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
251 /* Locking primitives to prevent parallel imports of the same module
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
252 in different threads to return with a partially loaded module.
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
253 These calls are serialized by the global interpreter lock. */
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
254
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
255 #ifdef WITH_THREAD
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
256
9203
07dae6fdae79 Renamed thread.h to pythread.h.
Guido van Rossum <guido@python.org>
parents: 9187
diff changeset
257 #include "pythread.h"
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
258
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9431
diff changeset
259 static PyThread_type_lock import_lock = 0;
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
260 static long import_lock_thread = -1;
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
261 static int import_lock_level = 0;
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
262
56007
b0d03fa073ec Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
Thomas Wouters <thomas@python.org>
parents: 55441
diff changeset
263 void
b0d03fa073ec Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
Thomas Wouters <thomas@python.org>
parents: 55441
diff changeset
264 _PyImport_AcquireLock(void)
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
265 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
266 long me = PyThread_get_thread_ident();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
267 if (me == -1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
268 return; /* Too bad */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
269 if (import_lock == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
270 import_lock = PyThread_allocate_lock();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
271 if (import_lock == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
272 return; /* Nothing much we can do. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
273 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
274 if (import_lock_thread == me) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
275 import_lock_level++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
276 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
277 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
278 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
279 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
280 PyThreadState *tstate = PyEval_SaveThread();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
281 PyThread_acquire_lock(import_lock, 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
282 PyEval_RestoreThread(tstate);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
283 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
284 import_lock_thread = me;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
285 import_lock_level = 1;
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
286 }
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
287
56007
b0d03fa073ec Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
Thomas Wouters <thomas@python.org>
parents: 55441
diff changeset
288 int
b0d03fa073ec Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
Thomas Wouters <thomas@python.org>
parents: 55441
diff changeset
289 _PyImport_ReleaseLock(void)
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
290 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
291 long me = PyThread_get_thread_ident();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
292 if (me == -1 || import_lock == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
293 return 0; /* Too bad */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
294 if (import_lock_thread != me)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
295 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
296 import_lock_level--;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
297 if (import_lock_level == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
298 import_lock_thread = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
299 PyThread_release_lock(import_lock);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
300 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
301 return 1;
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
302 }
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
303
59210
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 58250
diff changeset
304 /* This function is called from PyOS_AfterFork to ensure that newly
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 58250
diff changeset
305 created child processes do not share locks with the parent.
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 58250
diff changeset
306 We now acquire the import lock around fork() calls but on some platforms
4afc50d15544 Issue #7242: On Solaris 9 and earlier calling os.fork() from within a
Gregory P. Smith <greg@mad-scientist.com>
parents: 58250
diff changeset
307 (Solaris 9 and earlier? see isue7242) that still left us with problems. */
35550
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
308
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
309 void
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
310 _PyImport_ReInitLock(void)
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
311 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
312 if (import_lock != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
313 import_lock = PyThread_allocate_lock();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
314 import_lock_thread = -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
315 import_lock_level = 0;
35550
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
316 }
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
317
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
318 #endif
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
319
19596
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19238
diff changeset
320 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
321 imp_lock_held(PyObject *self, PyObject *noargs)
19596
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19238
diff changeset
322 {
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19238
diff changeset
323 #ifdef WITH_THREAD
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
324 return PyBool_FromLong(import_lock_thread != -1);
19596
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19238
diff changeset
325 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
326 return PyBool_FromLong(0);
19596
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19238
diff changeset
327 #endif
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19238
diff changeset
328 }
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19238
diff changeset
329
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
330 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
331 imp_acquire_lock(PyObject *self, PyObject *noargs)
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
332 {
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
333 #ifdef WITH_THREAD
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
334 _PyImport_AcquireLock();
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
335 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
336 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
337 return Py_None;
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
338 }
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
339
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
340 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
341 imp_release_lock(PyObject *self, PyObject *noargs)
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
342 {
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
343 #ifdef WITH_THREAD
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
344 if (_PyImport_ReleaseLock() < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
345 PyErr_SetString(PyExc_RuntimeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
346 "not holding the import lock");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
347 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
348 }
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
349 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
350 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
351 return Py_None;
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
352 }
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
353
41128
45ce388ec59a Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents: 41102
diff changeset
354 static void
41139
9db3e325dd88 Fix some style nits:
Neal Norwitz <nnorwitz@gmail.com>
parents: 41128
diff changeset
355 imp_modules_reloading_clear(void)
41128
45ce388ec59a Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents: 41102
diff changeset
356 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
357 PyInterpreterState *interp = PyThreadState_Get()->interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
358 if (interp->modules_reloading != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
359 PyDict_Clear(interp->modules_reloading);
41128
45ce388ec59a Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents: 41102
diff changeset
360 }
45ce388ec59a Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Collin Winter <collinw@gmail.com>
parents: 41102
diff changeset
361
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
362 /* Helper for sys */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
363
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
364 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
365 PyImport_GetModuleDict(void)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
366 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
367 PyInterpreterState *interp = PyThreadState_GET()->interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
368 if (interp->modules == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
369 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
370 return interp->modules;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
371 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
372
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
373
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
374 /* List of names to clear in sys */
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
375 static char* sys_deletes[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
376 "path", "argv", "ps1", "ps2", "exitfunc",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
377 "exc_type", "exc_value", "exc_traceback",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
378 "last_type", "last_value", "last_traceback",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
379 "path_hooks", "path_importer_cache", "meta_path",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
380 /* misc stuff */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
381 "flags", "float_info",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
382 NULL
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
383 };
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
384
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
385 static char* sys_files[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
386 "stdin", "__stdin__",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
387 "stdout", "__stdout__",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
388 "stderr", "__stderr__",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
389 NULL
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
390 };
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
391
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
392
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
393 /* Un-initialize things, as good as we can */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
394
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
395 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
396 PyImport_Cleanup(void)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
397 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
398 Py_ssize_t pos, ndone;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
399 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
400 PyObject *key, *value, *dict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
401 PyInterpreterState *interp = PyThreadState_GET()->interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
402 PyObject *modules = interp->modules;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
403
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
404 if (modules == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
405 return; /* Already done */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
406
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
407 /* Delete some special variables first. These are common
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
408 places where user values hide and people complain when their
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
409 destructors fail. Since the modules containing them are
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
410 deleted *last* of all, they would come too late in the normal
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
411 destruction order. Sigh. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
412
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
413 value = PyDict_GetItemString(modules, "__builtin__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
414 if (value != NULL && PyModule_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
415 dict = PyModule_GetDict(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
416 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
417 PySys_WriteStderr("# clear __builtin__._\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
418 PyDict_SetItemString(dict, "_", Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
419 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
420 value = PyDict_GetItemString(modules, "sys");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
421 if (value != NULL && PyModule_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
422 char **p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
423 PyObject *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
424 dict = PyModule_GetDict(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
425 for (p = sys_deletes; *p != NULL; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
426 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
427 PySys_WriteStderr("# clear sys.%s\n", *p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
428 PyDict_SetItemString(dict, *p, Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
429 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
430 for (p = sys_files; *p != NULL; p+=2) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
431 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
432 PySys_WriteStderr("# restore sys.%s\n", *p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
433 v = PyDict_GetItemString(dict, *(p+1));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
434 if (v == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
435 v = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
436 PyDict_SetItemString(dict, *p, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
437 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
438 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
439
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
440 /* First, delete __main__ */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
441 value = PyDict_GetItemString(modules, "__main__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
442 if (value != NULL && PyModule_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
443 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
444 PySys_WriteStderr("# cleanup __main__\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
445 _PyModule_Clear(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
446 PyDict_SetItemString(modules, "__main__", Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
447 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
448
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
449 /* The special treatment of __builtin__ here is because even
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
450 when it's not referenced as a module, its dictionary is
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
451 referenced by almost every module's __builtins__. Since
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
452 deleting a module clears its dictionary (even if there are
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
453 references left to it), we need to delete the __builtin__
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
454 module last. Likewise, we don't delete sys until the very
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
455 end because it is implicitly referenced (e.g. by print).
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
456
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
457 Also note that we 'delete' modules by replacing their entry
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
458 in the modules dict with None, rather than really deleting
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
459 them; this avoids a rehash of the modules dictionary and
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
460 also marks them as "non existent" so they won't be
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
461 re-imported. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
462
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
463 /* Next, repeatedly delete modules with a reference count of
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
464 one (skipping __builtin__ and sys) and delete them */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
465 do {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
466 ndone = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
467 pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
468 while (PyDict_Next(modules, &pos, &key, &value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
469 if (value->ob_refcnt != 1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
470 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
471 if (PyString_Check(key) && PyModule_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
472 name = PyString_AS_STRING(key);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
473 if (strcmp(name, "__builtin__") == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
474 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
475 if (strcmp(name, "sys") == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
476 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
477 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
478 PySys_WriteStderr(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
479 "# cleanup[1] %s\n", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
480 _PyModule_Clear(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
481 PyDict_SetItem(modules, key, Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
482 ndone++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
483 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
484 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
485 } while (ndone > 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
486
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
487 /* Next, delete all modules (still skipping __builtin__ and sys) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
488 pos = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
489 while (PyDict_Next(modules, &pos, &key, &value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
490 if (PyString_Check(key) && PyModule_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
491 name = PyString_AS_STRING(key);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
492 if (strcmp(name, "__builtin__") == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
493 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
494 if (strcmp(name, "sys") == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
495 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
496 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
497 PySys_WriteStderr("# cleanup[2] %s\n", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
498 _PyModule_Clear(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
499 PyDict_SetItem(modules, key, Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
500 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
501 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
502
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
503 /* Next, delete sys and __builtin__ (in that order) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
504 value = PyDict_GetItemString(modules, "sys");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
505 if (value != NULL && PyModule_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
506 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
507 PySys_WriteStderr("# cleanup sys\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
508 _PyModule_Clear(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
509 PyDict_SetItemString(modules, "sys", Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
510 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
511 value = PyDict_GetItemString(modules, "__builtin__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
512 if (value != NULL && PyModule_Check(value)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
513 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
514 PySys_WriteStderr("# cleanup __builtin__\n");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
515 _PyModule_Clear(value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
516 PyDict_SetItemString(modules, "__builtin__", Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
517 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
518
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
519 /* Finally, clear and delete the modules directory */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
520 PyDict_Clear(modules);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
521 interp->modules = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
522 Py_DECREF(modules);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
523 Py_CLEAR(interp->modules_reloading);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
524 }
166
28923e44254c Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
525
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
526
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
527 /* Helper for pythonrun.c -- return magic number */
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
528
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
529 long
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
530 PyImport_GetMagicNumber(void)
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
531 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
532 return pyc_magic;
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
533 }
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
534
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
535
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
536 /* Magic for extension modules (built-in as well as dynamically
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
537 loaded). To prevent initializing an extension module more than
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
538 once, we keep a static dictionary 'extensions' keyed by module name
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
539 (for built-in modules) or by filename (for dynamically loaded
19238
5b87ee44000b Fixed typo in comment leading up to _PyImport_FixupExtension().
Barry Warsaw <barry@python.org>
parents: 19063
diff changeset
540 modules), containing these modules. A copy of the module's
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
541 dictionary is stored by calling _PyImport_FixupExtension()
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
542 immediately after the module initialization function succeeds. A
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
543 copy can be retrieved from there by calling
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
544 _PyImport_FindExtension(). */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
545
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
546 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
547 _PyImport_FixupExtension(char *name, char *filename)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
549 PyObject *modules, *mod, *dict, *copy;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
550 if (extensions == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
551 extensions = PyDict_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
552 if (extensions == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
553 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
554 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
555 modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
556 mod = PyDict_GetItemString(modules, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
557 if (mod == NULL || !PyModule_Check(mod)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
558 PyErr_Format(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
559 "_PyImport_FixupExtension: module %.200s not loaded", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
560 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
561 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
562 dict = PyModule_GetDict(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
563 if (dict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
564 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
565 copy = PyDict_Copy(dict);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
566 if (copy == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
567 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
568 PyDict_SetItemString(extensions, filename, copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
569 Py_DECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
570 return copy;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
571 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
572
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
573 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
574 _PyImport_FindExtension(char *name, char *filename)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
575 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
576 PyObject *dict, *mod, *mdict;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
577 if (extensions == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
578 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
579 dict = PyDict_GetItemString(extensions, filename);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
580 if (dict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
581 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
582 mod = PyImport_AddModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
583 if (mod == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
584 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
585 mdict = PyModule_GetDict(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
586 if (mdict == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
587 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
588 if (PyDict_Update(mdict, dict))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
589 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
590 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
591 PySys_WriteStderr("import %s # previously loaded (%s)\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
592 name, filename);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
593 return mod;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
595
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
596
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
597 /* Get the module object corresponding to a module name.
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
598 First check the modules dictionary if there's one there,
30722
651fc8d98d99 Fix a bunch of typos in documentation, docstrings and comments.
Walter Dörwald <walter@livinglogic.de>
parents: 30311
diff changeset
599 if not, create a new one and insert it in the modules dictionary.
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
600 Because the former action is most common, THIS DOES NOT RETURN A
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
601 'NEW' REFERENCE! */
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
602
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
603 PyObject *
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35757
diff changeset
604 PyImport_AddModule(const char *name)
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
605 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
606 PyObject *modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
607 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
608
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
609 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
610 PyModule_Check(m))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
611 return m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
612 m = PyModule_New(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
613 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
614 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
615 if (PyDict_SetItemString(modules, name, m) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
616 Py_DECREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
617 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
618 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
619 Py_DECREF(m); /* Yes, it still exists, in modules! */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
620
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
621 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
622 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
623
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
624 /* Remove name from sys.modules, if it's there. */
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
625 static void
60021
16a7d32605bb make naming convention consistent
Benjamin Peterson <benjamin@python.org>
parents: 59499
diff changeset
626 remove_module(const char *name)
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
627 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
628 PyObject *modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
629 if (PyDict_GetItemString(modules, name) == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
630 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
631 if (PyDict_DelItemString(modules, name) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
632 Py_FatalError("import: deleting existing key in"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
633 "sys.modules failed");
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
634 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
635
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
636 /* Execute a code object in a module and return the module object
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
637 * WITH INCREMENTED REFERENCE COUNT. If an error occurs, name is
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
638 * removed from sys.modules, to avoid leaving damaged module objects
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
639 * in sys.modules. The caller may wish to restore the original
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
640 * module object (if any) in this case; PyImport_ReloadModule is an
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
641 * example.
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
642 */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
643 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
644 PyImport_ExecCodeModule(char *name, PyObject *co)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
645 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
646 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
7232
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
647 }
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
648
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
649 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
650 PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname)
7232
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
651 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
652 PyObject *modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
653 PyObject *m, *d, *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
654
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
655 m = PyImport_AddModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
656 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
657 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
658 /* If the module is being reloaded, we get the old module back
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
659 and re-use its dict to exec the new code. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
660 d = PyModule_GetDict(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
661 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
662 if (PyDict_SetItemString(d, "__builtins__",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
663 PyEval_GetBuiltins()) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
664 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
665 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
666 /* Remember the filename as the __file__ attribute */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
667 v = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
668 if (pathname != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
669 v = PyString_FromString(pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
670 if (v == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
671 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
672 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
673 if (v == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
674 v = ((PyCodeObject *)co)->co_filename;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
675 Py_INCREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
676 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
677 if (PyDict_SetItemString(d, "__file__", v) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
678 PyErr_Clear(); /* Not important enough to report */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
679 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
680
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
681 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
682 if (v == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
683 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
684 Py_DECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
685
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
686 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
687 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
688 "Loaded module %.200s not found in sys.modules",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
689 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
690 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
691 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
692
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
693 Py_INCREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
694
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
695 return m;
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
696
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
697 error:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
698 remove_module(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
699 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
701
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
702
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
703 /* Given a pathname for a Python source file, fill a buffer with the
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
704 pathname for the corresponding compiled file. Return the pathname
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
705 for the compiled file, or NULL if there's no space in the buffer.
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
706 Doesn't set an exception. */
609
0ad5b35e3ad8 Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents: 510
diff changeset
707
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
708 static char *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
709 make_compiled_pathname(char *pathname, char *buf, size_t buflen)
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
710 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
711 size_t len = strlen(pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
712 if (len+2 > buflen)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
713 return NULL;
18992
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
714
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23879
diff changeset
715 #ifdef MS_WINDOWS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
716 /* Treat .pyw as if it were .py. The case of ".pyw" must match
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
717 that used in _PyImport_StandardFiletab. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
718 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
719 --len; /* pretend 'w' isn't there */
18992
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
720 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
721 memcpy(buf, pathname, len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
722 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
723 buf[len+1] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
724
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
725 return buf;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
726 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
727
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
728
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
729 /* Given a pathname for a Python source file, its time of last
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
730 modification, and a pathname for a compiled file, check whether the
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
731 compiled file represents the same version of the source. If so,
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
732 return a FILE pointer for the compiled file, positioned just after
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
733 the header; if not, return NULL.
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
734 Doesn't set an exception. */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
735
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
736 static FILE *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
737 check_compiled_module(char *pathname, time_t mtime, char *cpathname)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
738 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
739 FILE *fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
740 long magic;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
741 long pyc_mtime;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
742
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
743 fp = fopen(cpathname, "rb");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
744 if (fp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
745 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
746 magic = PyMarshal_ReadLongFromFile(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
747 if (magic != pyc_magic) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
748 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
749 PySys_WriteStderr("# %s has bad magic\n", cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
750 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
751 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
752 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
753 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
754 if (pyc_mtime != mtime) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
755 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
756 PySys_WriteStderr("# %s has bad mtime\n", cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
757 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
758 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
759 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
760 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
761 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
762 return fp;
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
763 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
764
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
765
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
766 /* Read a code object from a file and check it for validity */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
767
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
768 static PyCodeObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
769 read_compiled_module(char *cpathname, FILE *fp)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
770 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
771 PyObject *co;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
772
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
773 co = PyMarshal_ReadLastObjectFromFile(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
774 if (co == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
775 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
776 if (!PyCode_Check(co)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
777 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
778 "Non-code object in %.200s", cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
779 Py_DECREF(co);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
780 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
781 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
782 return (PyCodeObject *)co;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
783 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
784
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
785
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
786 /* Load a module from a compiled file, execute it, and return its
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
787 module object WITH INCREMENTED REFERENCE COUNT */
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
788
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
789 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
790 load_compiled_module(char *name, char *cpathname, FILE *fp)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
791 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
792 long magic;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
793 PyCodeObject *co;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
794 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
795
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
796 magic = PyMarshal_ReadLongFromFile(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
797 if (magic != pyc_magic) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
798 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
799 "Bad magic number in %.200s", cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
800 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
801 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
802 (void) PyMarshal_ReadLongFromFile(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
803 co = read_compiled_module(cpathname, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
804 if (co == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
805 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
806 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
807 PySys_WriteStderr("import %s # precompiled from %s\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
808 name, cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
809 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
810 Py_DECREF(co);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
811
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
812 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
813 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
814
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
815 /* Parse a source file and return the corresponding code object */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
816
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
817 static PyCodeObject *
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
818 parse_source_module(const char *pathname, FILE *fp)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
819 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
820 PyCodeObject *co = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
821 mod_ty mod;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
822 PyCompilerFlags flags;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
823 PyArena *arena = PyArena_New();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
824 if (arena == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
825 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
826
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
827 flags.cf_flags = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
828
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
829 mod = PyParser_ASTFromFile(fp, pathname, Py_file_input, 0, 0, &flags,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
830 NULL, arena);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
831 if (mod) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
832 co = PyAST_Compile(mod, pathname, NULL, arena);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
833 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
834 PyArena_Free(arena);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
835 return co;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
836 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
837
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
838
14708
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
839 /* Helper to open a bytecode file for writing in exclusive mode */
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
840
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
841 static FILE *
45470
3915d5f650fa Issue #2051 and patch from Alexander Belopolsky:
Christian Heimes <christian@cheimes.de>
parents: 45059
diff changeset
842 open_exclusive(char *filename, mode_t mode)
14708
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
843 {
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
844 #if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
845 /* Use O_EXCL to avoid a race condition when another process tries to
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
846 write the same file. When that happens, our open() call fails,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
847 which is just fine (since it's only a cache).
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
848 XXX If the file exists and is writable but the directory is not
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
849 writable, the file will never be written. Oh well.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
850 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
851 int fd;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
852 (void) unlink(filename);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
853 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
14921
3db8b8031a6a The 2.0b2 change to write .pyc files in exclusive mode (if possible)
Tim Peters <tim.peters@gmail.com>
parents: 14708
diff changeset
854 #ifdef O_BINARY
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
855 |O_BINARY /* necessary for Windows */
14921
3db8b8031a6a The 2.0b2 change to write .pyc files in exclusive mode (if possible)
Tim Peters <tim.peters@gmail.com>
parents: 14708
diff changeset
856 #endif
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26461
diff changeset
857 #ifdef __VMS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
858 , mode, "ctxt=bin", "shr=nil"
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26461
diff changeset
859 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
860 , mode
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26461
diff changeset
861 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
862 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
863 if (fd < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
864 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
865 return fdopen(fd, "wb");
14708
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
866 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
867 /* Best we can do -- on Windows this can't happen anyway */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
868 return fopen(filename, "wb");
14708
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
869 #endif
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
870 }
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
871
89b2aee43e0b On Unix, use O_EXCL when creating the .pyc/.pyo files, to avoid a race condition
Guido van Rossum <guido@python.org>
parents: 14375
diff changeset
872
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
873 /* Write a compiled module to a file, placing the time of last
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
874 modification of its source into the header.
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
875 Errors are ignored, if a write error occurs an attempt is made to
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
876 remove the file. */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
877
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
878 static void
45470
3915d5f650fa Issue #2051 and patch from Alexander Belopolsky:
Christian Heimes <christian@cheimes.de>
parents: 45059
diff changeset
879 write_compiled_module(PyCodeObject *co, char *cpathname, struct stat *srcstat)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
880 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
881 FILE *fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
882 time_t mtime = srcstat->st_mtime;
55141
adfd6b73d364 Update issue 6070 patch to match the patch that was actually tested
R. David Murray <rdmurray@bitdance.com>
parents: 55132
diff changeset
883 #ifdef MS_WINDOWS /* since Windows uses different permissions */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
884 mode_t mode = srcstat->st_mode & ~S_IEXEC;
55141
adfd6b73d364 Update issue 6070 patch to match the patch that was actually tested
R. David Murray <rdmurray@bitdance.com>
parents: 55132
diff changeset
885 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
886 mode_t mode = srcstat->st_mode & ~S_IXUSR & ~S_IXGRP & ~S_IXOTH;
61269
fff439f089fe Fix whitespace.
Brett Cannon <bcannon@gmail.com>
parents: 61268
diff changeset
887 #endif
45470
3915d5f650fa Issue #2051 and patch from Alexander Belopolsky:
Christian Heimes <christian@cheimes.de>
parents: 45059
diff changeset
888
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
889 fp = open_exclusive(cpathname, mode);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
890 if (fp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
891 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
892 PySys_WriteStderr(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
893 "# can't create %s\n", cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
894 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
895 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
896 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
897 /* First write a 0 for mtime */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
898 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
899 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
900 if (fflush(fp) != 0 || ferror(fp)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
901 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
902 PySys_WriteStderr("# can't write %s\n", cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
903 /* Don't keep partial file */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
904 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
905 (void) unlink(cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
906 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
907 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
908 /* Now write the true mtime */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
909 fseek(fp, 4L, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
910 assert(mtime < LONG_MAX);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
911 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
912 fflush(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
913 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
914 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
915 PySys_WriteStderr("# wrote %s\n", cpathname);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
916 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
917
50616
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
918 static void
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
919 update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
920 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
921 PyObject *constants, *tmp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
922 Py_ssize_t i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
923
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
924 if (!_PyString_Eq(co->co_filename, oldname))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
925 return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
926
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
927 tmp = co->co_filename;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
928 co->co_filename = newname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
929 Py_INCREF(co->co_filename);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
930 Py_DECREF(tmp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
931
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
932 constants = co->co_consts;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
933 n = PyTuple_GET_SIZE(constants);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
934 for (i = 0; i < n; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
935 tmp = PyTuple_GET_ITEM(constants, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
936 if (PyCode_Check(tmp))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
937 update_code_filenames((PyCodeObject *)tmp,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
938 oldname, newname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
939 }
50616
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
940 }
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
941
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
942 static int
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
943 update_compiled_module(PyCodeObject *co, char *pathname)
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
944 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
945 PyObject *oldname, *newname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
946
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
947 if (strcmp(PyString_AsString(co->co_filename), pathname) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
948 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
949
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
950 newname = PyString_FromString(pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
951 if (newname == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
952 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
953
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
954 oldname = co->co_filename;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
955 Py_INCREF(oldname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
956 update_code_filenames(co, oldname, newname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
957 Py_DECREF(oldname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
958 Py_DECREF(newname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
959 return 1;
50616
fc0000682bfe Issue #1180193: When importing a module from a .pyc (or .pyo) file with
Antoine Pitrou <solipsis@pitrou.net>
parents: 50491
diff changeset
960 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
961
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
962 /* Load a source module from a given file and return its module
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
963 object WITH INCREMENTED REFERENCE COUNT. If there's a matching
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
964 byte-compiled file, use that instead. */
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
965
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
966 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
967 load_source_module(char *name, char *pathname, FILE *fp)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
968 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
969 struct stat st;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
970 FILE *fpc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
971 char buf[MAXPATHLEN+1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
972 char *cpathname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
973 PyCodeObject *co;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
974 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
975
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
976 if (fstat(fileno(fp), &st) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
977 PyErr_Format(PyExc_RuntimeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
978 "unable to get file status from '%s'",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
979 pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
980 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
981 }
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
982 #if SIZEOF_TIME_T > 4
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
983 /* Python's .pyc timestamp handling presumes that the timestamp fits
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
984 in 4 bytes. This will be fine until sometime in the year 2038,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
985 when a 4-byte signed time_t will overflow.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
986 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
987 if (st.st_mtime >> 32) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
988 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
989 "modification time overflows a 4 byte field");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
990 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
991 }
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
992 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
993 cpathname = make_compiled_pathname(pathname, buf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
994 (size_t)MAXPATHLEN + 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
995 if (cpathname != NULL &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
996 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
997 co = read_compiled_module(cpathname, fpc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
998 fclose(fpc);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
999 if (co == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1000 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1001 if (update_compiled_module(co, pathname) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1002 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1003 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1004 PySys_WriteStderr("import %s # precompiled from %s\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1005 name, cpathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1006 pathname = cpathname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1007 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1008 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1009 co = parse_source_module(pathname, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1010 if (co == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1011 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1012 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1013 PySys_WriteStderr("import %s # from %s\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1014 name, pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1015 if (cpathname) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1016 PyObject *ro = PySys_GetObject("dont_write_bytecode");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1017 if (ro == NULL || !PyObject_IsTrue(ro))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1018 write_compiled_module(co, cpathname, &st);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1019 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1020 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1021 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1022 Py_DECREF(co);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1023
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1024 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1025 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1026
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1027
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1028 /* Forward */
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1029 static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1030 static struct filedescr *find_module(char *, char *, PyObject *,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1031 char *, size_t, FILE **, PyObject **);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
1032 static struct _frozen *find_frozen(char *name);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1033
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1034 /* Load a package and return its module object WITH INCREMENTED
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1035 REFERENCE COUNT */
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1036
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1037 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1038 load_package(char *name, char *pathname)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1039 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1040 PyObject *m, *d;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1041 PyObject *file = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1042 PyObject *path = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1043 int err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1044 char buf[MAXPATHLEN+1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1045 FILE *fp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1046 struct filedescr *fdp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1047
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1048 m = PyImport_AddModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1049 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1050 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1051 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1052 PySys_WriteStderr("import %s # directory %s\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1053 name, pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1054 d = PyModule_GetDict(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1055 file = PyString_FromString(pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1056 if (file == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1057 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1058 path = Py_BuildValue("[O]", file);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1059 if (path == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1060 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1061 err = PyDict_SetItemString(d, "__file__", file);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1062 if (err == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1063 err = PyDict_SetItemString(d, "__path__", path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1064 if (err != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1065 goto error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1066 buf[0] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1067 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1068 if (fdp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1069 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1070 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1071 Py_INCREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1072 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1073 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1074 m = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1075 goto cleanup;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1076 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1077 m = load_module(name, fp, buf, fdp->type, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1078 if (fp != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1079 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1080 goto cleanup;
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1081
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1082 error:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1083 m = NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1084 cleanup:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1085 Py_XDECREF(path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1086 Py_XDECREF(file);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1087 return m;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1088 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1089
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1090
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1091 /* Helper to test for built-in module */
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1092
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1093 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1094 is_builtin(char *name)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1095 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1096 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1097 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1098 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1099 if (PyImport_Inittab[i].initfunc == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1100 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1101 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1102 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1103 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1104 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1105 return 0;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1106 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1107
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1108
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1109 /* Return an importer object for a sys.path/pkg.__path__ item 'p',
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1110 possibly by fetching it from the path_importer_cache dict. If it
39901
f90ba306c287 Very minor grammatical fix in a comment.
Brett Cannon <bcannon@gmail.com>
parents: 39827
diff changeset
1111 wasn't yet cached, traverse path_hooks until a hook is found
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1112 that can handle the path item. Return None if no hook could;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1113 this tells our caller it should fall back to the builtin
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1114 import mechanism. Cache the result in path_importer_cache.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1115 Returns a borrowed reference. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1116
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1117 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1118 get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1119 PyObject *p)
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1120 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1121 PyObject *importer;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1122 Py_ssize_t j, nhooks;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1123
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1124 /* These conditions are the caller's responsibility: */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1125 assert(PyList_Check(path_hooks));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1126 assert(PyDict_Check(path_importer_cache));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1127
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1128 nhooks = PyList_Size(path_hooks);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1129 if (nhooks < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1130 return NULL; /* Shouldn't happen */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1131
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1132 importer = PyDict_GetItem(path_importer_cache, p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1133 if (importer != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1134 return importer;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1135
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1136 /* set path_importer_cache[p] to None to avoid recursion */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1137 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1138 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1139
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1140 for (j = 0; j < nhooks; j++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1141 PyObject *hook = PyList_GetItem(path_hooks, j);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1142 if (hook == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1143 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1144 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1145 if (importer != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1146 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1147
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1148 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1149 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1150 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1151 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1152 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1153 if (importer == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1154 importer = PyObject_CallFunctionObjArgs(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1155 (PyObject *)&PyNullImporter_Type, p, NULL
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1156 );
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1157 if (importer == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1158 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1159 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1160 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1161 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1162 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1163 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1164 if (importer != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1165 int err = PyDict_SetItem(path_importer_cache, p, importer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1166 Py_DECREF(importer);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1167 if (err != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1168 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1169 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1170 return importer;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1171 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1172
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents: 43792
diff changeset
1173 PyAPI_FUNC(PyObject *)
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents: 43792
diff changeset
1174 PyImport_GetImporter(PyObject *path) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1175 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1176
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1177 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1178 if ((path_hooks = PySys_GetObject("path_hooks"))) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1179 importer = get_path_importer(path_importer_cache,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1180 path_hooks, path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1181 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1182 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1183 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1184 return importer;
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents: 43792
diff changeset
1185 }
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents: 43792
diff changeset
1186
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1187 /* Search the path (default sys.path) for a module. Return the
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1188 corresponding filedescr struct, and (via return arguments) the
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1189 pathname and an open file. Return NULL if the module is not found. */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1190
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1191 #ifdef MS_COREDLL
13663
29da35543833 ANSIfy as many declarations as possible.
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1192 extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1193 char *, Py_ssize_t);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1194 #endif
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1195
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1196 static int case_ok(char *, Py_ssize_t, Py_ssize_t, char *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
1197 static int find_init_module(char *); /* Forward */
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1198 static struct filedescr importhookdescr = {"", "", IMP_HOOK};
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1199
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1200 static struct filedescr *
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1201 find_module(char *fullname, char *subname, PyObject *path, char *buf,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1202 size_t buflen, FILE **p_fp, PyObject **p_loader)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1203 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1204 Py_ssize_t i, npath;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1205 size_t len, namelen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1206 struct filedescr *fdp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1207 char *filemode;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1208 FILE *fp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1209 PyObject *path_hooks, *path_importer_cache;
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1210 #ifndef RISCOS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1211 struct stat statbuf;
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1212 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1213 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1214 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1215 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1216 char name[MAXPATHLEN+1];
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1217 #if defined(PYOS_OS2)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1218 size_t saved_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1219 size_t saved_namelen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1220 char *saved_buf = NULL;
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1221 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1222 if (p_loader != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1223 *p_loader = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1224
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1225 if (strlen(subname) > MAXPATHLEN) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1226 PyErr_SetString(PyExc_OverflowError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1227 "module name is too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1228 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1229 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1230 strcpy(name, subname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1231
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1232 /* sys.meta_path import hook */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1233 if (p_loader != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1234 PyObject *meta_path;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1235
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1236 meta_path = PySys_GetObject("meta_path");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1237 if (meta_path == NULL || !PyList_Check(meta_path)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1238 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1239 "sys.meta_path must be a list of "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1240 "import hooks");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1241 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1242 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1243 Py_INCREF(meta_path); /* zap guard */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1244 npath = PyList_Size(meta_path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1245 for (i = 0; i < npath; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1246 PyObject *loader;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1247 PyObject *hook = PyList_GetItem(meta_path, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1248 loader = PyObject_CallMethod(hook, "find_module",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1249 "sO", fullname,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1250 path != NULL ?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1251 path : Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1252 if (loader == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1253 Py_DECREF(meta_path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1254 return NULL; /* true error */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1255 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1256 if (loader != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1257 /* a loader was found */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1258 *p_loader = loader;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1259 Py_DECREF(meta_path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1260 return &importhookdescr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1261 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1262 Py_DECREF(loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1263 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1264 Py_DECREF(meta_path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1265 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1266
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1267 if (path != NULL && PyString_Check(path)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1268 /* The only type of submodule allowed inside a "frozen"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1269 package are other frozen modules or packages. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1270 if (PyString_Size(path) + 1 + strlen(name) >= (size_t)buflen) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1271 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1272 "full frozen module name too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1273 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1274 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1275 strcpy(buf, PyString_AsString(path));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1276 strcat(buf, ".");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1277 strcat(buf, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1278 strcpy(name, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1279 if (find_frozen(name) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1280 strcpy(buf, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1281 return &fd_frozen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1282 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1283 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1284 "No frozen submodule named %.200s", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1285 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1286 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1287 if (path == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1288 if (is_builtin(name)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1289 strcpy(buf, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1290 return &fd_builtin;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1291 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1292 if ((find_frozen(name)) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1293 strcpy(buf, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1294 return &fd_frozen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1295 }
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1296
4111
90216e907345 PYTHONWIN -> MS_COREDLL
Guido van Rossum <guido@python.org>
parents: 3964
diff changeset
1297 #ifdef MS_COREDLL
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1298 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1299 if (fp != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1300 *p_fp = fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1301 return fdp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1302 }
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1303 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1304 path = PySys_GetObject("path");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1305 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1306 if (path == NULL || !PyList_Check(path)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1307 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1308 "sys.path must be a list of directory names");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1309 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1310 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1311
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1312 path_hooks = PySys_GetObject("path_hooks");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1313 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1314 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1315 "sys.path_hooks must be a list of "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1316 "import hooks");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1317 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1318 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1319 path_importer_cache = PySys_GetObject("path_importer_cache");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1320 if (path_importer_cache == NULL ||
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1321 !PyDict_Check(path_importer_cache)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1322 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1323 "sys.path_importer_cache must be a dict");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1324 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1325 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1326
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1327 npath = PyList_Size(path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1328 namelen = strlen(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1329 for (i = 0; i < npath; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1330 PyObject *copy = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1331 PyObject *v = PyList_GetItem(path, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1332 if (!v)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1333 return NULL;
23879
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1334 #ifdef Py_USING_UNICODE
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1335 if (PyUnicode_Check(v)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1336 copy = PyUnicode_Encode(PyUnicode_AS_UNICODE(v),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1337 PyUnicode_GET_SIZE(v), Py_FileSystemDefaultEncoding, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1338 if (copy == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1339 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1340 v = copy;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1341 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1342 else
23879
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1343 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1344 if (!PyString_Check(v))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1345 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1346 len = PyString_GET_SIZE(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1347 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1348 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1349 continue; /* Too long */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1350 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1351 strcpy(buf, PyString_AS_STRING(v));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1352 if (strlen(buf) != len) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1353 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1354 continue; /* v contains '\0' */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1355 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1356
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1357 /* sys.path_hooks import hook */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1358 if (p_loader != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1359 PyObject *importer;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1360
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1361 importer = get_path_importer(path_importer_cache,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1362 path_hooks, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1363 if (importer == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1364 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1365 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1366 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1367 /* Note: importer is a borrowed reference */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1368 if (importer != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1369 PyObject *loader;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1370 loader = PyObject_CallMethod(importer,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1371 "find_module",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1372 "s", fullname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1373 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1374 if (loader == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1375 return NULL; /* error */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1376 if (loader != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1377 /* a loader was found */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1378 *p_loader = loader;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1379 return &importhookdescr;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1380 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1381 Py_DECREF(loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1382 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1383 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1384 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1385 /* no hook was found, use builtin import */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1386
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1387 if (len > 0 && buf[len-1] != SEP
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1388 #ifdef ALTSEP
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1389 && buf[len-1] != ALTSEP
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1390 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1391 )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1392 buf[len++] = SEP;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1393 strcpy(buf+len, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1394 len += namelen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1395
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1396 /* Check for package import (buf holds a directory name,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1397 and there's an __init__ module in that directory */
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1398 #ifdef HAVE_STAT
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1399 if (stat(buf, &statbuf) == 0 && /* it exists */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1400 S_ISDIR(statbuf.st_mode) && /* it's a directory */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1401 case_ok(buf, len, namelen, name)) { /* case matches */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1402 if (find_init_module(buf)) { /* and has __init__.py */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1403 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1404 return &fd_package;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1405 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1406 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1407 char warnstr[MAXPATHLEN+80];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1408 sprintf(warnstr, "Not importing directory "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1409 "'%.*s': missing __init__.py",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1410 MAXPATHLEN, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1411 if (PyErr_Warn(PyExc_ImportWarning,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1412 warnstr)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1413 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1414 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1415 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1416 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1417 }
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1418 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1419 /* XXX How are you going to test for directories? */
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1420 #ifdef RISCOS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1421 if (isdir(buf) &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1422 case_ok(buf, len, namelen, name)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1423 if (find_init_module(buf)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1424 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1425 return &fd_package;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1426 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1427 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1428 char warnstr[MAXPATHLEN+80];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1429 sprintf(warnstr, "Not importing directory "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1430 "'%.*s': missing __init__.py",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1431 MAXPATHLEN, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1432 if (PyErr_Warn(PyExc_ImportWarning,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1433 warnstr)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1434 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1435 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1436 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1437 }
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1438 #endif
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1439 #endif
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1440 #if defined(PYOS_OS2)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1441 /* take a snapshot of the module spec for restoration
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1442 * after the 8 character DLL hackery
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1443 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1444 saved_buf = strdup(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1445 saved_len = len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1446 saved_namelen = namelen;
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1447 #endif /* PYOS_OS2 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1448 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
42734
9087337f3a3f Second half of #1752175: #ifdef out references to PyImport_DynLoadFiletab if HAVE_DYNAMIC_LOADING is not defined.
Georg Brandl <georg@python.org>
parents: 42688
diff changeset
1449 #if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1450 /* OS/2 limits DLLs to 8 character names (w/o
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1451 extension)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1452 * so if the name is longer than that and its a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1453 * dynamically loaded module we're going to try,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1454 * truncate the name before trying
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1455 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1456 if (strlen(subname) > 8) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1457 /* is this an attempt to load a C extension? */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1458 const struct filedescr *scan;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1459 scan = _PyImport_DynLoadFiletab;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1460 while (scan->suffix != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1461 if (!strcmp(scan->suffix, fdp->suffix))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1462 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1463 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1464 scan++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1465 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1466 if (scan->suffix != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1467 /* yes, so truncate the name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1468 namelen = 8;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1469 len -= strlen(subname) - namelen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1470 buf[len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1471 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1472 }
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1473 #endif /* PYOS_OS2 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1474 strcpy(buf+len, fdp->suffix);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1475 if (Py_VerboseFlag > 1)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1476 PySys_WriteStderr("# trying %s\n", buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1477 filemode = fdp->mode;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1478 if (filemode[0] == 'U')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1479 filemode = "r" PY_STDIOTEXTMODE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1480 fp = fopen(buf, filemode);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1481 if (fp != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1482 if (case_ok(buf, len, namelen, name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1483 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1484 else { /* continue search */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1485 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1486 fp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1487 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1488 }
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1489 #if defined(PYOS_OS2)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1490 /* restore the saved snapshot */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1491 strcpy(buf, saved_buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1492 len = saved_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1493 namelen = saved_namelen;
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1494 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1495 }
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1496 #if defined(PYOS_OS2)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1497 /* don't need/want the module name snapshot anymore */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1498 if (saved_buf)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1499 {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1500 free(saved_buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1501 saved_buf = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1502 }
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1503 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1504 Py_XDECREF(copy);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1505 if (fp != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1506 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1507 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1508 if (fp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1509 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1510 "No module named %.200s", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1511 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1512 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1513 *p_fp = fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1514 return fdp;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1515 }
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1516
33680
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1517 /* Helpers for main.c
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1518 * Find the source file corresponding to a named module
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1519 */
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1520 struct filedescr *
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1521 _PyImport_FindModule(const char *name, PyObject *path, char *buf,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1522 size_t buflen, FILE **p_fp, PyObject **p_loader)
33680
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1523 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1524 return find_module((char *) name, (char *) name, path,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1525 buf, buflen, p_fp, p_loader);
33680
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1526 }
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1527
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1528 PyAPI_FUNC(int) _PyImport_IsScript(struct filedescr * fd)
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1529 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1530 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
33680
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1531 }
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1532
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1533 /* case_ok(char* buf, Py_ssize_t len, Py_ssize_t namelen, char* name)
16902
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1534 * The arguments here are tricky, best shown by example:
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1535 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1536 * ^ ^ ^ ^
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1537 * |--------------------- buf ---------------------|
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1538 * |------------------- len ------------------|
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1539 * |------ name -------|
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1540 * |----- namelen -----|
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1541 * buf is the full path, but len only counts up to (& exclusive of) the
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1542 * extension. name is the module name, also exclusive of extension.
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1543 *
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1544 * We've already done a successful stat() or fopen() on buf, so know that
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1545 * there's some match, possibly case-insensitive.
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1546 *
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1547 * case_ok() is to return 1 if there's a case-sensitive match for
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1548 * name, else 0. case_ok() is also to return 1 if envar PYTHONCASEOK
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1549 * exists.
16902
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1550 *
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1551 * case_ok() is used to implement case-sensitive import semantics even
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1552 * on platforms with case-insensitive filesystems. It's trivial to implement
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1553 * for case-sensitive filesystems. It's pretty much a cross-platform
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1554 * nightmare for systems with case-insensitive filesystems.
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1555 */
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1556
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1557 /* First we may need a pile of platform-specific header files; the sequence
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1558 * of #if's here should match the sequence in the body of case_ok().
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1559 */
34961
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1560 #if defined(MS_WINDOWS)
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1561 #include <windows.h>
15802
eb1767b068fe SF Patch #103154 by jlt63: Cygwin Check Import Case Patch.
Guido van Rossum <guido@python.org>
parents: 15401
diff changeset
1562
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1563 #elif defined(DJGPP)
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1564 #include <dir.h>
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1565
34961
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1566 #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
16880
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1567 #include <sys/types.h>
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1568 #include <dirent.h>
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1569
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1570 #elif defined(PYOS_OS2)
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1571 #define INCL_DOS
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1572 #define INCL_DOSERRORS
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1573 #define INCL_NOPMAPI
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1574 #include <os2.h>
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1575
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20527
diff changeset
1576 #elif defined(RISCOS)
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20527
diff changeset
1577 #include "oslib/osfscontrol.h"
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1578 #endif
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1579
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1580 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1581 case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1582 {
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1583 /* Pick a platform-specific implementation; the sequence of #if's here should
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1584 * match the sequence just above.
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1585 */
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1586
34961
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1587 /* MS_WINDOWS */
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1588 #if defined(MS_WINDOWS)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1589 WIN32_FIND_DATA data;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1590 HANDLE h;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1591
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1592 if (Py_GETENV("PYTHONCASEOK") != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1593 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1594
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1595 h = FindFirstFile(buf, &data);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1596 if (h == INVALID_HANDLE_VALUE) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1597 PyErr_Format(PyExc_NameError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1598 "Can't find file for module %.100s\n(filename %.300s)",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1599 name, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1600 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1601 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1602 FindClose(h);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1603 return strncmp(data.cFileName, name, namelen) == 0;
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1604
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1605 /* DJGPP */
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1606 #elif defined(DJGPP)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1607 struct ffblk ffblk;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1608 int done;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1609
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1610 if (Py_GETENV("PYTHONCASEOK") != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1611 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1612
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1613 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1614 if (done) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1615 PyErr_Format(PyExc_NameError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1616 "Can't find file for module %.100s\n(filename %.300s)",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1617 name, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1618 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1619 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1620 return strncmp(ffblk.ff_name, name, namelen) == 0;
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1621
34961
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1622 /* new-fangled macintosh (macosx) or Cygwin */
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1623 #elif (defined(__MACH__) && defined(__APPLE__) || defined(__CYGWIN__)) && defined(HAVE_DIRENT_H)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1624 DIR *dirp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1625 struct dirent *dp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1626 char dirname[MAXPATHLEN + 1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1627 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1628
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1629 if (Py_GETENV("PYTHONCASEOK") != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1630 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1631
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1632 /* Copy the dir component into dirname; substitute "." if empty */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1633 if (dirlen <= 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1634 dirname[0] = '.';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1635 dirname[1] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1636 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1637 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1638 assert(dirlen <= MAXPATHLEN);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1639 memcpy(dirname, buf, dirlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1640 dirname[dirlen] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1641 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1642 /* Open the directory and search the entries for an exact match. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1643 dirp = opendir(dirname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1644 if (dirp) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1645 char *nameWithExt = buf + len - namelen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1646 while ((dp = readdir(dirp)) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1647 const int thislen =
16880
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1648 #ifdef _DIRENT_HAVE_D_NAMELEN
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1649 dp->d_namlen;
16880
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1650 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1651 strlen(dp->d_name);
16880
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1652 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1653 if (thislen >= namelen &&
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1654 strcmp(dp->d_name, nameWithExt) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1655 (void)closedir(dirp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1656 return 1; /* Found */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1657 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1658 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1659 (void)closedir(dirp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1660 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1661 return 0 ; /* Not found */
16880
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1662
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20527
diff changeset
1663 /* RISC OS */
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20527
diff changeset
1664 #elif defined(RISCOS)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1665 char canon[MAXPATHLEN+1]; /* buffer for the canonical form of the path */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1666 char buf2[MAXPATHLEN+2];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1667 char *nameWithExt = buf+len-namelen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1668 int canonlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1669 os_error *e;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1670
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1671 if (Py_GETENV("PYTHONCASEOK") != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1672 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1673
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1674 /* workaround:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1675 append wildcard, otherwise case of filename wouldn't be touched */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1676 strcpy(buf2, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1677 strcat(buf2, "*");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1678
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1679 e = xosfscontrol_canonicalise_path(buf2,canon,0,0,MAXPATHLEN+1,&canonlen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1680 canonlen = MAXPATHLEN+1-canonlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1681 if (e || canonlen<=0 || canonlen>(MAXPATHLEN+1) )
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1682 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1683 if (strcmp(nameWithExt, canon+canonlen-strlen(nameWithExt))==0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1684 return 1; /* match */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1685
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1686 return 0;
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 20527
diff changeset
1687
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1688 /* OS/2 */
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1689 #elif defined(PYOS_OS2)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1690 HDIR hdir = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1691 ULONG srchcnt = 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1692 FILEFINDBUF3 ffbuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1693 APIRET rc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1694
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1695 if (Py_GETENV("PYTHONCASEOK") != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1696 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1697
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1698 rc = DosFindFirst(buf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1699 &hdir,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1700 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1701 &ffbuf, sizeof(ffbuf),
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1702 &srchcnt,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1703 FIL_STANDARD);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1704 if (rc != NO_ERROR)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1705 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1706 return strncmp(ffbuf.achName, name, namelen) == 0;
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1707
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1708 /* assuming it's a case-sensitive filesystem, so there's nothing to do! */
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1709 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1710 return 1;
7305
067e4813c6d6 Added DJGPP version of check_case(), by Pit Scrorpion (Hans Nowak).
Guido van Rossum <guido@python.org>
parents: 7290
diff changeset
1711
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1712 #endif
7305
067e4813c6d6 Added DJGPP version of check_case(), by Pit Scrorpion (Hans Nowak).
Guido van Rossum <guido@python.org>
parents: 7290
diff changeset
1713 }
067e4813c6d6 Added DJGPP version of check_case(), by Pit Scrorpion (Hans Nowak).
Guido van Rossum <guido@python.org>
parents: 7290
diff changeset
1714
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1715
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1716 #ifdef HAVE_STAT
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1717 /* Helper to look for __init__.py or __init__.py[co] in potential package */
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1718 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1719 find_init_module(char *buf)
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1720 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1721 const size_t save_len = strlen(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1722 size_t i = save_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1723 char *pname; /* pointer to start of __init__ */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1724 struct stat statbuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1725
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1726 /* For calling case_ok(buf, len, namelen, name):
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1727 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1728 * ^ ^ ^ ^
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1729 * |--------------------- buf ---------------------|
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1730 * |------------------- len ------------------|
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1731 * |------ name -------|
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1732 * |----- namelen -----|
18388
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1733 */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1734 if (save_len + 13 >= MAXPATHLEN)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1735 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1736 buf[i++] = SEP;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1737 pname = buf + i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1738 strcpy(pname, "__init__.py");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1739 if (stat(buf, &statbuf) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1740 if (case_ok(buf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1741 save_len + 9, /* len("/__init__") */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1742 8, /* len("__init__") */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1743 pname)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1744 buf[save_len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1745 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1746 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1747 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1748 i += strlen(pname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1749 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1750 if (stat(buf, &statbuf) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1751 if (case_ok(buf,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1752 save_len + 9, /* len("/__init__") */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1753 8, /* len("__init__") */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1754 pname)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1755 buf[save_len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1756 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1757 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1758 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1759 buf[save_len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1760 return 0;
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1761 }
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1762
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1763 #else
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1764
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1765 #ifdef RISCOS
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1766 static int
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1767 find_init_module(buf)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1768 char *buf;
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1769 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1770 int save_len = strlen(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1771 int i = save_len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1772
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1773 if (save_len + 13 >= MAXPATHLEN)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1774 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1775 buf[i++] = SEP;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1776 strcpy(buf+i, "__init__/py");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1777 if (isfile(buf)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1778 buf[save_len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1779 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1780 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1781
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1782 if (Py_OptimizeFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1783 strcpy(buf+i, "o");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1784 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1785 strcpy(buf+i, "c");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1786 if (isfile(buf)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1787 buf[save_len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1788 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1789 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1790 buf[save_len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1791 return 0;
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1792 }
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1793 #endif /*RISCOS*/
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1794
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1795 #endif /* HAVE_STAT */
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1796
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1797
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
1798 static int init_builtin(char *); /* Forward */
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1799
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1800 /* Load an external module using the default search path and return
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
1801 its module object WITH INCREMENTED REFERENCE COUNT */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
1802
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1803 static PyObject *
55441
6c7b4b470827 Better name a variable: 'buf' seems to imply a mutable buffer.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 55141
diff changeset
1804 load_module(char *name, FILE *fp, char *pathname, int type, PyObject *loader)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
1805 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1806 PyObject *modules;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1807 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1808 int err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1809
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1810 /* First check that there's an open file (if we need one) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1811 switch (type) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1812 case PY_SOURCE:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1813 case PY_COMPILED:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1814 if (fp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1815 PyErr_Format(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1816 "file object required for import (type code %d)",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1817 type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1818 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1819 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1820 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1821
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1822 switch (type) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1823
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1824 case PY_SOURCE:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1825 m = load_source_module(name, pathname, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1826 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1827
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1828 case PY_COMPILED:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1829 m = load_compiled_module(name, pathname, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1830 break;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1831
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
1832 #ifdef HAVE_DYNAMIC_LOADING
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1833 case C_EXTENSION:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1834 m = _PyImport_LoadDynamicModule(name, pathname, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1835 break;
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
1836 #endif
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1837
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1838 case PKG_DIRECTORY:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1839 m = load_package(name, pathname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1840 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1841
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1842 case C_BUILTIN:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1843 case PY_FROZEN:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1844 if (pathname != NULL && pathname[0] != '\0')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1845 name = pathname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1846 if (type == C_BUILTIN)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1847 err = init_builtin(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1848 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1849 err = PyImport_ImportFrozenModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1850 if (err < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1851 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1852 if (err == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1853 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1854 "Purported %s module %.200s not found",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1855 type == C_BUILTIN ?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1856 "builtin" : "frozen",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1857 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1858 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1859 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1860 modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1861 m = PyDict_GetItemString(modules, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1862 if (m == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1863 PyErr_Format(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1864 PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1865 "%s module %.200s not properly initialized",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1866 type == C_BUILTIN ?
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1867 "builtin" : "frozen",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1868 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1869 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1870 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1871 Py_INCREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1872 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1873
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1874 case IMP_HOOK: {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1875 if (loader == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1876 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1877 "import hook without loader");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1878 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1879 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1880 m = PyObject_CallMethod(loader, "load_module", "s", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1881 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1882 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1883
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1884 default:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1885 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1886 "Don't know how to import %.200s (type code %d)",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1887 name, type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1888 m = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1889
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1890 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1891
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1892 return m;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1893 }
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
1894
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
1895
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1896 /* Initialize a built-in module.
39978
47ab0c27dd1f Fix minor typo in a comment.
Brett Cannon <bcannon@gmail.com>
parents: 39901
diff changeset
1897 Return 1 for success, 0 if the module is not found, and -1 with
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1898 an exception set if the initialization failed. */
141
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1899
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1900 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1901 init_builtin(char *name)
141
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1902 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1903 struct _inittab *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1904
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1905 if (_PyImport_FindExtension(name, name) != NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1906 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1907
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1908 for (p = PyImport_Inittab; p->name != NULL; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1909 if (strcmp(name, p->name) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1910 if (p->initfunc == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1911 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1912 "Cannot re-init internal module %.200s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1913 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1914 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1915 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1916 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1917 PySys_WriteStderr("import %s # builtin\n", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1918 (*p->initfunc)();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1919 if (PyErr_Occurred())
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1920 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1921 if (_PyImport_FixupExtension(name, name) == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1922 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1923 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1924 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1925 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1926 return 0;
141
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1927 }
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1928
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1929
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1930 /* Frozen modules */
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1931
3683
ec39264311ff struct frozen is now struct _frozen and comes from import.h.
Guido van Rossum <guido@python.org>
parents: 3590
diff changeset
1932 static struct _frozen *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1933 find_frozen(char *name)
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1934 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1935 struct _frozen *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1936
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1937 for (p = PyImport_FrozenModules; ; p++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1938 if (p->name == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1939 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1940 if (strcmp(p->name, name) == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1941 break;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1942 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1943 return p;
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1944 }
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1945
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1946 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1947 get_frozen_object(char *name)
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1948 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1949 struct _frozen *p = find_frozen(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1950 int size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1951
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1952 if (p == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1953 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1954 "No such frozen object named %.200s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1955 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1956 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1957 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1958 if (p->code == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1959 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1960 "Excluded frozen object named %.200s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1961 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1962 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1963 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1964 size = p->size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1965 if (size < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1966 size = -size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1967 return PyMarshal_ReadObjectFromString((char *)p->code, size);
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1968 }
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1969
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1970 /* Initialize a frozen module.
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1971 Return 1 for succes, 0 if the module is not found, and -1 with
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1972 an exception set if the initialization failed.
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1973 This function is also used from frozenmain.c */
2294
6263e2fd9398 init_frozen non-static, frozenmain.c not in Makefile any more
Guido van Rossum <guido@python.org>
parents: 2270
diff changeset
1974
6263e2fd9398 init_frozen non-static, frozenmain.c not in Makefile any more
Guido van Rossum <guido@python.org>
parents: 2270
diff changeset
1975 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1976 PyImport_ImportFrozenModule(char *name)
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1977 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1978 struct _frozen *p = find_frozen(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1979 PyObject *co;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1980 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1981 int ispackage;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1982 int size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1983
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1984 if (p == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1985 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1986 if (p->code == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1987 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1988 "Excluded frozen object named %.200s",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1989 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1990 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1991 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1992 size = p->size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1993 ispackage = (size < 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1994 if (ispackage)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1995 size = -size;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1996 if (Py_VerboseFlag)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1997 PySys_WriteStderr("import %s # frozen%s\n",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1998 name, ispackage ? " package" : "");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
1999 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2000 if (co == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2001 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2002 if (!PyCode_Check(co)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2003 PyErr_Format(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2004 "frozen object %.200s is not a code object",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2005 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2006 goto err_return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2007 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2008 if (ispackage) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2009 /* Set __path__ to the package name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2010 PyObject *d, *s;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2011 int err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2012 m = PyImport_AddModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2013 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2014 goto err_return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2015 d = PyModule_GetDict(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2016 s = PyString_InternFromString(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2017 if (s == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2018 goto err_return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2019 err = PyDict_SetItemString(d, "__path__", s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2020 Py_DECREF(s);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2021 if (err != 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2022 goto err_return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2023 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2024 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2025 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2026 goto err_return;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2027 Py_DECREF(co);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2028 Py_DECREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2029 return 1;
39099
775f85b98e65 Fix memory leak under some conditions.
Neal Norwitz <nnorwitz@gmail.com>
parents: 39070
diff changeset
2030 err_return:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2031 Py_DECREF(co);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2032 return -1;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2033 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2034
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2035
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2036 /* Import a module, either built-in, frozen, or external, and return
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
2037 its module object WITH INCREMENTED REFERENCE COUNT */
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2038
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2039 PyObject *
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35757
diff changeset
2040 PyImport_ImportModule(const char *name)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2041 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2042 PyObject *pname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2043 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2044
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2045 pname = PyString_FromString(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2046 if (pname == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2047 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2048 result = PyImport_Import(pname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2049 Py_DECREF(pname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2050 return result;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2051 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2052
44439
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2053 /* Import a module without blocking
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2054 *
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2055 * At first it tries to fetch the module from sys.modules. If the module was
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2056 * never loaded before it loads it with PyImport_ImportModule() unless another
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2057 * thread holds the import lock. In the latter case the function raises an
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2058 * ImportError instead of blocking.
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2059 *
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2060 * Returns the module object with incremented ref count.
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2061 */
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2062 PyObject *
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2063 PyImport_ImportModuleNoBlock(const char *name)
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2064 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2065 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2066 PyObject *modules;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2067 long me;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2068
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2069 /* Try to get the module from sys.modules[name] */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2070 modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2071 if (modules == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2072 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2073
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2074 result = PyDict_GetItemString(modules, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2075 if (result != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2076 Py_INCREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2077 return result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2078 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2079 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2080 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2081 }
48920
176c409fd747 Fix compilation when --without-threads is given #3683
Benjamin Peterson <benjamin@python.org>
parents: 48189
diff changeset
2082 #ifdef WITH_THREAD
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2083 /* check the import lock
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2084 * me might be -1 but I ignore the error here, the lock function
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2085 * takes care of the problem */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2086 me = PyThread_get_thread_ident();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2087 if (import_lock_thread == -1 || import_lock_thread == me) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2088 /* no thread or me is holding the lock */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2089 return PyImport_ImportModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2090 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2091 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2092 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2093 "Failed to import %.200s because the import lock"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2094 "is held by another thread.",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2095 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2096 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2097 }
48920
176c409fd747 Fix compilation when --without-threads is given #3683
Benjamin Peterson <benjamin@python.org>
parents: 48189
diff changeset
2098 #else
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2099 return PyImport_ImportModule(name);
48920
176c409fd747 Fix compilation when --without-threads is given #3683
Benjamin Peterson <benjamin@python.org>
parents: 48189
diff changeset
2100 #endif
44439
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2101 }
0cbe94bea2c7 Modified PyImport_Import and PyImport_ImportModule to always use absolute imports by calling __import__ with an explicit level of 0
Christian Heimes <christian@cheimes.de>
parents: 44334
diff changeset
2102
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2103 /* Forward declarations for helper routines */
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2104 static PyObject *get_parent(PyObject *globals, char *buf,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2105 Py_ssize_t *p_buflen, int level);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
2106 static PyObject *load_next(PyObject *mod, PyObject *altmod,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2107 char **p_name, char *buf, Py_ssize_t *p_buflen);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
2108 static int mark_miss(char *name);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
2109 static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2110 char *buf, Py_ssize_t buflen, int recursive);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
2111 static PyObject * import_submodule(PyObject *mod, char *name, char *fullname);
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2112
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2113 /* The Magnum Opus of dotted-name import :-) */
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2114
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
2115 static PyObject *
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2116 import_module_level(char *name, PyObject *globals, PyObject *locals,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2117 PyObject *fromlist, int level)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2118 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2119 char buf[MAXPATHLEN+1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2120 Py_ssize_t buflen = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2121 PyObject *parent, *head, *next, *tail;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2122
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2123 if (strchr(name, '/') != NULL
44614
dc9d95b717c5 Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents: 44569
diff changeset
2124 #ifdef MS_WINDOWS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2125 || strchr(name, '\\') != NULL
44614
dc9d95b717c5 Fixed #1776. __import__() no longer imports modules by file name
Christian Heimes <christian@cheimes.de>
parents: 44569
diff changeset
2126 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2127 ) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2128 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2129 "Import by filename is not supported.");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2130 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2131 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2132
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2133 parent = get_parent(globals, buf, &buflen, level);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2134 if (parent == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2135 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2136
61773
62c0e5f14138 Turned out that if you used explicit relative import syntax
Brett Cannon <bcannon@gmail.com>
parents: 61463
diff changeset
2137 head = load_next(parent, level < 0 ? Py_None : parent, &name, buf,
62c0e5f14138 Turned out that if you used explicit relative import syntax
Brett Cannon <bcannon@gmail.com>
parents: 61463
diff changeset
2138 &buflen);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2139 if (head == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2140 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2141
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2142 tail = head;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2143 Py_INCREF(tail);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2144 while (name) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2145 next = load_next(tail, tail, &name, buf, &buflen);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2146 Py_DECREF(tail);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2147 if (next == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2148 Py_DECREF(head);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2149 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2150 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2151 tail = next;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2152 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2153 if (tail == Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2154 /* If tail is Py_None, both get_parent and load_next found
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2155 an empty module name: someone called __import__("") or
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2156 doctored faulty bytecode */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2157 Py_DECREF(tail);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2158 Py_DECREF(head);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2159 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2160 "Empty module name");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2161 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2162 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2163
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2164 if (fromlist != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2165 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2166 fromlist = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2167 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2168
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2169 if (fromlist == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2170 Py_DECREF(tail);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2171 return head;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2172 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2173
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2174 Py_DECREF(head);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2175 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2176 Py_DECREF(tail);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2177 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2178 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2179
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2180 return tail;
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2181 }
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2182
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2183 PyObject *
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2184 PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2185 PyObject *fromlist, int level)
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2186 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2187 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2188 _PyImport_AcquireLock();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2189 result = import_module_level(name, globals, locals, fromlist, level);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2190 if (_PyImport_ReleaseLock() < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2191 Py_XDECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2192 PyErr_SetString(PyExc_RuntimeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2193 "not holding the import lock");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2194 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2195 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2196 return result;
7533
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
2197 }
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
2198
32156
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2199 /* Return the package that an import is being performed in. If globals comes
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2200 from the module foo.bar.bat (not itself a package), this returns the
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2201 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
38471
9268c30782c0 Correct None refcount issue in Mac modules. (Are they
Georg Brandl <georg@python.org>
parents: 38465
diff changeset
2202 the package's entry in sys.modules is returned, as a borrowed reference.
32156
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2203
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2204 The *name* of the returned package is returned in buf, with the length of
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2205 the name in *p_buflen.
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2206
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2207 If globals doesn't come from a package or a module in a package, or a
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2208 corresponding entry is not found in sys.modules, Py_None is returned.
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2209 */
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2210 static PyObject *
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2211 get_parent(PyObject *globals, char *buf, Py_ssize_t *p_buflen, int level)
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2212 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2213 static PyObject *namestr = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2214 static PyObject *pathstr = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2215 static PyObject *pkgstr = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2216 PyObject *pkgname, *modname, *modpath, *modules, *parent;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2217 int orig_level = level;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2218
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2219 if (globals == NULL || !PyDict_Check(globals) || !level)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2220 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2221
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2222 if (namestr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2223 namestr = PyString_InternFromString("__name__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2224 if (namestr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2225 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2226 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2227 if (pathstr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2228 pathstr = PyString_InternFromString("__path__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2229 if (pathstr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2230 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2231 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2232 if (pkgstr == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2233 pkgstr = PyString_InternFromString("__package__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2234 if (pkgstr == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2235 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2236 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2237
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2238 *buf = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2239 *p_buflen = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2240 pkgname = PyDict_GetItem(globals, pkgstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2241
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2242 if ((pkgname != NULL) && (pkgname != Py_None)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2243 /* __package__ is set, so use it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2244 Py_ssize_t len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2245 if (!PyString_Check(pkgname)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2246 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2247 "__package__ set to non-string");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2248 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2249 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2250 len = PyString_GET_SIZE(pkgname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2251 if (len == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2252 if (level > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2253 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2254 "Attempted relative import in non-package");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2255 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2256 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2257 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2258 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2259 if (len > MAXPATHLEN) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2260 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2261 "Package name too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2262 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2263 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2264 strcpy(buf, PyString_AS_STRING(pkgname));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2265 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2266 /* __package__ not set, so figure it out and set it */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2267 modname = PyDict_GetItem(globals, namestr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2268 if (modname == NULL || !PyString_Check(modname))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2269 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2270
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2271 modpath = PyDict_GetItem(globals, pathstr);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2272 if (modpath != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2273 /* __path__ is set, so modname is already the package name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2274 Py_ssize_t len = PyString_GET_SIZE(modname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2275 int error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2276 if (len > MAXPATHLEN) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2277 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2278 "Module name too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2279 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2280 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2281 strcpy(buf, PyString_AS_STRING(modname));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2282 error = PyDict_SetItem(globals, pkgstr, modname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2283 if (error) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2284 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2285 "Could not set __package__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2286 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2287 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2288 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2289 /* Normal module, so work out the package name if any */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2290 char *start = PyString_AS_STRING(modname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2291 char *lastdot = strrchr(start, '.');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2292 size_t len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2293 int error;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2294 if (lastdot == NULL && level > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2295 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2296 "Attempted relative import in non-package");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2297 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2298 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2299 if (lastdot == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2300 error = PyDict_SetItem(globals, pkgstr, Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2301 if (error) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2302 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2303 "Could not set __package__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2304 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2305 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2306 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2307 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2308 len = lastdot - start;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2309 if (len >= MAXPATHLEN) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2310 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2311 "Module name too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2312 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2313 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2314 strncpy(buf, start, len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2315 buf[len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2316 pkgname = PyString_FromString(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2317 if (pkgname == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2318 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2319 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2320 error = PyDict_SetItem(globals, pkgstr, pkgname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2321 Py_DECREF(pkgname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2322 if (error) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2323 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2324 "Could not set __package__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2325 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2326 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2327 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2328 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2329 while (--level > 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2330 char *dot = strrchr(buf, '.');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2331 if (dot == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2332 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2333 "Attempted relative import beyond "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2334 "toplevel package");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2335 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2336 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2337 *dot = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2338 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2339 *p_buflen = strlen(buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2340
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2341 modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2342 parent = PyDict_GetItemString(modules, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2343 if (parent == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2344 if (orig_level < 1) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2345 PyObject *err_msg = PyString_FromFormat(
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2346 "Parent module '%.200s' not found "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2347 "while handling absolute import", buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2348 if (err_msg == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2349 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2350 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2351 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2352 PyString_AsString(err_msg), 1)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2353 *buf = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2354 *p_buflen = 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2355 parent = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2356 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2357 Py_DECREF(err_msg);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2358 } else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2359 PyErr_Format(PyExc_SystemError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2360 "Parent module '%.200s' not loaded, "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2361 "cannot perform relative import", buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2362 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2363 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2364 return parent;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2365 /* We expect, but can't guarantee, if parent != None, that:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2366 - parent.__name__ == buf
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2367 - parent.__dict__ is globals
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2368 If this is violated... Who cares? */
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2369 }
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2370
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2371 /* altmod is either None or same as mod */
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2372 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2373 load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2374 Py_ssize_t *p_buflen)
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2375 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2376 char *name = *p_name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2377 char *dot = strchr(name, '.');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2378 size_t len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2379 char *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2380 PyObject *result;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2381
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2382 if (strlen(name) == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2383 /* completely empty module name should only happen in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2384 'from . import' (or '__import__("")')*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2385 Py_INCREF(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2386 *p_name = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2387 return mod;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2388 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2389
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2390 if (dot == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2391 *p_name = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2392 len = strlen(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2393 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2394 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2395 *p_name = dot+1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2396 len = dot-name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2397 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2398 if (len == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2399 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2400 "Empty module name");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2401 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2402 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2403
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2404 p = buf + *p_buflen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2405 if (p != buf)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2406 *p++ = '.';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2407 if (p+len-buf >= MAXPATHLEN) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2408 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2409 "Module name too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2410 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2411 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2412 strncpy(p, name, len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2413 p[len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2414 *p_buflen = p+len-buf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2415
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2416 result = import_submodule(mod, p, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2417 if (result == Py_None && altmod != mod) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2418 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2419 /* Here, altmod must be None and mod must not be None */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2420 result = import_submodule(altmod, p, p);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2421 if (result != NULL && result != Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2422 if (mark_miss(buf) != 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2423 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2424 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2425 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2426 strncpy(buf, name, len);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2427 buf[len] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2428 *p_buflen = len;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2429 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2430 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2431 if (result == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2432 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2433
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2434 if (result == Py_None) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2435 Py_DECREF(result);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2436 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2437 "No module named %.200s", name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2438 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2439 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2440
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2441 return result;
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2442 }
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2443
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2444 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2445 mark_miss(char *name)
6252
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2446 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2447 PyObject *modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2448 return PyDict_SetItemString(modules, name, Py_None);
6252
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2449 }
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2450
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2451 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2452 ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2453 int recursive)
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2454 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2455 int i;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2456
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2457 if (!PyObject_HasAttrString(mod, "__path__"))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2458 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2459
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2460 for (i = 0; ; i++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2461 PyObject *item = PySequence_GetItem(fromlist, i);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2462 int hasit;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2463 if (item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2464 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2465 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2466 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2467 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2468 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2469 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2470 if (!PyString_Check(item)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2471 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2472 "Item in ``from list'' not a string");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2473 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2474 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2475 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2476 if (PyString_AS_STRING(item)[0] == '*') {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2477 PyObject *all;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2478 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2479 /* See if the package defines __all__ */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2480 if (recursive)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2481 continue; /* Avoid endless recursion */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2482 all = PyObject_GetAttrString(mod, "__all__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2483 if (all == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2484 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2485 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2486 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2487 Py_DECREF(all);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2488 if (!ret)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2489 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2490 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2491 continue;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2492 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2493 hasit = PyObject_HasAttr(mod, item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2494 if (!hasit) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2495 char *subname = PyString_AS_STRING(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2496 PyObject *submod;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2497 char *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2498 if (buflen + strlen(subname) >= MAXPATHLEN) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2499 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2500 "Module name too long");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2501 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2502 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2503 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2504 p = buf + buflen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2505 *p++ = '.';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2506 strcpy(p, subname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2507 submod = import_submodule(mod, subname, buf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2508 Py_XDECREF(submod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2509 if (submod == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2510 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2511 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2512 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2513 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2514 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2515 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2516
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2517 /* NOTREACHED */
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2518 }
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2519
29501
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2520 static int
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2521 add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2522 PyObject *modules)
29501
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2523 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2524 if (mod == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2525 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2526 /* Irrespective of the success of this load, make a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2527 reference to it in the parent package module. A copy gets
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2528 saved in the modules dictionary under the full name, so get a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2529 reference from there, if need be. (The exception is when the
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2530 load failed with a SyntaxError -- then there's no trace in
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2531 sys.modules. In that case, of course, do nothing extra.) */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2532 if (submod == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2533 submod = PyDict_GetItemString(modules, fullname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2534 if (submod == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2535 return 1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2536 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2537 if (PyModule_Check(mod)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2538 /* We can't use setattr here since it can give a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2539 * spurious warning if the submodule name shadows a
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2540 * builtin name */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2541 PyObject *dict = PyModule_GetDict(mod);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2542 if (!dict)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2543 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2544 if (PyDict_SetItemString(dict, subname, submod) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2545 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2546 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2547 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2548 if (PyObject_SetAttrString(mod, subname, submod) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2549 return 0;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2550 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2551 return 1;
29501
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2552 }
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2553
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2554 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2555 import_submodule(PyObject *mod, char *subname, char *fullname)
6249
f10aad73e029 Phase two of package import. "import a.b.c" and all variants now do the
Guido van Rossum <guido@python.org>
parents: 6243
diff changeset
2556 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2557 PyObject *modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2558 PyObject *m = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2559
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2560 /* Require:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2561 if mod == None: subname == fullname
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2562 else: mod.__name__ + "." + subname == fullname
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2563 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2564
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2565 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2566 Py_INCREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2567 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2568 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2569 PyObject *path, *loader = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2570 char buf[MAXPATHLEN+1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2571 struct filedescr *fdp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2572 FILE *fp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2573
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2574 if (mod == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2575 path = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2576 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2577 path = PyObject_GetAttrString(mod, "__path__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2578 if (path == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2579 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2580 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2581 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2582 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2583 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2584
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2585 buf[0] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2586 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2587 &fp, &loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2588 Py_XDECREF(path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2589 if (fdp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2590 if (!PyErr_ExceptionMatches(PyExc_ImportError))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2591 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2592 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2593 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2594 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2595 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2596 m = load_module(fullname, fp, buf, fdp->type, loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2597 Py_XDECREF(loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2598 if (fp)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2599 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2600 if (!add_submodule(mod, m, fullname, subname, modules)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2601 Py_XDECREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2602 m = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2603 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2604 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2605
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2606 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2607 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2608
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2609
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2610 /* Re-import a module of any kind and return its module object, WITH
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2611 INCREMENTED REFERENCE COUNT */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2612
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2613 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2614 PyImport_ReloadModule(PyObject *m)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2615 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2616 PyInterpreterState *interp = PyThreadState_Get()->interp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2617 PyObject *modules_reloading = interp->modules_reloading;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2618 PyObject *modules = PyImport_GetModuleDict();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2619 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2620 char *name, *subname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2621 char buf[MAXPATHLEN+1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2622 struct filedescr *fdp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2623 FILE *fp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2624 PyObject *newm;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2625
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2626 if (modules_reloading == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2627 Py_FatalError("PyImport_ReloadModule: "
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2628 "no modules_reloading dictionary!");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2629 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2630 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2631
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2632 if (m == NULL || !PyModule_Check(m)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2633 PyErr_SetString(PyExc_TypeError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2634 "reload() argument must be module");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2635 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2636 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2637 name = PyModule_GetName(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2638 if (name == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2639 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2640 if (m != PyDict_GetItemString(modules, name)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2641 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2642 "reload(): module %.200s not in sys.modules",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2643 name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2644 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2645 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2646 existing_m = PyDict_GetItemString(modules_reloading, name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2647 if (existing_m != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2648 /* Due to a recursive reload, this module is already
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2649 being reloaded. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2650 Py_INCREF(existing_m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2651 return existing_m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2652 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2653 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2654 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2655
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2656 subname = strrchr(name, '.');
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2657 if (subname == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2658 subname = name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2659 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2660 PyObject *parentname, *parent;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2661 parentname = PyString_FromStringAndSize(name, (subname-name));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2662 if (parentname == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2663 imp_modules_reloading_clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2664 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2665 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2666 parent = PyDict_GetItem(modules, parentname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2667 if (parent == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2668 PyErr_Format(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2669 "reload(): parent %.200s not in sys.modules",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2670 PyString_AS_STRING(parentname));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2671 Py_DECREF(parentname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2672 imp_modules_reloading_clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2673 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2674 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2675 Py_DECREF(parentname);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2676 subname++;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2677 path = PyObject_GetAttrString(parent, "__path__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2678 if (path == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2679 PyErr_Clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2680 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2681 buf[0] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2682 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2683 Py_XDECREF(path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2684
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2685 if (fdp == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2686 Py_XDECREF(loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2687 imp_modules_reloading_clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2688 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2689 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2690
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2691 newm = load_module(name, fp, buf, fdp->type, loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2692 Py_XDECREF(loader);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2693
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2694 if (fp)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2695 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2696 if (newm == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2697 /* load_module probably removed name from modules because of
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2698 * the error. Put back the original module object. We're
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2699 * going to return NULL in this case regardless of whether
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2700 * replacing name succeeds, so the return value is ignored.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2701 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2702 PyDict_SetItemString(modules, name, m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2703 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2704 imp_modules_reloading_clear();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2705 return newm;
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
2706 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2707
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2708
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2709 /* Higher-level import emulator which emulates the "import" statement
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2710 more accurately -- it invokes the __import__() function from the
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2711 builtins of the current globals. This means that the import is
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2712 done using whatever import hooks are installed in the current
9680
8b61e1273b4b Improve comment for PyImport_Import() as suggested by Bill Tutt.
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
2713 environment, e.g. by "rexec".
8b61e1273b4b Improve comment for PyImport_Import() as suggested by Bill Tutt.
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
2714 A dummy list ["__doc__"] is passed as the 4th argument so that
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
2715 e.g. PyImport_Import(PyString_FromString("win32com.client.gencache"))
9680
8b61e1273b4b Improve comment for PyImport_Import() as suggested by Bill Tutt.
Guido van Rossum <guido@python.org>
parents: 9679
diff changeset
2716 will return <module "gencache"> instead of <module "win32com">. */
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2717
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2718 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2719 PyImport_Import(PyObject *module_name)
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2720 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2721 static PyObject *silly_list = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2722 static PyObject *builtins_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2723 static PyObject *import_str = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2724 PyObject *globals = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2725 PyObject *import = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2726 PyObject *builtins = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2727 PyObject *r = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2728
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2729 /* Initialize constant string objects */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2730 if (silly_list == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2731 import_str = PyString_InternFromString("__import__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2732 if (import_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2733 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2734 builtins_str = PyString_InternFromString("__builtins__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2735 if (builtins_str == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2736 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2737 silly_list = Py_BuildValue("[s]", "__doc__");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2738 if (silly_list == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2739 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2740 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2741
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2742 /* Get the builtins from current globals */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2743 globals = PyEval_GetGlobals();
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2744 if (globals != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2745 Py_INCREF(globals);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2746 builtins = PyObject_GetItem(globals, builtins_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2747 if (builtins == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2748 goto err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2749 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2750 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2751 /* No globals -- use standard builtins, and fake globals */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2752 builtins = PyImport_ImportModuleLevel("__builtin__",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2753 NULL, NULL, NULL, 0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2754 if (builtins == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2755 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2756 globals = Py_BuildValue("{OO}", builtins_str, builtins);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2757 if (globals == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2758 goto err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2759 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2760
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2761 /* Get the __import__ function from the builtins */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2762 if (PyDict_Check(builtins)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2763 import = PyObject_GetItem(builtins, import_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2764 if (import == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2765 PyErr_SetObject(PyExc_KeyError, import_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2766 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2767 else
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2768 import = PyObject_GetAttr(builtins, import_str);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2769 if (import == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2770 goto err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2771
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2772 /* Call the __import__ function with the proper argument list
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2773 * Always use absolute import here. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2774 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2775 globals, silly_list, 0, NULL);
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2776
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2777 err:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2778 Py_XDECREF(globals);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2779 Py_XDECREF(builtins);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2780 Py_XDECREF(import);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2781
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2782 return r;
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2783 }
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2784
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2785
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2786 /* Module 'imp' provides Python access to the primitives used for
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2787 importing modules.
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2788 */
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2789
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2790 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
2791 imp_get_magic(PyObject *self, PyObject *noargs)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2792 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2793 char buf[4];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2794
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2795 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2796 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2797 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2798 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2799
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2800 return PyString_FromStringAndSize(buf, 4);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2801 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2802
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2803 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
2804 imp_get_suffixes(PyObject *self, PyObject *noargs)
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2805 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2806 PyObject *list;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2807 struct filedescr *fdp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2808
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2809 list = PyList_New(0);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2810 if (list == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2811 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2812 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2813 PyObject *item = Py_BuildValue("ssi",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2814 fdp->suffix, fdp->mode, fdp->type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2815 if (item == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2816 Py_DECREF(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2817 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2818 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2819 if (PyList_Append(list, item) < 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2820 Py_DECREF(list);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2821 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2822 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2823 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2824 Py_DECREF(item);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2825 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2826 return list;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2827 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2828
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2829 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2830 call_find_module(char *name, PyObject *path)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2831 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2832 extern int fclose(FILE *);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2833 PyObject *fob, *ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2834 struct filedescr *fdp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2835 char pathname[MAXPATHLEN+1];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2836 FILE *fp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2837
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2838 pathname[0] = '\0';
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2839 if (path == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2840 path = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2841 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2842 if (fdp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2843 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2844 if (fp != NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2845 fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2846 if (fob == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2847 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2848 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2849 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2850 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2851 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2852 fob = Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2853 Py_INCREF(fob);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2854 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2855 ret = Py_BuildValue("Os(ssi)",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2856 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2857 Py_DECREF(fob);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2858 return ret;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2859 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2860
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2861 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2862 imp_find_module(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2863 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2864 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2865 PyObject *path = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2866 if (!PyArg_ParseTuple(args, "s|O:find_module", &name, &path))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2867 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2868 return call_find_module(name, path);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2869 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2870
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2871 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2872 imp_init_builtin(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2873 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2874 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2875 int ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2876 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2877 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2878 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2879 ret = init_builtin(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2880 if (ret < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2881 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2882 if (ret == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2883 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2884 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2885 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2886 m = PyImport_AddModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2887 Py_XINCREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2888 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2889 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2890
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2891 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2892 imp_init_frozen(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2893 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2894 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2895 int ret;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2896 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2897 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2898 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2899 ret = PyImport_ImportFrozenModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2900 if (ret < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2901 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2902 if (ret == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2903 Py_INCREF(Py_None);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2904 return Py_None;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2905 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2906 m = PyImport_AddModule(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2907 Py_XINCREF(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2908 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2909 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2910
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2911 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2912 imp_get_frozen_object(PyObject *self, PyObject *args)
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
2913 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2914 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2915
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2916 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2917 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2918 return get_frozen_object(name);
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
2919 }
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
2920
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2921 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2922 imp_is_builtin(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2923 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2924 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2925 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2926 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2927 return PyInt_FromLong(is_builtin(name));
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2928 }
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2929
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2930 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2931 imp_is_frozen(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2932 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2933 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2934 struct _frozen *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2935 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2936 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2937 p = find_frozen(name);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2938 return PyBool_FromLong((long) (p == NULL ? 0 : p->size));
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2939 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2940
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2941 static FILE *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2942 get_file(char *pathname, PyObject *fob, char *mode)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2943 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2944 FILE *fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2945 if (fob == NULL) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2946 if (mode[0] == 'U')
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2947 mode = "r" PY_STDIOTEXTMODE;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2948 fp = fopen(pathname, mode);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2949 if (fp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2950 PyErr_SetFromErrno(PyExc_IOError);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2951 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2952 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2953 fp = PyFile_AsFile(fob);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2954 if (fp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2955 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2956 "bad/closed file object");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2957 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2958 return fp;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2959 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2960
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2961 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2962 imp_load_compiled(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2963 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2964 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2965 char *pathname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2966 PyObject *fob = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2967 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2968 FILE *fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2969 if (!PyArg_ParseTuple(args, "ss|O!:load_compiled", &name, &pathname,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2970 &PyFile_Type, &fob))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2971 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2972 fp = get_file(pathname, fob, "rb");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2973 if (fp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2974 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2975 m = load_compiled_module(name, pathname, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2976 if (fob == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2977 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2978 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2979 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2980
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
2981 #ifdef HAVE_DYNAMIC_LOADING
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
2982
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2983 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2984 imp_load_dynamic(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2985 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2986 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2987 char *pathname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2988 PyObject *fob = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2989 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2990 FILE *fp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2991 if (!PyArg_ParseTuple(args, "ss|O!:load_dynamic", &name, &pathname,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2992 &PyFile_Type, &fob))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2993 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2994 if (fob) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2995 fp = get_file(pathname, fob, "r");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2996 if (fp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2997 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2998 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
2999 m = _PyImport_LoadDynamicModule(name, pathname, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3000 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3001 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3002
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
3003 #endif /* HAVE_DYNAMIC_LOADING */
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
3004
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3005 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3006 imp_load_source(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3007 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3008 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3009 char *pathname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3010 PyObject *fob = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3011 PyObject *m;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3012 FILE *fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3013 if (!PyArg_ParseTuple(args, "ss|O!:load_source", &name, &pathname,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3014 &PyFile_Type, &fob))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3015 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3016 fp = get_file(pathname, fob, "r");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3017 if (fp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3018 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3019 m = load_source_module(name, pathname, fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3020 if (fob == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3021 fclose(fp);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3022 return m;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3023 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3024
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3025 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3026 imp_load_module(PyObject *self, PyObject *args)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3027 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3028 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3029 PyObject *fob;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3030 char *pathname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3031 char *suffix; /* Unused */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3032 char *mode;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3033 int type;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3034 FILE *fp;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3035
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3036 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3037 &name, &fob, &pathname,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3038 &suffix, &mode, &type))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3039 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3040 if (*mode) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3041 /* Mode must start with 'r' or 'U' and must not contain '+'.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3042 Implicit in this test is the assumption that the mode
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3043 may contain other modifiers like 'b' or 't'. */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3044
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3045 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3046 PyErr_Format(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3047 "invalid file open mode %.200s", mode);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3048 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3049 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3050 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3051 if (fob == Py_None)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3052 fp = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3053 else {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3054 if (!PyFile_Check(fob)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3055 PyErr_SetString(PyExc_ValueError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3056 "load_module arg#2 should be a file or None");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3057 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3058 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3059 fp = get_file(pathname, fob, mode);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3060 if (fp == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3061 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3062 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3063 return load_module(name, fp, pathname, type, NULL);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3064 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3065
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3066 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3067 imp_load_package(PyObject *self, PyObject *args)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3068 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3069 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3070 char *pathname;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3071 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3072 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3073 return load_package(name, pathname);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3074 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3075
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3076 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3077 imp_new_module(PyObject *self, PyObject *args)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3078 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3079 char *name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3080 if (!PyArg_ParseTuple(args, "s:new_module", &name))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3081 return NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3082 return PyModule_New(name);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3083 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3084
48189
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3085 static PyObject *
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3086 imp_reload(PyObject *self, PyObject *v)
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3087 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3088 return PyImport_ReloadModule(v);
48189
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3089 }
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3090
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3091
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3092 /* Doc strings */
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3093
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3094 PyDoc_STRVAR(doc_imp,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3095 "This module provides the components needed to build your own\n\
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3096 __import__ function. Undocumented functions are obsolete.");
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3097
48189
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3098 PyDoc_STRVAR(doc_reload,
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3099 "reload(module) -> module\n\
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3100 \n\
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3101 Reload the module. The module must have been successfully imported before.");
53ade9e68b56 Add imp.reload(). This to help with transitioning to 3.0 the reload() built-in
Brett Cannon <bcannon@gmail.com>
parents: 47944
diff changeset
3102
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3103 PyDoc_STRVAR(doc_find_module,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3104 "find_module(name, [path]) -> (file, filename, (suffix, mode, type))\n\
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3105 Search for a module. If path is omitted or None, search for a\n\
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3106 built-in, frozen or special module and continue search in sys.path.\n\
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3107 The module name cannot contain '.'; to search for a submodule of a\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3108 package, pass the submodule name and the package's __path__.");
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3109
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3110 PyDoc_STRVAR(doc_load_module,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3111 "load_module(name, file, filename, (suffix, mode, type)) -> module\n\
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3112 Load a module, given information returned by find_module().\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3113 The module name must include the full package name, if any.");
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3114
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3115 PyDoc_STRVAR(doc_get_magic,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3116 "get_magic() -> string\n\
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3117 Return the magic number for .pyc or .pyo files.");
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3118
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3119 PyDoc_STRVAR(doc_get_suffixes,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3120 "get_suffixes() -> [(suffix, mode, type), ...]\n\
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3121 Return a list of (suffix, mode, type) tuples describing the files\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3122 that find_module() looks for.");
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3123
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3124 PyDoc_STRVAR(doc_new_module,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3125 "new_module(name) -> module\n\
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3126 Create a new module. Do not enter it in sys.modules.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3127 The module name must include the full package name, if any.");
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3128
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3129 PyDoc_STRVAR(doc_lock_held,
32879
d3d609d657fd lock_held() docs: Use True/False instead of 1/0. The LaTeX docs were
Tim Peters <tim.peters@gmail.com>
parents: 32878
diff changeset
3130 "lock_held() -> boolean\n\
d3d609d657fd lock_held() docs: Use True/False instead of 1/0. The LaTeX docs were
Tim Peters <tim.peters@gmail.com>
parents: 32878
diff changeset
3131 Return True if the import lock is currently held, else False.\n\
d3d609d657fd lock_held() docs: Use True/False instead of 1/0. The LaTeX docs were
Tim Peters <tim.peters@gmail.com>
parents: 32878
diff changeset
3132 On platforms without threads, return False.");
19596
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 19238
diff changeset
3133
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3134 PyDoc_STRVAR(doc_acquire_lock,
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3135 "acquire_lock() -> None\n\
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents: 27852
diff changeset
3136 Acquires the interpreter's import lock for the current thread.\n\
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents: 27852
diff changeset
3137 This lock should be used by import hooks to ensure thread-safety\n\
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents: 27852
diff changeset
3138 when importing modules.\n\
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3139 On platforms without threads, this function does nothing.");
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3140
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3141 PyDoc_STRVAR(doc_release_lock,
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3142 "release_lock() -> None\n\
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3143 Release the interpreter's import lock.\n\
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3144 On platforms without threads, this function does nothing.");
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3145
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3146 static PyMethodDef imp_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3147 {"reload", imp_reload, METH_O, doc_reload},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3148 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3149 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3150 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3151 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3152 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3153 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3154 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3155 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3156 /* The rest are obsolete */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3157 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3158 {"init_builtin", imp_init_builtin, METH_VARARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3159 {"init_frozen", imp_init_frozen, METH_VARARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3160 {"is_builtin", imp_is_builtin, METH_VARARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3161 {"is_frozen", imp_is_frozen, METH_VARARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3162 {"load_compiled", imp_load_compiled, METH_VARARGS},
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
3163 #ifdef HAVE_DYNAMIC_LOADING
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3164 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
3165 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3166 {"load_package", imp_load_package, METH_VARARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3167 {"load_source", imp_load_source, METH_VARARGS},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3168 {NULL, NULL} /* sentinel */
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3169 };
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3170
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8613
diff changeset
3171 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3172 setint(PyObject *d, char *name, int value)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3173 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3174 PyObject *v;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3175 int err;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3176
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3177 v = PyInt_FromLong((long)value);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3178 err = PyDict_SetItemString(d, name, v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3179 Py_XDECREF(v);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3180 return err;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3181 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3182
39253
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3183 typedef struct {
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3184 PyObject_HEAD
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3185 } NullImporter;
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3186
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3187 static int
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3188 NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3189 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3190 char *path;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3191 Py_ssize_t pathlen;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3192
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3193 if (!_PyArg_NoKeywords("NullImporter()", kwds))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3194 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3195
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3196 if (!PyArg_ParseTuple(args, "s:NullImporter",
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3197 &path))
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3198 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3199
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3200 pathlen = strlen(path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3201 if (pathlen == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3202 PyErr_SetString(PyExc_ImportError, "empty pathname");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3203 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3204 } else {
39253
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3205 #ifndef RISCOS
50671
ca098fc18ec6 Issue 3677: Fix import from UNC paths on Windows.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50616
diff changeset
3206 #ifndef MS_WINDOWS
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3207 struct stat statbuf;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3208 int rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3209
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3210 rv = stat(path, &statbuf);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3211 if (rv == 0) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3212 /* it exists */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3213 if (S_ISDIR(statbuf.st_mode)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3214 /* it's a directory */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3215 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3216 "existing directory");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3217 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3218 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3219 }
50671
ca098fc18ec6 Issue 3677: Fix import from UNC paths on Windows.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50616
diff changeset
3220 #else /* MS_WINDOWS */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3221 DWORD rv;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3222 /* see issue1293 and issue3677:
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3223 * stat() on Windows doesn't recognise paths like
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3224 * "e:\\shared\\" and "\\\\whiterab-c2znlh\\shared" as dirs.
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3225 */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3226 rv = GetFileAttributesA(path);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3227 if (rv != INVALID_FILE_ATTRIBUTES) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3228 /* it exists */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3229 if (rv & FILE_ATTRIBUTE_DIRECTORY) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3230 /* it's a directory */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3231 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3232 "existing directory");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3233 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3234 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3235 }
50671
ca098fc18ec6 Issue 3677: Fix import from UNC paths on Windows.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50616
diff changeset
3236 #endif
ca098fc18ec6 Issue 3677: Fix import from UNC paths on Windows.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 50616
diff changeset
3237 #else /* RISCOS */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3238 if (object_exists(path)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3239 /* it exists */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3240 if (isdir(path)) {
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3241 /* it's a directory */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3242 PyErr_SetString(PyExc_ImportError,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3243 "existing directory");
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3244 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3245 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3246 }
39253
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3247 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3248 }
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3249 return 0;
39253
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3250 }
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3251
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3252 static PyObject *
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3253 NullImporter_find_module(NullImporter *self, PyObject *args)
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3254 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3255 Py_RETURN_NONE;
39253
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3256 }
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3257
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3258 static PyMethodDef NullImporter_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3259 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3260 "Always return None"
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3261 },
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3262 {NULL} /* Sentinel */
39253
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3263 };
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3264
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3265
43895
4cec0c6392c1 Patch #1739468: Directories and zipfiles containing __main__.py are now executable
Nick Coghlan <ncoghlan@gmail.com>
parents: 43792
diff changeset
3266 PyTypeObject PyNullImporter_Type = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3267 PyVarObject_HEAD_INIT(NULL, 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3268 "imp.NullImporter", /*tp_name*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3269 sizeof(NullImporter), /*tp_basicsize*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3270 0, /*tp_itemsize*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3271 0, /*tp_dealloc*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3272 0, /*tp_print*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3273 0, /*tp_getattr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3274 0, /*tp_setattr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3275 0, /*tp_compare*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3276 0, /*tp_repr*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3277 0, /*tp_as_number*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3278 0, /*tp_as_sequence*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3279 0, /*tp_as_mapping*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3280 0, /*tp_hash */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3281 0, /*tp_call*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3282 0, /*tp_str*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3283 0, /*tp_getattro*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3284 0, /*tp_setattro*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3285 0, /*tp_as_buffer*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3286 Py_TPFLAGS_DEFAULT, /*tp_flags*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3287 "Null importer object", /* tp_doc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3288 0, /* tp_traverse */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3289 0, /* tp_clear */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3290 0, /* tp_richcompare */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3291 0, /* tp_weaklistoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3292 0, /* tp_iter */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3293 0, /* tp_iternext */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3294 NullImporter_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3295 0, /* tp_members */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3296 0, /* tp_getset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3297 0, /* tp_base */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3298 0, /* tp_dict */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3299 0, /* tp_descr_get */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3300 0, /* tp_descr_set */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3301 0, /* tp_dictoffset */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3302 (initproc)NullImporter_init, /* tp_init */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3303 0, /* tp_alloc */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3304 PyType_GenericNew /* tp_new */
39253
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3305 };
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3306
3f4485b20e79 Bug #1529871: The speed enhancement patch #921466 broke Python's compliance
Phillip J. Eby <pje@telecommunity.com>
parents: 39130
diff changeset
3307
30311
f003e48f562d Bug #794140: cygwin builds do not embed
Jason Tishler <jason@tishler.net>
parents: 29812
diff changeset
3308 PyMODINIT_FUNC
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3309 initimp(void)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3310 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3311 PyObject *m, *d;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3312
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3313 if (PyType_Ready(&PyNullImporter_Type) < 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3314 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3315
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3316 m = Py_InitModule4("imp", imp_methods, doc_imp,
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3317 NULL, PYTHON_API_VERSION);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3318 if (m == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3319 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3320 d = PyModule_GetDict(m);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3321 if (d == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3322 goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3323
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3324 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3325 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3326 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3327 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3328 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3329 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3330 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3331 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3332 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3333 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3334
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3335 Py_INCREF(&PyNullImporter_Type);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3336 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3337 failure:
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3338 ;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3339 }
8330
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3340
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3341
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12543
diff changeset
3342 /* API for embedding applications that want to add their own entries
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12543
diff changeset
3343 to the table of built-in modules. This should normally be called
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12543
diff changeset
3344 *before* Py_Initialize(). When the table resize fails, -1 is
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12543
diff changeset
3345 returned and the existing table is unchanged.
8330
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3346
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3347 After a similar function by Just van Rossum. */
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3348
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3349 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3350 PyImport_ExtendInittab(struct _inittab *newtab)
8330
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3351 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3352 static struct _inittab *our_copy = NULL;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3353 struct _inittab *p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3354 int i, n;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3355
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3356 /* Count the number of entries in both tables */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3357 for (n = 0; newtab[n].name != NULL; n++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3358 ;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3359 if (n == 0)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3360 return 0; /* Nothing to do */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3361 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3362 ;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3363
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3364 /* Allocate new memory for the combined table */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3365 p = our_copy;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3366 PyMem_RESIZE(p, struct _inittab, i+n+1);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3367 if (p == NULL)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3368 return -1;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3369
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3370 /* Copy the tables into the new memory */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3371 if (our_copy != PyImport_Inittab)
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3372 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3373 PyImport_Inittab = our_copy = p;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3374 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3375
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3376 return 0;
8330
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3377 }
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3378
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3379 /* Shorthand to add a single entry given a name and a function */
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3380
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3381 int
52718
1ef3da6c434c PyImport_AppendInittab() took a char * as a first argument even though that
Brett Cannon <bcannon@gmail.com>
parents: 51920
diff changeset
3382 PyImport_AppendInittab(const char *name, void (*initfunc)(void))
8330
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3383 {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3384 struct _inittab newtab[2];
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3385
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3386 memset(newtab, '\0', sizeof newtab);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3387
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3388 newtab[0].name = (char *)name;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3389 newtab[0].initfunc = initfunc;
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3390
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 61269
diff changeset
3391 return PyImport_ExtendInittab(newtab);
8330
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3392 }
37655
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37488
diff changeset
3393
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37488
diff changeset
3394 #ifdef __cplusplus
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37488
diff changeset
3395 }
2f3f2e32dfd9 spread the extern "C" { } magic pixie dust around. Python itself builds now
Anthony Baxter <anthonybaxter@gmail.com>
parents: 37488
diff changeset
3396 #endif