annotate Objects/moduleobject.c @ 39496:352ffe77b71d legacy-trunk

Patch #1535500: fix segfault in BZ2File.writelines and make sure it raises the correct exceptions.
author Georg Brandl <georg@python.org>
date Mon, 14 Aug 2006 21:42:55 +0000
parents 7b2e436a4a21
children 6620a510e9a9 29ae667f3239
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: 95
diff changeset
1
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Module object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
4 #include "Python.h"
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
5 #include "structmember.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
8 PyObject_HEAD
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
9 PyObject *md_dict;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
10 } PyModuleObject;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
20595
c4d9d8674506 Add missing "static" declarations (found by "make smelly").
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 20277
diff changeset
12 static PyMemberDef module_members[] = {
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
13 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
14 {0}
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
15 };
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
16
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
17 PyObject *
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33490
diff changeset
18 PyModule_New(const char *name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
20 PyModuleObject *m;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
21 PyObject *nameobj;
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
22 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 if (m == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
25 nameobj = PyString_FromString(name);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
26 m->md_dict = PyDict_New();
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
27 if (m->md_dict == NULL || nameobj == NULL)
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
28 goto fail;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
29 if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
30 goto fail;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
31 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
2081
afecdec996ff initialize __doc__ to None
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
32 goto fail;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
33 Py_DECREF(nameobj);
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
34 PyObject_GC_Track(m);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
35 return (PyObject *)m;
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
36
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
37 fail:
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
38 Py_XDECREF(nameobj);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
39 Py_DECREF(m);
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
40 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
43 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
44 PyModule_GetDict(PyObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 {
22300
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
46 PyObject *d;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
47 if (!PyModule_Check(m)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
48 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 }
22300
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
51 d = ((PyModuleObject *)m) -> md_dict;
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
52 if (d == NULL)
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
53 ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
54 return d;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
57 char *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
58 PyModule_GetName(PyObject *m)
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
59 {
22300
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
60 PyObject *d;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
61 PyObject *nameobj;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
62 if (!PyModule_Check(m)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
63 PyErr_BadArgument();
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
64 return NULL;
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
65 }
22300
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
66 d = ((PyModuleObject *)m)->md_dict;
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
67 if (d == NULL ||
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
68 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
69 !PyString_Check(nameobj))
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
70 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
71 PyErr_SetString(PyExc_SystemError, "nameless module");
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
72 return NULL;
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
73 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
74 return PyString_AsString(nameobj);
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
75 }
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
76
10161
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
77 char *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
78 PyModule_GetFilename(PyObject *m)
10161
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
79 {
22300
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
80 PyObject *d;
10161
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
81 PyObject *fileobj;
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
82 if (!PyModule_Check(m)) {
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
83 PyErr_BadArgument();
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
84 return NULL;
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
85 }
22300
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
86 d = ((PyModuleObject *)m)->md_dict;
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
87 if (d == NULL ||
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
88 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
89 !PyString_Check(fileobj))
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
90 {
10161
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
91 PyErr_SetString(PyExc_SystemError, "module filename missing");
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
92 return NULL;
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
93 }
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
94 return PyString_AsString(fileobj);
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
95 }
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
96
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
97 void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
98 _PyModule_Clear(PyObject *m)
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
99 {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
100 /* To make the execution order of destructors for global
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
101 objects a bit more predictable, we first zap all objects
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
102 whose name starts with a single underscore, before we clear
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
103 the entire dictionary. We zap them by replacing them with
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
104 None, rather than deleting them from the dictionary, to
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
105 avoid rehashing the dictionary (to some extent). */
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
106
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35999
diff changeset
107 Py_ssize_t pos;
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
108 PyObject *key, *value;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
109 PyObject *d;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
110
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
111 d = ((PyModuleObject *)m)->md_dict;
22300
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
112 if (d == NULL)
54bcc5ec3a51 backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents: 20595
diff changeset
113 return;
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
114
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
115 /* First, clear only names starting with a single underscore */
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
116 pos = 0;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
117 while (PyDict_Next(d, &pos, &key, &value)) {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
118 if (value != Py_None && PyString_Check(key)) {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
119 char *s = PyString_AsString(key);
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
120 if (s[0] == '_' && s[1] != '_') {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
121 if (Py_VerboseFlag > 1)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 7401
diff changeset
122 PySys_WriteStderr("# clear[1] %s\n", s);
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
123 PyDict_SetItem(d, key, Py_None);
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
124 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
125 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
126 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
127
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
128 /* Next, clear all names except for __builtins__ */
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
129 pos = 0;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
130 while (PyDict_Next(d, &pos, &key, &value)) {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
131 if (value != Py_None && PyString_Check(key)) {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
132 char *s = PyString_AsString(key);
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
133 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
134 if (Py_VerboseFlag > 1)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 7401
diff changeset
135 PySys_WriteStderr("# clear[2] %s\n", s);
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
136 PyDict_SetItem(d, key, Py_None);
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
137 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
138 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
139 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
140
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
141 /* Note: we leave __builtins__ in place, so that destructors
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
142 of non-global objects defined in this module can still use
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
143 builtins, in particularly 'None'. */
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
144
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
145 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
146
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
149 static int
23652
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
150 module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
151 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36501
diff changeset
152 static char *kwlist[] = {"name", "doc", NULL};
23652
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
153 PyObject *dict, *name = Py_None, *doc = Py_None;
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33490
diff changeset
154 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|O:module.__init__",
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33490
diff changeset
155 kwlist, &name, &doc))
23652
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
156 return -1;
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
157 dict = m->md_dict;
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
158 if (dict == NULL) {
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
159 dict = PyDict_New();
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
160 if (dict == NULL)
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
161 return -1;
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
162 m->md_dict = dict;
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
163 }
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
164 if (PyDict_SetItemString(dict, "__name__", name) < 0)
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
165 return -1;
b40165a4efe9 Surprising fix for SF bug 563060: module can be used as base class.
Guido van Rossum <guido@python.org>
parents: 22975
diff changeset
166 if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
167 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
168 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
169 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
170
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 static void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
172 module_dealloc(PyModuleObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
174 PyObject_GC_UnTrack(m);
2238
b5dcba7b732d apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents: 2081
diff changeset
175 if (m->md_dict != NULL) {
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
176 _PyModule_Clear((PyObject *)m);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
177 Py_DECREF(m->md_dict);
2238
b5dcba7b732d apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents: 2081
diff changeset
178 }
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
179 m->ob_type->tp_free((PyObject *)m);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
182 static PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
183 module_repr(PyModuleObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 {
10161
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
185 char *name;
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
186 char *filename;
19322
1fec03012346 module_repr(): Instead of fixing the maximum buf size to 400,
Barry Warsaw <barry@python.org>
parents: 18933
diff changeset
187
10161
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
188 name = PyModule_GetName((PyObject *)m);
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
189 if (name == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
190 PyErr_Clear();
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
191 name = "?";
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
192 }
10161
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
193 filename = PyModule_GetFilename((PyObject *)m);
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
194 if (filename == NULL) {
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
195 PyErr_Clear();
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19322
diff changeset
196 return PyString_FromFormat("<module '%s' (built-in)>", name);
10161
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
197 }
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19322
diff changeset
198 return PyString_FromFormat("<module '%s' from '%s'>", name, filename);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
201 /* We only need a traverse function, no clear function: If the module
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
202 is in a cycle, md_dict will be cleared as well, which will break
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
203 the cycle. */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
204 static int
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
205 module_traverse(PyModuleObject *m, visitproc visit, void *arg)
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
206 {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
207 Py_VISIT(m->md_dict);
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
208 return 0;
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
209 }
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
210
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23654
diff changeset
211 PyDoc_STRVAR(module_doc,
23654
b86eaeb0d7dd Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents: 23652
diff changeset
212 "module(name[, doc])\n\
b86eaeb0d7dd Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents: 23652
diff changeset
213 \n\
b86eaeb0d7dd Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents: 23652
diff changeset
214 Create a module object.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 23654
diff changeset
215 The name must be a string; the optional doc argument can have any type.");
23654
b86eaeb0d7dd Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents: 23652
diff changeset
216
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
217 PyTypeObject PyModule_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
218 PyObject_HEAD_INIT(&PyType_Type)
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
219 0, /* ob_size */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
220 "module", /* tp_name */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
221 sizeof(PyModuleObject), /* tp_size */
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
222 0, /* tp_itemsize */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
223 (destructor)module_dealloc, /* tp_dealloc */
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
224 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
225 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
226 0, /* tp_setattr */
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
227 0, /* tp_compare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
228 (reprfunc)module_repr, /* tp_repr */
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
229 0, /* tp_as_number */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
230 0, /* tp_as_sequence */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
231 0, /* tp_as_mapping */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
232 0, /* tp_hash */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
233 0, /* tp_call */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
234 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
235 PyObject_GenericGetAttr, /* tp_getattro */
29910
0e2215b8226d Remove code that tried to warn about shadowing builtin names after a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 29397
diff changeset
236 PyObject_GenericSetAttr, /* tp_setattro */
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
237 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
238 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
239 Py_TPFLAGS_BASETYPE, /* tp_flags */
23654
b86eaeb0d7dd Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents: 23652
diff changeset
240 module_doc, /* tp_doc */
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
241 (traverseproc)module_traverse, /* tp_traverse */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
242 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
243 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
244 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
245 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
246 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
247 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
248 module_members, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
249 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
250 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
251 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
252 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
253 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
254 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
255 (initproc)module_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
256 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
257 PyType_GenericNew, /* tp_new */
22975
29e6cb832497 PyObject_GC_Del can now be used as a function designator.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 22300
diff changeset
258 PyObject_GC_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 };