Mercurial > cpython-lru_cache-weakref
annotate Objects/xxobject.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 65d2d8e67da3 |
| children | 55f8c701c9da |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
1 /*********************************************************** |
|
2059
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1908
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1908
diff
changeset
|
3 The Netherlands. |
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
4 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
5 All Rights Reserved |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
6 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
15 permission. |
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
29 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
30 ******************************************************************/ |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
111
diff
changeset
|
31 |
|
110
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
32 /* Use this file as a template to start implementing a new object type. |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
33 If your objects will be called foobar, start by copying this file to |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
34 foobarobject.c, changing all occurrences of xx to foobar and all |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
35 occurrences of Xx by Foobar. You will probably want to delete all |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
36 references to 'x_attr' and add your own types of attributes |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
37 instead. Maybe you want to name your local variables other than |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
38 'xp'. If your object type is needed in other files, you'll have to |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
39 create a file "foobarobject.h"; see intobject.h for an example. */ |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
40 |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
41 |
| 6 | 42 /* Xx objects */ |
| 43 | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
44 #include "Python.h" |
|
110
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
45 |
| 6 | 46 typedef struct { |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
47 PyObject_HEAD |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
48 PyObject *x_attr; /* Attributes dictionary */ |
| 6 | 49 } xxobject; |
| 50 | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
51 staticforward PyTypeObject Xxtype; |
| 6 | 52 |
|
110
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
53 #define is_xxobject(v) ((v)->ob_type == &Xxtype) |
|
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
54 |
| 6 | 55 static xxobject * |
| 56 newxxobject(arg) | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
57 PyObject *arg; |
| 6 | 58 { |
|
111
d08f752b56c0
Changed bogus ref to textobject into xxobject.
Guido van Rossum <guido@python.org>
parents:
110
diff
changeset
|
59 xxobject *xp; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
60 xp = PyObject_NEW(xxobject, &Xxtype); |
| 6 | 61 if (xp == NULL) |
| 62 return NULL; | |
| 63 xp->x_attr = NULL; | |
| 64 return xp; | |
| 65 } | |
| 66 | |
| 67 /* Xx methods */ | |
| 68 | |
| 69 static void | |
| 70 xx_dealloc(xp) | |
| 71 xxobject *xp; | |
| 72 { | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
73 Py_XDECREF(xp->x_attr); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
74 PyMem_DEL(xp); |
| 6 | 75 } |
| 76 | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
77 static PyObject * |
| 6 | 78 xx_demo(self, args) |
| 79 xxobject *self; | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
80 PyObject *args; |
| 6 | 81 { |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
82 if (!PyArg_NoArgs(args)) |
| 6 | 83 return NULL; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
84 Py_INCREF(Py_None); |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
85 return Py_None; |
| 6 | 86 } |
| 87 | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
88 static Py_MethodDef xx_methods[] = { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
89 {"demo", (PyCFunction)xx_demo}, |
| 6 | 90 {NULL, NULL} /* sentinel */ |
| 91 }; | |
| 92 | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
93 static PyObject * |
| 6 | 94 xx_getattr(xp, name) |
| 95 xxobject *xp; | |
| 96 char *name; | |
| 97 { | |
| 98 if (xp->x_attr != NULL) { | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
99 PyObject *v = PyDict_GetItemString(xp->x_attr, name); |
| 6 | 100 if (v != NULL) { |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
101 Py_INCREF(v); |
| 6 | 102 return v; |
| 103 } | |
| 104 } | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
105 return Py_FindMethod(xx_methods, (PyObject *)xp, name); |
| 6 | 106 } |
| 107 | |
| 108 static int | |
| 109 xx_setattr(xp, name, v) | |
| 110 xxobject *xp; | |
| 111 char *name; | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
112 PyObject *v; |
| 6 | 113 { |
| 114 if (xp->x_attr == NULL) { | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
115 xp->x_attr = PyDict_New(); |
| 6 | 116 if (xp->x_attr == NULL) |
|
110
e4b1d9e6980a
Fixed a bit (still no warranties).
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
117 return -1; |
| 6 | 118 } |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
954
diff
changeset
|
119 if (v == NULL) { |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
120 int rv = PyDict_DelItemString(xp->x_attr, name); |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
954
diff
changeset
|
121 if (rv < 0) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
122 PyErr_SetString(PyExc_AttributeError, |
|
1031
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
954
diff
changeset
|
123 "delete non-existing xx attribute"); |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
954
diff
changeset
|
124 return rv; |
|
7a11a70f7f44
classobject.c moduleobject.c stdwinmodule.c xxobject.c:
Guido van Rossum <guido@python.org>
parents:
954
diff
changeset
|
125 } |
| 6 | 126 else |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
127 return PyDict_SetItemString(xp->x_attr, name, v); |
| 6 | 128 } |
| 129 | |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
130 static PyTypeObject Xxtype = { |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
131 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 132 0, /*ob_size*/ |
| 133 "xx", /*tp_name*/ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
134 sizeof(xxobject), /*tp_basicsize*/ |
| 6 | 135 0, /*tp_itemsize*/ |
| 136 /* methods */ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
137 (destructor)xx_dealloc, /*tp_dealloc*/ |
|
954
266a8ba20784
* xxobject.c: added tp_as_number, tp_as_sequence, tp_as_mapping to
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
138 0, /*tp_print*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
139 (getattrfunc)xx_getattr, /*tp_getattr*/ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
140 (setattrfunc)xx_setattr, /*tp_setattr*/ |
|
954
266a8ba20784
* xxobject.c: added tp_as_number, tp_as_sequence, tp_as_mapping to
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
141 0, /*tp_compare*/ |
|
266a8ba20784
* xxobject.c: added tp_as_number, tp_as_sequence, tp_as_mapping to
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
142 0, /*tp_repr*/ |
|
266a8ba20784
* xxobject.c: added tp_as_number, tp_as_sequence, tp_as_mapping to
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
143 0, /*tp_as_number*/ |
|
266a8ba20784
* xxobject.c: added tp_as_number, tp_as_sequence, tp_as_mapping to
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
144 0, /*tp_as_sequence*/ |
|
266a8ba20784
* xxobject.c: added tp_as_number, tp_as_sequence, tp_as_mapping to
Guido van Rossum <guido@python.org>
parents:
901
diff
changeset
|
145 0, /*tp_as_mapping*/ |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
146 0, /*tp_hash*/ |
| 6 | 147 }; |
