Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.c @ 41558:29ae667f3239
Checkpoint. Manipulated things so that string literals are always
unicode, and a few other compensating changes, e.g. str <- unicode,
chr <- unichr, and repr() of a unicode string no longer starts
with 'u'. Lots of unit tests are broken, but some basic things
work, in particular distutils works so the extensions can be built,
and test_builtin.py works.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 27 Apr 2007 23:53:51 +0000 |
| parents | f731859e9b3b |
| children | 2c54763c4504 |
| 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; | |
|
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 | 41 } |
| 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 | 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 | 49 return NULL; |
| 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 | 55 } |
| 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; |
|
41558
29ae667f3239
Checkpoint. Manipulated things so that string literals are always
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
62 char *s; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
63 if (!PyModule_Check(m)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
64 PyErr_BadArgument(); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
65 return NULL; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
66 } |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
67 d = ((PyModuleObject *)m)->md_dict; |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
68 if (d == NULL || |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
69 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL || |
|
41558
29ae667f3239
Checkpoint. Manipulated things so that string literals are always
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
70 !(PyString_Check(nameobj) || PyUnicode_Check(nameobj))) |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
71 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
72 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
|
73 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
|
74 } |
|
41558
29ae667f3239
Checkpoint. Manipulated things so that string literals are always
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
75 if (PyUnicode_Check(nameobj)) |
|
29ae667f3239
Checkpoint. Manipulated things so that string literals are always
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
76 nameobj = _PyUnicode_AsDefaultEncodedString(nameobj, "replace"); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
77 return PyString_AsString(nameobj); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
78 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
79 |
|
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
|
80 char * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
81 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
|
82 { |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 } |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
89 d = ((PyModuleObject *)m)->md_dict; |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
90 if (d == NULL || |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
91 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL || |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
92 !PyString_Check(fileobj)) |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
93 { |
|
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
|
94 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
|
95 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
|
96 } |
|
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 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
|
98 } |
|
5c1ebf23f1d7
Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents:
9329
diff
changeset
|
99 |
|
7401
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
100 void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
101 _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
|
102 { |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
103 /* 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
110 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
|
111 PyObject *key, *value; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
112 PyObject *d; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
113 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
114 d = ((PyModuleObject *)m)->md_dict; |
|
22300
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
115 if (d == NULL) |
|
54bcc5ec3a51
backport gvanrossum's checkin of
Michael W. Hudson <mwh@python.net>
parents:
20595
diff
changeset
|
116 return; |
|
7401
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
117 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
118 /* 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
|
119 pos = 0; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
120 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
|
121 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
|
122 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
|
123 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
|
124 if (Py_VerboseFlag > 1) |
|
9329
7f242f29a45f
Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents:
7401
diff
changeset
|
125 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
|
126 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
|
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 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
131 /* 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
|
132 pos = 0; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
133 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
|
134 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
|
135 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
|
136 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
|
137 if (Py_VerboseFlag > 1) |
|
9329
7f242f29a45f
Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents:
7401
diff
changeset
|
138 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
|
139 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
|
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 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
144 /* 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
|
145 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
|
146 builtins, in particularly 'None'. */ |
|
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 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
149 |
| 6 | 150 /* Methods */ |
| 151 | |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
152 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
|
153 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
|
154 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
155 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
|
156 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
|
157 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
|
158 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
|
159 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
|
160 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
|
161 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
|
162 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
|
163 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
|
164 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
|
165 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
|
166 } |
|
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 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
|
168 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
|
169 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
|
170 return -1; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
171 return 0; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
172 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
173 |
| 6 | 174 static void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
175 module_dealloc(PyModuleObject *m) |
| 6 | 176 { |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
177 PyObject_GC_UnTrack(m); |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
178 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
|
179 _PyModule_Clear((PyObject *)m); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
180 Py_DECREF(m->md_dict); |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
181 } |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20012
diff
changeset
|
182 m->ob_type->tp_free((PyObject *)m); |
| 6 | 183 } |
| 184 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
185 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
186 module_repr(PyModuleObject *m) |
| 6 | 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 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
|
189 char *filename; |
|
19322
1fec03012346
module_repr(): Instead of fixing the maximum buf size to 400,
Barry Warsaw <barry@python.org>
parents:
18933
diff
changeset
|
190 |
|
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
|
191 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
|
192 if (name == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
193 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
|
194 name = "?"; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
195 } |
|
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
|
196 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
|
197 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
|
198 PyErr_Clear(); |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19322
diff
changeset
|
199 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
|
200 } |
|
19476
0e70df98424e
repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents:
19322
diff
changeset
|
201 return PyString_FromFormat("<module '%s' from '%s'>", name, filename); |
| 6 | 202 } |
| 203 | |
|
15686
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
204 /* 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
|
205 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
|
206 the cycle. */ |
|
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
207 static int |
|
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
208 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
|
209 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
210 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
|
211 return 0; |
|
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
212 } |
|
986d1c3ccdde
Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
213 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23654
diff
changeset
|
214 PyDoc_STRVAR(module_doc, |
|
23654
b86eaeb0d7dd
Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents:
23652
diff
changeset
|
215 "module(name[, doc])\n\ |
|
b86eaeb0d7dd
Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents:
23652
diff
changeset
|
216 \n\ |
|
b86eaeb0d7dd
Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents:
23652
diff
changeset
|
217 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
|
218 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
|
219 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
220 PyTypeObject PyModule_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
221 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
|
222 0, /* ob_size */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
223 "module", /* tp_name */ |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
224 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
|
225 0, /* tp_itemsize */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
226 (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
|
227 0, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
228 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
229 0, /* tp_setattr */ |
|
17807
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
230 0, /* tp_compare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
231 (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
|
232 0, /* tp_as_number */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
233 0, /* tp_as_sequence */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
234 0, /* tp_as_mapping */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
235 0, /* tp_hash */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
236 0, /* tp_call */ |
|
1579d309676e
Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents:
15686
diff
changeset
|
237 0, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
238 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
|
239 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
|
240 0, /* tp_as_buffer */ |
|
19581
fd48a55ac364
Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
19476
diff
changeset
|
241 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
|
242 Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
23654
b86eaeb0d7dd
Add a docstring to the module type.
Guido van Rossum <guido@python.org>
parents:
23652
diff
changeset
|
243 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
|
244 (traverseproc)module_traverse, /* tp_traverse */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
245 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
246 0, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
247 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
248 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
249 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
250 0, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
251 module_members, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
252 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
253 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
254 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
255 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
256 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
257 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
258 (initproc)module_init, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
259 PyType_GenericAlloc, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17811
diff
changeset
|
260 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
|
261 PyObject_GC_Del, /* tp_free */ |
| 6 | 262 }; |
