annotate Python/import.c @ 49020:3fa066d2a397

#3773: Check for errors around the use of PyTokenizer_FindEncoding(). reviewed by Brett Cannon.
author Amaury Forgeot d'Arc <amauryfa@gmail.com>
date Thu, 04 Sep 2008 22:34:09 +0000
parents 812ea105b1b0
children f7123d45d0c4
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"
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
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
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
21 #ifdef __cplusplus
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
22 extern "C" {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
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
45495
48488fa50729 Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
25 #ifdef MS_WINDOWS
48488fa50729 Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
26 /* for stat.st_mode */
48488fa50729 Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
27 typedef unsigned short mode_t;
48488fa50729 Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
28 #endif
48488fa50729 Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45471
diff changeset
29
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
30 extern time_t PyOS_GetLastModificationTime(char *, FILE *);
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
31 /* In getmtime.c */
1351
e149c77d173c Makefile, import.c: Lance's alternative module search (allow .pyc file
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
32
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
33 /* 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
34 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
35
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
36 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
37 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
38 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
39
36988
49c6eb688906 Checkpoint. 218 tests are okay; 53 are failing. Done so far:
Guido van Rossum <guido@python.org>
parents: 36926
diff changeset
40 The magic numbers must be spaced apart at least 2 values, as the
32422
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32294
diff changeset
41 -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
42 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
43
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
44 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
45 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
46 10.
25085
5fe4d93df429 Because MWH changed the bytecode again, moved the magic number *back*
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
47
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
48 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
49 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
50 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
51 Python 1.5.2: 20121
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
52 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
53 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
54 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
55 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
56 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
57 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
58 Python 2.2: 60717
23851
d6ae544acd24 Fix SF bug # 561858 Assertion with very long lists
Neal Norwitz <nnorwitz@gmail.com>
parents: 23832
diff changeset
59 Python 2.3a0: 62011
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24071
diff changeset
60 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
61 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
62 Python 2.4a0: 62041
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 32882
diff changeset
63 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
64 Python 2.4b1: 62061
35008
44e3f21f0525 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34961
diff changeset
65 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
66 Python 2.5a0: 62081 (ast-branch)
36695
b6d2f6fd3e11 PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents: 36501
diff changeset
67 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
68 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
69 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
70 Python 2.5b3: 62111 (fix wrong code: x += yield)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
71 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
72 storing constants that should have been removed)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
73 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44142
diff changeset
74 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
45773
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45495
diff changeset
75 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
36988
49c6eb688906 Checkpoint. 218 tests are okay; 53 are failing. Done so far:
Guido van Rossum <guido@python.org>
parents: 36926
diff changeset
76 Python 3000: 3000
39678
f87a3e61853d Remove the UNARY_CONVERT opcode (was used for backticks). Also bumped up the
Brett Cannon <bcannon@gmail.com>
parents: 39610
diff changeset
77 3010 (removed UNARY_CONVERT)
39693
d0b83c4630c0 SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents: 39678
diff changeset
78 3020 (added BUILD_SET)
40215
1cce0526d9b0 Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents: 39693
diff changeset
79 3030 (added keyword-only parameters)
40530
7a35d3d1ac5a Update the magic number now that signature annotations were added.
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
80 3040 (added signature annotations)
40774
793e0323d4d6 Essential changes for print function changes.
Guido van Rossum <guido@python.org>
parents: 40530
diff changeset
81 3050 (print becomes a function)
41226
19f7ff443718 Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents: 40774
diff changeset
82 3060 (PEP 3115 metaclass syntax)
43569
19319e70fc3e Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 43225
diff changeset
83 3070 (PEP 3109 raise changes)
19319e70fc3e Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents: 43225
diff changeset
84 3080 (PEP 3137 make __file__ and __name__ unicode)
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43762
diff changeset
85 3090 (kill str8 interning)
44349
4dc01402d78a Merged revisions 59541-59561 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44142
diff changeset
86 3100 (merge from 2.6a0, see 62151)
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
87 3102 (__file__ points to source file)
45773
832c820e9d14 Merged revisions 61239-61249,61252-61257,61260-61264,61269-61275,61278-61279,61285-61286,61288-61290,61298,61303-61305,61312-61314,61317,61329,61332,61344,61350-61351,61363-61376,61378-61379,61382-61383,61387-61388,61392,61395-61396,61402-61403 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45495
diff changeset
88 Python 3.0a4: 3110 (WITH_CLEANUP optimization).
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
89 Python 3.0a5: 3130 (lexical exception stacking, including POP_EXCEPT)
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
90 */
47594
212a1fee6bf9 #3021: Antoine Pitrou's Lexical exception handlers
Benjamin Peterson <benjamin@python.org>
parents: 47580
diff changeset
91 #define MAGIC (3130 | ((long)'\r'<<16) | ((long)'\n'<<24))
487
77b80a91d357 New magic word; and check it.
Guido van Rossum <guido@python.org>
parents: 340
diff changeset
92
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
93 /* 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
94 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
95 compiler works which are enabled by command line switches. */
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
96 static long pyc_magic = MAGIC;
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
97
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
98 /* 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
99 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
100
6541
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
101 /* 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
102 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
103
43605
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
104 /* Method from Parser/tokenizer.c */
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
105 extern char * PyTokenizer_FindEncoding(int);
43605
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
106
6541
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
107 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
108
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
109 /* 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
110 struct filedescr * _PyImport_Filetab = NULL;
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
111
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
112 static const struct filedescr _PyImport_StandardFiletab[] = {
23554
e5242f58a5b6 File modes in filedescr entries are also passed to Python, so we now put "U"
Jack Jansen <jack.jansen@cwi.nl>
parents: 23029
diff changeset
113 {".py", "U", PY_SOURCE},
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23879
diff changeset
114 #ifdef MS_WINDOWS
23554
e5242f58a5b6 File modes in filedescr entries are also passed to Python, so we now put "U"
Jack Jansen <jack.jansen@cwi.nl>
parents: 23029
diff changeset
115 {".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
116 #endif
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
117 {".pyc", "rb", PY_COMPILED},
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
118 {0, 0}
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
119 };
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
120
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
121
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
122 /* Initialize things */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
123
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
124 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
125 _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
126 {
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
127 const struct filedescr *scan;
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
128 struct filedescr *filetab;
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
129 int countD = 0;
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
130 int countS = 0;
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
131
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
132 /* prepare _PyImport_Filetab: copy entries from
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
133 _PyImport_DynLoadFiletab and _PyImport_StandardFiletab.
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
134 */
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42622
diff changeset
135 #ifdef HAVE_DYNAMIC_LOADING
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
136 for (scan = _PyImport_DynLoadFiletab; scan->suffix != NULL; ++scan)
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
137 ++countD;
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42622
diff changeset
138 #endif
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
139 for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
140 ++countS;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12543
diff changeset
141 filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
142 if (filetab == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
143 Py_FatalError("Can't initialize import file table.");
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42622
diff changeset
144 #ifdef HAVE_DYNAMIC_LOADING
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
145 memcpy(filetab, _PyImport_DynLoadFiletab,
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
146 countD * sizeof(struct filedescr));
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42622
diff changeset
147 #endif
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
148 memcpy(filetab + countD, _PyImport_StandardFiletab,
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
149 countS * sizeof(struct filedescr));
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
150 filetab[countD + countS].suffix = NULL;
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
151
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
152 _PyImport_Filetab = filetab;
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
153
5180
6bc56259cc4c When -O is given, use ".pyo" instead of ".pyc".
Guido van Rossum <guido@python.org>
parents: 5043
diff changeset
154 if (Py_OptimizeFlag) {
11602
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
155 /* Replace ".pyc" with ".pyo" in _PyImport_Filetab */
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
156 for (; filetab->suffix != NULL; filetab++) {
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
157 if (strcmp(filetab->suffix, ".pyc") == 0)
5738ad737114 In _PyImport_Init(), dynamically construct the table of legal suffixes
Guido van Rossum <guido@python.org>
parents: 10530
diff changeset
158 filetab->suffix = ".pyo";
5180
6bc56259cc4c When -O is given, use ".pyo" instead of ".pyc".
Guido van Rossum <guido@python.org>
parents: 5043
diff changeset
159 }
6bc56259cc4c When -O is given, use ".pyo" instead of ".pyc".
Guido van Rossum <guido@python.org>
parents: 5043
diff changeset
160 }
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
161
41558
29ae667f3239 Checkpoint. Manipulated things so that string literals are always
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
162 {
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
163 /* Fix the pyc_magic so that byte compiled code created
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
164 using the all-Unicode method doesn't interfere with
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
165 code created in normal operation mode. */
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
166 pyc_magic = MAGIC + 1;
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
167 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
168 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
169
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
170 void
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
171 _PyImportHooks_Init(void)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
172 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
173 PyObject *v, *path_hooks = NULL, *zimpimport;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
174 int err = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
175
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
176 /* adding sys.path_hooks and sys.path_importer_cache, setting up
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
177 zipimport */
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
178 if (PyType_Ready(&PyNullImporter_Type) < 0)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
179 goto error;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
180
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
181 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
182 PySys_WriteStderr("# installing zipimport hook\n");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
183
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
184 v = PyList_New(0);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
185 if (v == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
186 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
187 err = PySys_SetObject("meta_path", v);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
188 Py_DECREF(v);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
189 if (err)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
190 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
191 v = PyDict_New();
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
192 if (v == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
193 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
194 err = PySys_SetObject("path_importer_cache", v);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
195 Py_DECREF(v);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
196 if (err)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
197 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
198 path_hooks = PyList_New(0);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
199 if (path_hooks == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
200 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
201 err = PySys_SetObject("path_hooks", path_hooks);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
202 if (err) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
203 error:
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
204 PyErr_Print();
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
205 Py_FatalError("initializing sys.meta_path, sys.path_hooks, "
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
206 "path_importer_cache, or NullImporter failed"
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
207 );
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
208 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
209
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
210 zimpimport = PyImport_ImportModule("zipimport");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
211 if (zimpimport == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
212 PyErr_Clear(); /* No zip import module -- okay */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
213 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
214 PySys_WriteStderr("# can't import zipimport\n");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
215 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
216 else {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
217 PyObject *zipimporter = PyObject_GetAttrString(zimpimport,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
218 "zipimporter");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
219 Py_DECREF(zimpimport);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
220 if (zipimporter == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
221 PyErr_Clear(); /* No zipimporter object -- okay */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
222 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
223 PySys_WriteStderr(
29812
3b41c633d769 - fix typo
Fred Drake <fdrake@acm.org>
parents: 29501
diff changeset
224 "# can't import zipimport.zipimporter\n");
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
225 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
226 else {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
227 /* sys.path_hooks.append(zipimporter) */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
228 err = PyList_Append(path_hooks, zipimporter);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
229 Py_DECREF(zipimporter);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
230 if (err)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
231 goto error;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
232 if (Py_VerboseFlag)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
233 PySys_WriteStderr(
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
234 "# installed zipimport hook\n");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
235 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
236 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
237 Py_DECREF(path_hooks);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
238 }
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 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
241 _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
242 {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
243 Py_XDECREF(extensions);
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
244 extensions = NULL;
14991
5d41ca49a10a _PyImport_Fini(): Closed small memory leak when an embedded app calls
Barry Warsaw <barry@python.org>
parents: 14921
diff changeset
245 PyMem_DEL(_PyImport_Filetab);
5d41ca49a10a _PyImport_Fini(): Closed small memory leak when an embedded app calls
Barry Warsaw <barry@python.org>
parents: 14921
diff changeset
246 _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
247 }
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
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
250 /* 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
251 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
252 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
253
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 #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
255
9203
07dae6fdae79 Renamed thread.h to pythread.h.
Guido van Rossum <guido@python.org>
parents: 9187
diff changeset
256 #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
257
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9431
diff changeset
258 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
259 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
260 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
261
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 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
263 lock_import(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
264 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9431
diff changeset
265 long me = PyThread_get_thread_ident();
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
266 if (me == -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
267 return; /* Too bad */
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
268 if (import_lock == NULL) {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9431
diff changeset
269 import_lock = PyThread_allocate_lock();
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
270 if (import_lock == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
271 return; /* Nothing much we can do. */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
272 }
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
273 if (import_lock_thread == me) {
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
274 import_lock_level++;
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
275 return;
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
276 }
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
277 if (import_lock_thread != -1 || !PyThread_acquire_lock(import_lock, 0))
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
278 {
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
279 PyThreadState *tstate = PyEval_SaveThread();
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9431
diff changeset
280 PyThread_acquire_lock(import_lock, 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
281 PyEval_RestoreThread(tstate);
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
282 }
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
283 import_lock_thread = me;
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
284 import_lock_level = 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
285 }
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
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
287 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
288 unlock_import(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
289 {
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9431
diff changeset
290 long me = PyThread_get_thread_ident();
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
291 if (me == -1 || import_lock == NULL)
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
292 return 0; /* Too bad */
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
293 if (import_lock_thread != me)
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
294 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
295 import_lock_level--;
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
296 if (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
297 import_lock_thread = -1;
9679
877d23bd5bdc Thanks to Chris Herborth, the thread primitives now have proper Py*
Guido van Rossum <guido@python.org>
parents: 9431
diff changeset
298 PyThread_release_lock(import_lock);
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
299 }
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
300 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
301 }
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
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
303 /* This function is called from PyOS_AfterFork to ensure that newly
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
304 created child processes do not share locks with the parent. */
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
305
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
306 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
307 _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
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 #ifdef _AIX
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 if (import_lock != NULL)
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 import_lock = PyThread_allocate_lock();
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
312 #endif
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
313 }
724bbd489ad4 - Changes donated by Elemental Security to make it work on AIX 5.3
Guido van Rossum <guido@python.org>
parents: 35542
diff changeset
314
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
315 #else
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
316
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
317 #define lock_import()
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
318 #define unlock_import() 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
319
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
320 #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
321
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 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
323 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
324 {
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 #ifdef WITH_THREAD
22866
a561ff1b4ffa Return bools from functions named is_<whatever>().
Guido van Rossum <guido@python.org>
parents: 22705
diff changeset
326 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
327 #else
22866
a561ff1b4ffa Return bools from functions named is_<whatever>().
Guido van Rossum <guido@python.org>
parents: 22705
diff changeset
328 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
329 #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
330 }
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
331
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 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
333 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
334 {
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
335 #ifdef WITH_THREAD
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
336 lock_import();
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
337 #endif
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents: 27852
diff changeset
338 Py_INCREF(Py_None);
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents: 27852
diff changeset
339 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
340 }
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
341
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
342 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
343 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
344 {
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
345 #ifdef WITH_THREAD
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
346 if (unlock_import() < 0) {
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
347 PyErr_SetString(PyExc_RuntimeError,
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
348 "not holding the import lock");
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
349 return NULL;
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
350 }
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
351 #endif
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents: 27852
diff changeset
352 Py_INCREF(Py_None);
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents: 27852
diff changeset
353 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
354 }
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
355
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
356 static void
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
357 imp_modules_reloading_clear(void)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
358 {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
359 PyInterpreterState *interp = PyThreadState_Get()->interp;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
360 if (interp->modules_reloading != NULL)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
361 PyDict_Clear(interp->modules_reloading);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
362 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
363
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
364 /* Helper for sys */
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
365
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
366 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
367 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
368 {
31797
846b3ee7e106 Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31778
diff changeset
369 PyInterpreterState *interp = PyThreadState_GET()->interp;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
370 if (interp->modules == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
371 Py_FatalError("PyImport_GetModuleDict: no module dictionary!");
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
372 return interp->modules;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
373 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
374
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
375
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
376 /* 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
377 static char* sys_deletes[] = {
41253
f29f1a8b7869 Patch #1680961: remove sys.exitfunc and replace it with a private C API. Also, reimplement atexit in C so it can take advantage of this private API.
Collin Winter <collinw@gmail.com>
parents: 41226
diff changeset
378 "path", "argv", "ps1", "ps2",
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
379 "last_type", "last_value", "last_traceback",
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
380 "path_hooks", "path_importer_cache", "meta_path",
45071
f380d991390e Merged revisions 60441-60474 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44634
diff changeset
381 /* misc stuff */
f380d991390e Merged revisions 60441-60474 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44634
diff changeset
382 "flags", "float_info",
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
383 NULL
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
384 };
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
385
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
386 static char* sys_files[] = {
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
387 "stdin", "__stdin__",
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
388 "stdout", "__stdout__",
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
389 "stderr", "__stderr__",
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
390 NULL
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
391 };
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
392
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
393
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
394 /* Un-initialize things, as good as we can */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
395
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
396 void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
397 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
398 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
399 Py_ssize_t pos, ndone;
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
400 char *name;
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
401 PyObject *key, *value, *dict;
31797
846b3ee7e106 Changed random calls to PyThreadState_Get() to use the macro
Nicholas Bastin <nick.bastin@gmail.com>
parents: 31778
diff changeset
402 PyInterpreterState *interp = PyThreadState_GET()->interp;
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
403 PyObject *modules = interp->modules;
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
404
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
405 if (modules == NULL)
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
406 return; /* Already done */
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
407
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
408 /* Delete some special variables first. These are common
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
409 places where user values hide and people complain when their
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
410 destructors fail. Since the modules containing them are
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
411 deleted *last* of all, they would come too late in the normal
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
412 destruction order. Sigh. */
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
413
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
414 value = PyDict_GetItemString(modules, "builtins");
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
415 if (value != NULL && PyModule_Check(value)) {
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
416 dict = PyModule_GetDict(value);
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
417 if (Py_VerboseFlag)
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
418 PySys_WriteStderr("# clear builtins._\n");
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
419 PyDict_SetItemString(dict, "_", Py_None);
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
420 }
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
421 value = PyDict_GetItemString(modules, "sys");
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
422 if (value != NULL && PyModule_Check(value)) {
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
423 char **p;
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
424 PyObject *v;
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
425 dict = PyModule_GetDict(value);
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
426 for (p = sys_deletes; *p != NULL; p++) {
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
427 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
428 PySys_WriteStderr("# clear sys.%s\n", *p);
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
429 PyDict_SetItemString(dict, *p, Py_None);
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
430 }
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
431 for (p = sys_files; *p != NULL; p+=2) {
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
432 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
433 PySys_WriteStderr("# restore sys.%s\n", *p);
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
434 v = PyDict_GetItemString(dict, *(p+1));
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
435 if (v == NULL)
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
436 v = Py_None;
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
437 PyDict_SetItemString(dict, *p, v);
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
438 }
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
439 }
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
440
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
441 /* First, delete __main__ */
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
442 value = PyDict_GetItemString(modules, "__main__");
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
443 if (value != NULL && PyModule_Check(value)) {
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
444 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
445 PySys_WriteStderr("# cleanup __main__\n");
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
446 _PyModule_Clear(value);
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
447 PyDict_SetItemString(modules, "__main__", Py_None);
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
448 }
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
449
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
450 /* The special treatment of "builtins" here is because even
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
451 when it's not referenced as a module, its dictionary is
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
452 referenced by almost every module's __builtins__. Since
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
453 deleting a module clears its dictionary (even if there are
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
454 references left to it), we need to delete the "builtins"
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
455 module last. Likewise, we don't delete sys until the very
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
456 end because it is implicitly referenced (e.g. by print).
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
457
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
458 Also note that we 'delete' modules by replacing their entry
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
459 in the modules dict with None, rather than really deleting
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
460 them; this avoids a rehash of the modules dictionary and
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
461 also marks them as "non existent" so they won't be
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
462 re-imported. */
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
463
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
464 /* Next, repeatedly delete modules with a reference count of
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
465 one (skipping builtins and sys) and delete them */
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
466 do {
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
467 ndone = 0;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
468 pos = 0;
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
469 while (PyDict_Next(modules, &pos, &key, &value)) {
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
470 if (value->ob_refcnt != 1)
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
471 continue;
42862
ed1594a16bdb Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42859
diff changeset
472 if (PyUnicode_Check(key) && PyModule_Check(value)) {
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48540
diff changeset
473 name = _PyUnicode_AsString(key);
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
474 if (strcmp(name, "builtins") == 0)
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
475 continue;
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
476 if (strcmp(name, "sys") == 0)
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
477 continue;
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
478 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
479 PySys_WriteStderr(
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
480 "# cleanup[1] %s\n", name);
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
481 _PyModule_Clear(value);
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
482 PyDict_SetItem(modules, key, Py_None);
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
483 ndone++;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
484 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
485 }
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
486 } while (ndone > 0);
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
487
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
488 /* Next, delete all modules (still skipping builtins and sys) */
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
489 pos = 0;
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
490 while (PyDict_Next(modules, &pos, &key, &value)) {
42862
ed1594a16bdb Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42859
diff changeset
491 if (PyUnicode_Check(key) && PyModule_Check(value)) {
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48540
diff changeset
492 name = _PyUnicode_AsString(key);
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
493 if (strcmp(name, "builtins") == 0)
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
494 continue;
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
495 if (strcmp(name, "sys") == 0)
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
496 continue;
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
497 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
498 PySys_WriteStderr("# cleanup[2] %s\n", name);
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
499 _PyModule_Clear(value);
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
500 PyDict_SetItem(modules, key, Py_None);
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
501 }
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
502 }
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
503
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
504 /* Next, delete sys and builtins (in that order) */
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
505 value = PyDict_GetItemString(modules, "sys");
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
506 if (value != NULL && PyModule_Check(value)) {
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
507 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
508 PySys_WriteStderr("# cleanup sys\n");
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
509 _PyModule_Clear(value);
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
510 PyDict_SetItemString(modules, "sys", Py_None);
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
511 }
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
512 value = PyDict_GetItemString(modules, "builtins");
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
513 if (value != NULL && PyModule_Check(value)) {
7202
fed3d200fb56 Two more refinements of the cleanup process.
Guido van Rossum <guido@python.org>
parents: 7111
diff changeset
514 if (Py_VerboseFlag)
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
515 PySys_WriteStderr("# cleanup builtins\n");
7403
185c1d47ce7a Moved clear_carefully() to _PyModule_Clear() in moduleobject.c
Guido van Rossum <guido@python.org>
parents: 7367
diff changeset
516 _PyModule_Clear(value);
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
517 PyDict_SetItemString(modules, "builtins", Py_None);
7111
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
518 }
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
519
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
520 /* Finally, clear and delete the modules directory */
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
521 PyDict_Clear(modules);
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
522 interp->modules = NULL;
f063f1eb2964 Rewritten PyImport_Cleanup() and its helper, clear_carefully(). They
Guido van Rossum <guido@python.org>
parents: 6907
diff changeset
523 Py_DECREF(modules);
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
524 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
525 }
166
28923e44254c Added forward declaration (to satisfy Standard C).
Guido van Rossum <guido@python.org>
parents: 145
diff changeset
526
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
527
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
528 /* 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
529
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
530 long
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
531 PyImport_GetMagicNumber(void)
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
532 {
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
533 return pyc_magic;
1886
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
534 }
ac6a9f90f6fd * Import/pythonrun.h, Python/{import,pythonrun}.c,
Guido van Rossum <guido@python.org>
parents: 1877
diff changeset
535
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
537 /* 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
538 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
539 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
540 (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
541 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
542 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
543 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
544 copy can be retrieved from there by calling
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
545 _PyImport_FindExtension().
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
546
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
547 Modules which do support multiple multiple initialization set
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
548 their m_size field to a non-negative number (indicating the size
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
549 of the module-specific state). They are still recorded in the
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
550 extensions dictionary, to avoid loading shared libraries twice.
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
551 */
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
552
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
553 int
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
554 _PyImport_FixupExtension(PyObject *mod, char *name, char *filename)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 {
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
556 PyObject *modules, *dict;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
557 struct PyModuleDef *def;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
558 if (extensions == NULL) {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
559 extensions = PyDict_New();
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
560 if (extensions == NULL)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
561 return -1;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
562 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
563 if (mod == NULL || !PyModule_Check(mod)) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
564 PyErr_BadInternalCall();
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
565 return -1;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
566 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
567 def = PyModule_GetDef(mod);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
568 if (!def) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
569 PyErr_BadInternalCall();
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
570 return -1;
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 modules = PyImport_GetModuleDict();
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
573 if (PyDict_SetItemString(modules, name, mod) < 0)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
574 return -1;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
575 if (_PyState_AddModule(mod, def) < 0) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
576 PyDict_DelItemString(modules, name);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
577 return -1;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
578 }
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
579 if (def->m_size == -1) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
580 if (def->m_base.m_copy) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
581 /* Somebody already imported the module,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
582 likely under a different name.
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
583 XXX this should really not happen. */
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
584 Py_DECREF(def->m_base.m_copy);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
585 def->m_base.m_copy = NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
586 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
587 dict = PyModule_GetDict(mod);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
588 if (dict == NULL)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
589 return -1;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
590 def->m_base.m_copy = PyDict_Copy(dict);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
591 if (def->m_base.m_copy == NULL)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
592 return -1;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
593 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
594 PyDict_SetItemString(extensions, filename, (PyObject*)def);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
595 return 0;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
596 }
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
597
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
598 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
599 _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
600 {
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
601 PyObject *mod, *mdict;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
602 PyModuleDef* def;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
603 if (extensions == NULL)
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
604 return NULL;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
605 def = (PyModuleDef*)PyDict_GetItemString(extensions, filename);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
606 if (def == NULL)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
607 return NULL;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
608 if (def->m_size == -1) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
609 /* Module does not support repeated initialization */
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
610 if (def->m_base.m_copy == NULL)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
611 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
612 mod = PyImport_AddModule(name);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
613 if (mod == NULL)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
614 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
615 mdict = PyModule_GetDict(mod);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
616 if (mdict == NULL)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
617 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
618 if (PyDict_Update(mdict, def->m_base.m_copy))
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
619 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
620 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
621 else {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
622 if (def->m_base.m_init == NULL)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
623 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
624 mod = def->m_base.m_init();
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
625 if (mod == NULL)
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
626 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
627 PyDict_SetItemString(PyImport_GetModuleDict(), name, mod);
49003
812ea105b1b0 Fix a memory leak in reloading extension modules #3667
Benjamin Peterson <benjamin@python.org>
parents: 48946
diff changeset
628 Py_DECREF(mod);
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
629 }
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
630 if (_PyState_AddModule(mod, def) < 0) {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
631 PyDict_DelItemString(PyImport_GetModuleDict(), name);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
632 Py_DECREF(mod);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
633 return NULL;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
634 }
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
635 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
636 PySys_WriteStderr("import %s # previously loaded (%s)\n",
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
637 name, filename);
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
638 return mod;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
639
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
642
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
643 /* 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
644 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
645 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
646 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
647 '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
648
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
649 PyObject *
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35757
diff changeset
650 PyImport_AddModule(const char *name)
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
651 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
652 PyObject *modules = PyImport_GetModuleDict();
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
653 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
654
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
655 if ((m = PyDict_GetItemString(modules, name)) != NULL &&
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
656 PyModule_Check(m))
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
657 return m;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
658 m = PyModule_New(name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
659 if (m == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 return NULL;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
661 if (PyDict_SetItemString(modules, name, m) != 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
662 Py_DECREF(m);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
663 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
665 Py_DECREF(m); /* Yes, it still exists, in modules! */
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
666
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
667 return m;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
668 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
669
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
670 /* 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
671 static void
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
672 _RemoveModule(const char *name)
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
673 {
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
674 PyObject *modules = PyImport_GetModuleDict();
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
675 if (PyDict_GetItemString(modules, name) == NULL)
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
676 return;
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
677 if (PyDict_DelItemString(modules, name) < 0)
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
678 Py_FatalError("import: deleting existing key in"
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
679 "sys.modules failed");
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
680 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
681
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
682 static PyObject * get_sourcefile(const char *file);
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
683
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
684 /* 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
685 * 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
686 * 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
687 * 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
688 * 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
689 * example.
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
690 */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
691 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
692 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
693 {
7232
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
694 return PyImport_ExecCodeModuleEx(name, co, (char *)NULL);
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
695 }
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
696
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
697 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
698 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
699 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
700 PyObject *modules = PyImport_GetModuleDict();
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
701 PyObject *m, *d, *v;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
702
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
703 m = PyImport_AddModule(name);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
704 if (m == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
705 return NULL;
31320
ca4374662632 Add comment to mollify Tim.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30979
diff changeset
706 /* If the module is being reloaded, we get the old module back
ca4374662632 Add comment to mollify Tim.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30979
diff changeset
707 and re-use its dict to exec the new code. */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
708 d = PyModule_GetDict(m);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
709 if (PyDict_GetItemString(d, "__builtins__") == NULL) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
710 if (PyDict_SetItemString(d, "__builtins__",
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
711 PyEval_GetBuiltins()) != 0)
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
712 goto error;
2098
b7be0382f9ee __builtins__ mods (and sys_checkinterval for ceval.c)
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
713 }
3557
cee4a6d8415e Remember source filename as <module>.__file__.
Guido van Rossum <guido@python.org>
parents: 3492
diff changeset
714 /* Remember the filename as the __file__ attribute */
7232
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
715 v = NULL;
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
716 if (pathname != NULL) {
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
717 v = get_sourcefile(pathname);
7232
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
718 if (v == NULL)
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
719 PyErr_Clear();
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
720 }
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
721 if (v == NULL) {
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
722 v = ((PyCodeObject *)co)->co_filename;
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
723 Py_INCREF(v);
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
724 }
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
725 if (PyDict_SetItemString(d, "__file__", v) != 0)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
726 PyErr_Clear(); /* Not important enough to report */
7232
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
727 Py_DECREF(v);
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
728
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
729 v = PyEval_EvalCode((PyCodeObject *)co, d, d);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
730 if (v == NULL)
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
731 goto error;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
732 Py_DECREF(v);
5758
bfe9ba74c19e Fix problem discovered by Greg McFarlane: when an imported module
Guido van Rossum <guido@python.org>
parents: 5511
diff changeset
733
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
734 if ((m = PyDict_GetItemString(modules, name)) == NULL) {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
735 PyErr_Format(PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
736 "Loaded module %.200s not found in sys.modules",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
737 name);
5758
bfe9ba74c19e Fix problem discovered by Greg McFarlane: when an imported module
Guido van Rossum <guido@python.org>
parents: 5511
diff changeset
738 return NULL;
bfe9ba74c19e Fix problem discovered by Greg McFarlane: when an imported module
Guido van Rossum <guido@python.org>
parents: 5511
diff changeset
739 }
bfe9ba74c19e Fix problem discovered by Greg McFarlane: when an imported module
Guido van Rossum <guido@python.org>
parents: 5511
diff changeset
740
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
741 Py_INCREF(m);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
742
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
743 return m;
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
744
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
745 error:
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
746 _RemoveModule(name);
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
747 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
749
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
750
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
751 /* 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
752 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
753 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
754 Doesn't set an exception. */
609
0ad5b35e3ad8 Support for dynamic loading added.
Guido van Rossum <guido@python.org>
parents: 510
diff changeset
755
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
756 static char *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
757 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
758 {
18992
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
759 size_t len = strlen(pathname);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
760 if (len+2 > buflen)
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
761 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
762
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 23879
diff changeset
763 #ifdef MS_WINDOWS
18992
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
764 /* Treat .pyw as if it were .py. The case of ".pyw" must match
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
765 that used in _PyImport_StandardFiletab. */
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
766 if (len >= 4 && strcmp(&pathname[len-4], ".pyw") == 0)
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
767 --len; /* pretend 'w' isn't there */
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
768 #endif
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
769 memcpy(buf, pathname, len);
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
770 buf[len] = Py_OptimizeFlag ? 'o' : 'c';
7fa1f87ca6b2 Derived from SF patch #446899 Permit import of .pyw under Windows, from
Tim Peters <tim.peters@gmail.com>
parents: 18933
diff changeset
771 buf[len+1] = '\0';
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
772
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
773 return buf;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
774 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
775
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
776
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
777 /* 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
778 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
779 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
780 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
781 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
782 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
783
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
784 static FILE *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
785 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
786 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
787 FILE *fp;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
788 long magic;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
789 long pyc_mtime;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
790
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
791 fp = fopen(cpathname, "rb");
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
792 if (fp == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
793 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
794 magic = PyMarshal_ReadLongFromFile(fp);
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
795 if (magic != pyc_magic) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
796 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
797 PySys_WriteStderr("# %s has bad magic\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
798 fclose(fp);
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
799 return NULL;
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
800 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
801 pyc_mtime = PyMarshal_ReadLongFromFile(fp);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
802 if (pyc_mtime != mtime) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
803 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
804 PySys_WriteStderr("# %s has bad mtime\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
805 fclose(fp);
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
806 return NULL;
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
807 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
808 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
809 PySys_WriteStderr("# %s matches %s\n", cpathname, pathname);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
810 return fp;
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
811 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
812
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 /* 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
815
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
816 static PyCodeObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
817 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
818 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
819 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
820
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
821 co = PyMarshal_ReadLastObjectFromFile(fp);
31824
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 31797
diff changeset
822 if (co == NULL)
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 31797
diff changeset
823 return NULL;
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 31797
diff changeset
824 if (!PyCode_Check(co)) {
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 31797
diff changeset
825 PyErr_Format(PyExc_ImportError,
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 31797
diff changeset
826 "Non-code object in %.200s", cpathname);
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 31797
diff changeset
827 Py_DECREF(co);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
828 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
829 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
830 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
831 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
832
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
833
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
834 /* 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
835 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
836
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
837 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
838 load_compiled_module(char *name, char *cpathname, FILE *fp)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 {
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
840 long magic;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
841 PyCodeObject *co;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
842 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
843
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
844 magic = PyMarshal_ReadLongFromFile(fp);
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
845 if (magic != pyc_magic) {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
846 PyErr_Format(PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
847 "Bad magic number in %.200s", cpathname);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
848 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
849 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
850 (void) PyMarshal_ReadLongFromFile(fp);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
851 co = read_compiled_module(cpathname, fp);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
852 if (co == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
853 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
854 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
855 PySys_WriteStderr("import %s # precompiled from %s\n",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
856 name, cpathname);
7232
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
857 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, cpathname);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
858 Py_DECREF(co);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
859
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
860 return m;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
861 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
862
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
863 /* 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
864
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
865 static PyCodeObject *
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
866 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
867 {
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
868 PyCodeObject *co = NULL;
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
869 mod_ty mod;
46120
78504d604b44 Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46015
diff changeset
870 PyCompilerFlags flags;
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
871 PyArena *arena = PyArena_New();
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
872 if (arena == NULL)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
873 return NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
874
46125
36d828d27c36 Merged revisions 61954,61956-61957 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46120
diff changeset
875 flags.cf_flags = 0;
43225
38f3f9ef193d Convert code from sys.stdin.encoding to UTF-8 in
Martin v. Löwis <martin@v.loewis.de>
parents: 43107
diff changeset
876 mod = PyParser_ASTFromFile(fp, pathname, NULL,
46120
78504d604b44 Merged revisions 61952-61953 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 46015
diff changeset
877 Py_file_input, 0, 0, &flags,
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
878 NULL, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
879 if (mod) {
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
880 co = PyAST_Compile(mod, pathname, NULL, arena);
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35678
diff changeset
881 }
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
882 PyArena_Free(arena);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
883 return co;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
884 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
885
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
886
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
887 /* 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
888
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
889 static FILE *
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
890 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
891 {
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
892 #if defined(O_EXCL)&&defined(O_CREAT)&&defined(O_WRONLY)&&defined(O_TRUNC)
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
893 /* Use O_EXCL to avoid a race condition when another process tries to
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
894 write the same file. When that happens, our open() call fails,
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
895 which is just fine (since it's only a cache).
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
896 XXX If the file exists and is writable but the directory is not
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
897 writable, the file will never be written. Oh well.
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
898 */
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
899 int fd;
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
900 (void) unlink(filename);
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
901 fd = open(filename, O_EXCL|O_CREAT|O_WRONLY|O_TRUNC
3db8b8031a6a The 2.0b2 change to write .pyc files in exclusive mode (if possible)
Tim Peters <tim.peters@gmail.com>
parents: 14708
diff changeset
902 #ifdef O_BINARY
3db8b8031a6a The 2.0b2 change to write .pyc files in exclusive mode (if possible)
Tim Peters <tim.peters@gmail.com>
parents: 14708
diff changeset
903 |O_BINARY /* necessary for Windows */
3db8b8031a6a The 2.0b2 change to write .pyc files in exclusive mode (if possible)
Tim Peters <tim.peters@gmail.com>
parents: 14708
diff changeset
904 #endif
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26461
diff changeset
905 #ifdef __VMS
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
906 , mode, "ctxt=bin", "shr=nil"
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26461
diff changeset
907 #else
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
908 , mode
26550
b9c912e2c3c0 Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 26461
diff changeset
909 #endif
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
910 );
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
911 if (fd < 0)
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
912 return NULL;
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
913 return fdopen(fd, "wb");
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
914 #else
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
915 /* Best we can do -- on Windows this can't happen anyway */
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
916 return fopen(filename, "wb");
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
917 #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
918 }
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
919
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
920
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
921 /* 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
922 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
923 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
924 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
925
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
926 static void
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
927 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
928 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
929 FILE *fp;
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
930 time_t mtime = srcstat->st_mtime;
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
931 mode_t mode = srcstat->st_mode;
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
932
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
933 fp = open_exclusive(cpathname, mode);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
934 if (fp == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
935 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
936 PySys_WriteStderr(
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
937 "# can't create %s\n", cpathname);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
938 return;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
939 }
32422
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32294
diff changeset
940 PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
941 /* First write a 0 for mtime */
32422
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32294
diff changeset
942 PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32294
diff changeset
943 PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
31824
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 31797
diff changeset
944 if (fflush(fp) != 0 || ferror(fp)) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
945 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
946 PySys_WriteStderr("# can't write %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
947 /* Don't keep partial file */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
948 fclose(fp);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
949 (void) unlink(cpathname);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
950 return;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
951 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
952 /* Now write the true mtime */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
953 fseek(fp, 4L, 0);
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
954 assert(mtime < LONG_MAX);
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36712
diff changeset
955 PyMarshal_WriteLongToFile((long)mtime, fp, Py_MARSHAL_VERSION);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
956 fflush(fp);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
957 fclose(fp);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
958 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
959 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
960 }
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
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
963 /* 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
964 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
965 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
966
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
967 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
968 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
969 {
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
970 struct stat st;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
971 FILE *fpc;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
972 char buf[MAXPATHLEN+1];
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
973 char *cpathname;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
974 PyCodeObject *co;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
975 PyObject *m;
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
976
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
977 if (fstat(fileno(fp), &st) != 0) {
35678
f0951fdaed37 Fix SF bug #976608, Unhelpful error message when mtime of a module is -1
Neal Norwitz <nnorwitz@gmail.com>
parents: 35550
diff changeset
978 PyErr_Format(PyExc_RuntimeError,
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
979 "unable to get file status from '%s'",
35678
f0951fdaed37 Fix SF bug #976608, Unhelpful error message when mtime of a module is -1
Neal Norwitz <nnorwitz@gmail.com>
parents: 35550
diff changeset
980 pathname);
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
981 return NULL;
35678
f0951fdaed37 Fix SF bug #976608, Unhelpful error message when mtime of a module is -1
Neal Norwitz <nnorwitz@gmail.com>
parents: 35550
diff changeset
982 }
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
983 #if SIZEOF_TIME_T > 4
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
984 /* Python's .pyc timestamp handling presumes that the timestamp fits
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
985 in 4 bytes. This will be fine until sometime in the year 2038,
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
986 when a 4-byte signed time_t will overflow.
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
987 */
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
988 if (st.st_mtime >> 32) {
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
989 PyErr_SetString(PyExc_OverflowError,
16893
f1c8ea92ee85 Suppress a compiler warning under OpenVMS; time_t is unsigned on (at least)
Fred Drake <fdrake@acm.org>
parents: 16881
diff changeset
990 "modification time overflows a 4 byte field");
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
991 return NULL;
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
992 }
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
993 #endif
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
994 cpathname = make_compiled_pathname(pathname, buf,
17502
c5bab794dcde split long line
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17090
diff changeset
995 (size_t)MAXPATHLEN + 1);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
996 if (cpathname != NULL &&
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
997 (fpc = check_compiled_module(pathname, st.st_mtime, cpathname))) {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
998 co = read_compiled_module(cpathname, fpc);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
999 fclose(fpc);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1000 if (co == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1001 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1002 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
1003 PySys_WriteStderr("import %s # precompiled from %s\n",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1004 name, cpathname);
9050
f3ebdb2274e4 __file__ used to be always set to the .pyc (or .pyo) file, even if
Guido van Rossum <guido@python.org>
parents: 8934
diff changeset
1005 pathname = cpathname;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1006 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1007 else {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1008 co = parse_source_module(pathname, fp);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1009 if (co == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1010 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1011 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
1012 PySys_WriteStderr("import %s # from %s\n",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1013 name, pathname);
44587
fb5a6330f71b Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
1014 if (cpathname) {
fb5a6330f71b Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
1015 PyObject *ro = PySys_GetObject("dont_write_bytecode");
fb5a6330f71b Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
1016 if (ro == NULL || !PyObject_IsTrue(ro))
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45071
diff changeset
1017 write_compiled_module(co, cpathname, &st);
44587
fb5a6330f71b Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
1018 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1019 }
9050
f3ebdb2274e4 __file__ used to be always set to the .pyc (or .pyo) file, even if
Guido van Rossum <guido@python.org>
parents: 8934
diff changeset
1020 m = PyImport_ExecCodeModuleEx(name, (PyObject *)co, pathname);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1021 Py_DECREF(co);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1022
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1023 return m;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1024 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1025
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1026 /* Get source file -> unicode or None
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1027 * Returns the path to the py file if available, else the given path
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1028 */
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1029 static PyObject *
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1030 get_sourcefile(const char *file)
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1031 {
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1032 char py[MAXPATHLEN + 1];
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1033 Py_ssize_t len;
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1034 PyObject *u;
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1035 struct stat statbuf;
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1036
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1037 if (!file || !*file) {
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1038 Py_RETURN_NONE;
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1039 }
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1040
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1041 len = strlen(file);
44634
06f9f51ab8c0 Good catch Neal!
Christian Heimes <christian@cheimes.de>
parents: 44617
diff changeset
1042 /* match '*.py?' */
06f9f51ab8c0 Good catch Neal!
Christian Heimes <christian@cheimes.de>
parents: 44617
diff changeset
1043 if (len > MAXPATHLEN || PyOS_strnicmp(&file[len-4], ".py", 3) != 0) {
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1044 return PyUnicode_DecodeFSDefault(file);
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1045 }
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1046
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1047 strncpy(py, file, len-1);
46015
10b0d81a2145 Properly terminate file name.
Martin v. Löwis <martin@v.loewis.de>
parents: 45773
diff changeset
1048 py[len-1] = '\0';
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1049 if (stat(py, &statbuf) == 0 &&
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1050 S_ISREG(statbuf.st_mode)) {
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1051 u = PyUnicode_DecodeFSDefault(py);
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1052 }
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1053 else {
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1054 u = PyUnicode_DecodeFSDefault(file);
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1055 }
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1056 return u;
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1057 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1058
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1059 /* Forward */
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1060 static PyObject *load_module(char *, FILE *, char *, int, PyObject *);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1061 static struct filedescr *find_module(char *, char *, PyObject *,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1062 char *, size_t, FILE **, PyObject **);
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1063 static struct _frozen * find_frozen(char *);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1064
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1065 /* 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
1066 REFERENCE COUNT */
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1067
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1068 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1069 load_package(char *name, char *pathname)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1070 {
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1071 PyObject *m, *d;
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1072 PyObject *file = NULL;
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1073 PyObject *path = NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1074 int err;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1075 char buf[MAXPATHLEN+1];
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1076 FILE *fp = NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1077 struct filedescr *fdp;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1078
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1079 m = PyImport_AddModule(name);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1080 if (m == NULL)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1081 return NULL;
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
1082 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
1083 PySys_WriteStderr("import %s # directory %s\n",
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
1084 name, pathname);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1085 d = PyModule_GetDict(m);
44583
73e1bf179a01 Another patch for #1762972: __file__ points to the py file instead pyo/pyc file
Christian Heimes <christian@cheimes.de>
parents: 44567
diff changeset
1086 file = get_sourcefile(pathname);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1087 if (file == NULL)
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1088 goto error;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1089 path = Py_BuildValue("[O]", file);
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1090 if (path == NULL)
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1091 goto error;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1092 err = PyDict_SetItemString(d, "__file__", file);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1093 if (err == 0)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1094 err = PyDict_SetItemString(d, "__path__", path);
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1095 if (err != 0)
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1096 goto error;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1097 buf[0] = '\0';
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1098 fdp = find_module(name, "__init__", path, buf, sizeof(buf), &fp, NULL);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1099 if (fdp == NULL) {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1100 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1101 PyErr_Clear();
32294
82fe6832e505 Fix a refcount bug in an obscure code corner.
Thomas Heller <theller@ctypes.org>
parents: 32156
diff changeset
1102 Py_INCREF(m);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1103 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1104 else
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1105 m = NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1106 goto cleanup;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1107 }
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1108 m = load_module(name, fp, buf, fdp->type, NULL);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1109 if (fp != NULL)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1110 fclose(fp);
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1111 goto cleanup;
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1112
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1113 error:
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
1114 m = NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1115 cleanup:
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1116 Py_XDECREF(path);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1117 Py_XDECREF(file);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1118 return m;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1119 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1120
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1121
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1122 /* Helper to test for built-in module */
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1123
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1124 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1125 is_builtin(char *name)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1126 {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1127 int i;
6541
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
1128 for (i = 0; PyImport_Inittab[i].name != NULL; i++) {
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
1129 if (strcmp(name, PyImport_Inittab[i].name) == 0) {
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
1130 if (PyImport_Inittab[i].initfunc == NULL)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1131 return -1;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1132 else
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1133 return 1;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1134 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1135 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1136 return 0;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1137 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1138
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1139
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1140 /* 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
1141 possibly by fetching it from the path_importer_cache dict. If it
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1142 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
1143 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
1144 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
1145 import mechanism. Cache the result in path_importer_cache.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1146 Returns a borrowed reference. */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1147
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1148 static PyObject *
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1149 get_path_importer(PyObject *path_importer_cache, PyObject *path_hooks,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1150 PyObject *p)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1151 {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1152 PyObject *importer;
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36712
diff changeset
1153 Py_ssize_t j, nhooks;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1154
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1155 /* These conditions are the caller's responsibility: */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1156 assert(PyList_Check(path_hooks));
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1157 assert(PyDict_Check(path_importer_cache));
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1158
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1159 nhooks = PyList_Size(path_hooks);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1160 if (nhooks < 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1161 return NULL; /* Shouldn't happen */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1162
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1163 importer = PyDict_GetItem(path_importer_cache, p);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1164 if (importer != NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1165 return importer;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1166
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1167 /* set path_importer_cache[p] to None to avoid recursion */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1168 if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1169 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1170
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1171 for (j = 0; j < nhooks; j++) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1172 PyObject *hook = PyList_GetItem(path_hooks, j);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1173 if (hook == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1174 return NULL;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1175 importer = PyObject_CallFunctionObjArgs(hook, p, NULL);
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1176 if (importer != NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1177 break;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1178
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1179 if (!PyErr_ExceptionMatches(PyExc_ImportError)) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1180 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1181 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1182 PyErr_Clear();
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1183 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1184 if (importer == NULL) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1185 importer = PyObject_CallFunctionObjArgs(
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1186 (PyObject *)&PyNullImporter_Type, p, NULL
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1187 );
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1188 if (importer == NULL) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1189 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1190 PyErr_Clear();
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1191 return Py_None;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1192 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1193 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1194 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1195 if (importer != NULL) {
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1196 int err = PyDict_SetItem(path_importer_cache, p, importer);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1197 Py_DECREF(importer);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1198 if (err != 0)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1199 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1200 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1201 return importer;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1202 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1203
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1204 PyAPI_FUNC(PyObject *)
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1205 PyImport_GetImporter(PyObject *path) {
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1206 PyObject *importer=NULL, *path_importer_cache=NULL, *path_hooks=NULL;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1207
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1208 if ((path_importer_cache = PySys_GetObject("path_importer_cache"))) {
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1209 if ((path_hooks = PySys_GetObject("path_hooks"))) {
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1210 importer = get_path_importer(path_importer_cache,
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1211 path_hooks, path);
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1212 }
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1213 }
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1214 Py_XINCREF(importer); /* get_path_importer returns a borrowed reference */
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1215 return importer;
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1216 }
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
1217
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1218 /* 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
1219 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
1220 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
1221
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1222 #ifdef MS_COREDLL
13663
29da35543833 ANSIfy as many declarations as possible.
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
1223 extern FILE *PyWin_FindRegisteredModule(const char *, struct filedescr **,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1224 char *, Py_ssize_t);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1225 #endif
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1226
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1227 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
1228 static int find_init_module(char *); /* Forward */
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1229 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
1230
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1231 static struct filedescr *
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1232 find_module(char *fullname, char *subname, PyObject *path, char *buf,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1233 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
1234 {
36866
9eb24f92a784 Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents: 36712
diff changeset
1235 Py_ssize_t i, npath;
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
1236 size_t len, namelen;
4596
4fa19659591e Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
1237 struct filedescr *fdp = NULL;
23554
e5242f58a5b6 File modes in filedescr entries are also passed to Python, so we now put "U"
Jack Jansen <jack.jansen@cwi.nl>
parents: 23029
diff changeset
1238 char *filemode;
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1239 FILE *fp = NULL;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1240 PyObject *path_hooks, *path_importer_cache;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1241 struct stat statbuf;
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1242 static struct filedescr fd_frozen = {"", "", PY_FROZEN};
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1243 static struct filedescr fd_builtin = {"", "", C_BUILTIN};
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1244 static struct filedescr fd_package = {"", "", PKG_DIRECTORY};
8934
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1245 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
1246 #if 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
1247 size_t saved_len;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1248 size_t saved_namelen;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1249 char *saved_buf = NULL;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1250 #endif
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1251 if (p_loader != NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1252 *p_loader = NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1253
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1254 if (strlen(subname) > MAXPATHLEN) {
20527
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1255 PyErr_SetString(PyExc_OverflowError,
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1256 "module name is too long");
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
1257 return NULL;
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
1258 }
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1259 strcpy(name, subname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1260
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1261 /* sys.meta_path import hook */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1262 if (p_loader != NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1263 PyObject *meta_path;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1264
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1265 meta_path = PySys_GetObject("meta_path");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1266 if (meta_path == NULL || !PyList_Check(meta_path)) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1267 PyErr_SetString(PyExc_ImportError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1268 "sys.meta_path must be a list of "
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1269 "import hooks");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1270 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1271 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1272 Py_INCREF(meta_path); /* zap guard */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1273 npath = PyList_Size(meta_path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1274 for (i = 0; i < npath; i++) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1275 PyObject *loader;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1276 PyObject *hook = PyList_GetItem(meta_path, i);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1277 loader = PyObject_CallMethod(hook, "find_module",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1278 "sO", fullname,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1279 path != NULL ?
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1280 path : Py_None);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1281 if (loader == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1282 Py_DECREF(meta_path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1283 return NULL; /* true error */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1284 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1285 if (loader != Py_None) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1286 /* a loader was found */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1287 *p_loader = loader;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1288 Py_DECREF(meta_path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1289 return &importhookdescr;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1290 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1291 Py_DECREF(loader);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1292 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1293 Py_DECREF(meta_path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1294 }
8934
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1295
42347
5e66bb69c1e3 Fix import of frozen package submodules to use Unicode. Fixes test_frozen.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1296 if (path != NULL && PyUnicode_Check(path)) {
20527
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1297 /* The only type of submodule allowed inside a "frozen"
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1298 package are other frozen modules or packages. */
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48540
diff changeset
1299 char *p = _PyUnicode_AsString(path);
42347
5e66bb69c1e3 Fix import of frozen package submodules to use Unicode. Fixes test_frozen.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1300 if (strlen(p) + 1 + strlen(name) >= (size_t)buflen) {
8934
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1301 PyErr_SetString(PyExc_ImportError,
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1302 "full frozen module name too long");
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1303 return NULL;
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1304 }
42347
5e66bb69c1e3 Fix import of frozen package submodules to use Unicode. Fixes test_frozen.
Guido van Rossum <guido@python.org>
parents: 42334
diff changeset
1305 strcpy(buf, p);
8934
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1306 strcat(buf, ".");
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1307 strcat(buf, name);
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1308 strcpy(name, buf);
20527
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1309 if (find_frozen(name) != NULL) {
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1310 strcpy(buf, name);
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1311 return &fd_frozen;
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1312 }
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1313 PyErr_Format(PyExc_ImportError,
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1314 "No frozen submodule named %.200s", name);
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1315 return NULL;
8934
20b83ca06ce8 Patch by Just van Rossum that changes how we search for submodules of
Guido van Rossum <guido@python.org>
parents: 8845
diff changeset
1316 }
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1317 if (path == NULL) {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1318 if (is_builtin(name)) {
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1319 strcpy(buf, name);
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1320 return &fd_builtin;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1321 }
20249
1acecc908ee8 Remove a couple of unused local variables (bug #445960, compiler warnings
Greg Ward <gward@python.net>
parents: 19596
diff changeset
1322 if ((find_frozen(name)) != NULL) {
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1323 strcpy(buf, name);
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1324 return &fd_frozen;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1325 }
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1326
4111
90216e907345 PYTHONWIN -> MS_COREDLL
Guido van Rossum <guido@python.org>
parents: 3964
diff changeset
1327 #ifdef MS_COREDLL
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1328 fp = PyWin_FindRegisteredModule(name, &fdp, buf, buflen);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1329 if (fp != NULL) {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1330 *p_fp = fp;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1331 return fdp;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1332 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1333 #endif
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1334 path = PySys_GetObject("path");
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1335 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1336 if (path == NULL || !PyList_Check(path)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1337 PyErr_SetString(PyExc_ImportError,
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1338 "sys.path must be a list of directory names");
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1339 return NULL;
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1340 }
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1341
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1342 path_hooks = PySys_GetObject("path_hooks");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1343 if (path_hooks == NULL || !PyList_Check(path_hooks)) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1344 PyErr_SetString(PyExc_ImportError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1345 "sys.path_hooks must be a list of "
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1346 "import hooks");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1347 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1348 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1349 path_importer_cache = PySys_GetObject("path_importer_cache");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1350 if (path_importer_cache == NULL ||
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1351 !PyDict_Check(path_importer_cache)) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1352 PyErr_SetString(PyExc_ImportError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1353 "sys.path_importer_cache must be a dict");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1354 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1355 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1356
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1357 npath = PyList_Size(path);
1905
61c3c1a2fdb4 Fix various potential buffer overrun problems.
Guido van Rossum <guido@python.org>
parents: 1886
diff changeset
1358 namelen = strlen(name);
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1359 for (i = 0; i < npath; i++) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1360 PyObject *v = PyList_GetItem(path, i);
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
1361 PyObject *origv = v;
41666
85d51cb8ae5e More uniform approach to getting (UTF8) bytes out of a string.
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
1362 const char *base;
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
1363 Py_ssize_t size;
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
1364 if (!v)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
1365 return NULL;
42898
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42862
diff changeset
1366 if (PyUnicode_Check(v)) {
47599
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1367 v = PyUnicode_AsEncodedString(v,
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1368 Py_FileSystemDefaultEncoding, NULL);
42898
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42862
diff changeset
1369 if (v == NULL)
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42862
diff changeset
1370 return NULL;
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42862
diff changeset
1371 }
47599
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1372 else if (!PyBytes_Check(v))
42898
0020bc784c76 Some changes in preparation of stricter rules about mixing str and bytes.
Guido van Rossum <guido@python.org>
parents: 42862
diff changeset
1373 continue;
47599
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1374 else
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1375 Py_INCREF(v);
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1376
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46644
diff changeset
1377 base = PyBytes_AS_STRING(v);
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46644
diff changeset
1378 size = PyBytes_GET_SIZE(v);
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
1379 len = size;
23879
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1380 if (len + 2 + namelen + MAXSUFFIXSIZE >= buflen) {
47599
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1381 Py_DECREF(v);
1905
61c3c1a2fdb4 Fix various potential buffer overrun problems.
Guido van Rossum <guido@python.org>
parents: 1886
diff changeset
1382 continue; /* Too long */
23879
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1383 }
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
1384 strcpy(buf, base);
47599
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1385 Py_DECREF(v);
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
1386
23879
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1387 if (strlen(buf) != len) {
1905
61c3c1a2fdb4 Fix various potential buffer overrun problems.
Guido van Rossum <guido@python.org>
parents: 1886
diff changeset
1388 continue; /* v contains '\0' */
23879
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1389 }
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1390
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1391 /* sys.path_hooks import hook */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1392 if (p_loader != NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1393 PyObject *importer;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1394
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1395 importer = get_path_importer(path_importer_cache,
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41587
diff changeset
1396 path_hooks, origv);
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1397 if (importer == NULL) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1398 return NULL;
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1399 }
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1400 /* Note: importer is a borrowed reference */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1401 if (importer != Py_None) {
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1402 PyObject *loader;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1403 loader = PyObject_CallMethod(importer,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1404 "find_module",
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1405 "s", fullname);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1406 if (loader == NULL)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1407 return NULL; /* error */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1408 if (loader != Py_None) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1409 /* a loader was found */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1410 *p_loader = loader;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1411 return &importhookdescr;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1412 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1413 Py_DECREF(loader);
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1414 continue;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1415 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1416 }
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1417 /* no hook was found, use builtin import */
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1418
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1419 if (len > 0 && buf[len-1] != SEP
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1420 #ifdef ALTSEP
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1421 && buf[len-1] != ALTSEP
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1422 #endif
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1423 )
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1424 buf[len++] = SEP;
15401
c45c5080c844 Rip out DOS-8x3 support.
Guido van Rossum <guido@python.org>
parents: 14991
diff changeset
1425 strcpy(buf+len, name);
c45c5080c844 Rip out DOS-8x3 support.
Guido van Rossum <guido@python.org>
parents: 14991
diff changeset
1426 len += namelen;
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1427
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1428 /* Check for package import (buf holds a directory name,
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1429 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
1430 #ifdef HAVE_STAT
23879
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1431 if (stat(buf, &statbuf) == 0 && /* it exists */
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1432 S_ISDIR(statbuf.st_mode) && /* it's a directory */
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1433 case_ok(buf, len, namelen, name)) { /* case matches */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1434 if (find_init_module(buf)) { /* and has __init__.py */
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1435 return &fd_package;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1436 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1437 else {
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1438 char warnstr[MAXPATHLEN+80];
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1439 sprintf(warnstr, "Not importing directory "
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1440 "'%.*s': missing __init__.py",
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1441 MAXPATHLEN, buf);
42560
0fcc18aba385 PyErr_Warn is deprecated in 2.5 - goes away for 3.0
Skip Montanaro <skip@pobox.com>
parents: 42428
diff changeset
1442 if (PyErr_WarnEx(PyExc_ImportWarning,
0fcc18aba385 PyErr_Warn is deprecated in 2.5 - goes away for 3.0
Skip Montanaro <skip@pobox.com>
parents: 42428
diff changeset
1443 warnstr, 1)) {
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1444 return NULL;
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1445 }
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37879
diff changeset
1446 }
23879
08475c87d24f Apply diff2.txt from SF patch http://www.python.org/sf/566999
Walter Dörwald <walter@livinglogic.de>
parents: 23851
diff changeset
1447 }
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1448 #endif
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1449 #if 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
1450 /* take a snapshot of the module spec for restoration
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1451 * after the 8 character DLL hackery
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1452 */
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1453 saved_buf = strdup(buf);
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1454 saved_len = len;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1455 saved_namelen = namelen;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1456 #endif /* PYOS_OS2 */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1457 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42622
diff changeset
1458 #if defined(PYOS_OS2) && defined(HAVE_DYNAMIC_LOADING)
23585
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
1459 /* OS/2 limits DLLs to 8 character names (w/o
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
1460 extension)
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1461 * so if the name is longer than that and its a
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1462 * dynamically loaded module we're going to try,
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1463 * truncate the name before trying
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1464 */
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1465 if (strlen(subname) > 8) {
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1466 /* is this an attempt to load a C extension? */
23585
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
1467 const struct filedescr *scan;
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
1468 scan = _PyImport_DynLoadFiletab;
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1469 while (scan->suffix != NULL) {
23585
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
1470 if (!strcmp(scan->suffix, fdp->suffix))
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1471 break;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1472 else
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1473 scan++;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1474 }
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1475 if (scan->suffix != NULL) {
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1476 /* yes, so truncate the name */
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1477 namelen = 8;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1478 len -= strlen(subname) - namelen;
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1479 buf[len] = '\0';
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1480 }
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1481 }
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1482 #endif /* PYOS_OS2 */
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1483 strcpy(buf+len, fdp->suffix);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1484 if (Py_VerboseFlag > 1)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
1485 PySys_WriteStderr("# trying %s\n", buf);
23554
e5242f58a5b6 File modes in filedescr entries are also passed to Python, so we now put "U"
Jack Jansen <jack.jansen@cwi.nl>
parents: 23029
diff changeset
1486 filemode = fdp->mode;
32878
ca1aeb8ab380 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 32422
diff changeset
1487 if (filemode[0] == 'U')
23585
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
1488 filemode = "r" PY_STDIOTEXTMODE;
23554
e5242f58a5b6 File modes in filedescr entries are also passed to Python, so we now put "U"
Jack Jansen <jack.jansen@cwi.nl>
parents: 23029
diff changeset
1489 fp = fopen(buf, filemode);
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1490 if (fp != NULL) {
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1491 if (case_ok(buf, len, namelen, name))
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1492 break;
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1493 else { /* continue search */
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1494 fclose(fp);
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1495 fp = NULL;
16423
5c40113409c3 Steve Majewski's patch #103495, MatchFilename() and find_module()
Barry Warsaw <barry@python.org>
parents: 16311
diff changeset
1496 }
5c40113409c3 Steve Majewski's patch #103495, MatchFilename() and find_module()
Barry Warsaw <barry@python.org>
parents: 16311
diff changeset
1497 }
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1498 #if 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
1499 /* restore the saved snapshot */
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1500 strcpy(buf, saved_buf);
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1501 len = saved_len;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1502 namelen = saved_namelen;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1503 #endif
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1504 }
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1505 #if 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
1506 /* don't need/want the module name snapshot anymore */
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1507 if (saved_buf)
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1508 {
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1509 free(saved_buf);
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1510 saved_buf = NULL;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1511 }
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1512 #endif
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1513 if (fp != NULL)
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1514 break;
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1515 }
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1516 if (fp == NULL) {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1517 PyErr_Format(PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1518 "No module named %.200s", name);
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1519 return NULL;
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1520 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1521 *p_fp = fp;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1522 return fdp;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1523 }
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1524
33680
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1525 /* 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
1526 * 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
1527 */
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1528 struct filedescr *
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1529 _PyImport_FindModule(const char *name, PyObject *path, char *buf,
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1530 size_t buflen, FILE **p_fp, PyObject **p_loader)
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 return find_module((char *) name, (char *) name, path,
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1533 buf, buflen, p_fp, p_loader);
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1534 }
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1535
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1536 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
1537 {
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1538 return fd->type == PY_SOURCE || fd->type == PY_COMPILED;
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1539 }
bcafb68ad985 SF patch #1035498: -m option to run a module as a script
Raymond Hettinger <python@rcn.com>
parents: 33542
diff changeset
1540
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1541 /* 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
1542 * 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
1543 * /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
1544 * ^ ^ ^ ^
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1545 * |--------------------- buf ---------------------|
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1546 * |------------------- len ------------------|
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1547 * |------ name -------|
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1548 * |----- namelen -----|
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1549 * 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
1550 * 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
1551 *
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1552 * 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
1553 * 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
1554 *
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1555 * 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
1556 * 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
1557 * 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
1558 *
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1559 * 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
1560 * 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
1561 * 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
1562 * 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
1563 */
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1564
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1565 /* 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
1566 * 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
1567 */
34961
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1568 #if defined(MS_WINDOWS)
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1569 #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
1570
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1571 #elif defined(DJGPP)
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1572 #include <dir.h>
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1573
34961
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1574 #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
1575 #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
1576 #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
1577
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1578 #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
1579 #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
1580 #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
1581 #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
1582 #include <os2.h>
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1583 #endif
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1584
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1585 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
1586 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
1587 {
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1588 /* 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
1589 * match the sequence just above.
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1590 */
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1591
34961
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1592 /* MS_WINDOWS */
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1593 #if defined(MS_WINDOWS)
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1594 WIN32_FIND_DATA data;
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1595 HANDLE h;
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1596
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18803
diff changeset
1597 if (Py_GETENV("PYTHONCASEOK") != NULL)
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1598 return 1;
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1599
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1600 h = FindFirstFile(buf, &data);
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1601 if (h == INVALID_HANDLE_VALUE) {
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1602 PyErr_Format(PyExc_NameError,
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1603 "Can't find file for module %.100s\n(filename %.300s)",
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1604 name, buf);
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1605 return 0;
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1606 }
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1607 FindClose(h);
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1608 return strncmp(data.cFileName, name, namelen) == 0;
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1609
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1610 /* DJGPP */
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1611 #elif defined(DJGPP)
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1612 struct ffblk ffblk;
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1613 int done;
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1614
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18803
diff changeset
1615 if (Py_GETENV("PYTHONCASEOK") != NULL)
8544
35a2151fb1c3 Ignore Windows case check for ALL CAPS 8.3 files
Guido van Rossum <guido@python.org>
parents: 8374
diff changeset
1616 return 1;
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1617
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1618 done = findfirst(buf, &ffblk, FA_ARCH|FA_RDONLY|FA_HIDDEN|FA_DIREC);
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1619 if (done) {
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1620 PyErr_Format(PyExc_NameError,
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1621 "Can't find file for module %.100s\n(filename %.300s)",
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1622 name, buf);
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1623 return 0;
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1624 }
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1625 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
1626
34961
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1627 /* new-fangled macintosh (macosx) or Cygwin */
4a33c58c319c Patch #1197318: Cygwin case-sensitive import patch
Jason Tishler <jason@tishler.net>
parents: 33680
diff changeset
1628 #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
1629 DIR *dirp;
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1630 struct dirent *dp;
16902
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1631 char dirname[MAXPATHLEN + 1];
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1632 const int dirlen = len - namelen - 1; /* don't want trailing SEP */
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
1633
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18803
diff changeset
1634 if (Py_GETENV("PYTHONCASEOK") != NULL)
16896
fa84f9f41092 More fiddling w/ the new-fangled Mac import code.
Tim Peters <tim.peters@gmail.com>
parents: 16893
diff changeset
1635 return 1;
fa84f9f41092 More fiddling w/ the new-fangled Mac import code.
Tim Peters <tim.peters@gmail.com>
parents: 16893
diff changeset
1636
16902
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1637 /* Copy the dir component into dirname; substitute "." if empty */
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1638 if (dirlen <= 0) {
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1639 dirname[0] = '.';
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1640 dirname[1] = '\0';
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
1641 }
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1642 else {
16902
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1643 assert(dirlen <= MAXPATHLEN);
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1644 memcpy(dirname, buf, dirlen);
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1645 dirname[dirlen] = '\0';
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
1646 }
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1647 /* Open the directory and search the entries for an exact match. */
16902
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1648 dirp = opendir(dirname);
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
1649 if (dirp) {
16932
abc8c6c025ce Thanks to Steven Majewski, finally putting MacOS X imports to bed for 2.1b1.
Tim Peters <tim.peters@gmail.com>
parents: 16902
diff changeset
1650 char *nameWithExt = buf + len - namelen;
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
1651 while ((dp = readdir(dirp)) != NULL) {
16896
fa84f9f41092 More fiddling w/ the new-fangled Mac import code.
Tim Peters <tim.peters@gmail.com>
parents: 16893
diff changeset
1652 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
1653 #ifdef _DIRENT_HAVE_D_NAMELEN
16896
fa84f9f41092 More fiddling w/ the new-fangled Mac import code.
Tim Peters <tim.peters@gmail.com>
parents: 16893
diff changeset
1654 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
1655 #else
16896
fa84f9f41092 More fiddling w/ the new-fangled Mac import code.
Tim Peters <tim.peters@gmail.com>
parents: 16893
diff changeset
1656 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
1657 #endif
16902
f55f35dc5320 More MacOSX fiddling. As noted in a comment, I believe all variations
Tim Peters <tim.peters@gmail.com>
parents: 16896
diff changeset
1658 if (thislen >= namelen &&
16932
abc8c6c025ce Thanks to Steven Majewski, finally putting MacOS X imports to bed for 2.1b1.
Tim Peters <tim.peters@gmail.com>
parents: 16902
diff changeset
1659 strcmp(dp->d_name, nameWithExt) == 0) {
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
1660 (void)closedir(dirp);
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1661 return 1; /* Found */
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 }
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1663 }
16896
fa84f9f41092 More fiddling w/ the new-fangled Mac import code.
Tim Peters <tim.peters@gmail.com>
parents: 16893
diff changeset
1664 (void)closedir(dirp);
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
1665 }
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1666 return 0 ; /* Not found */
bdcd79873924 In Steven's apparent absence, check in *something* with a non-zero chance
Tim Peters <tim.peters@gmail.com>
parents: 16830
diff changeset
1667
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1668 /* OS/2 */
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1669 #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
1670 HDIR hdir = 1;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1671 ULONG srchcnt = 1;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1672 FILEFINDBUF3 ffbuf;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1673 APIRET rc;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1674
44587
fb5a6330f71b Merged revisions 59822-59841 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44583
diff changeset
1675 if (Py_GETENV("PYTHONCASEOK") != NULL)
22054
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1676 return 1;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1677
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1678 rc = DosFindFirst(buf,
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1679 &hdir,
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1680 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1681 &ffbuf, sizeof(ffbuf),
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1682 &srchcnt,
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1683 FIL_STANDARD);
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1684 if (rc != NO_ERROR)
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1685 return 0;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1686 return strncmp(ffbuf.achName, name, namelen) == 0;
5cbcfde3ad55 OS/2 EMX port changes (Python part of patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 21886
diff changeset
1687
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1688 /* 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
1689 #else
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1690 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
1691
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
1692 #endif
7305
067e4813c6d6 Added DJGPP version of check_case(), by Pit Scrorpion (Hans Nowak).
Guido van Rossum <guido@python.org>
parents: 7290
diff changeset
1693 }
067e4813c6d6 Added DJGPP version of check_case(), by Pit Scrorpion (Hans Nowak).
Guido van Rossum <guido@python.org>
parents: 7290
diff changeset
1694
7290
72d531d54572 Add case checking feature on import.
Guido van Rossum <guido@python.org>
parents: 7232
diff changeset
1695
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1696 #ifdef HAVE_STAT
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1697 /* 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
1698 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1699 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
1700 {
18388
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1701 const size_t save_len = strlen(buf);
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
1702 size_t i = save_len;
18388
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1703 char *pname; /* pointer to start of __init__ */
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1704 struct stat statbuf;
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1705
18388
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1706 /* For calling case_ok(buf, len, namelen, name):
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1707 * /a/b/c/d/e/f/g/h/i/j/k/some_long_module_name.py\0
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1708 * ^ ^ ^ ^
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1709 * |--------------------- buf ---------------------|
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1710 * |------------------- len ------------------|
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1711 * |------ name -------|
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1712 * |----- namelen -----|
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1713 */
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1714 if (save_len + 13 >= MAXPATHLEN)
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1715 return 0;
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1716 buf[i++] = SEP;
18388
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1717 pname = buf + i;
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1718 strcpy(pname, "__init__.py");
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1719 if (stat(buf, &statbuf) == 0) {
18388
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1720 if (case_ok(buf,
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1721 save_len + 9, /* len("/__init__") */
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1722 8, /* len("__init__") */
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1723 pname)) {
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1724 buf[save_len] = '\0';
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1725 return 1;
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1726 }
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1727 }
18388
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1728 i += strlen(pname);
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1729 strcpy(buf+i, Py_OptimizeFlag ? "o" : "c");
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1730 if (stat(buf, &statbuf) == 0) {
18388
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1731 if (case_ok(buf,
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1732 save_len + 9, /* len("/__init__") */
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1733 8, /* len("__init__") */
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1734 pname)) {
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1735 buf[save_len] = '\0';
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1736 return 1;
06332b3c4335 SF bug #438295: [Windows] __init__.py cause strange behavior
Tim Peters <tim.peters@gmail.com>
parents: 17642
diff changeset
1737 }
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1738 }
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1739 buf[save_len] = '\0';
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1740 return 0;
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1741 }
16947
b25131bc6ec7 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16932
diff changeset
1742
6542
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1743 #endif /* HAVE_STAT */
62bdb1cbe0f5 New policy for package imports: only a directory containing
Guido van Rossum <guido@python.org>
parents: 6541
diff changeset
1744
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1362
diff changeset
1745
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
1746 static int init_builtin(char *); /* Forward */
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1747
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1748 /* 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
1749 its module object WITH INCREMENTED REFERENCE COUNT */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
1750
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1751 static PyObject *
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1752 load_module(char *name, FILE *fp, char *buf, int type, PyObject *loader)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
1753 {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1754 PyObject *modules;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1755 PyObject *m;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1756 int err;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1757
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1758 /* First check that there's an open file (if we need one) */
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1759 switch (type) {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1760 case PY_SOURCE:
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1761 case PY_COMPILED:
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1762 if (fp == NULL) {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1763 PyErr_Format(PyExc_ValueError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1764 "file object required for import (type code %d)",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1765 type);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1766 return NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1767 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1768 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1769
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1770 switch (type) {
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1771
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1772 case PY_SOURCE:
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1773 m = load_source_module(name, buf, fp);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1774 break;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1775
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1776 case PY_COMPILED:
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1777 m = load_compiled_module(name, buf, fp);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1778 break;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1779
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
1780 #ifdef HAVE_DYNAMIC_LOADING
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1781 case C_EXTENSION:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1782 m = _PyImport_LoadDynamicModule(name, buf, fp);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1783 break;
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
1784 #endif
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1785
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1786 case PKG_DIRECTORY:
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1787 m = load_package(name, buf);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1788 break;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1789
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1790 case C_BUILTIN:
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1791 case PY_FROZEN:
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1792 if (buf != NULL && buf[0] != '\0')
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1793 name = buf;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1794 if (type == C_BUILTIN)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1795 err = init_builtin(name);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1796 else
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1797 err = PyImport_ImportFrozenModule(name);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1798 if (err < 0)
6299
0054a3840a75 Crrected a flow control error that caused the wrong error message when
Guido van Rossum <guido@python.org>
parents: 6285
diff changeset
1799 return NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1800 if (err == 0) {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1801 PyErr_Format(PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1802 "Purported %s module %.200s not found",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1803 type == C_BUILTIN ?
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1804 "builtin" : "frozen",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1805 name);
6299
0054a3840a75 Crrected a flow control error that caused the wrong error message when
Guido van Rossum <guido@python.org>
parents: 6285
diff changeset
1806 return NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1807 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1808 modules = PyImport_GetModuleDict();
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1809 m = PyDict_GetItemString(modules, name);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1810 if (m == NULL) {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1811 PyErr_Format(
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1812 PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1813 "%s module %.200s not properly initialized",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1814 type == C_BUILTIN ?
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1815 "builtin" : "frozen",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1816 name);
6299
0054a3840a75 Crrected a flow control error that caused the wrong error message when
Guido van Rossum <guido@python.org>
parents: 6285
diff changeset
1817 return NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1818 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1819 Py_INCREF(m);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1820 break;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1821
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1822 case IMP_HOOK: {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1823 if (loader == NULL) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1824 PyErr_SetString(PyExc_ImportError,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1825 "import hook without loader");
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1826 return NULL;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1827 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1828 m = PyObject_CallMethod(loader, "load_module", "s", name);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1829 break;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1830 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
1831
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1832 default:
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1833 PyErr_Format(PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1834 "Don't know how to import %.200s (type code %d)",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1835 name, type);
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
1836 m = NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1837
141
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1838 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1839
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1840 return m;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1841 }
40
f28ac7f621c7 Added reload() functionality.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
1842
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 74
diff changeset
1843
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1844 /* Initialize a built-in module.
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
1845 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
1846 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
1847
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1848 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1849 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
1850 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
1851 struct _inittab *p;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
1852
20249
1acecc908ee8 Remove a couple of unused local variables (bug #445960, compiler warnings
Greg Ward <gward@python.net>
parents: 19596
diff changeset
1853 if (_PyImport_FindExtension(name, name) != NULL)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
1854 return 1;
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
1855
6541
b2dfff06789c Instead of using _PyImport_Inittab[] directly, use the new "official"
Guido van Rossum <guido@python.org>
parents: 6454
diff changeset
1856 for (p = PyImport_Inittab; p->name != NULL; p++) {
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
1857 PyObject *mod;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
1858 if (strcmp(name, p->name) == 0) {
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
1859 if (p->initfunc == NULL) {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1860 PyErr_Format(PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1861 "Cannot re-init internal module %.200s",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1862 name);
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
1863 return -1;
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
1864 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1865 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
1866 PySys_WriteStderr("import %s # builtin\n", name);
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
1867 mod = (*p->initfunc)();
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
1868 if (mod == 0)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1869 return -1;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
1870 if (_PyImport_FixupExtension(mod, name, name) < 0)
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
1871 return -1;
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
1872 /* FixupExtension has put the module into sys.modules,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
1873 so we can release our own reference. */
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
1874 Py_DECREF(mod);
141
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1875 return 1;
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1876 }
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1877 }
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1878 return 0;
bf5a8539d8ba Call the init function of a built-in module here.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1879 }
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1880
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1881
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1882 /* 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
1883
3683
ec39264311ff struct frozen is now struct _frozen and comes from import.h.
Guido van Rossum <guido@python.org>
parents: 3590
diff changeset
1884 static struct _frozen *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1885 find_frozen(char *name)
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1886 {
3683
ec39264311ff struct frozen is now struct _frozen and comes from import.h.
Guido van Rossum <guido@python.org>
parents: 3590
diff changeset
1887 struct _frozen *p;
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1888
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1889 for (p = PyImport_FrozenModules; ; p++) {
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1890 if (p->name == NULL)
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1891 return NULL;
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1892 if (strcmp(p->name, name) == 0)
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1893 break;
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1894 }
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1895 return p;
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1896 }
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1897
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1898 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1899 get_frozen_object(char *name)
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1900 {
3683
ec39264311ff struct frozen is now struct _frozen and comes from import.h.
Guido van Rossum <guido@python.org>
parents: 3590
diff changeset
1901 struct _frozen *p = find_frozen(name);
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1902 int size;
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1903
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1904 if (p == NULL) {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1905 PyErr_Format(PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1906 "No such frozen object named %.200s",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1907 name);
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1908 return NULL;
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1909 }
20527
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1910 if (p->code == NULL) {
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1911 PyErr_Format(PyExc_ImportError,
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1912 "Excluded frozen object named %.200s",
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1913 name);
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1914 return NULL;
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1915 }
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1916 size = p->size;
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1917 if (size < 0)
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1918 size = -size;
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1919 return PyMarshal_ReadObjectFromString((char *)p->code, size);
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1920 }
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1921
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1922 /* Initialize a frozen module.
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1923 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
1924 an exception set if the initialization failed.
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1925 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
1926
6263e2fd9398 init_frozen non-static, frozenmain.c not in Makefile any more
Guido van Rossum <guido@python.org>
parents: 2270
diff changeset
1927 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
1928 PyImport_ImportFrozenModule(char *name)
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1929 {
3683
ec39264311ff struct frozen is now struct _frozen and comes from import.h.
Guido van Rossum <guido@python.org>
parents: 3590
diff changeset
1930 struct _frozen *p = find_frozen(name);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1931 PyObject *co;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1932 PyObject *m;
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1933 int ispackage;
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1934 int size;
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1935
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1936 if (p == NULL)
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
1937 return 0;
20527
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1938 if (p->code == NULL) {
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1939 PyErr_Format(PyExc_ImportError,
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1940 "Excluded frozen object named %.200s",
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1941 name);
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1942 return -1;
3d3b6b4cb79f First part of SF patch #416704: More robust freeze, by Toby Dickenson.
Guido van Rossum <guido@python.org>
parents: 20249
diff changeset
1943 }
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1944 size = p->size;
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1945 ispackage = (size < 0);
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1946 if (ispackage)
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1947 size = -size;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1948 if (Py_VerboseFlag)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 9203
diff changeset
1949 PySys_WriteStderr("import %s # frozen%s\n",
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1950 name, ispackage ? " package" : "");
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1951 co = PyMarshal_ReadObjectFromString((char *)p->code, size);
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1952 if (co == NULL)
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1953 return -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1954 if (!PyCode_Check(co)) {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1955 PyErr_Format(PyExc_TypeError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1956 "frozen object %.200s is not a code object",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
1957 name);
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1958 goto err_return;
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
1959 }
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1960 if (ispackage) {
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1961 /* Set __path__ to the package name */
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1962 PyObject *d, *s;
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1963 int err;
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1964 m = PyImport_AddModule(name);
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1965 if (m == NULL)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1966 goto err_return;
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1967 d = PyModule_GetDict(m);
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
1968 s = PyUnicode_InternFromString(name);
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1969 if (s == NULL)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1970 goto err_return;
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1971 err = PyDict_SetItemString(d, "__path__", s);
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1972 Py_DECREF(s);
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1973 if (err != 0)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1974 goto err_return;
7552
c8e5f704623d Support for frozen packages, matching the revamped Tools/freeze.
Guido van Rossum <guido@python.org>
parents: 7535
diff changeset
1975 }
7232
74c57288a02b Added PyImport_ExecCodeModuleEx(), which adds an extra parameter to
Guido van Rossum <guido@python.org>
parents: 7202
diff changeset
1976 m = PyImport_ExecCodeModuleEx(name, co, "<frozen>");
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1977 if (m == NULL)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1978 goto err_return;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1979 Py_DECREF(co);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1980 Py_DECREF(m);
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
1981 return 1;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1982 err_return:
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1983 Py_DECREF(co);
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
1984 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
1985 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1986
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1987
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
1988 /* 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
1989 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
1990
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
1991 PyObject *
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35757
diff changeset
1992 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
1993 {
16534
b59f3a1eb123 This modified version of a patch by Thomas Heller allows __import__
Marc-André Lemburg <mal@egenix.com>
parents: 16433
diff changeset
1994 PyObject *pname;
b59f3a1eb123 This modified version of a patch by Thomas Heller allows __import__
Marc-André Lemburg <mal@egenix.com>
parents: 16433
diff changeset
1995 PyObject *result;
b59f3a1eb123 This modified version of a patch by Thomas Heller allows __import__
Marc-André Lemburg <mal@egenix.com>
parents: 16433
diff changeset
1996
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
1997 pname = PyUnicode_FromString(name);
28392
5ee29f1364f3 SF patch #708201, unchecked return value in import.c by Jason Harper
Neal Norwitz <nnorwitz@gmail.com>
parents: 27917
diff changeset
1998 if (pname == NULL)
5ee29f1364f3 SF patch #708201, unchecked return value in import.c by Jason Harper
Neal Norwitz <nnorwitz@gmail.com>
parents: 27917
diff changeset
1999 return NULL;
16534
b59f3a1eb123 This modified version of a patch by Thomas Heller allows __import__
Marc-André Lemburg <mal@egenix.com>
parents: 16433
diff changeset
2000 result = PyImport_Import(pname);
b59f3a1eb123 This modified version of a patch by Thomas Heller allows __import__
Marc-André Lemburg <mal@egenix.com>
parents: 16433
diff changeset
2001 Py_DECREF(pname);
b59f3a1eb123 This modified version of a patch by Thomas Heller allows __import__
Marc-André Lemburg <mal@egenix.com>
parents: 16433
diff changeset
2002 return result;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2003 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2004
44441
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2005 /* Import a module without blocking
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2006 *
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2007 * At first it tries to fetch the module from sys.modules. If the module was
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2008 * never loaded before it loads it with PyImport_ImportModule() unless another
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2009 * thread holds the import lock. In the latter case the function raises an
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2010 * ImportError instead of blocking.
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2011 *
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2012 * Returns the module object with incremented ref count.
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2013 */
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2014 PyObject *
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2015 PyImport_ImportModuleNoBlock(const char *name)
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2016 {
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2017 PyObject *result;
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2018 PyObject *modules;
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2019 long me;
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2020
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2021 /* Try to get the module from sys.modules[name] */
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2022 modules = PyImport_GetModuleDict();
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2023 if (modules == NULL)
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2024 return NULL;
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2025
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2026 result = PyDict_GetItemString(modules, name);
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2027 if (result != NULL) {
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2028 Py_INCREF(result);
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2029 return result;
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2030 }
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2031 else {
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2032 PyErr_Clear();
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2033 }
48946
3b9608118f8d Merged revisions 66045,66048-66049,66053,66060,66062-66063,66065,66067,66071-66074,66080,66082-66083,66090-66093,66097-66099,66103,66105,66110,66118 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
2034 #ifdef WITH_THREAD
44441
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2035 /* check the import lock
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2036 * me might be -1 but I ignore the error here, the lock function
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2037 * takes care of the problem */
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2038 me = PyThread_get_thread_ident();
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2039 if (import_lock_thread == -1 || import_lock_thread == me) {
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2040 /* no thread or me is holding the lock */
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2041 return PyImport_ImportModule(name);
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2042 }
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2043 else {
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2044 PyErr_Format(PyExc_ImportError,
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2045 "Failed to import %.200s because the import lock"
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2046 "is held by another thread.",
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2047 name);
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2048 return NULL;
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2049 }
48946
3b9608118f8d Merged revisions 66045,66048-66049,66053,66060,66062-66063,66065,66067,66071-66074,66080,66082-66083,66090-66093,66097-66099,66103,66105,66110,66118 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
2050 #else
3b9608118f8d Merged revisions 66045,66048-66049,66053,66060,66062-66063,66065,66067,66071-66074,66080,66082-66083,66090-66093,66097-66099,66103,66105,66110,66118 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
2051 return PyImport_ImportModule(name);
3b9608118f8d Merged revisions 66045,66048-66049,66053,66060,66062-66063,66065,66067,66071-66074,66080,66082-66083,66090-66093,66097-66099,66103,66105,66110,66118 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 48660
diff changeset
2052 #endif
44441
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2053 }
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2054
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
2055 /* 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
2056 static PyObject *get_parent(PyObject *globals, char *buf,
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2057 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
2058 static PyObject *load_next(PyObject *mod, PyObject *altmod,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2059 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
2060 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
2061 static int ensure_fromlist(PyObject *mod, PyObject *fromlist,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2062 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
2063 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
2064
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
2065 /* 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
2066
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
2067 static PyObject *
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2068 import_module_level(char *name, PyObject *globals, PyObject *locals,
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2069 PyObject *fromlist, int level)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2070 {
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
2071 char buf[MAXPATHLEN+1];
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2072 Py_ssize_t buflen = 0;
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
2073 PyObject *parent, *head, *next, *tail;
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
2074
44617
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2075 if (strchr(name, '/') != NULL
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2076 #ifdef MS_WINDOWS
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2077 || strchr(name, '\\') != NULL
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2078 #endif
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2079 ) {
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2080 PyErr_SetString(PyExc_ImportError,
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2081 "Import by filename is not supported.");
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2082 return NULL;
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2083 }
a29adc3b0f26 Merged revisions 59864-59882 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44587
diff changeset
2084
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2085 parent = get_parent(globals, buf, &buflen, 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
2086 if (parent == NULL)
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
2087 return NULL;
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
2088
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
2089 head = load_next(parent, Py_None, &name, buf, &buflen);
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
2090 if (head == NULL)
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
2091 return NULL;
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
2092
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
2093 tail = head;
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
2094 Py_INCREF(tail);
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
2095 while (name) {
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
2096 next = load_next(tail, tail, &name, buf, &buflen);
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
2097 Py_DECREF(tail);
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
2098 if (next == NULL) {
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
2099 Py_DECREF(head);
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
2100 return NULL;
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
2101 }
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
2102 tail = next;
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 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2104 if (tail == Py_None) {
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2105 /* If tail is Py_None, both get_parent and load_next found
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2106 an empty module name: someone called __import__("") or
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2107 doctored faulty bytecode */
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2108 Py_DECREF(tail);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2109 Py_DECREF(head);
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2110 PyErr_SetString(PyExc_ValueError,
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2111 "Empty module name");
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2112 return NULL;
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2113 }
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
2114
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
2115 if (fromlist != NULL) {
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
2116 if (fromlist == Py_None || !PyObject_IsTrue(fromlist))
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
2117 fromlist = NULL;
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
2118 }
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
2119
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
2120 if (fromlist == NULL) {
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
2121 Py_DECREF(tail);
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
2122 return head;
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
2123 }
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
2124
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
2125 Py_DECREF(head);
6285
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2126 if (!ensure_fromlist(tail, fromlist, buf, buflen, 0)) {
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
2127 Py_DECREF(tail);
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
2128 return NULL;
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
2129 }
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
2130
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
2131 return tail;
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
2132 }
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
2133
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2134 PyObject *
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2135 PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals,
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2136 PyObject *fromlist, int level)
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2137 {
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2138 PyObject *result;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2139 lock_import();
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2140 result = import_module_level(name, globals, locals, fromlist, level);
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
2141 if (unlock_import() < 0) {
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
2142 Py_XDECREF(result);
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
2143 PyErr_SetString(PyExc_RuntimeError,
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
2144 "not holding the import lock");
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
2145 return NULL;
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
2146 }
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
2147 return result;
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
2148 }
ea8184bff12d Add a single Python-wide (!) lock on import. Only one thread at a
Guido van Rossum <guido@python.org>
parents: 7403
diff changeset
2149
32156
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2150 /* 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
2151 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
2152 sys.modules entry for foo.bar. If globals is from a package's __init__.py,
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2153 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
2154
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2155 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
2156 the name in *p_buflen.
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2157
56a4ec066621 starting to add comments to explain what's here
Fred Drake <fdrake@acm.org>
parents: 31851
diff changeset
2158 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
2159 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
2160 */
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
2161 static PyObject *
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2162 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
2163 {
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
2164 static PyObject *namestr = NULL;
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
2165 static PyObject *pathstr = NULL;
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2166 static PyObject *pkgstr = NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2167 PyObject *pkgname, *modname, *modpath, *modules, *parent;
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2168 int orig_level = 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
2169
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2170 if (globals == NULL || !PyDict_Check(globals) || !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
2171 return Py_None;
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
2172
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
2173 if (namestr == NULL) {
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2174 namestr = PyUnicode_InternFromString("__name__");
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
2175 if (namestr == NULL)
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
2176 return NULL;
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
2177 }
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
2178 if (pathstr == NULL) {
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2179 pathstr = PyUnicode_InternFromString("__path__");
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
2180 if (pathstr == NULL)
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 return NULL;
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 }
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2183 if (pkgstr == NULL) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2184 pkgstr = PyUnicode_InternFromString("__package__");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2185 if (pkgstr == NULL)
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2186 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2187 }
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
2188
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
2189 *buf = '\0';
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
2190 *p_buflen = 0;
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2191 pkgname = PyDict_GetItem(globals, pkgstr);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2192
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2193 if ((pkgname != NULL) && (pkgname != Py_None)) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2194 /* __package__ is set, so use it */
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46547
diff changeset
2195 char *pkgname_str;
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2196 Py_ssize_t len;
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46547
diff changeset
2197
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2198 if (!PyUnicode_Check(pkgname)) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2199 PyErr_SetString(PyExc_ValueError,
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2200 "__package__ set to non-string");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2201 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2202 }
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48540
diff changeset
2203 pkgname_str = _PyUnicode_AsStringAndSize(pkgname, &len);
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2204 if (len == 0) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2205 if (level > 0) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2206 PyErr_SetString(PyExc_ValueError,
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2207 "Attempted relative import in non-package");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2208 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2209 }
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2210 return Py_None;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2211 }
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 if (len > MAXPATHLEN) {
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
2213 PyErr_SetString(PyExc_ValueError,
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2214 "Package name too long");
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
2215 return NULL;
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
2216 }
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46547
diff changeset
2217 strcpy(buf, pkgname_str);
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2218 } else {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2219 /* __package__ not set, so figure it out and set it */
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2220 modname = PyDict_GetItem(globals, namestr);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2221 if (modname == NULL || !PyUnicode_Check(modname))
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2222 return Py_None;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2223
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2224 modpath = PyDict_GetItem(globals, pathstr);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2225 if (modpath != NULL) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2226 /* __path__ is set, so modname is already the package name */
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46547
diff changeset
2227 char *modname_str;
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46547
diff changeset
2228 Py_ssize_t len;
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2229 int error;
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46547
diff changeset
2230
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48540
diff changeset
2231 modname_str = _PyUnicode_AsStringAndSize(modname, &len);
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2232 if (len > MAXPATHLEN) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2233 PyErr_SetString(PyExc_ValueError,
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2234 "Module name too long");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2235 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2236 }
46644
ea594306ea85 Issue #1950: Fixed misusage of PyUnicode_AsString().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 46547
diff changeset
2237 strcpy(buf, modname_str);
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2238 error = PyDict_SetItem(globals, pkgstr, modname);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2239 if (error) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2240 PyErr_SetString(PyExc_ValueError,
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2241 "Could not set __package__");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2242 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2243 }
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2244 } else {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2245 /* Normal module, so work out the package name if any */
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48540
diff changeset
2246 char *start = _PyUnicode_AsString(modname);
44142
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2247 char *lastdot = strrchr(start, '.');
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2248 size_t len;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2249 int error;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2250 if (lastdot == NULL && level > 0) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2251 PyErr_SetString(PyExc_ValueError,
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2252 "Attempted relative import in non-package");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2253 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2254 }
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2255 if (lastdot == NULL) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2256 error = PyDict_SetItem(globals, pkgstr, Py_None);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2257 if (error) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2258 PyErr_SetString(PyExc_ValueError,
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2259 "Could not set __package__");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2260 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2261 }
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2262 return Py_None;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2263 }
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2264 len = lastdot - start;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2265 if (len >= MAXPATHLEN) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2266 PyErr_SetString(PyExc_ValueError,
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2267 "Module name too long");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2268 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2269 }
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2270 strncpy(buf, start, len);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2271 buf[len] = '\0';
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2272 pkgname = PyUnicode_FromString(buf);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2273 if (pkgname == NULL) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2274 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2275 }
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2276 error = PyDict_SetItem(globals, pkgstr, pkgname);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2277 Py_DECREF(pkgname);
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2278 if (error) {
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2279 PyErr_SetString(PyExc_ValueError,
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2280 "Could not set __package__");
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2281 return NULL;
c8b339cfef1f Manual merge of PEP 366 implementation from trunk (the automatic merge choked on the PyString->PyUnicode changes)
Nick Coghlan <ncoghlan@gmail.com>
parents: 44134
diff changeset
2282 }
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2283 }
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
2284 }
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2285 while (--level > 0) {
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2286 char *dot = strrchr(buf, '.');
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2287 if (dot == NULL) {
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2288 PyErr_SetString(PyExc_ValueError,
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2289 "Attempted relative import beyond "
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 40215
diff changeset
2290 "toplevel package");
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2291 return NULL;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2292 }
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2293 *dot = '\0';
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2294 }
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2295 *p_buflen = strlen(buf);
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2296
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
2297 modules = PyImport_GetModuleDict();
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
2298 parent = PyDict_GetItemString(modules, buf);
48499
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2299 if (parent == NULL) {
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2300 if (orig_level < 1) {
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2301 PyObject *err_msg = PyBytes_FromFormat(
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2302 "Parent module '%.200s' not found "
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2303 "while handling absolute import", buf);
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2304 if (err_msg == NULL) {
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2305 return NULL;
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2306 }
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2307 if (!PyErr_WarnEx(PyExc_RuntimeWarning,
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2308 PyBytes_AsString(err_msg), 1)) {
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2309 *buf = '\0';
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2310 *p_buflen = 0;
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2311 parent = Py_None;
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2312 }
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2313 Py_DECREF(err_msg);
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2314 } else {
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2315 PyErr_Format(PyExc_SystemError,
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2316 "Parent module '%.200s' not loaded, "
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2317 "cannot perform relative import", buf);
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2318 }
de0bf60f1245 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47599
diff changeset
2319 }
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
2320 return parent;
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
2321 /* We expect, but can't guarantee, if parent != None, that:
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
2322 - parent.__name__ == buf
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
2323 - parent.__dict__ is globals
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
2324 If this is violated... Who cares? */
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
2325 }
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
2326
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2327 /* 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
2328 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2329 load_next(PyObject *mod, PyObject *altmod, char **p_name, char *buf,
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2330 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
2331 {
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
2332 char *name = *p_name;
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
2333 char *dot = strchr(name, '.');
13226
12437deb0680 Trent Mick <trentm@activestate.com>:
Fred Drake <fdrake@acm.org>
parents: 13181
diff changeset
2334 size_t len;
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
2335 char *p;
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
2336 PyObject *result;
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
2337
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2338 if (strlen(name) == 0) {
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2339 /* completely empty module name should only happen in
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
2340 'from . import' (or '__import__("")')*/
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2341 Py_INCREF(mod);
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2342 *p_name = NULL;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2343 return mod;
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2344 }
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2345
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
2346 if (dot == NULL) {
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
2347 *p_name = NULL;
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
2348 len = strlen(name);
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
2349 }
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
2350 else {
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
2351 *p_name = dot+1;
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
2352 len = dot-name;
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
2353 }
8012
2b09a5d26368 Reject empty module names -- otherwise __import__("") does something
Guido van Rossum <guido@python.org>
parents: 7987
diff changeset
2354 if (len == 0) {
2b09a5d26368 Reject empty module names -- otherwise __import__("") does something
Guido van Rossum <guido@python.org>
parents: 7987
diff changeset
2355 PyErr_SetString(PyExc_ValueError,
2b09a5d26368 Reject empty module names -- otherwise __import__("") does something
Guido van Rossum <guido@python.org>
parents: 7987
diff changeset
2356 "Empty module name");
2b09a5d26368 Reject empty module names -- otherwise __import__("") does something
Guido van Rossum <guido@python.org>
parents: 7987
diff changeset
2357 return NULL;
2b09a5d26368 Reject empty module names -- otherwise __import__("") does something
Guido van Rossum <guido@python.org>
parents: 7987
diff changeset
2358 }
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
2359
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
2360 p = buf + *p_buflen;
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
2361 if (p != buf)
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
2362 *p++ = '.';
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
2363 if (p+len-buf >= MAXPATHLEN) {
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
2364 PyErr_SetString(PyExc_ValueError,
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
2365 "Module name too long");
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
2366 return NULL;
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
2367 }
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
2368 strncpy(p, name, len);
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 p[len] = '\0';
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 *p_buflen = p+len-buf;
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
2371
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 result = import_submodule(mod, p, buf);
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
2373 if (result == Py_None && altmod != mod) {
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
2374 Py_DECREF(result);
6252
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2375 /* Here, altmod must be None and mod must not be None */
6256
ce2092714484 Bugfix: import A.B from inside package was busted by mark_miss optimization.
Guido van Rossum <guido@python.org>
parents: 6252
diff changeset
2376 result = import_submodule(altmod, p, p);
6252
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2377 if (result != NULL && result != Py_None) {
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2378 if (mark_miss(buf) != 0) {
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2379 Py_DECREF(result);
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2380 return NULL;
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2381 }
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2382 strncpy(buf, name, len);
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2383 buf[len] = '\0';
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2384 *p_buflen = len;
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2385 }
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
2386 }
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
2387 if (result == NULL)
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
2388 return NULL;
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
2389
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
2390 if (result == Py_None) {
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
2391 Py_DECREF(result);
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
2392 PyErr_Format(PyExc_ImportError,
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
2393 "No module named %.200s", name);
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
2394 return NULL;
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
2395 }
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
2396
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
2397 return result;
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
2398 }
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
2399
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
2400 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2401 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
2402 {
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2403 PyObject *modules = PyImport_GetModuleDict();
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2404 return PyDict_SetItemString(modules, name, Py_None);
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2405 }
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2406
6429db5d0af7 Significant speedup -- when a submodule imports a global module, add a
Guido van Rossum <guido@python.org>
parents: 6250
diff changeset
2407 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
2408 ensure_fromlist(PyObject *mod, PyObject *fromlist, char *buf, Py_ssize_t buflen,
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2409 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
2410 {
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
2411 int i;
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
2412
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
2413 if (!PyObject_HasAttrString(mod, "__path__"))
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
2414 return 1;
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
2415
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
2416 for (i = 0; ; i++) {
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
2417 PyObject *item = PySequence_GetItem(fromlist, i);
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
2418 int hasit;
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
2419 if (item == NULL) {
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
2420 if (PyErr_ExceptionMatches(PyExc_IndexError)) {
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
2421 PyErr_Clear();
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
2422 return 1;
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
2423 }
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
2424 return 0;
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
2425 }
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2426 if (!PyUnicode_Check(item)) {
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
2427 PyErr_SetString(PyExc_TypeError,
42862
ed1594a16bdb Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42859
diff changeset
2428 "Item in ``from list'' not a string");
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
2429 Py_DECREF(item);
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
2430 return 0;
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
2431 }
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2432 if (PyUnicode_AS_UNICODE(item)[0] == '*') {
6285
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2433 PyObject *all;
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
2434 Py_DECREF(item);
6285
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2435 /* See if the package defines __all__ */
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2436 if (recursive)
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2437 continue; /* Avoid endless recursion */
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2438 all = PyObject_GetAttrString(mod, "__all__");
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2439 if (all == NULL)
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2440 PyErr_Clear();
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2441 else {
31778
c4bae089bfc8 Decref all if ensure_fromlist fails. Fixes #876533.
Martin v. Löwis <martin@v.loewis.de>
parents: 31320
diff changeset
2442 int ret = ensure_fromlist(mod, all, buf, buflen, 1);
c4bae089bfc8 Decref all if ensure_fromlist fails. Fixes #876533.
Martin v. Löwis <martin@v.loewis.de>
parents: 31320
diff changeset
2443 Py_DECREF(all);
c4bae089bfc8 Decref all if ensure_fromlist fails. Fixes #876533.
Martin v. Löwis <martin@v.loewis.de>
parents: 31320
diff changeset
2444 if (!ret)
6285
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2445 return 0;
337054e9d4fa Added support for __all__, which should be a list of modules to be
Guido van Rossum <guido@python.org>
parents: 6256
diff changeset
2446 }
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
2447 continue;
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
2448 }
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
2449 hasit = PyObject_HasAttr(mod, item);
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
2450 if (!hasit) {
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2451 PyObject *item8;
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2452 char *subname;
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
2453 PyObject *submod;
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 char *p;
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2455 if (!Py_FileSystemDefaultEncoding) {
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2456 item8 = PyUnicode_EncodeASCII(PyUnicode_AsUnicode(item),
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2457 PyUnicode_GetSize(item),
42057
3473a685e5e7 Revert 55876. Use PyUnicode_AsEncodedString instead.
Martin v. Löwis <martin@v.loewis.de>
parents: 42013
diff changeset
2458 NULL);
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2459 } else {
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43762
diff changeset
2460 item8 = PyUnicode_AsEncodedString(item,
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43762
diff changeset
2461 Py_FileSystemDefaultEncoding, NULL);
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2462 }
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2463 if (!item8) {
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2464 PyErr_SetString(PyExc_ValueError, "Cannot encode path item");
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2465 return 0;
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2466 }
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46644
diff changeset
2467 subname = PyBytes_AS_STRING(item8);
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
2468 if (buflen + strlen(subname) >= MAXPATHLEN) {
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
2469 PyErr_SetString(PyExc_ValueError,
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
2470 "Module name too long");
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
2471 Py_DECREF(item);
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
2472 return 0;
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
2473 }
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
2474 p = buf + buflen;
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
2475 *p++ = '.';
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
2476 strcpy(p, subname);
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
2477 submod = import_submodule(mod, subname, buf);
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2478 Py_DECREF(item8);
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
2479 Py_XDECREF(submod);
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
2480 if (submod == NULL) {
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
2481 Py_DECREF(item);
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
2482 return 0;
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
2483 }
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
2484 }
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
2485 Py_DECREF(item);
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
2486 }
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
2487
6375
b68185d94777 Remove unreachable "return 1" at end of ensure_fromlist().
Guido van Rossum <guido@python.org>
parents: 6305
diff changeset
2488 /* 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
2489 }
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
2490
29501
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2491 static int
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2492 add_submodule(PyObject *mod, PyObject *submod, char *fullname, char *subname,
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2493 PyObject *modules)
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2494 {
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2495 if (mod == Py_None)
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2496 return 1;
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2497 /* Irrespective of the success of this load, make a
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2498 reference to it in the parent package module. A copy gets
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2499 saved in the modules dictionary under the full name, so get a
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2500 reference from there, if need be. (The exception is when the
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2501 load failed with a SyntaxError -- then there's no trace in
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2502 sys.modules. In that case, of course, do nothing extra.) */
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2503 if (submod == NULL) {
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2504 submod = PyDict_GetItemString(modules, fullname);
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2505 if (submod == NULL)
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2506 return 1;
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2507 }
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2508 if (PyModule_Check(mod)) {
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2509 /* We can't use setattr here since it can give a
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2510 * spurious warning if the submodule name shadows a
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2511 * builtin name */
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2512 PyObject *dict = PyModule_GetDict(mod);
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2513 if (!dict)
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2514 return 0;
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2515 if (PyDict_SetItemString(dict, subname, submod) < 0)
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2516 return 0;
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2517 }
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2518 else {
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2519 if (PyObject_SetAttrString(mod, subname, submod) < 0)
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2520 return 0;
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2521 }
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2522 return 1;
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2523 }
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2524
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
2525 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2526 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
2527 {
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
2528 PyObject *modules = PyImport_GetModuleDict();
29501
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2529 PyObject *m = NULL;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2530
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
2531 /* Require:
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
2532 if mod == None: subname == fullname
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
2533 else: mod.__name__ + "." + subname == fullname
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
2534 */
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
2535
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
2536 if ((m = PyDict_GetItemString(modules, fullname)) != NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2537 Py_INCREF(m);
2198
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
2538 }
27557e70a8f0 fix import related leaks
Guido van Rossum <guido@python.org>
parents: 2128
diff changeset
2539 else {
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
2540 PyObject *path, *loader = NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2541 char buf[MAXPATHLEN+1];
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2542 struct filedescr *fdp;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2543 FILE *fp = NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2544
8374
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2545 if (mod == Py_None)
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2546 path = NULL;
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2547 else {
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2548 path = PyObject_GetAttrString(mod, "__path__");
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2549 if (path == NULL) {
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2550 PyErr_Clear();
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2551 Py_INCREF(Py_None);
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2552 return Py_None;
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2553 }
a1bfe5bc6d8e Fix a curious bug: statements like "import sys.time" would succeed,
Guido van Rossum <guido@python.org>
parents: 8330
diff changeset
2554 }
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
2555
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2556 buf[0] = '\0';
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
2557 fdp = find_module(fullname, subname, path, buf, MAXPATHLEN+1,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
2558 &fp, &loader);
8613
e3d3bbbd93f3 Fix two smal memory leaks discovered by Vadim Chugunov.
Guido van Rossum <guido@python.org>
parents: 8544
diff changeset
2559 Py_XDECREF(path);
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
2560 if (fdp == NULL) {
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
2561 if (!PyErr_ExceptionMatches(PyExc_ImportError))
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
2562 return NULL;
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
2563 PyErr_Clear();
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
2564 Py_INCREF(Py_None);
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
2565 return Py_None;
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
2566 }
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
2567 m = load_module(fullname, fp, buf, fdp->type, loader);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
2568 Py_XDECREF(loader);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2569 if (fp)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2570 fclose(fp);
29501
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2571 if (!add_submodule(mod, m, fullname, subname, modules)) {
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2572 Py_XDECREF(m);
6c6b321d1d10 Don't use the module object setattr when importing submodules. Instead,
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 28392
diff changeset
2573 m = NULL;
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
2574 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2575 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2576
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2577 return m;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2578 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2579
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2580
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2581 /* 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
2582 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
2583
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2584 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2585 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
2586 {
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2587 PyInterpreterState *interp = PyThreadState_Get()->interp;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2588 PyObject *modules_reloading = interp->modules_reloading;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
2589 PyObject *modules = PyImport_GetModuleDict();
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2590 PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
6250
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2591 char *name, *subname;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2592 char buf[MAXPATHLEN+1];
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2593 struct filedescr *fdp;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2594 FILE *fp = NULL;
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2595 PyObject *newm;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2596
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2597 if (modules_reloading == NULL) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2598 Py_FatalError("PyImport_ReloadModule: "
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41666
diff changeset
2599 "no modules_reloading dictionary!");
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2600 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2601 }
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2602
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2603 if (m == NULL || !PyModule_Check(m)) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2604 PyErr_SetString(PyExc_TypeError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2605 "reload() argument must be module");
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2606 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2607 }
42859
036598de4660 Fix a couple of warnings
Neal Norwitz <nnorwitz@gmail.com>
parents: 42775
diff changeset
2608 name = (char*)PyModule_GetName(m);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2609 if (name == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2610 return NULL;
5926
8d2194e22e67 The last of the mass checkins for separate (sub)interpreters.
Guido van Rossum <guido@python.org>
parents: 5894
diff changeset
2611 if (m != PyDict_GetItemString(modules, name)) {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2612 PyErr_Format(PyExc_ImportError,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2613 "reload(): module %.200s not in sys.modules",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2614 name);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2615 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2616 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2617 existing_m = PyDict_GetItemString(modules_reloading, name);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2618 if (existing_m != NULL) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2619 /* Due to a recursive reload, this module is already
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2620 being reloaded. */
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2621 Py_INCREF(existing_m);
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2622 return existing_m;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2623 }
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2624 if (PyDict_SetItemString(modules_reloading, name, m) < 0)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2625 return NULL;
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2626
6250
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2627 subname = strrchr(name, '.');
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2628 if (subname == NULL)
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2629 subname = name;
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2630 else {
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2631 PyObject *parentname, *parent;
43762
de3b63f8b17b Applied patch #1379 with a minor tweak.
Christian Heimes <christian@cheimes.de>
parents: 43712
diff changeset
2632 parentname = PyUnicode_FromStringAndSize(name, (subname-name));
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2633 if (parentname == NULL) {
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2634 imp_modules_reloading_clear();
6250
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2635 return NULL;
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2636 }
6250
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2637 parent = PyDict_GetItem(modules, parentname);
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2638 if (parent == NULL) {
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2639 PyErr_Format(PyExc_ImportError,
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2640 "reload(): parent %.200s not in sys.modules",
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48540
diff changeset
2641 _PyUnicode_AsString(parentname));
35542
81259009ba2c Patch #1290454: Fix reload() error message when parent module is not in
Georg Brandl <georg@python.org>
parents: 35008
diff changeset
2642 Py_DECREF(parentname);
41969
9d02a4adf574 Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41666
diff changeset
2643 imp_modules_reloading_clear();
6250
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2644 return NULL;
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2645 }
35542
81259009ba2c Patch #1290454: Fix reload() error message when parent module is not in
Georg Brandl <georg@python.org>
parents: 35008
diff changeset
2646 Py_DECREF(parentname);
6250
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2647 subname++;
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2648 path = PyObject_GetAttrString(parent, "__path__");
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2649 if (path == NULL)
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2650 PyErr_Clear();
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2651 }
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2652 buf[0] = '\0';
33542
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2653 fdp = find_module(name, subname, path, buf, MAXPATHLEN+1, &fp, &loader);
6250
3fa4b7ae0e6b Fix reload() for package submodules.
Guido van Rossum <guido@python.org>
parents: 6249
diff changeset
2654 Py_XDECREF(path);
33542
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2655
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2656 if (fdp == NULL) {
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2657 Py_XDECREF(loader);
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2658 imp_modules_reloading_clear();
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2659 return NULL;
33542
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2660 }
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2661
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2662 newm = load_module(name, fp, buf, fdp->type, loader);
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2663 Py_XDECREF(loader);
d74ddcff0724 Fix for SF bug #1029475 : reload() doesn't work with PEP 302 loaders.
Phillip J. Eby <pje@telecommunity.com>
parents: 33539
diff changeset
2664
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2665 if (fp)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2666 fclose(fp);
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2667 if (newm == NULL) {
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2668 /* load_module probably removed name from modules because of
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2669 * the error. Put back the original module object. We're
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2670 * going to return NULL in this case regardless of whether
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2671 * replacing name succeeds, so the return value is ignored.
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2672 */
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2673 PyDict_SetItemString(modules, name, m);
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2674 }
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41253
diff changeset
2675 imp_modules_reloading_clear();
32882
331e60d8ce6d PyImport_ExecCodeModuleEx(): remove module from sys.modules in error cases.
Tim Peters <tim.peters@gmail.com>
parents: 32879
diff changeset
2676 return newm;
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
2677 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2678
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2679
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2680 /* 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
2681 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
2682 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
2683 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
2684 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
2685 A dummy list ["__doc__"] is passed as the 4th argument so that
42862
ed1594a16bdb Use unicode and remove support for some uses of str8.
Neal Norwitz <nnorwitz@gmail.com>
parents: 42859
diff changeset
2686 e.g. PyImport_Import(PyUnicode_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
2687 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
2688
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2689 PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2690 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
2691 {
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2692 static PyObject *silly_list = NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2693 static PyObject *builtins_str = NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2694 static PyObject *import_str = NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2695 PyObject *globals = NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2696 PyObject *import = NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2697 PyObject *builtins = NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2698 PyObject *r = NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2699
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2700 /* Initialize constant string objects */
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2701 if (silly_list == NULL) {
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2702 import_str = PyUnicode_InternFromString("__import__");
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2703 if (import_str == NULL)
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2704 return NULL;
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41969
diff changeset
2705 builtins_str = PyUnicode_InternFromString("__builtins__");
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2706 if (builtins_str == NULL)
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2707 return NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2708 silly_list = Py_BuildValue("[s]", "__doc__");
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2709 if (silly_list == NULL)
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2710 return NULL;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2711 }
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2712
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2713 /* Get the builtins from current globals */
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2714 globals = PyEval_GetGlobals();
16712
6341f8d909ac The code in PyImport_Import() tried to save itself a bit of work and
Guido van Rossum <guido@python.org>
parents: 16534
diff changeset
2715 if (globals != NULL) {
9431
87b9554c51d4 Jim Fulton writes:
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
2716 Py_INCREF(globals);
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2717 builtins = PyObject_GetItem(globals, builtins_str);
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2718 if (builtins == NULL)
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2719 goto err;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2720 }
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2721 else {
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2722 /* No globals -- use standard builtins, and fake globals */
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2723 PyErr_Clear();
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2724
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43897
diff changeset
2725 builtins = PyImport_ImportModuleLevel("builtins",
36712
5c36c222e7ca SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents: 36695
diff changeset
2726 NULL, NULL, NULL, 0);
16712
6341f8d909ac The code in PyImport_Import() tried to save itself a bit of work and
Guido van Rossum <guido@python.org>
parents: 16534
diff changeset
2727 if (builtins == NULL)
6341f8d909ac The code in PyImport_Import() tried to save itself a bit of work and
Guido van Rossum <guido@python.org>
parents: 16534
diff changeset
2728 return NULL;
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2729 globals = Py_BuildValue("{OO}", builtins_str, builtins);
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2730 if (globals == NULL)
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2731 goto err;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2732 }
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2733
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2734 /* Get the __import__ function from the builtins */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18808
diff changeset
2735 if (PyDict_Check(builtins)) {
16986
c5a417e8d84c Add some spaces around the "=" in assignments.
Fred Drake <fdrake@acm.org>
parents: 16947
diff changeset
2736 import = PyObject_GetItem(builtins, import_str);
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18808
diff changeset
2737 if (import == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18808
diff changeset
2738 PyErr_SetObject(PyExc_KeyError, import_str);
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 18808
diff changeset
2739 }
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2740 else
16986
c5a417e8d84c Add some spaces around the "=" in assignments.
Fred Drake <fdrake@acm.org>
parents: 16947
diff changeset
2741 import = PyObject_GetAttr(builtins, import_str);
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2742 if (import == NULL)
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2743 goto err;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2744
44441
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2745 /* Call the __import__ function with the proper argument list
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2746 * Always use absolute import here. */
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2747 r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
b91ca87e6f32 Merged revisions 59666-59679 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44349
diff changeset
2748 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
2749
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2750 err:
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2751 Py_XDECREF(globals);
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2752 Py_XDECREF(builtins);
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2753 Py_XDECREF(import);
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
2754
6028
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2755 return r;
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2756 }
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2757
292193170da1 Added Jim Fulton's PyImport_Import(), which calls whatever
Guido van Rossum <guido@python.org>
parents: 5980
diff changeset
2758
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2759 /* 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
2760 importing modules.
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2761 */
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2762
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2763 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
2764 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
2765 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2766 char buf[4];
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2767
12543
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
2768 buf[0] = (char) ((pyc_magic >> 0) & 0xff);
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
2769 buf[1] = (char) ((pyc_magic >> 8) & 0xff);
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
2770 buf[2] = (char) ((pyc_magic >> 16) & 0xff);
c8dbd5f70288 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12536
diff changeset
2771 buf[3] = (char) ((pyc_magic >> 24) & 0xff);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2772
47301
23ab586c427a Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents: 46644
diff changeset
2773 return PyBytes_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
2774 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2775
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2776 static PyObject *
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
2777 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
2778 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2779 PyObject *list;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2780 struct filedescr *fdp;
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2781
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2782 list = PyList_New(0);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2783 if (list == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2784 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2785 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2786 PyObject *item = Py_BuildValue("ssi",
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2787 fdp->suffix, fdp->mode, fdp->type);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2788 if (item == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2789 Py_DECREF(list);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2790 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2791 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2792 if (PyList_Append(list, item) < 0) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2793 Py_DECREF(list);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2794 Py_DECREF(item);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2795 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2796 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2797 Py_DECREF(item);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2798 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2799 return list;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2800 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2801
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2802 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2803 call_find_module(char *name, PyObject *path)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2804 {
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13266
diff changeset
2805 extern int fclose(FILE *);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2806 PyObject *fob, *ret;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2807 struct filedescr *fdp;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2808 char pathname[MAXPATHLEN+1];
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2809 FILE *fp = NULL;
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2810 int fd = -1;
43608
f0bcc11ca965 Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
Brett Cannon <bcannon@gmail.com>
parents: 43605
diff changeset
2811 char *found_encoding = NULL;
43605
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
2812 char *encoding = NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2813
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2814 pathname[0] = '\0';
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
2815 if (path == Py_None)
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
2816 path = NULL;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
2817 fdp = find_module(NULL, name, path, pathname, MAXPATHLEN+1, &fp, NULL);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2818 if (fdp == NULL)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2819 return NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2820 if (fp != NULL) {
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2821 fd = fileno(fp);
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2822 if (fd != -1)
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2823 fd = dup(fd);
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2824 fclose(fp);
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2825 fp = NULL;
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2826 }
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2827 if (fd != -1) {
43605
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
2828 if (strchr(fdp->mode, 'b') == NULL) {
43608
f0bcc11ca965 Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
Brett Cannon <bcannon@gmail.com>
parents: 43605
diff changeset
2829 /* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
f0bcc11ca965 Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
Brett Cannon <bcannon@gmail.com>
parents: 43605
diff changeset
2830 memory. */
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2831 found_encoding = PyTokenizer_FindEncoding(fd);
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2832 lseek(fd, 0, 0); /* Reset position */
49020
3fa066d2a397 #3773: Check for errors around the use of PyTokenizer_FindEncoding().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49003
diff changeset
2833 if (found_encoding == NULL && PyErr_Occurred())
3fa066d2a397 #3773: Check for errors around the use of PyTokenizer_FindEncoding().
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49003
diff changeset
2834 return NULL;
43608
f0bcc11ca965 Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
Brett Cannon <bcannon@gmail.com>
parents: 43605
diff changeset
2835 encoding = (found_encoding != NULL) ? found_encoding :
43605
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
2836 (char*)PyUnicode_GetDefaultEncoding();
a2bb76d11d66 Patch 1267 by Christian Heimes.
Guido van Rossum <guido@python.org>
parents: 43569
diff changeset
2837 }
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2838 fob = PyFile_FromFd(fd, pathname, fdp->mode, -1,
44134
e08b12c3b913 Add an errors parameter to open() and TextIOWrapper() to specify error handling.
Guido van Rossum <guido@python.org>
parents: 44099
diff changeset
2839 (char*)encoding, NULL, NULL, 1);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2840 if (fob == NULL) {
43632
0067a6ab4854 Issue 1267, continued.
Guido van Rossum <guido@python.org>
parents: 43608
diff changeset
2841 close(fd);
43608
f0bcc11ca965 Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
Brett Cannon <bcannon@gmail.com>
parents: 43605
diff changeset
2842 PyMem_FREE(found_encoding);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2843 return NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2844 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2845 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2846 else {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2847 fob = Py_None;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2848 Py_INCREF(fob);
16828
3b05212c1616 Implement PEP 235: Import on Case-Insensitive Platforms.
Tim Peters <tim.peters@gmail.com>
parents: 16712
diff changeset
2849 }
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2850 ret = Py_BuildValue("Os(ssi)",
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2851 fob, pathname, fdp->suffix, fdp->mode, fdp->type);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2852 Py_DECREF(fob);
43608
f0bcc11ca965 Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
Brett Cannon <bcannon@gmail.com>
parents: 43605
diff changeset
2853 PyMem_FREE(found_encoding);
f0bcc11ca965 Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
Brett Cannon <bcannon@gmail.com>
parents: 43605
diff changeset
2854
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2855 return ret;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2856 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2857
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2858 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2859 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
2860 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2861 char *name;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2862 PyObject *path = NULL;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
2863 if (!PyArg_ParseTuple(args, "s|O: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
2864 return NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2865 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
2866 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2867
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2868 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2869 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
2870 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2871 char *name;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2872 int ret;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2873 PyObject *m;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
2874 if (!PyArg_ParseTuple(args, "s:init_builtin", &name))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2875 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2876 ret = init_builtin(name);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2877 if (ret < 0)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2878 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2879 if (ret == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2880 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2881 return Py_None;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2882 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2883 m = PyImport_AddModule(name);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2884 Py_XINCREF(m);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2885 return m;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2886 }
1844
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2887
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2888 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2889 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
2890 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2891 char *name;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2892 int ret;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2893 PyObject *m;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
2894 if (!PyArg_ParseTuple(args, "s:init_frozen", &name))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2895 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2896 ret = PyImport_ImportFrozenModule(name);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2897 if (ret < 0)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2898 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2899 if (ret == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2900 Py_INCREF(Py_None);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2901 return Py_None;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2902 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2903 m = PyImport_AddModule(name);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2904 Py_XINCREF(m);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2905 return m;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2906 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2907
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2908 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2909 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
2910 {
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
2911 char *name;
3139
df07f28711fd Removed unused variables
Jack Jansen <jack.jansen@cwi.nl>
parents: 2891
diff changeset
2912
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
2913 if (!PyArg_ParseTuple(args, "s:get_frozen_object", &name))
2891
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
2914 return NULL;
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
2915 return get_frozen_object(name);
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
2916 }
964d76f49a5e add imp.get_frozen_object()
Guido van Rossum <guido@python.org>
parents: 2868
diff changeset
2917
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2918 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2919 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
2920 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2921 char *name;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
2922 if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2923 return NULL;
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
2924 return PyLong_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
2925 }
d1e6c9a64a56 * Python/import.c: support *.o/*.so as alternative for
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
2926
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2927 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2928 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
2929 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2930 char *name;
8544
35a2151fb1c3 Ignore Windows case check for ALL CAPS 8.3 files
Guido van Rossum <guido@python.org>
parents: 8374
diff changeset
2931 struct _frozen *p;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
2932 if (!PyArg_ParseTuple(args, "s:is_frozen", &name))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2933 return NULL;
8544
35a2151fb1c3 Ignore Windows case check for ALL CAPS 8.3 files
Guido van Rossum <guido@python.org>
parents: 8374
diff changeset
2934 p = find_frozen(name);
22866
a561ff1b4ffa Return bools from functions named is_<whatever>().
Guido van Rossum <guido@python.org>
parents: 22705
diff changeset
2935 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
2936 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2937
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2938 static FILE *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2939 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
2940 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2941 FILE *fp;
42071
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2942 if (mode[0] == 'U')
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2943 mode = "r" PY_STDIOTEXTMODE;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2944 if (fob == NULL) {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2945 fp = fopen(pathname, mode);
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2946 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2947 else {
42071
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2948 int fd = PyObject_AsFileDescriptor(fob);
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2949 if (fd == -1)
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2950 return NULL;
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2951 /* XXX This will leak a FILE struct. Fix this!!!!
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2952 (But it doesn't leak a file descrioptor!) */
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2953 fp = fdopen(fd, mode);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2954 }
42071
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2955 if (fp == NULL)
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2956 PyErr_SetFromErrno(PyExc_IOError);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2957 return fp;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2958 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2959
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2960 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2961 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
2962 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2963 char *name;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2964 char *pathname;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2965 PyObject *fob = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2966 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
2967 FILE *fp;
42071
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2968 if (!PyArg_ParseTuple(args, "ss|O:load_compiled",
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2969 &name, &pathname, &fob))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2970 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2971 fp = get_file(pathname, fob, "rb");
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2972 if (fp == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2973 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2974 m = load_compiled_module(name, pathname, fp);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2975 if (fob == NULL)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2976 fclose(fp);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2977 return m;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2978 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2979
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
2980 #ifdef HAVE_DYNAMIC_LOADING
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
2981
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2982 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
2983 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
2984 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2985 char *name;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2986 char *pathname;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2987 PyObject *fob = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2988 PyObject *m;
2812
fb3daf9b2456 new MAGIC; some changes to default files for imp.load_... functions
Guido van Rossum <guido@python.org>
parents: 2749
diff changeset
2989 FILE *fp = NULL;
42071
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2990 if (!PyArg_ParseTuple(args, "ss|O:load_dynamic",
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
2991 &name, &pathname, &fob))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
2992 return NULL;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2993 if (fob) {
2812
fb3daf9b2456 new MAGIC; some changes to default files for imp.load_... functions
Guido van Rossum <guido@python.org>
parents: 2749
diff changeset
2994 fp = get_file(pathname, fob, "r");
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2995 if (fp == NULL)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2996 return NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
2997 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
2998 m = _PyImport_LoadDynamicModule(name, pathname, fp);
2812
fb3daf9b2456 new MAGIC; some changes to default files for imp.load_... functions
Guido van Rossum <guido@python.org>
parents: 2749
diff changeset
2999 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
3000 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3001
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
3002 #endif /* HAVE_DYNAMIC_LOADING */
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
3003
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3004 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3005 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
3006 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3007 char *name;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3008 char *pathname;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3009 PyObject *fob = NULL;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3010 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
3011 FILE *fp;
42071
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
3012 if (!PyArg_ParseTuple(args, "ss|O:load_source",
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
3013 &name, &pathname, &fob))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3014 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3015 fp = get_file(pathname, fob, "r");
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3016 if (fp == NULL)
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3017 return NULL;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3018 m = load_source_module(name, pathname, fp);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3019 if (fob == NULL)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3020 fclose(fp);
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3021 return m;
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3022 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3023
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3024 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3025 imp_load_module(PyObject *self, PyObject *args)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3026 {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3027 char *name;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3028 PyObject *fob;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3029 char *pathname;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3030 char *suffix; /* Unused */
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3031 char *mode;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3032 int type;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3033 FILE *fp;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3034
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
3035 if (!PyArg_ParseTuple(args, "sOs(ssi):load_module",
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3036 &name, &fob, &pathname,
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3037 &suffix, &mode, &type))
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3038 return NULL;
23586
fba57e40f547 imp_load_module(): correct and comment the sense of the test for '+'
Guido van Rossum <guido@python.org>
parents: 23585
diff changeset
3039 if (*mode) {
fba57e40f547 imp_load_module(): correct and comment the sense of the test for '+'
Guido van Rossum <guido@python.org>
parents: 23585
diff changeset
3040 /* Mode must start with 'r' or 'U' and must not contain '+'.
fba57e40f547 imp_load_module(): correct and comment the sense of the test for '+'
Guido van Rossum <guido@python.org>
parents: 23585
diff changeset
3041 Implicit in this test is the assumption that the mode
fba57e40f547 imp_load_module(): correct and comment the sense of the test for '+'
Guido van Rossum <guido@python.org>
parents: 23585
diff changeset
3042 may contain other modifiers like 'b' or 't'. */
fba57e40f547 imp_load_module(): correct and comment the sense of the test for '+'
Guido van Rossum <guido@python.org>
parents: 23585
diff changeset
3043
fba57e40f547 imp_load_module(): correct and comment the sense of the test for '+'
Guido van Rossum <guido@python.org>
parents: 23585
diff changeset
3044 if (!(*mode == 'r' || *mode == 'U') || strchr(mode, '+')) {
23585
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
3045 PyErr_Format(PyExc_ValueError,
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
3046 "invalid file open mode %.200s", mode);
0e8370e70888 Cover a few corners in the 'U' mode integration to make imp work.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23554
diff changeset
3047 return NULL;
23586
fba57e40f547 imp_load_module(): correct and comment the sense of the test for '+'
Guido van Rossum <guido@python.org>
parents: 23585
diff changeset
3048 }
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3049 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3050 if (fob == Py_None)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3051 fp = NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3052 else {
42071
825041fc8e2c Rip out the file object's implementation.
Guido van Rossum <guido@python.org>
parents: 42057
diff changeset
3053 fp = get_file(NULL, fob, mode);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3054 if (fp == NULL)
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3055 return NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3056 }
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
3057 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
3058 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3059
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3060 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3061 imp_load_package(PyObject *self, PyObject *args)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3062 {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3063 char *name;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3064 char *pathname;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
3065 if (!PyArg_ParseTuple(args, "ss:load_package", &name, &pathname))
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3066 return NULL;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3067 return load_package(name, pathname);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3068 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3069
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3070 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3071 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
3072 {
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3073 char *name;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 11618
diff changeset
3074 if (!PyArg_ParseTuple(args, "s:new_module", &name))
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3075 return NULL;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3076 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
3077 }
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3078
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3079 static PyObject *
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3080 imp_reload(PyObject *self, PyObject *v)
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3081 {
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3082 return PyImport_ReloadModule(v);
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3083 }
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3084
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3085 PyDoc_STRVAR(doc_reload,
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3086 "reload(module) -> module\n\
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3087 \n\
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3088 Reload the module. The module must have been successfully imported before.");
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3089
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3090 /* Doc strings */
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3091
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3092 PyDoc_STRVAR(doc_imp,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3093 "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
3094 __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
3095
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3096 PyDoc_STRVAR(doc_find_module,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3097 "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
3098 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
3099 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
3100 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
3101 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
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_load_module,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3104 "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
3105 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
3106 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
3107
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3108 PyDoc_STRVAR(doc_get_magic,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3109 "get_magic() -> string\n\
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3110 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
3111
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3112 PyDoc_STRVAR(doc_get_suffixes,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3113 "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
3114 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
3115 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
3116
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3117 PyDoc_STRVAR(doc_new_module,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3118 "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
3119 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
3120 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
3121
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23586
diff changeset
3122 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
3123 "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
3124 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
3125 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
3126
27852
4b75a0b76857 Provide access to the import lock, fixing SF bug #580952. This is
Guido van Rossum <guido@python.org>
parents: 27357
diff changeset
3127 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
3128 "acquire_lock() -> None\n\
27853
5620b0786271 Cleanup from patch #683257:
Neal Norwitz <nnorwitz@gmail.com>
parents: 27852
diff changeset
3129 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
3130 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
3131 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
3132 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
3133
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_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
3135 "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
3136 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
3137 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
3138
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3139 static PyMethodDef imp_methods[] = {
27917
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
3140 {"find_module", imp_find_module, METH_VARARGS, doc_find_module},
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
3141 {"get_magic", imp_get_magic, METH_NOARGS, doc_get_magic},
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
3142 {"get_suffixes", imp_get_suffixes, METH_NOARGS, doc_get_suffixes},
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
3143 {"load_module", imp_load_module, METH_VARARGS, doc_load_module},
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
3144 {"new_module", imp_new_module, METH_VARARGS, doc_new_module},
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
3145 {"lock_held", imp_lock_held, METH_NOARGS, doc_lock_held},
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
3146 {"acquire_lock", imp_acquire_lock, METH_NOARGS, doc_acquire_lock},
c346d095791d Remove PyArg_ParseTuple() for methods which take no args,
Neal Norwitz <nnorwitz@gmail.com>
parents: 27853
diff changeset
3147 {"release_lock", imp_release_lock, METH_NOARGS, doc_release_lock},
44567
cdc285bd0efb Issue #1762972: Readded the reload() function as imp.reload()
Christian Heimes <christian@cheimes.de>
parents: 44441
diff changeset
3148 {"reload", imp_reload, METH_O, doc_reload},
6305
6e1fa76e7940 Added docstrings. Not for the obsolete functions though.
Guido van Rossum <guido@python.org>
parents: 6304
diff changeset
3149 /* The rest are obsolete */
22705
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3150 {"get_frozen_object", imp_get_frozen_object, METH_VARARGS},
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3151 {"init_builtin", imp_init_builtin, METH_VARARGS},
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3152 {"init_frozen", imp_init_frozen, METH_VARARGS},
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3153 {"is_builtin", imp_is_builtin, METH_VARARGS},
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3154 {"is_frozen", imp_is_frozen, METH_VARARGS},
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3155 {"load_compiled", imp_load_compiled, METH_VARARGS},
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
3156 #ifdef HAVE_DYNAMIC_LOADING
22705
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3157 {"load_dynamic", imp_load_dynamic, METH_VARARGS},
11618
528559a0b2f2 Cleanup patches from Greg Stein:
Guido van Rossum <guido@python.org>
parents: 11602
diff changeset
3158 #endif
22705
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3159 {"load_package", imp_load_package, METH_VARARGS},
8703d2052339 Use symbolic METH_VARARGS instead of 1 for ml_flags
Neal Norwitz <nnorwitz@gmail.com>
parents: 22651
diff changeset
3160 {"load_source", imp_load_source, METH_VARARGS},
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3161 {NULL, NULL} /* sentinel */
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3162 };
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3163
8822
bd47ee068b83 Changes for BeOS, QNX and long long, by Chris Herborth.
Guido van Rossum <guido@python.org>
parents: 8613
diff changeset
3164 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3165 setint(PyObject *d, char *name, int value)
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3166 {
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3167 PyObject *v;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3168 int err;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3169
44099
f324631462a2 Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents: 44098
diff changeset
3170 v = PyLong_FromLong((long)value);
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3171 err = PyDict_SetItemString(d, name, v);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3172 Py_XDECREF(v);
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3173 return err;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3174 }
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3175
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3176 typedef struct {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3177 PyObject_HEAD
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3178 } NullImporter;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3179
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3180 static int
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3181 NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3182 {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3183 char *path;
43789
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3184 Py_ssize_t pathlen;
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3185
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3186 if (!_PyArg_NoKeywords("NullImporter()", kwds))
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3187 return -1;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3188
47599
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
3189 if (!PyArg_ParseTuple(args, "es:NullImporter",
e2eb70235392 Issue 1342: Python could not start if installed in a directory
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 47594
diff changeset
3190 Py_FileSystemDefaultEncoding, &path))
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3191 return -1;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3192
43789
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3193 pathlen = strlen(path);
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3194 if (pathlen == 0) {
48540
40b0e3318a8b #3368: free string allocated by "es" ParseTuple format.
Georg Brandl <georg@python.org>
parents: 48499
diff changeset
3195 PyMem_Free(path);
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3196 PyErr_SetString(PyExc_ImportError, "empty pathname");
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3197 return -1;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3198 } else {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3199 struct stat statbuf;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3200 int rv;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3201
43790
dba2491cf717 Cosmetic changes to Christian's patch r58903.
Guido van Rossum <guido@python.org>
parents: 43789
diff changeset
3202 rv = stat(path, &statbuf);
43789
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3203 #ifdef MS_WINDOWS
43790
dba2491cf717 Cosmetic changes to Christian's patch r58903.
Guido van Rossum <guido@python.org>
parents: 43789
diff changeset
3204 /* MS Windows stat() chokes on paths like C:\path\. Try to
dba2491cf717 Cosmetic changes to Christian's patch r58903.
Guido van Rossum <guido@python.org>
parents: 43789
diff changeset
3205 * recover *one* time by stripping off a trailing slash or
dba2491cf717 Cosmetic changes to Christian's patch r58903.
Guido van Rossum <guido@python.org>
parents: 43789
diff changeset
3206 * backslash. http://bugs.python.org/issue1293
43789
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3207 */
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3208 if (rv != 0 && pathlen <= MAXPATHLEN &&
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3209 (path[pathlen-1] == '/' || path[pathlen-1] == '\\')) {
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3210 char mangled[MAXPATHLEN+1];
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3211
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3212 strcpy(mangled, path);
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3213 mangled[pathlen-1] = '\0';
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3214 rv = stat(mangled, &statbuf);
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3215 }
1247d24b287b Patch for bug http://bugs.python.org/issue1293
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
3216 #endif
48540
40b0e3318a8b #3368: free string allocated by "es" ParseTuple format.
Georg Brandl <georg@python.org>
parents: 48499
diff changeset
3217 PyMem_Free(path);
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3218 if (rv == 0) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3219 /* it exists */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3220 if (S_ISDIR(statbuf.st_mode)) {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3221 /* it's a directory */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3222 PyErr_SetString(PyExc_ImportError,
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3223 "existing directory");
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3224 return -1;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3225 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3226 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3227 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3228 return 0;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3229 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3230
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3231 static PyObject *
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3232 NullImporter_find_module(NullImporter *self, PyObject *args)
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3233 {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3234 Py_RETURN_NONE;
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3235 }
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3236
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3237 static PyMethodDef NullImporter_methods[] = {
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3238 {"find_module", (PyCFunction)NullImporter_find_module, METH_VARARGS,
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3239 "Always return None"
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3240 },
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3241 {NULL} /* Sentinel */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3242 };
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3243
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3244
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
3245 PyTypeObject PyNullImporter_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42071
diff changeset
3246 PyVarObject_HEAD_INIT(NULL, 0)
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3247 "imp.NullImporter", /*tp_name*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3248 sizeof(NullImporter), /*tp_basicsize*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3249 0, /*tp_itemsize*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3250 0, /*tp_dealloc*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3251 0, /*tp_print*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3252 0, /*tp_getattr*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3253 0, /*tp_setattr*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3254 0, /*tp_compare*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3255 0, /*tp_repr*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3256 0, /*tp_as_number*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3257 0, /*tp_as_sequence*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3258 0, /*tp_as_mapping*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3259 0, /*tp_hash */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3260 0, /*tp_call*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3261 0, /*tp_str*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3262 0, /*tp_getattro*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3263 0, /*tp_setattro*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3264 0, /*tp_as_buffer*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3265 Py_TPFLAGS_DEFAULT, /*tp_flags*/
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3266 "Null importer object", /* tp_doc */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3267 0, /* tp_traverse */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3268 0, /* tp_clear */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3269 0, /* tp_richcompare */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3270 0, /* tp_weaklistoffset */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3271 0, /* tp_iter */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3272 0, /* tp_iternext */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3273 NullImporter_methods, /* tp_methods */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3274 0, /* tp_members */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3275 0, /* tp_getset */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3276 0, /* tp_base */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3277 0, /* tp_dict */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3278 0, /* tp_descr_get */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3279 0, /* tp_descr_set */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3280 0, /* tp_dictoffset */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3281 (initproc)NullImporter_init, /* tp_init */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3282 0, /* tp_alloc */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3283 PyType_GenericNew /* tp_new */
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3284 };
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3285
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3286 static struct PyModuleDef impmodule = {
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3287 PyModuleDef_HEAD_INIT,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3288 "imp",
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3289 doc_imp,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3290 0,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3291 imp_methods,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3292 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3293 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3294 NULL,
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3295 NULL
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3296 };
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
3297
30311
f003e48f562d Bug #794140: cygwin builds do not embed
Jason Tishler <jason@tishler.net>
parents: 29812
diff changeset
3298 PyMODINIT_FUNC
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3299 PyInit_imp(void)
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3300 {
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3301 PyObject *m, *d;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3302
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
3303 if (PyType_Ready(&PyNullImporter_Type) < 0)
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3304 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3305
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3306 m = PyModule_Create(&impmodule);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
3307 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36065
diff changeset
3308 goto failure;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5317
diff changeset
3309 d = PyModule_GetDict(m);
39610
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
3310 if (d == NULL)
3674f20ac781 Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents: 39442
diff changeset
3311 goto failure;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3312
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3313 if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3314 if (setint(d, "PY_SOURCE", PY_SOURCE) < 0) goto failure;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3315 if (setint(d, "PY_COMPILED", PY_COMPILED) < 0) goto failure;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3316 if (setint(d, "C_EXTENSION", C_EXTENSION) < 0) goto failure;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3317 if (setint(d, "PY_RESOURCE", PY_RESOURCE) < 0) goto failure;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3318 if (setint(d, "PKG_DIRECTORY", PKG_DIRECTORY) < 0) goto failure;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3319 if (setint(d, "C_BUILTIN", C_BUILTIN) < 0) goto failure;
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3320 if (setint(d, "PY_FROZEN", PY_FROZEN) < 0) goto failure;
8845
b933f584a26d Added handling for Mac code resource modules (Jack Jansen).
Guido van Rossum <guido@python.org>
parents: 8822
diff changeset
3321 if (setint(d, "PY_CODERESOURCE", PY_CODERESOURCE) < 0) goto failure;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 26796
diff changeset
3322 if (setint(d, "IMP_HOOK", IMP_HOOK) < 0) goto failure;
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3323
43897
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
3324 Py_INCREF(&PyNullImporter_Type);
41f7c13ef161 Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43790
diff changeset
3325 PyModule_AddObject(m, "NullImporter", (PyObject *)&PyNullImporter_Type);
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3326 return m;
6243
4279f4eb210a First part of package support.
Guido van Rossum <guido@python.org>
parents: 6055
diff changeset
3327 failure:
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3328 Py_XDECREF(m);
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3329 return NULL;
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3330
2052
d7e91437f0a2 Lots of changes, most minor (fatal() instead of abort(), use of
Guido van Rossum <guido@python.org>
parents: 2040
diff changeset
3331 }
8330
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3332
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3333
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12543
diff changeset
3334 /* 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
3335 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
3336 *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
3337 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
3338
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3339 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
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 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13552
diff changeset
3342 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
3343 {
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3344 static struct _inittab *our_copy = NULL;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3345 struct _inittab *p;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3346 int i, n;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3347
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3348 /* Count the number of entries in both tables */
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3349 for (n = 0; newtab[n].name != NULL; n++)
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3350 ;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3351 if (n == 0)
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3352 return 0; /* Nothing to do */
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3353 for (i = 0; PyImport_Inittab[i].name != NULL; i++)
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3354 ;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3355
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3356 /* Allocate new memory for the combined table */
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12543
diff changeset
3357 p = our_copy;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12543
diff changeset
3358 PyMem_RESIZE(p, struct _inittab, i+n+1);
8330
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3359 if (p == NULL)
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3360 return -1;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3361
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3362 /* Copy the tables into the new memory */
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3363 if (our_copy != PyImport_Inittab)
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3364 memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3365 PyImport_Inittab = our_copy = p;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3366 memcpy(p+i, newtab, (n+1) * sizeof(struct _inittab));
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3367
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3368 return 0;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3369 }
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3370
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3371 /* 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
3372
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3373 int
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47301
diff changeset
3374 PyImport_AppendInittab(char *name, PyObject* (*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
3375 {
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3376 struct _inittab newtab[2];
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 memset(newtab, '\0', sizeof newtab);
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3379
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3380 newtab[0].name = name;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3381 newtab[0].initfunc = initfunc;
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3382
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3383 return PyImport_ExtendInittab(newtab);
7a1e7da922f8 New APIs for embedding applications that want to add their own entries
Guido van Rossum <guido@python.org>
parents: 8012
diff changeset
3384 }
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
3385
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
3386 #ifdef __cplusplus
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
3387 }
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36988
diff changeset
3388 #endif