annotate Objects/moduleobject.c @ 20737:00a581bdedeb legacy-trunk

When overriding __str__ or __repr__, set the tp_print slot to NULL.
author Guido van Rossum <guido@python.org>
date Mon, 29 Oct 2001 14:33:44 +0000
parents c4d9d8674506
children 54bcc5ec3a51
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 *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
18 PyModule_New(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 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
46 if (!PyModule_Check(m)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
47 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
50 return ((PyModuleObject *)m) -> md_dict;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
53 char *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
54 PyModule_GetName(PyObject *m)
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
55 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
56 PyObject *nameobj;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
57 if (!PyModule_Check(m)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
58 PyErr_BadArgument();
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
59 return NULL;
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
60 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
61 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
62 "__name__");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
63 if (nameobj == NULL || !PyString_Check(nameobj)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
64 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
65 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
66 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
67 return PyString_AsString(nameobj);
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
68 }
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
69
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
70 char *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
71 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
72 {
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
73 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
74 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
75 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
76 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
77 }
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
78 fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
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 "__file__");
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 if (fileobj == NULL || !PyString_Check(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
81 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
82 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
83 }
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 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
85 }
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
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
87 void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
88 _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
89 {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
90 /* 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
91 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
92 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
93 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
94 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
95 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
96
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
97 int pos;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
98 PyObject *key, *value;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
99 PyObject *d;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
100
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
101 d = ((PyModuleObject *)m)->md_dict;
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 /* 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
104 pos = 0;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
105 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
106 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
107 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
108 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
109 if (Py_VerboseFlag > 1)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 7401
diff changeset
110 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
111 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
112 }
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 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
115
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
116 /* 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
117 pos = 0;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
118 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
119 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
120 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
121 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
122 if (Py_VerboseFlag > 1)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 7401
diff changeset
123 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
124 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
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
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
129 /* 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
130 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
131 builtins, in particularly 'None'. */
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
132
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
133 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
134
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
137 static int
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
138 module_init(PyModuleObject *m, PyObject *args, PyObject *kw)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
139 {
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
140 m->md_dict = PyDict_New();
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
141 if (m->md_dict == NULL)
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
142 return -1;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
143 return 0;
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
144 }
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
145
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 static void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
147 module_dealloc(PyModuleObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 {
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
149 PyObject_GC_UnTrack(m);
2238
b5dcba7b732d apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents: 2081
diff changeset
150 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
151 _PyModule_Clear((PyObject *)m);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
152 Py_DECREF(m->md_dict);
2238
b5dcba7b732d apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents: 2081
diff changeset
153 }
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
154 m->ob_type->tp_free((PyObject *)m);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
157 static PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
158 module_repr(PyModuleObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 {
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
160 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
161 char *filename;
19322
1fec03012346 module_repr(): Instead of fixing the maximum buf size to 400,
Barry Warsaw <barry@python.org>
parents: 18933
diff changeset
162
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
163 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
164 if (name == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
165 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
166 name = "?";
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
167 }
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
168 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
169 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
170 PyErr_Clear();
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19322
diff changeset
171 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
172 }
19476
0e70df98424e repr's converted to using PyString_FromFormat() instead of sprintf'ing
Barry Warsaw <barry@python.org>
parents: 19322
diff changeset
173 return PyString_FromFormat("<module '%s' from '%s'>", name, filename);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
176 /* 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
177 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
178 the cycle. */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
179 static int
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
180 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
181 {
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
182 if (m->md_dict != NULL)
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
183 return visit(m->md_dict, arg);
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
184 return 0;
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
185 }
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
186
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
187 PyTypeObject PyModule_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
188 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
189 0, /* ob_size */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
190 "module", /* tp_name */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
191 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
192 0, /* tp_itemsize */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
193 (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
194 0, /* tp_print */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
195 0, /* tp_getattr */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
196 0, /* tp_setattr */
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
197 0, /* tp_compare */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
198 (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
199 0, /* tp_as_number */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
200 0, /* tp_as_sequence */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
201 0, /* tp_as_mapping */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
202 0, /* tp_hash */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
203 0, /* tp_call */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
204 0, /* tp_str */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
205 PyObject_GenericGetAttr, /* tp_getattro */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
206 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
207 0, /* tp_as_buffer */
19581
fd48a55ac364 Use new GC API.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19476
diff changeset
208 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
209 Py_TPFLAGS_BASETYPE, /* tp_flags */
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
210 0, /* tp_doc */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
211 (traverseproc)module_traverse, /* tp_traverse */
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
212 0, /* tp_clear */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
213 0, /* tp_richcompare */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
214 0, /* tp_weaklistoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
215 0, /* tp_iter */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
216 0, /* tp_iternext */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
217 0, /* tp_methods */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
218 module_members, /* tp_members */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
219 0, /* tp_getset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
220 0, /* tp_base */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
221 0, /* tp_dict */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
222 0, /* tp_descr_get */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
223 0, /* tp_descr_set */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
224 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
225 (initproc)module_init, /* tp_init */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
226 PyType_GenericAlloc, /* tp_alloc */
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17811
diff changeset
227 PyType_GenericNew, /* tp_new */
20277
2983b54256c7 Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
228 _PyObject_GC_Del, /* tp_free */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 };