Mercurial > cpython-lru_cache-weakref
annotate Objects/moduleobject.c @ 1257:1cabc2b6c970 legacy-trunk
* Changed all copyright messages to include 1993.
* Stubs for faster implementation of local variables (not yet finished)
* Added function name to code object. Print it for code and function
objects. THIS MAKES THE .PYC FILE FORMAT INCOMPATIBLE (the version
number has changed accordingly)
* Print address of self for built-in methods
* New internal functions getattro and setattro (getattr/setattr with
string object arg)
* Replaced "dictobject" with more powerful "mappingobject"
* New per-type functio tp_hash to implement arbitrary object hashing,
and hashobject() to interface to it
* Added built-in functions hash(v) and hasattr(v, 'name')
* classobject: made some functions static that accidentally weren't;
added __hash__ special instance method to implement hash()
* Added proper comparison for built-in methods and functions
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 29 Mar 1993 10:43:31 +0000 |
| parents | f54e64c8b9b7 |
| children | 9fed5726a36d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
3 Amsterdam, The Netherlands. |
| 145 | 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 | |
| 86 static object * | |
| 86 | 87 module_repr(m) |
| 6 | 88 moduleobject *m; |
| 89 { | |
| 90 char buf[100]; | |
| 86 | 91 sprintf(buf, "<module '%.80s'>", getstringvalue(m->md_name)); |
| 6 | 92 return newstringobject(buf); |
| 93 } | |
| 94 | |
| 95 static object * | |
| 86 | 96 module_getattr(m, name) |
| 6 | 97 moduleobject *m; |
| 98 char *name; | |
| 99 { | |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
100 object *res; |
| 86 | 101 if (strcmp(name, "__dict__") == 0) { |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
102 INCREF(m->md_dict); |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
103 return m->md_dict; |
|
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
104 } |
| 86 | 105 if (strcmp(name, "__name__") == 0) { |
| 106 INCREF(m->md_name); | |
| 107 return m->md_name; | |
| 108 } | |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
109 res = dictlookup(m->md_dict, name); |
|
14
874c8fd565fa
New error handling in getattr().
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
110 if (res == NULL) |
| 475 | 111 err_setstr(AttributeError, name); |
| 6 | 112 else |
| 113 INCREF(res); | |
| 114 return res; | |
| 115 } | |
| 116 | |
| 117 static int | |
| 86 | 118 module_setattr(m, name, v) |
| 6 | 119 moduleobject *m; |
| 120 char *name; | |
| 121 object *v; | |
| 122 { | |
| 86 | 123 if (strcmp(name, "__dict__") == 0 || strcmp(name, "__name__") == 0) { |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
124 err_setstr(TypeError, "read-only special attribute"); |
| 95 | 125 return -1; |
|
25
32639878d16b
Added __dict to module getattr.
Guido van Rossum <guido@python.org>
parents:
14
diff
changeset
|
126 } |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
127 if (v == NULL) { |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
128 int rv = dictremove(m->md_dict, name); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
129 if (rv < 0) |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
130 err_setstr(AttributeError, |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
131 "delete non-existing module attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
132 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
133 } |
| 6 | 134 else |
| 135 return dictinsert(m->md_dict, name, v); | |
| 136 } | |
| 137 | |
| 138 typeobject Moduletype = { | |
| 139 OB_HEAD_INIT(&Typetype) | |
| 140 0, /*ob_size*/ | |
| 141 "module", /*tp_name*/ | |
| 142 sizeof(moduleobject), /*tp_size*/ | |
| 143 0, /*tp_itemsize*/ | |
| 86 | 144 module_dealloc, /*tp_dealloc*/ |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
145 0, /*tp_print*/ |
| 86 | 146 module_getattr, /*tp_getattr*/ |
| 147 module_setattr, /*tp_setattr*/ | |
| 148 0, /*tp_compare*/ | |
| 149 module_repr, /*tp_repr*/ | |
| 6 | 150 }; |
