Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.c @ 49955:878d40468c4e legacy-trunk
Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
| author | Vinay Sajip <vinay_sajip@yahoo.co.uk> |
|---|---|
| date | Wed, 03 Dec 2008 23:22:58 +0000 |
| parents | 52294af4b663 |
| children | e38335d4df91 20d09549b82c |
| rev | line source |
|---|---|
| 145 | 1 |
| 6 | 2 /* Module object implementation */ |
| 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 | 6 |
| 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 | 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 | 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 | 23 if (m == NULL) |
| 24 return NULL; | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
25 nameobj = PyString_FromString(name); |
|
5396
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; |
| 44116 | 33 if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0) |
| 34 goto fail; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
35 Py_DECREF(nameobj); |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
36 PyObject_GC_Track(m); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
37 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
|
38 |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
39 fail: |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
40 Py_XDECREF(nameobj); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
41 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
|
42 return NULL; |
| 6 | 43 } |
| 44 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
45 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
46 PyModule_GetDict(PyObject *m) |
| 6 | 47 { |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
48 PyObject *d; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
49 if (!PyModule_Check(m)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
50 PyErr_BadInternalCall(); |
| 6 | 51 return NULL; |
| 52 } | |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
53 d = ((PyModuleObject *)m) -> md_dict; |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
54 if (d == NULL) |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
55 ((PyModuleObject *)m) -> md_dict = d = PyDict_New(); |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
56 return d; |
| 6 | 57 } |
| 58 | |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
59 char * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
60 PyModule_GetName(PyObject *m) |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
61 { |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
62 PyObject *d; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
63 PyObject *nameobj; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
64 if (!PyModule_Check(m)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
65 PyErr_BadArgument(); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
66 return NULL; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
67 } |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
68 d = ((PyModuleObject *)m)->md_dict; |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
69 if (d == NULL || |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
70 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL || |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
71 !PyString_Check(nameobj)) |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
72 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
73 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
|
74 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
|
75 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
76 return PyString_AsString(nameobj); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
77 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
78 |
|
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 char * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
80 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
|
81 { |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 } |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
88 d = ((PyModuleObject *)m)->md_dict; |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
89 if (d == NULL || |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
90 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL || |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
91 !PyString_Check(fileobj)) |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
92 { |
|
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
|
93 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
|
94 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
|
95 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
96 return PyString_AsString(fileobj); |
|
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
|
97 } |
|
5c1ebf23f1d7
Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents:
9329
diff
changeset
|
98 |
|
7401
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
99 void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
100 _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
|
101 { |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
102 /* 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
109 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
|
110 PyObject *key, *value; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
111 PyObject *d; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
112 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
113 d = ((PyModuleObject *)m)->md_dict; |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
114 if (d == NULL) |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
115 return; |
|
7401
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
116 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
117 /* 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
|
118 pos = 0; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
119 while (PyDict_Next(d, &pos, &key, &value)) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
120 if (value != Py_None && PyString_Check(key)) { |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
121 char *s = PyString_AsString(key); |
|
7401
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
122 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
|
123 if (Py_VerboseFlag > 1) |
|
9329
7f242f29a45f
Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents:
7401
diff
changeset
|
124 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
|
125 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
|
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 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
129 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
130 /* 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
|
131 pos = 0; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
132 while (PyDict_Next(d, &pos, &key, &value)) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
133 if (value != Py_None && PyString_Check(key)) { |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
134 char *s = PyString_AsString(key); |
|
7401
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
135 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
|
136 if (Py_VerboseFlag > 1) |
|
9329
7f242f29a45f
Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents:
7401
diff
changeset
|
137 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
|
138 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
|
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 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
142 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
143 /* 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
|
144 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
|
145 builtins, in particularly 'None'. */ |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
146 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
147 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
148 |
| 6 | 149 /* Methods */ |
| 150 | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
151 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
|
152 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
|
153 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
154 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
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
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 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
|
162 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
|
163 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
|
164 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
|
165 } |
|
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, "__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
|
167 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
|
168 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
|
169 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
170 return 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
171 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
172 |
| 6 | 173 static void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
174 module_dealloc(PyModuleObject *m) |
| 6 | 175 { |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
176 PyObject_GC_UnTrack(m); |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
177 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
|
178 _PyModule_Clear((PyObject *)m); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
179 Py_DECREF(m->md_dict); |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
180 } |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44116
diff
changeset
|
181 Py_TYPE(m)->tp_free((PyObject *)m); |
| 6 | 182 } |
| 183 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
184 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
185 module_repr(PyModuleObject *m) |
| 6 | 186 { |
|
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
|
187 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
|
188 char *filename; |
|
19322
1fec03012346
module_repr(): Instead of fixing the maximum buf size to 400,
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
189 |
|
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
|
190 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
|
191 if (name == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
192 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
|
193 name = "?"; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
194 } |
|
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
|
195 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
|
196 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
|
197 PyErr_Clear(); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
198 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
|
199 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
200 return PyString_FromFormat("<module '%s' from '%s'>", name, filename); |
| 6 | 201 } |
| 202 | |
|
15686
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
203 /* 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
|
204 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
|
205 the cycle. */ |
|
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
206 static int |
|
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
207 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
|
208 { |
|
37756
7b2e436a4a21
Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
209 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
|
210 return 0; |
|
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
211 } |
|
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
212 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23654
diff
changeset
|
213 PyDoc_STRVAR(module_doc, |
|
23654
b86eaeb0d7dd
Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents:
23652
diff
changeset
|
214 "module(name[, doc])\n\ |
|
b86eaeb0d7dd
Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents:
23652
diff
changeset
|
215 \n\ |
|
b86eaeb0d7dd
Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents:
23652
diff
changeset
|
216 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
|
217 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
|
218 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
219 PyTypeObject PyModule_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
37756
diff
changeset
|
220 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
17807
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
221 "module", /* tp_name */ |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
222 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
|
223 0, /* tp_itemsize */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
224 (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
|
225 0, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
226 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
227 0, /* tp_setattr */ |
|
17807
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
228 0, /* tp_compare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
229 (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
|
230 0, /* tp_as_number */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
231 0, /* tp_as_sequence */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
232 0, /* tp_as_mapping */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
233 0, /* tp_hash */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
234 0, /* tp_call */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
235 0, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
236 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
|
237 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
|
238 0, /* tp_as_buffer */ |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
239 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
|
240 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
23654
b86eaeb0d7dd
Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents:
23652
diff
changeset
|
241 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
|
242 (traverseproc)module_traverse, /* tp_traverse */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
243 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
244 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
245 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
246 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
247 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
248 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
249 module_members, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
250 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
251 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
252 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
253 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
254 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
255 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
256 (initproc)module_init, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
257 PyType_GenericAlloc, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
258 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
|
259 PyObject_GC_Del, /* tp_free */ |
| 6 | 260 }; |
