Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.c @ 892:11ea669c231e legacy-trunk
Documented new contents of lib5.tex
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 03 Jun 1992 17:56:44 +0000 |
| parents | 0cd197947d81 |
| children | 7a11a70f7f44 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
795
0cd197947d81
Copyright for 1992 added
Guido van Rossum <guido@python.org>
parents:
741
diff
changeset
|
2 Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
| 145 | 3 Netherlands. |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* Module object implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
| 6 | 28 |
| 29 typedef struct { | |
| 30 OB_HEAD | |
| 31 object *md_name; | |
| 32 object *md_dict; | |
| 33 } moduleobject; | |
| 34 | |
| 35 object * | |
| 36 newmoduleobject(name) | |
| 37 char *name; | |
| 38 { | |
| 39 moduleobject *m = NEWOBJ(moduleobject, &Moduletype); | |
| 40 if (m == NULL) | |
| 41 return NULL; | |
| 42 m->md_name = newstringobject(name); | |
| 43 m->md_dict = newdictobject(); | |
| 44 if (m->md_name == NULL || m->md_dict == NULL) { | |
| 45 DECREF(m); | |
| 46 return NULL; | |
| 47 } | |
| 48 return (object *)m; | |
| 49 } | |
| 50 | |
| 51 object * | |
| 52 getmoduledict(m) | |
| 53 object *m; | |
| 54 { | |
| 55 if (!is_moduleobject(m)) { | |
| 86 | 56 err_badcall(); |
| 6 | 57 return NULL; |
| 58 } | |
| 59 return ((moduleobject *)m) -> md_dict; | |
| 60 } | |
| 61 | |
|
43
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
62 char * |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
63 getmodulename(m) |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
64 object *m; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
65 { |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
66 if (!is_moduleobject(m)) { |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
67 err_badarg(); |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
68 return NULL; |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
69 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
70 return getstringvalue(((moduleobject *)m) -> md_name); |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
71 } |
|
ea21982a1532
Added getmodulename() public interface.
Guido van Rossum <guido@python.org>
parents:
25
diff
changeset
|
72 |
| 6 | 73 /* Methods */ |
| 74 | |
| 75 static void | |
| 86 | 76 module_dealloc(m) |
| 6 | 77 moduleobject *m; |
| 78 { | |
| 79 if (m->md_name != NULL) | |
| 80 DECREF(m->md_name); | |
| 81 if (m->md_dict != NULL) | |
| 82 DECREF(m->md_dict); | |
| 83 free((char *)m); | |
| 84 } | |
| 85 | |
| 741 | 86 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
87 static int |
| 86 | 88 module_print(m, fp, flags) |
| 6 | 89 moduleobject *m; |
| 90 FILE *fp; | |
| 741 | 91 int flags; /* Not used but required by interface */ |
| 6 | 92 { |
| 86 | 93 fprintf(fp, "<module '%s'>", getstringvalue(m->md_name)); |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
94 return 0; |
| 6 | 95 } |
| 96 | |
| 97 static object * | |
| 86 | 98 module_repr(m) |
| 6 | 99 moduleobject *m; |
| 100 { | |
| 101 char buf[100]; | |
| 86 | 102 sprintf(buf, "<module '%.80s'>", getstringvalue(m->md_name)); |
| 6 | 103 return newstringobject(buf); |
| 104 } | |
| 105 | |
| 106 static object * | |
| 86 | 107 module_getattr(m, name) |
| 6 | 108 moduleobject *m; |
| 109 char *name; | |
| 110 { | |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
111 object *res; |
| 86 | 112 if (strcmp(name, "__dict__") == 0) { |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
113 INCREF(m->md_dict); |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
114 return m->md_dict; |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
115 } |
| 86 | 116 if (strcmp(name, "__name__") == 0) { |
| 117 INCREF(m->md_name); | |
| 118 return m->md_name; | |
| 119 } | |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
120 res = dictlookup(m->md_dict, name); |
|
14
874c8fd565fa
New error handling in getattr().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
121 if (res == NULL) |
| 475 | 122 err_setstr(AttributeError, name); |
| 6 | 123 else |
| 124 INCREF(res); | |
| 125 return res; | |
| 126 } | |
| 127 | |
| 128 static int | |
| 86 | 129 module_setattr(m, name, v) |
| 6 | 130 moduleobject *m; |
| 131 char *name; | |
| 132 object *v; | |
| 133 { | |
| 86 | 134 if (strcmp(name, "__dict__") == 0 || strcmp(name, "__name__") == 0) { |
| 475 | 135 err_setstr(TypeError, "can't assign to reserved member name"); |
| 95 | 136 return -1; |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
137 } |
| 6 | 138 if (v == NULL) |
| 139 return dictremove(m->md_dict, name); | |
| 140 else | |
| 141 return dictinsert(m->md_dict, name, v); | |
| 142 } | |
| 143 | |
| 144 typeobject Moduletype = { | |
| 145 OB_HEAD_INIT(&Typetype) | |
| 146 0, /*ob_size*/ | |
| 147 "module", /*tp_name*/ | |
| 148 sizeof(moduleobject), /*tp_size*/ | |
| 149 0, /*tp_itemsize*/ | |
| 86 | 150 module_dealloc, /*tp_dealloc*/ |
| 151 module_print, /*tp_print*/ | |
| 152 module_getattr, /*tp_getattr*/ | |
| 153 module_setattr, /*tp_setattr*/ | |
| 154 0, /*tp_compare*/ | |
| 155 module_repr, /*tp_repr*/ | |
| 6 | 156 }; |
