annotate Objects/moduleobject.c @ 17581:c1937faabb23 2.1

Added support for optional explanation parameter to the \versionadded macro. Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most of the work in a helper function, with the do_cmd_*() wrappers just supplying a portion of the replacement text.
author Fred Drake <fdrake@acm.org>
date Wed, 18 Apr 2001 17:29:14 +0000
parents 986d1c3ccdde
children 1579d309676e
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 *
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
165 module_getattr(PyModuleObject *m, char *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;
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
168 char* modname;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 43
diff changeset
169 if (strcmp(name, "__dict__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
170 Py_INCREF(m->md_dict);
25
32639878d16b Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents: 14
diff changeset
171 return m->md_dict;
32639878d16b Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents: 14
diff changeset
172 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
173 res = PyDict_GetItemString(m->md_dict, name);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
174 if (res == NULL) {
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
175 modname = PyModule_GetName((PyObject *)m);
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
176 if (modname == NULL) {
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
177 PyErr_Clear();
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
178 modname = "?";
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
179 }
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
180 PyErr_Format(PyExc_AttributeError,
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
181 "'%.50s' module has no attribute '%.400s'",
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
182 modname, name);
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
183 }
5456
7316eecb1e6b removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
184 else
7316eecb1e6b removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
185 Py_INCREF(res);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 return res;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 }
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 static int
13457
ef2ebb3b0c64 ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents: 13256
diff changeset
190 module_setattr(PyModuleObject *m, char *name, PyObject *v)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 {
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
192 char* modname;
1410
315272ec4d5b * import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents: 1293
diff changeset
193 if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
194 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
195 "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
196 return -1;
25
32639878d16b Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents: 14
diff changeset
197 }
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
198 if (v == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
199 int rv = PyDict_DelItemString(m->md_dict, name);
15318
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
200 if (rv < 0) {
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
201 modname = PyModule_GetName((PyObject *)m);
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
202 if (modname == NULL) {
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
203 PyErr_Clear();
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
204 modname = "?";
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
205 }
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
206 PyErr_Format(PyExc_AttributeError,
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
207 "'%.50s' module has no attribute '%.400s'",
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
208 modname, name);
8aa947e1eee8 Ka-Ping Yee <ping@lfw.org>:
Fred Drake <fdrake@acm.org>
parents: 14375
diff changeset
209 }
1031
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
210 return rv;
7a11a70f7f44 classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
211 }
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
213 return PyDict_SetItemString(m->md_dict, name, v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
216 /* 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
217 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
218 the cycle. */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
219 static int
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
220 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
221 {
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
222 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
223 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
224 return 0;
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
225 }
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
226
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
227 PyTypeObject PyModule_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
228 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 0, /*ob_size*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 "module", /*tp_name*/
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
231 sizeof(PyModuleObject) + PyGC_HEAD_SIZE, /*tp_size*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 0, /*tp_itemsize*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1410
diff changeset
233 (destructor)module_dealloc, /*tp_dealloc*/
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 1031
diff changeset
234 0, /*tp_print*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1410
diff changeset
235 (getattrfunc)module_getattr, /*tp_getattr*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1410
diff changeset
236 (setattrfunc)module_setattr, /*tp_setattr*/
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 43
diff changeset
237 0, /*tp_compare*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1410
diff changeset
238 (reprfunc)module_repr, /*tp_repr*/
15686
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
239 0, /*tp_as_number*/
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
240 0, /*tp_as_sequence*/
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
241 0, /*tp_as_mapping*/
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
242 0, /* tp_hash */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
243 0, /* tp_call */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
244 0, /* tp_str */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
245 0, /* tp_getattro */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
246 0, /* tp_setattro */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
247 0, /* tp_as_buffer */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
248 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
249 0, /* tp_doc */
986d1c3ccdde Add garbage collection for module objects. Closes patch #102939 and
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 15318
diff changeset
250 (traverseproc)module_traverse, /* tp_traverse */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 };