Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.c @ 13778:f6e4a9dd67cd legacy-trunk
Rename the "parser" package to "parsers" to be compatible with the PyXML
project.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Mon, 31 Jul 2000 23:59:54 +0000 |
| parents | ef2ebb3b0c64 |
| children | 6f6f965c5e2d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
2 Copyright (c) 2000, BeOpen.com. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
5 All rights reserved. |
| 145 | 6 |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
7 See the file "Misc/COPYRIGHT" for information on usage and |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 145 | 9 ******************************************************************/ |
| 10 | |
| 6 | 11 /* Module object implementation */ |
| 12 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
13 #include "Python.h" |
| 6 | 14 |
| 15 typedef struct { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
16 PyObject_HEAD |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
17 PyObject *md_dict; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
18 } PyModuleObject; |
| 6 | 19 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
20 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
21 PyModule_New(char *name) |
| 6 | 22 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
23 PyModuleObject *m; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
24 PyObject *nameobj; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
25 m = PyObject_NEW(PyModuleObject, &PyModule_Type); |
| 6 | 26 if (m == NULL) |
| 27 return NULL; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
28 nameobj = PyString_FromString(name); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
29 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
|
30 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
|
31 goto fail; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
32 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
|
33 goto fail; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
34 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
|
35 goto fail; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
36 Py_DECREF(nameobj); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
37 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
|
38 |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
39 fail: |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
40 Py_XDECREF(nameobj); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
41 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
|
42 return NULL; |
| 6 | 43 } |
| 44 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
45 PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
46 PyModule_GetDict(PyObject *m) |
| 6 | 47 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
48 if (!PyModule_Check(m)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
49 PyErr_BadInternalCall(); |
| 6 | 50 return NULL; |
| 51 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
52 return ((PyModuleObject *)m) -> md_dict; |
| 6 | 53 } |
| 54 | |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
55 char * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
56 PyModule_GetName(PyObject *m) |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
57 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
58 PyObject *nameobj; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
59 if (!PyModule_Check(m)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
60 PyErr_BadArgument(); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
61 return NULL; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
62 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
63 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
64 "__name__"); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
65 if (nameobj == NULL || !PyString_Check(nameobj)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
66 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
|
67 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
|
68 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
69 return PyString_AsString(nameobj); |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
70 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
71 |
|
10161
5c1ebf23f1d7
Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents:
9329
diff
changeset
|
72 char * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
73 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
|
74 { |
|
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 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
|
76 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
|
77 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
|
78 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
|
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 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
|
81 "__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
|
82 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
|
83 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
|
84 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
|
85 } |
|
5c1ebf23f1d7
Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents:
9329
diff
changeset
|
86 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
|
87 } |
|
5c1ebf23f1d7
Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents:
9329
diff
changeset
|
88 |
|
7401
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
89 void |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
90 _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
|
91 { |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
92 /* 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 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
|
98 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
99 int pos; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
100 PyObject *key, *value; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
101 PyObject *d; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
102 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
103 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
|
104 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
105 /* 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
|
106 pos = 0; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
107 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
|
108 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
|
109 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
|
110 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
|
111 if (Py_VerboseFlag > 1) |
|
9329
7f242f29a45f
Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents:
7401
diff
changeset
|
112 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
|
113 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
|
114 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
115 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
116 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
117 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
118 /* 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
|
119 pos = 0; |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
120 while (PyDict_Next(d, &pos, &key, &value)) { |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
121 if (value != Py_None && PyString_Check(key)) { |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
122 char *s = PyString_AsString(key); |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
123 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) { |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
124 if (Py_VerboseFlag > 1) |
|
9329
7f242f29a45f
Replace fprintf(stderr, ...) with PySys_WriteStderr(...).
Guido van Rossum <guido@python.org>
parents:
7401
diff
changeset
|
125 PySys_WriteStderr("# clear[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
|
126 PyDict_SetItem(d, key, Py_None); |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
127 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
128 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
129 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
130 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
131 /* 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
|
132 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
|
133 builtins, in particularly 'None'. */ |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
134 |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
135 } |
|
8c01cfee14ba
Add internal routine _PyModule_Clear(), which does approximately what
Guido van Rossum <guido@python.org>
parents:
5456
diff
changeset
|
136 |
| 6 | 137 /* Methods */ |
| 138 | |
| 139 static void | |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
140 module_dealloc(PyModuleObject *m) |
| 6 | 141 { |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
142 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
|
143 _PyModule_Clear((PyObject *)m); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
144 Py_DECREF(m->md_dict); |
|
2238
b5dcba7b732d
apply dictclear to dict of deleted modules
Guido van Rossum <guido@python.org>
parents:
2081
diff
changeset
|
145 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
10161
diff
changeset
|
146 PyObject_DEL(m); |
| 6 | 147 } |
| 148 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
149 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
150 module_repr(PyModuleObject *m) |
| 6 | 151 { |
|
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
|
152 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
|
153 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
|
154 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
|
155 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
|
156 if (name == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
157 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
|
158 name = "?"; |
|
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
159 } |
|
10161
5c1ebf23f1d7
Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents:
9329
diff
changeset
|
160 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
|
161 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
|
162 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
|
163 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
|
164 } 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
|
165 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
|
166 } |
|
5c1ebf23f1d7
Patch by Chris Petrilli to display the origin of a module in its
Guido van Rossum <guido@python.org>
parents:
9329
diff
changeset
|
167 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
168 return PyString_FromString(buf); |
| 6 | 169 } |
| 170 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
171 static PyObject * |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
172 module_getattr(PyModuleObject *m, char *name) |
| 6 | 173 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
174 PyObject *res; |
| 86 | 175 if (strcmp(name, "__dict__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
176 Py_INCREF(m->md_dict); |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
177 return m->md_dict; |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
178 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
179 res = PyDict_GetItemString(m->md_dict, name); |
|
14
874c8fd565fa
New error handling in getattr().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
180 if (res == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
181 PyErr_SetString(PyExc_AttributeError, name); |
|
5456
7316eecb1e6b
removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
182 else |
|
7316eecb1e6b
removed last #ifdef SUPPORT_OBSOLETE_ACCESS bits.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
183 Py_INCREF(res); |
| 6 | 184 return res; |
| 185 } | |
| 186 | |
| 187 static int | |
|
13457
ef2ebb3b0c64
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13256
diff
changeset
|
188 module_setattr(PyModuleObject *m, char *name, PyObject *v) |
| 6 | 189 { |
|
1410
315272ec4d5b
* import.c (get_module): total rewrite, to ensure proper search order: for
Guido van Rossum <guido@python.org>
parents:
1293
diff
changeset
|
190 if (name[0] == '_' && strcmp(name, "__dict__") == 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
191 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
192 "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
|
193 return -1; |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
194 } |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
195 if (v == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
196 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
|
197 if (rv < 0) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
198 PyErr_SetString(PyExc_AttributeError, |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
199 "delete non-existing module attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
200 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
201 } |
| 6 | 202 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
203 return PyDict_SetItemString(m->md_dict, name, v); |
| 6 | 204 } |
| 205 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
206 PyTypeObject PyModule_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
207 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 208 0, /*ob_size*/ |
| 209 "module", /*tp_name*/ | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
210 sizeof(PyModuleObject), /*tp_size*/ |
| 6 | 211 0, /*tp_itemsize*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
212 (destructor)module_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
213 0, /*tp_print*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
214 (getattrfunc)module_getattr, /*tp_getattr*/ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
215 (setattrfunc)module_setattr, /*tp_setattr*/ |
| 86 | 216 0, /*tp_compare*/ |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1410
diff
changeset
|
217 (reprfunc)module_repr, /*tp_repr*/ |
| 6 | 218 }; |
