annotate Objects/moduleobject.c @ 18572:9b8564f6e34d legacy-trunk

Remove comments about XML and HTML; those sections are no longer part of this chapter. Minor change to match the style guide.
author Fred Drake <fdrake@acm.org>
date Sat, 14 Jul 2001 02:44:43 +0000
parents 222767eb28f7
children 09df3254b49d
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"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 typedef struct {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
7 PyObject_HEAD
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
8 PyObject *md_dict;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
9 } PyModuleObject;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
11 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
12 PyModule_New(char *name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
14 PyModuleObject *m;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
15 PyObject *nameobj;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
16 m = PyObject_NEW(PyModuleObject, &PyModule_Type);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 if (m == NULL)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
19 nameobj = PyString_FromString(name);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
20 m->md_dict = PyDict_New();
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
21 PyObject_GC_Init(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
22 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
23 goto fail;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
24 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
25 goto fail;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
26 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
27 goto fail;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
28 Py_DECREF(nameobj);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
29 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
30
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
31 fail:
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
32 Py_XDECREF(nameobj);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
33 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
34 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
37 PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
38 PyModule_GetDict(PyObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
40 if (!PyModule_Check(m)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
41 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
44 return ((PyModuleObject *)m) -> md_dict;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
47 char *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
48 PyModule_GetName(PyObject *m)
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
49 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
50 PyObject *nameobj;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
51 if (!PyModule_Check(m)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
52 PyErr_BadArgument();
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
53 return NULL;
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
54 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
55 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
56 "__name__");
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
57 if (nameobj == NULL || !PyString_Check(nameobj)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
58 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
59 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
60 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
61 return PyString_AsString(nameobj);
43
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
62 }
ea21982a1532 Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents: 25
diff changeset
63
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
64 char *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
65 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
66 {
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
67 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
68 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
69 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
70 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
71 }
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 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
73 "__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
74 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
75 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
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 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
79 }
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
7401
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
81 void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
82 _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
83 {
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
84 /* 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
85 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
86 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
87 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
88 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
89 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
90
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
91 int pos;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
92 PyObject *key, *value;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
93 PyObject *d;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
94
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
95 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
96
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
97 /* 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
98 pos = 0;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
99 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
100 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
101 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
102 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
103 if (Py_VerboseFlag > 1)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 7401
diff changeset
104 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
105 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
106 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
107 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
108 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
109
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
110 /* 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
111 pos = 0;
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
112 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
113 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
114 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
115 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
116 if (Py_VerboseFlag > 1)
9329
7f242f29a45f Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents: 7401
diff changeset
117 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
118 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
119 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
120 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
121 }
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
122
8c01cfee14ba Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents: 5456
diff changeset
123 /* 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
124 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
125 builtins, in particularly '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
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 static void
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
132 module_dealloc(PyModuleObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 {
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
134 PyObject_GC_Fini(m);
2238
b5dcba7b732d apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents: 2081
diff changeset
135 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
136 _PyModule_Clear((PyObject *)m);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
137 Py_DECREF(m->md_dict);
2238
b5dcba7b732d apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents: 2081
diff changeset
138 }
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
139 PyObject_DEL(PyObject_AS_GC(m));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
142 static PyObject *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
143 module_repr(PyModuleObject *m)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 {
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
145 char buf[400];
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
146 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
147 char *filename;
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
148 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
149 if (name == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
150 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
151 name = "?";
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
152 }
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
153 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
154 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
155 PyErr_Clear();
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
156 sprintf(buf, "<module '%.80s' (built-in)>", 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
157 } else {
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
158 sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename);
5c1ebf23f1d7 Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents: 9329
diff changeset
159 }
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
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
161 return PyString_FromString(buf);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
164 static PyObject *
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
165 module_getattro(PyModuleObject *m, PyObject *name)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
167 PyObject *res;
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
168 char *sname = PyString_AsString(name);
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
169
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
170 if (sname[0] == '_' && strcmp(sname, "__dict__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
171 Py_INCREF(m->md_dict);
25
32639878d16b Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents: 14
diff changeset
172 return m->md_dict;
32639878d16b Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents: 14
diff changeset
173 }
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
174 res = PyDict_GetItem(m->md_dict, name);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
175 if (res == NULL) {
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
176 char *modname = PyModule_GetName((PyObject *)m);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
177 if (modname == NULL) {
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
178 PyErr_Clear();
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
179 modname = "?";
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
180 }
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
181 PyErr_Format(PyExc_AttributeError,
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
182 "'%.50s' module has no attribute '%.400s'",
17811
222767eb28f7 Repair "module has no attribute xxx" error msg; bug introduced when
Tim Peters <tim.peters@gmail.com>
parents: 17807
diff changeset
183 modname, sname);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
184 }
5456
7316eecb1e6b removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
185 else
7316eecb1e6b removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
186 Py_INCREF(res);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 return res;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 static int
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
191 module_setattro(PyModuleObject *m, PyObject *name, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 {
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
193 char *sname = PyString_AsString(name);
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
194 if (sname[0] == '_' && strcmp(sname, "__dict__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
195 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
196 "read-only special attribute");
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
197 return -1;
25
32639878d16b Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents: 14
diff changeset
198 }
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
199 if (v == NULL) {
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
200 int rv = PyDict_DelItem(m->md_dict, name);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
201 if (rv < 0) {
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
202 char *modname = PyModule_GetName((PyObject *)m);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
203 if (modname == NULL) {
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
204 PyErr_Clear();
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
205 modname = "?";
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
206 }
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
207 PyErr_Format(PyExc_AttributeError,
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
208 "'%.50s' module has no attribute '%.400s'",
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
209 modname, sname);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
210 }
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
211 return rv;
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
212 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 else
17807
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
214 return PyDict_SetItem(m->md_dict, name, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
217 /* 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
218 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
219 the cycle. */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
220 static int
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
221 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
222 {
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
223 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
224 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
225 return 0;
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
226 }
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
227
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
228 PyTypeObject PyModule_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
229 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
230 0, /* ob_size */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
231 "module", /* tp_name */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
232 sizeof(PyModuleObject) + PyGC_HEAD_SIZE,/* tp_size */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
233 0, /* tp_itemsize */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
234 (destructor)module_dealloc, /* tp_dealloc */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
235 0, /* tp_print */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
236 0, /* tp_getattr */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
237 0, /* tp_setattr */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
238 0, /* tp_compare */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
239 (reprfunc)module_repr, /* tp_repr */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
240 0, /* tp_as_number */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
241 0, /* tp_as_sequence */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
242 0, /* tp_as_mapping */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
243 0, /* tp_hash */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
244 0, /* tp_call */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
245 0, /* tp_str */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
246 (getattrofunc)module_getattro, /* tp_getattro */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
247 (setattrofunc)module_setattro, /* tp_setattro */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
248 0, /* tp_as_buffer */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
249 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /* tp_flags */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
250 0, /* tp_doc */
1579d309676e Variant of patch #423262: Change module attribute get & set
Tim Peters <tim.peters@gmail.com>
parents: 15686
diff changeset
251 (traverseproc)module_traverse, /* tp_traverse */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 };