Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.c @ 15164:8930ec3bc00a legacy-trunk
Added additional test cases for pulldom modifications.
| author | Lars Gustäbel <lars@gustaebel.de> |
|---|---|
| date | Wed, 11 Oct 2000 22:36:00 +0000 |
| parents | 6f6f965c5e2d |
| children | 8aa947e1eee8 |
| rev | line source |
|---|---|
| 145 | 1 |
| 6 | 2 /* Module object implementation */ |
| 3 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
4 #include "Python.h" |
| 6 | 5 |
| 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 | 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 | 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 | 17 if (m == NULL) |
| 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(); |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
21 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
|
22 goto fail; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
23 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
|
24 goto fail; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
25 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
|
26 goto fail; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
27 Py_DECREF(nameobj); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
28 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
|
29 |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
30 fail: |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
31 Py_XDECREF(nameobj); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
32 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
|
33 return NULL; |
| 6 | 34 } |
| 35 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
36 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
37 PyModule_GetDict(PyObject *m) |
| 6 | 38 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
39 if (!PyModule_Check(m)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
40 PyErr_BadInternalCall(); |
| 6 | 41 return NULL; |
| 42 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
43 return ((PyModuleObject *)m) -> md_dict; |
| 6 | 44 } |
| 45 | |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
46 char * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
47 PyModule_GetName(PyObject *m) |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
48 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
49 PyObject *nameobj; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
50 if (!PyModule_Check(m)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
51 PyErr_BadArgument(); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
52 return NULL; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
53 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
54 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
55 "__name__"); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
56 if (nameobj == NULL || !PyString_Check(nameobj)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
57 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
|
58 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
|
59 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
60 return PyString_AsString(nameobj); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
61 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
62 |
|
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
|
63 char * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
64 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
|
65 { |
|
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 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
|
67 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
|
68 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
|
69 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
|
70 } |
|
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 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
|
72 "__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
|
73 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
|
74 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
|
75 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
|
76 } |
|
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 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
|
78 } |
|
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 |
|
7401
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
80 void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
81 _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
|
82 { |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
83 /* 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 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
|
89 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
90 int pos; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
91 PyObject *key, *value; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
92 PyObject *d; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
93 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
94 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
|
95 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
96 /* 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
|
97 pos = 0; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
98 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
|
99 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
|
100 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
|
101 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
|
102 if (Py_VerboseFlag > 1) |
|
9329
7f242f29a45f
Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents:
7401
diff
changeset
|
103 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
|
104 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
|
105 } |
|
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 /* 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
|
110 pos = 0; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
111 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
|
112 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
|
113 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
|
114 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
|
115 if (Py_VerboseFlag > 1) |
|
9329
7f242f29a45f
Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents:
7401
diff
changeset
|
116 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
|
117 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
|
118 } |
|
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 /* 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
|
123 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
|
124 builtins, in particularly '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 |
| 6 | 128 /* Methods */ |
| 129 | |
| 130 static void | |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
131 module_dealloc(PyModuleObject *m) |
| 6 | 132 { |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
133 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
|
134 _PyModule_Clear((PyObject *)m); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
135 Py_DECREF(m->md_dict); |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
136 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
10161
diff
changeset
|
137 PyObject_DEL(m); |
| 6 | 138 } |
| 139 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
140 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
141 module_repr(PyModuleObject *m) |
| 6 | 142 { |
|
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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 if (name == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
148 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
|
149 name = "?"; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
150 } |
|
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
|
151 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
|
152 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
|
153 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
|
154 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
|
155 } 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
|
156 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
|
157 } |
|
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 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
159 return PyString_FromString(buf); |
| 6 | 160 } |
| 161 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
162 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
163 module_getattr(PyModuleObject *m, char *name) |
| 6 | 164 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
165 PyObject *res; |
| 86 | 166 if (strcmp(name, "__dict__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
167 Py_INCREF(m->md_dict); |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
168 return m->md_dict; |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
169 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
170 res = PyDict_GetItemString(m->md_dict, name); |
|
14
874c8fd565fa
New error handling in getattr().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
171 if (res == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
172 PyErr_SetString(PyExc_AttributeError, name); |
|
5456
7316eecb1e6b
removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
173 else |
|
7316eecb1e6b
removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
174 Py_INCREF(res); |
| 6 | 175 return res; |
| 176 } | |
| 177 | |
| 178 static int | |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
179 module_setattr(PyModuleObject *m, char *name, PyObject *v) |
| 6 | 180 { |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
181 if (name[0] == '_' && strcmp(name, "__dict__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
182 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
183 "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
|
184 return -1; |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
185 } |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
186 if (v == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
187 int rv = PyDict_DelItemString(m->md_dict, name); |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
188 if (rv < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
189 PyErr_SetString(PyExc_AttributeError, |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
190 "delete non-existing module attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
191 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
192 } |
| 6 | 193 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
194 return PyDict_SetItemString(m->md_dict, name, v); |
| 6 | 195 } |
| 196 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
197 PyTypeObject PyModule_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
198 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 199 0, /*ob_size*/ |
| 200 "module", /*tp_name*/ | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
201 sizeof(PyModuleObject), /*tp_size*/ |
| 6 | 202 0, /*tp_itemsize*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
203 (destructor)module_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
204 0, /*tp_print*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
205 (getattrfunc)module_getattr, /*tp_getattr*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
206 (setattrfunc)module_setattr, /*tp_setattr*/ |
| 86 | 207 0, /*tp_compare*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
208 (reprfunc)module_repr, /*tp_repr*/ |
| 6 | 209 }; |
