1 | /* Descriptors -- a new, flexible way to describe attributes */
|
---|
2 |
|
---|
3 | #include "Python.h"
|
---|
4 | #include "structmember.h" /* Why is this not included in Python.h? */
|
---|
5 |
|
---|
6 | static void
|
---|
7 | descr_dealloc(PyDescrObject *descr)
|
---|
8 | {
|
---|
9 | _PyObject_GC_UNTRACK(descr);
|
---|
10 | Py_XDECREF(descr->d_type);
|
---|
11 | Py_XDECREF(descr->d_name);
|
---|
12 | PyObject_GC_Del(descr);
|
---|
13 | }
|
---|
14 |
|
---|
15 | static char *
|
---|
16 | descr_name(PyDescrObject *descr)
|
---|
17 | {
|
---|
18 | if (descr->d_name != NULL && PyString_Check(descr->d_name))
|
---|
19 | return PyString_AS_STRING(descr->d_name);
|
---|
20 | else
|
---|
21 | return "?";
|
---|
22 | }
|
---|
23 |
|
---|
24 | static PyObject *
|
---|
25 | descr_repr(PyDescrObject *descr, char *format)
|
---|
26 | {
|
---|
27 | return PyString_FromFormat(format, descr_name(descr),
|
---|
28 | descr->d_type->tp_name);
|
---|
29 | }
|
---|
30 |
|
---|
31 | static PyObject *
|
---|
32 | method_repr(PyMethodDescrObject *descr)
|
---|
33 | {
|
---|
34 | return descr_repr((PyDescrObject *)descr,
|
---|
35 | "<method '%s' of '%s' objects>");
|
---|
36 | }
|
---|
37 |
|
---|
38 | static PyObject *
|
---|
39 | member_repr(PyMemberDescrObject *descr)
|
---|
40 | {
|
---|
41 | return descr_repr((PyDescrObject *)descr,
|
---|
42 | "<member '%s' of '%s' objects>");
|
---|
43 | }
|
---|
44 |
|
---|
45 | static PyObject *
|
---|
46 | getset_repr(PyGetSetDescrObject *descr)
|
---|
47 | {
|
---|
48 | return descr_repr((PyDescrObject *)descr,
|
---|
49 | "<attribute '%s' of '%s' objects>");
|
---|
50 | }
|
---|
51 |
|
---|
52 | static PyObject *
|
---|
53 | wrapperdescr_repr(PyWrapperDescrObject *descr)
|
---|
54 | {
|
---|
55 | return descr_repr((PyDescrObject *)descr,
|
---|
56 | "<slot wrapper '%s' of '%s' objects>");
|
---|
57 | }
|
---|
58 |
|
---|
59 | static int
|
---|
60 | descr_check(PyDescrObject *descr, PyObject *obj, PyObject **pres)
|
---|
61 | {
|
---|
62 | if (obj == NULL) {
|
---|
63 | Py_INCREF(descr);
|
---|
64 | *pres = (PyObject *)descr;
|
---|
65 | return 1;
|
---|
66 | }
|
---|
67 | if (!PyObject_TypeCheck(obj, descr->d_type)) {
|
---|
68 | PyErr_Format(PyExc_TypeError,
|
---|
69 | "descriptor '%s' for '%s' objects "
|
---|
70 | "doesn't apply to '%s' object",
|
---|
71 | descr_name((PyDescrObject *)descr),
|
---|
72 | descr->d_type->tp_name,
|
---|
73 | obj->ob_type->tp_name);
|
---|
74 | *pres = NULL;
|
---|
75 | return 1;
|
---|
76 | }
|
---|
77 | return 0;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static PyObject *
|
---|
81 | classmethod_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
|
---|
82 | {
|
---|
83 | /* Ensure a valid type. Class methods ignore obj. */
|
---|
84 | if (type == NULL) {
|
---|
85 | if (obj != NULL)
|
---|
86 | type = (PyObject *)obj->ob_type;
|
---|
87 | else {
|
---|
88 | /* Wot - no type?! */
|
---|
89 | PyErr_Format(PyExc_TypeError,
|
---|
90 | "descriptor '%s' for type '%s' "
|
---|
91 | "needs either an object or a type",
|
---|
92 | descr_name((PyDescrObject *)descr),
|
---|
93 | descr->d_type->tp_name);
|
---|
94 | return NULL;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | if (!PyType_Check(type)) {
|
---|
98 | PyErr_Format(PyExc_TypeError,
|
---|
99 | "descriptor '%s' for type '%s' "
|
---|
100 | "needs a type, not a '%s' as arg 2",
|
---|
101 | descr_name((PyDescrObject *)descr),
|
---|
102 | descr->d_type->tp_name,
|
---|
103 | type->ob_type->tp_name);
|
---|
104 | return NULL;
|
---|
105 | }
|
---|
106 | if (!PyType_IsSubtype((PyTypeObject *)type, descr->d_type)) {
|
---|
107 | PyErr_Format(PyExc_TypeError,
|
---|
108 | "descriptor '%s' for type '%s' "
|
---|
109 | "doesn't apply to type '%s'",
|
---|
110 | descr_name((PyDescrObject *)descr),
|
---|
111 | descr->d_type->tp_name,
|
---|
112 | ((PyTypeObject *)type)->tp_name);
|
---|
113 | return NULL;
|
---|
114 | }
|
---|
115 | return PyCFunction_New(descr->d_method, type);
|
---|
116 | }
|
---|
117 |
|
---|
118 | static PyObject *
|
---|
119 | method_get(PyMethodDescrObject *descr, PyObject *obj, PyObject *type)
|
---|
120 | {
|
---|
121 | PyObject *res;
|
---|
122 |
|
---|
123 | if (descr_check((PyDescrObject *)descr, obj, &res))
|
---|
124 | return res;
|
---|
125 | return PyCFunction_New(descr->d_method, obj);
|
---|
126 | }
|
---|
127 |
|
---|
128 | static PyObject *
|
---|
129 | member_get(PyMemberDescrObject *descr, PyObject *obj, PyObject *type)
|
---|
130 | {
|
---|
131 | PyObject *res;
|
---|
132 |
|
---|
133 | if (descr_check((PyDescrObject *)descr, obj, &res))
|
---|
134 | return res;
|
---|
135 | return PyMember_GetOne((char *)obj, descr->d_member);
|
---|
136 | }
|
---|
137 |
|
---|
138 | static PyObject *
|
---|
139 | getset_get(PyGetSetDescrObject *descr, PyObject *obj, PyObject *type)
|
---|
140 | {
|
---|
141 | PyObject *res;
|
---|
142 |
|
---|
143 | if (descr_check((PyDescrObject *)descr, obj, &res))
|
---|
144 | return res;
|
---|
145 | if (descr->d_getset->get != NULL)
|
---|
146 | return descr->d_getset->get(obj, descr->d_getset->closure);
|
---|
147 | PyErr_Format(PyExc_AttributeError,
|
---|
148 | "attribute '%.300s' of '%.100s' objects is not readable",
|
---|
149 | descr_name((PyDescrObject *)descr),
|
---|
150 | descr->d_type->tp_name);
|
---|
151 | return NULL;
|
---|
152 | }
|
---|
153 |
|
---|
154 | static PyObject *
|
---|
155 | wrapperdescr_get(PyWrapperDescrObject *descr, PyObject *obj, PyObject *type)
|
---|
156 | {
|
---|
157 | PyObject *res;
|
---|
158 |
|
---|
159 | if (descr_check((PyDescrObject *)descr, obj, &res))
|
---|
160 | return res;
|
---|
161 | return PyWrapper_New((PyObject *)descr, obj);
|
---|
162 | }
|
---|
163 |
|
---|
164 | static int
|
---|
165 | descr_setcheck(PyDescrObject *descr, PyObject *obj, PyObject *value,
|
---|
166 | int *pres)
|
---|
167 | {
|
---|
168 | assert(obj != NULL);
|
---|
169 | if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) {
|
---|
170 | PyErr_Format(PyExc_TypeError,
|
---|
171 | "descriptor '%.200s' for '%.100s' objects "
|
---|
172 | "doesn't apply to '%.100s' object",
|
---|
173 | descr_name(descr),
|
---|
174 | descr->d_type->tp_name,
|
---|
175 | obj->ob_type->tp_name);
|
---|
176 | *pres = -1;
|
---|
177 | return 1;
|
---|
178 | }
|
---|
179 | return 0;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static int
|
---|
183 | member_set(PyMemberDescrObject *descr, PyObject *obj, PyObject *value)
|
---|
184 | {
|
---|
185 | int res;
|
---|
186 |
|
---|
187 | if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
|
---|
188 | return res;
|
---|
189 | return PyMember_SetOne((char *)obj, descr->d_member, value);
|
---|
190 | }
|
---|
191 |
|
---|
192 | static int
|
---|
193 | getset_set(PyGetSetDescrObject *descr, PyObject *obj, PyObject *value)
|
---|
194 | {
|
---|
195 | int res;
|
---|
196 |
|
---|
197 | if (descr_setcheck((PyDescrObject *)descr, obj, value, &res))
|
---|
198 | return res;
|
---|
199 | if (descr->d_getset->set != NULL)
|
---|
200 | return descr->d_getset->set(obj, value,
|
---|
201 | descr->d_getset->closure);
|
---|
202 | PyErr_Format(PyExc_AttributeError,
|
---|
203 | "attribute '%.300s' of '%.100s' objects is not writable",
|
---|
204 | descr_name((PyDescrObject *)descr),
|
---|
205 | descr->d_type->tp_name);
|
---|
206 | return -1;
|
---|
207 | }
|
---|
208 |
|
---|
209 | static PyObject *
|
---|
210 | methoddescr_call(PyMethodDescrObject *descr, PyObject *args, PyObject *kwds)
|
---|
211 | {
|
---|
212 | Py_ssize_t argc;
|
---|
213 | PyObject *self, *func, *result;
|
---|
214 |
|
---|
215 | /* Make sure that the first argument is acceptable as 'self' */
|
---|
216 | assert(PyTuple_Check(args));
|
---|
217 | argc = PyTuple_GET_SIZE(args);
|
---|
218 | if (argc < 1) {
|
---|
219 | PyErr_Format(PyExc_TypeError,
|
---|
220 | "descriptor '%.300s' of '%.100s' "
|
---|
221 | "object needs an argument",
|
---|
222 | descr_name((PyDescrObject *)descr),
|
---|
223 | descr->d_type->tp_name);
|
---|
224 | return NULL;
|
---|
225 | }
|
---|
226 | self = PyTuple_GET_ITEM(args, 0);
|
---|
227 | if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
|
---|
228 | PyErr_Format(PyExc_TypeError,
|
---|
229 | "descriptor '%.200s' "
|
---|
230 | "requires a '%.100s' object "
|
---|
231 | "but received a '%.100s'",
|
---|
232 | descr_name((PyDescrObject *)descr),
|
---|
233 | descr->d_type->tp_name,
|
---|
234 | self->ob_type->tp_name);
|
---|
235 | return NULL;
|
---|
236 | }
|
---|
237 |
|
---|
238 | func = PyCFunction_New(descr->d_method, self);
|
---|
239 | if (func == NULL)
|
---|
240 | return NULL;
|
---|
241 | args = PyTuple_GetSlice(args, 1, argc);
|
---|
242 | if (args == NULL) {
|
---|
243 | Py_DECREF(func);
|
---|
244 | return NULL;
|
---|
245 | }
|
---|
246 | result = PyEval_CallObjectWithKeywords(func, args, kwds);
|
---|
247 | Py_DECREF(args);
|
---|
248 | Py_DECREF(func);
|
---|
249 | return result;
|
---|
250 | }
|
---|
251 |
|
---|
252 | static PyObject *
|
---|
253 | classmethoddescr_call(PyMethodDescrObject *descr, PyObject *args,
|
---|
254 | PyObject *kwds)
|
---|
255 | {
|
---|
256 | PyObject *func, *result;
|
---|
257 |
|
---|
258 | func = PyCFunction_New(descr->d_method, (PyObject *)descr->d_type);
|
---|
259 | if (func == NULL)
|
---|
260 | return NULL;
|
---|
261 |
|
---|
262 | result = PyEval_CallObjectWithKeywords(func, args, kwds);
|
---|
263 | Py_DECREF(func);
|
---|
264 | return result;
|
---|
265 | }
|
---|
266 |
|
---|
267 | static PyObject *
|
---|
268 | wrapperdescr_call(PyWrapperDescrObject *descr, PyObject *args, PyObject *kwds)
|
---|
269 | {
|
---|
270 | Py_ssize_t argc;
|
---|
271 | PyObject *self, *func, *result;
|
---|
272 |
|
---|
273 | /* Make sure that the first argument is acceptable as 'self' */
|
---|
274 | assert(PyTuple_Check(args));
|
---|
275 | argc = PyTuple_GET_SIZE(args);
|
---|
276 | if (argc < 1) {
|
---|
277 | PyErr_Format(PyExc_TypeError,
|
---|
278 | "descriptor '%.300s' of '%.100s' "
|
---|
279 | "object needs an argument",
|
---|
280 | descr_name((PyDescrObject *)descr),
|
---|
281 | descr->d_type->tp_name);
|
---|
282 | return NULL;
|
---|
283 | }
|
---|
284 | self = PyTuple_GET_ITEM(args, 0);
|
---|
285 | if (!PyObject_IsInstance(self, (PyObject *)(descr->d_type))) {
|
---|
286 | PyErr_Format(PyExc_TypeError,
|
---|
287 | "descriptor '%.200s' "
|
---|
288 | "requires a '%.100s' object "
|
---|
289 | "but received a '%.100s'",
|
---|
290 | descr_name((PyDescrObject *)descr),
|
---|
291 | descr->d_type->tp_name,
|
---|
292 | self->ob_type->tp_name);
|
---|
293 | return NULL;
|
---|
294 | }
|
---|
295 |
|
---|
296 | func = PyWrapper_New((PyObject *)descr, self);
|
---|
297 | if (func == NULL)
|
---|
298 | return NULL;
|
---|
299 | args = PyTuple_GetSlice(args, 1, argc);
|
---|
300 | if (args == NULL) {
|
---|
301 | Py_DECREF(func);
|
---|
302 | return NULL;
|
---|
303 | }
|
---|
304 | result = PyEval_CallObjectWithKeywords(func, args, kwds);
|
---|
305 | Py_DECREF(args);
|
---|
306 | Py_DECREF(func);
|
---|
307 | return result;
|
---|
308 | }
|
---|
309 |
|
---|
310 | static PyObject *
|
---|
311 | method_get_doc(PyMethodDescrObject *descr, void *closure)
|
---|
312 | {
|
---|
313 | if (descr->d_method->ml_doc == NULL) {
|
---|
314 | Py_INCREF(Py_None);
|
---|
315 | return Py_None;
|
---|
316 | }
|
---|
317 | return PyString_FromString(descr->d_method->ml_doc);
|
---|
318 | }
|
---|
319 |
|
---|
320 | static PyMemberDef descr_members[] = {
|
---|
321 | {"__objclass__", T_OBJECT, offsetof(PyDescrObject, d_type), READONLY},
|
---|
322 | {"__name__", T_OBJECT, offsetof(PyDescrObject, d_name), READONLY},
|
---|
323 | {0}
|
---|
324 | };
|
---|
325 |
|
---|
326 | static PyGetSetDef method_getset[] = {
|
---|
327 | {"__doc__", (getter)method_get_doc},
|
---|
328 | {0}
|
---|
329 | };
|
---|
330 |
|
---|
331 | static PyObject *
|
---|
332 | member_get_doc(PyMemberDescrObject *descr, void *closure)
|
---|
333 | {
|
---|
334 | if (descr->d_member->doc == NULL) {
|
---|
335 | Py_INCREF(Py_None);
|
---|
336 | return Py_None;
|
---|
337 | }
|
---|
338 | return PyString_FromString(descr->d_member->doc);
|
---|
339 | }
|
---|
340 |
|
---|
341 | static PyGetSetDef member_getset[] = {
|
---|
342 | {"__doc__", (getter)member_get_doc},
|
---|
343 | {0}
|
---|
344 | };
|
---|
345 |
|
---|
346 | static PyObject *
|
---|
347 | getset_get_doc(PyGetSetDescrObject *descr, void *closure)
|
---|
348 | {
|
---|
349 | if (descr->d_getset->doc == NULL) {
|
---|
350 | Py_INCREF(Py_None);
|
---|
351 | return Py_None;
|
---|
352 | }
|
---|
353 | return PyString_FromString(descr->d_getset->doc);
|
---|
354 | }
|
---|
355 |
|
---|
356 | static PyGetSetDef getset_getset[] = {
|
---|
357 | {"__doc__", (getter)getset_get_doc},
|
---|
358 | {0}
|
---|
359 | };
|
---|
360 |
|
---|
361 | static PyObject *
|
---|
362 | wrapperdescr_get_doc(PyWrapperDescrObject *descr, void *closure)
|
---|
363 | {
|
---|
364 | if (descr->d_base->doc == NULL) {
|
---|
365 | Py_INCREF(Py_None);
|
---|
366 | return Py_None;
|
---|
367 | }
|
---|
368 | return PyString_FromString(descr->d_base->doc);
|
---|
369 | }
|
---|
370 |
|
---|
371 | static PyGetSetDef wrapperdescr_getset[] = {
|
---|
372 | {"__doc__", (getter)wrapperdescr_get_doc},
|
---|
373 | {0}
|
---|
374 | };
|
---|
375 |
|
---|
376 | static int
|
---|
377 | descr_traverse(PyObject *self, visitproc visit, void *arg)
|
---|
378 | {
|
---|
379 | PyDescrObject *descr = (PyDescrObject *)self;
|
---|
380 | Py_VISIT(descr->d_type);
|
---|
381 | return 0;
|
---|
382 | }
|
---|
383 |
|
---|
384 | static PyTypeObject PyMethodDescr_Type = {
|
---|
385 | PyObject_HEAD_INIT(&PyType_Type)
|
---|
386 | 0,
|
---|
387 | "method_descriptor",
|
---|
388 | sizeof(PyMethodDescrObject),
|
---|
389 | 0,
|
---|
390 | (destructor)descr_dealloc, /* tp_dealloc */
|
---|
391 | 0, /* tp_print */
|
---|
392 | 0, /* tp_getattr */
|
---|
393 | 0, /* tp_setattr */
|
---|
394 | 0, /* tp_compare */
|
---|
395 | (reprfunc)method_repr, /* tp_repr */
|
---|
396 | 0, /* tp_as_number */
|
---|
397 | 0, /* tp_as_sequence */
|
---|
398 | 0, /* tp_as_mapping */
|
---|
399 | 0, /* tp_hash */
|
---|
400 | (ternaryfunc)methoddescr_call, /* tp_call */
|
---|
401 | 0, /* tp_str */
|
---|
402 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
403 | 0, /* tp_setattro */
|
---|
404 | 0, /* tp_as_buffer */
|
---|
405 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
406 | 0, /* tp_doc */
|
---|
407 | descr_traverse, /* tp_traverse */
|
---|
408 | 0, /* tp_clear */
|
---|
409 | 0, /* tp_richcompare */
|
---|
410 | 0, /* tp_weaklistoffset */
|
---|
411 | 0, /* tp_iter */
|
---|
412 | 0, /* tp_iternext */
|
---|
413 | 0, /* tp_methods */
|
---|
414 | descr_members, /* tp_members */
|
---|
415 | method_getset, /* tp_getset */
|
---|
416 | 0, /* tp_base */
|
---|
417 | 0, /* tp_dict */
|
---|
418 | (descrgetfunc)method_get, /* tp_descr_get */
|
---|
419 | 0, /* tp_descr_set */
|
---|
420 | };
|
---|
421 |
|
---|
422 | /* This is for METH_CLASS in C, not for "f = classmethod(f)" in Python! */
|
---|
423 | static PyTypeObject PyClassMethodDescr_Type = {
|
---|
424 | PyObject_HEAD_INIT(&PyType_Type)
|
---|
425 | 0,
|
---|
426 | "classmethod_descriptor",
|
---|
427 | sizeof(PyMethodDescrObject),
|
---|
428 | 0,
|
---|
429 | (destructor)descr_dealloc, /* tp_dealloc */
|
---|
430 | 0, /* tp_print */
|
---|
431 | 0, /* tp_getattr */
|
---|
432 | 0, /* tp_setattr */
|
---|
433 | 0, /* tp_compare */
|
---|
434 | (reprfunc)method_repr, /* tp_repr */
|
---|
435 | 0, /* tp_as_number */
|
---|
436 | 0, /* tp_as_sequence */
|
---|
437 | 0, /* tp_as_mapping */
|
---|
438 | 0, /* tp_hash */
|
---|
439 | (ternaryfunc)classmethoddescr_call, /* tp_call */
|
---|
440 | 0, /* tp_str */
|
---|
441 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
442 | 0, /* tp_setattro */
|
---|
443 | 0, /* tp_as_buffer */
|
---|
444 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
445 | 0, /* tp_doc */
|
---|
446 | descr_traverse, /* tp_traverse */
|
---|
447 | 0, /* tp_clear */
|
---|
448 | 0, /* tp_richcompare */
|
---|
449 | 0, /* tp_weaklistoffset */
|
---|
450 | 0, /* tp_iter */
|
---|
451 | 0, /* tp_iternext */
|
---|
452 | 0, /* tp_methods */
|
---|
453 | descr_members, /* tp_members */
|
---|
454 | method_getset, /* tp_getset */
|
---|
455 | 0, /* tp_base */
|
---|
456 | 0, /* tp_dict */
|
---|
457 | (descrgetfunc)classmethod_get, /* tp_descr_get */
|
---|
458 | 0, /* tp_descr_set */
|
---|
459 | };
|
---|
460 |
|
---|
461 | static PyTypeObject PyMemberDescr_Type = {
|
---|
462 | PyObject_HEAD_INIT(&PyType_Type)
|
---|
463 | 0,
|
---|
464 | "member_descriptor",
|
---|
465 | sizeof(PyMemberDescrObject),
|
---|
466 | 0,
|
---|
467 | (destructor)descr_dealloc, /* tp_dealloc */
|
---|
468 | 0, /* tp_print */
|
---|
469 | 0, /* tp_getattr */
|
---|
470 | 0, /* tp_setattr */
|
---|
471 | 0, /* tp_compare */
|
---|
472 | (reprfunc)member_repr, /* tp_repr */
|
---|
473 | 0, /* tp_as_number */
|
---|
474 | 0, /* tp_as_sequence */
|
---|
475 | 0, /* tp_as_mapping */
|
---|
476 | 0, /* tp_hash */
|
---|
477 | 0, /* tp_call */
|
---|
478 | 0, /* tp_str */
|
---|
479 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
480 | 0, /* tp_setattro */
|
---|
481 | 0, /* tp_as_buffer */
|
---|
482 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
483 | 0, /* tp_doc */
|
---|
484 | descr_traverse, /* tp_traverse */
|
---|
485 | 0, /* tp_clear */
|
---|
486 | 0, /* tp_richcompare */
|
---|
487 | 0, /* tp_weaklistoffset */
|
---|
488 | 0, /* tp_iter */
|
---|
489 | 0, /* tp_iternext */
|
---|
490 | 0, /* tp_methods */
|
---|
491 | descr_members, /* tp_members */
|
---|
492 | member_getset, /* tp_getset */
|
---|
493 | 0, /* tp_base */
|
---|
494 | 0, /* tp_dict */
|
---|
495 | (descrgetfunc)member_get, /* tp_descr_get */
|
---|
496 | (descrsetfunc)member_set, /* tp_descr_set */
|
---|
497 | };
|
---|
498 |
|
---|
499 | static PyTypeObject PyGetSetDescr_Type = {
|
---|
500 | PyObject_HEAD_INIT(&PyType_Type)
|
---|
501 | 0,
|
---|
502 | "getset_descriptor",
|
---|
503 | sizeof(PyGetSetDescrObject),
|
---|
504 | 0,
|
---|
505 | (destructor)descr_dealloc, /* tp_dealloc */
|
---|
506 | 0, /* tp_print */
|
---|
507 | 0, /* tp_getattr */
|
---|
508 | 0, /* tp_setattr */
|
---|
509 | 0, /* tp_compare */
|
---|
510 | (reprfunc)getset_repr, /* tp_repr */
|
---|
511 | 0, /* tp_as_number */
|
---|
512 | 0, /* tp_as_sequence */
|
---|
513 | 0, /* tp_as_mapping */
|
---|
514 | 0, /* tp_hash */
|
---|
515 | 0, /* tp_call */
|
---|
516 | 0, /* tp_str */
|
---|
517 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
518 | 0, /* tp_setattro */
|
---|
519 | 0, /* tp_as_buffer */
|
---|
520 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
521 | 0, /* tp_doc */
|
---|
522 | descr_traverse, /* tp_traverse */
|
---|
523 | 0, /* tp_clear */
|
---|
524 | 0, /* tp_richcompare */
|
---|
525 | 0, /* tp_weaklistoffset */
|
---|
526 | 0, /* tp_iter */
|
---|
527 | 0, /* tp_iternext */
|
---|
528 | 0, /* tp_methods */
|
---|
529 | descr_members, /* tp_members */
|
---|
530 | getset_getset, /* tp_getset */
|
---|
531 | 0, /* tp_base */
|
---|
532 | 0, /* tp_dict */
|
---|
533 | (descrgetfunc)getset_get, /* tp_descr_get */
|
---|
534 | (descrsetfunc)getset_set, /* tp_descr_set */
|
---|
535 | };
|
---|
536 |
|
---|
537 | PyTypeObject PyWrapperDescr_Type = {
|
---|
538 | PyObject_HEAD_INIT(&PyType_Type)
|
---|
539 | 0,
|
---|
540 | "wrapper_descriptor",
|
---|
541 | sizeof(PyWrapperDescrObject),
|
---|
542 | 0,
|
---|
543 | (destructor)descr_dealloc, /* tp_dealloc */
|
---|
544 | 0, /* tp_print */
|
---|
545 | 0, /* tp_getattr */
|
---|
546 | 0, /* tp_setattr */
|
---|
547 | 0, /* tp_compare */
|
---|
548 | (reprfunc)wrapperdescr_repr, /* tp_repr */
|
---|
549 | 0, /* tp_as_number */
|
---|
550 | 0, /* tp_as_sequence */
|
---|
551 | 0, /* tp_as_mapping */
|
---|
552 | 0, /* tp_hash */
|
---|
553 | (ternaryfunc)wrapperdescr_call, /* tp_call */
|
---|
554 | 0, /* tp_str */
|
---|
555 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
556 | 0, /* tp_setattro */
|
---|
557 | 0, /* tp_as_buffer */
|
---|
558 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
559 | 0, /* tp_doc */
|
---|
560 | descr_traverse, /* tp_traverse */
|
---|
561 | 0, /* tp_clear */
|
---|
562 | 0, /* tp_richcompare */
|
---|
563 | 0, /* tp_weaklistoffset */
|
---|
564 | 0, /* tp_iter */
|
---|
565 | 0, /* tp_iternext */
|
---|
566 | 0, /* tp_methods */
|
---|
567 | descr_members, /* tp_members */
|
---|
568 | wrapperdescr_getset, /* tp_getset */
|
---|
569 | 0, /* tp_base */
|
---|
570 | 0, /* tp_dict */
|
---|
571 | (descrgetfunc)wrapperdescr_get, /* tp_descr_get */
|
---|
572 | 0, /* tp_descr_set */
|
---|
573 | };
|
---|
574 |
|
---|
575 | static PyDescrObject *
|
---|
576 | descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
|
---|
577 | {
|
---|
578 | PyDescrObject *descr;
|
---|
579 |
|
---|
580 | descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
|
---|
581 | if (descr != NULL) {
|
---|
582 | Py_XINCREF(type);
|
---|
583 | descr->d_type = type;
|
---|
584 | descr->d_name = PyString_InternFromString(name);
|
---|
585 | if (descr->d_name == NULL) {
|
---|
586 | Py_DECREF(descr);
|
---|
587 | descr = NULL;
|
---|
588 | }
|
---|
589 | }
|
---|
590 | return descr;
|
---|
591 | }
|
---|
592 |
|
---|
593 | PyObject *
|
---|
594 | PyDescr_NewMethod(PyTypeObject *type, PyMethodDef *method)
|
---|
595 | {
|
---|
596 | PyMethodDescrObject *descr;
|
---|
597 |
|
---|
598 | descr = (PyMethodDescrObject *)descr_new(&PyMethodDescr_Type,
|
---|
599 | type, method->ml_name);
|
---|
600 | if (descr != NULL)
|
---|
601 | descr->d_method = method;
|
---|
602 | return (PyObject *)descr;
|
---|
603 | }
|
---|
604 |
|
---|
605 | PyObject *
|
---|
606 | PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)
|
---|
607 | {
|
---|
608 | PyMethodDescrObject *descr;
|
---|
609 |
|
---|
610 | descr = (PyMethodDescrObject *)descr_new(&PyClassMethodDescr_Type,
|
---|
611 | type, method->ml_name);
|
---|
612 | if (descr != NULL)
|
---|
613 | descr->d_method = method;
|
---|
614 | return (PyObject *)descr;
|
---|
615 | }
|
---|
616 |
|
---|
617 | PyObject *
|
---|
618 | PyDescr_NewMember(PyTypeObject *type, PyMemberDef *member)
|
---|
619 | {
|
---|
620 | PyMemberDescrObject *descr;
|
---|
621 |
|
---|
622 | descr = (PyMemberDescrObject *)descr_new(&PyMemberDescr_Type,
|
---|
623 | type, member->name);
|
---|
624 | if (descr != NULL)
|
---|
625 | descr->d_member = member;
|
---|
626 | return (PyObject *)descr;
|
---|
627 | }
|
---|
628 |
|
---|
629 | PyObject *
|
---|
630 | PyDescr_NewGetSet(PyTypeObject *type, PyGetSetDef *getset)
|
---|
631 | {
|
---|
632 | PyGetSetDescrObject *descr;
|
---|
633 |
|
---|
634 | descr = (PyGetSetDescrObject *)descr_new(&PyGetSetDescr_Type,
|
---|
635 | type, getset->name);
|
---|
636 | if (descr != NULL)
|
---|
637 | descr->d_getset = getset;
|
---|
638 | return (PyObject *)descr;
|
---|
639 | }
|
---|
640 |
|
---|
641 | PyObject *
|
---|
642 | PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *base, void *wrapped)
|
---|
643 | {
|
---|
644 | PyWrapperDescrObject *descr;
|
---|
645 |
|
---|
646 | descr = (PyWrapperDescrObject *)descr_new(&PyWrapperDescr_Type,
|
---|
647 | type, base->name);
|
---|
648 | if (descr != NULL) {
|
---|
649 | descr->d_base = base;
|
---|
650 | descr->d_wrapped = wrapped;
|
---|
651 | }
|
---|
652 | return (PyObject *)descr;
|
---|
653 | }
|
---|
654 |
|
---|
655 |
|
---|
656 | /* --- Readonly proxy for dictionaries (actually any mapping) --- */
|
---|
657 |
|
---|
658 | /* This has no reason to be in this file except that adding new files is a
|
---|
659 | bit of a pain */
|
---|
660 |
|
---|
661 | typedef struct {
|
---|
662 | PyObject_HEAD
|
---|
663 | PyObject *dict;
|
---|
664 | } proxyobject;
|
---|
665 |
|
---|
666 | static Py_ssize_t
|
---|
667 | proxy_len(proxyobject *pp)
|
---|
668 | {
|
---|
669 | return PyObject_Size(pp->dict);
|
---|
670 | }
|
---|
671 |
|
---|
672 | static PyObject *
|
---|
673 | proxy_getitem(proxyobject *pp, PyObject *key)
|
---|
674 | {
|
---|
675 | return PyObject_GetItem(pp->dict, key);
|
---|
676 | }
|
---|
677 |
|
---|
678 | static PyMappingMethods proxy_as_mapping = {
|
---|
679 | (lenfunc)proxy_len, /* mp_length */
|
---|
680 | (binaryfunc)proxy_getitem, /* mp_subscript */
|
---|
681 | 0, /* mp_ass_subscript */
|
---|
682 | };
|
---|
683 |
|
---|
684 | static int
|
---|
685 | proxy_contains(proxyobject *pp, PyObject *key)
|
---|
686 | {
|
---|
687 | return PyDict_Contains(pp->dict, key);
|
---|
688 | }
|
---|
689 |
|
---|
690 | static PySequenceMethods proxy_as_sequence = {
|
---|
691 | 0, /* sq_length */
|
---|
692 | 0, /* sq_concat */
|
---|
693 | 0, /* sq_repeat */
|
---|
694 | 0, /* sq_item */
|
---|
695 | 0, /* sq_slice */
|
---|
696 | 0, /* sq_ass_item */
|
---|
697 | 0, /* sq_ass_slice */
|
---|
698 | (objobjproc)proxy_contains, /* sq_contains */
|
---|
699 | 0, /* sq_inplace_concat */
|
---|
700 | 0, /* sq_inplace_repeat */
|
---|
701 | };
|
---|
702 |
|
---|
703 | static PyObject *
|
---|
704 | proxy_has_key(proxyobject *pp, PyObject *key)
|
---|
705 | {
|
---|
706 | int res = PyDict_Contains(pp->dict, key);
|
---|
707 | if (res < 0)
|
---|
708 | return NULL;
|
---|
709 | return PyBool_FromLong(res);
|
---|
710 | }
|
---|
711 |
|
---|
712 | static PyObject *
|
---|
713 | proxy_get(proxyobject *pp, PyObject *args)
|
---|
714 | {
|
---|
715 | PyObject *key, *def = Py_None;
|
---|
716 |
|
---|
717 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &def))
|
---|
718 | return NULL;
|
---|
719 | return PyObject_CallMethod(pp->dict, "get", "(OO)", key, def);
|
---|
720 | }
|
---|
721 |
|
---|
722 | static PyObject *
|
---|
723 | proxy_keys(proxyobject *pp)
|
---|
724 | {
|
---|
725 | return PyMapping_Keys(pp->dict);
|
---|
726 | }
|
---|
727 |
|
---|
728 | static PyObject *
|
---|
729 | proxy_values(proxyobject *pp)
|
---|
730 | {
|
---|
731 | return PyMapping_Values(pp->dict);
|
---|
732 | }
|
---|
733 |
|
---|
734 | static PyObject *
|
---|
735 | proxy_items(proxyobject *pp)
|
---|
736 | {
|
---|
737 | return PyMapping_Items(pp->dict);
|
---|
738 | }
|
---|
739 |
|
---|
740 | static PyObject *
|
---|
741 | proxy_iterkeys(proxyobject *pp)
|
---|
742 | {
|
---|
743 | return PyObject_CallMethod(pp->dict, "iterkeys", NULL);
|
---|
744 | }
|
---|
745 |
|
---|
746 | static PyObject *
|
---|
747 | proxy_itervalues(proxyobject *pp)
|
---|
748 | {
|
---|
749 | return PyObject_CallMethod(pp->dict, "itervalues", NULL);
|
---|
750 | }
|
---|
751 |
|
---|
752 | static PyObject *
|
---|
753 | proxy_iteritems(proxyobject *pp)
|
---|
754 | {
|
---|
755 | return PyObject_CallMethod(pp->dict, "iteritems", NULL);
|
---|
756 | }
|
---|
757 | static PyObject *
|
---|
758 | proxy_copy(proxyobject *pp)
|
---|
759 | {
|
---|
760 | return PyObject_CallMethod(pp->dict, "copy", NULL);
|
---|
761 | }
|
---|
762 |
|
---|
763 | static PyMethodDef proxy_methods[] = {
|
---|
764 | {"has_key", (PyCFunction)proxy_has_key, METH_O,
|
---|
765 | PyDoc_STR("D.has_key(k) -> True if D has a key k, else False")},
|
---|
766 | {"get", (PyCFunction)proxy_get, METH_VARARGS,
|
---|
767 | PyDoc_STR("D.get(k[,d]) -> D[k] if D.has_key(k), else d."
|
---|
768 | " d defaults to None.")},
|
---|
769 | {"keys", (PyCFunction)proxy_keys, METH_NOARGS,
|
---|
770 | PyDoc_STR("D.keys() -> list of D's keys")},
|
---|
771 | {"values", (PyCFunction)proxy_values, METH_NOARGS,
|
---|
772 | PyDoc_STR("D.values() -> list of D's values")},
|
---|
773 | {"items", (PyCFunction)proxy_items, METH_NOARGS,
|
---|
774 | PyDoc_STR("D.items() -> list of D's (key, value) pairs, as 2-tuples")},
|
---|
775 | {"iterkeys", (PyCFunction)proxy_iterkeys, METH_NOARGS,
|
---|
776 | PyDoc_STR("D.iterkeys() -> an iterator over the keys of D")},
|
---|
777 | {"itervalues",(PyCFunction)proxy_itervalues, METH_NOARGS,
|
---|
778 | PyDoc_STR("D.itervalues() -> an iterator over the values of D")},
|
---|
779 | {"iteritems", (PyCFunction)proxy_iteritems, METH_NOARGS,
|
---|
780 | PyDoc_STR("D.iteritems() ->"
|
---|
781 | " an iterator over the (key, value) items of D")},
|
---|
782 | {"copy", (PyCFunction)proxy_copy, METH_NOARGS,
|
---|
783 | PyDoc_STR("D.copy() -> a shallow copy of D")},
|
---|
784 | {0}
|
---|
785 | };
|
---|
786 |
|
---|
787 | static void
|
---|
788 | proxy_dealloc(proxyobject *pp)
|
---|
789 | {
|
---|
790 | _PyObject_GC_UNTRACK(pp);
|
---|
791 | Py_DECREF(pp->dict);
|
---|
792 | PyObject_GC_Del(pp);
|
---|
793 | }
|
---|
794 |
|
---|
795 | static PyObject *
|
---|
796 | proxy_getiter(proxyobject *pp)
|
---|
797 | {
|
---|
798 | return PyObject_GetIter(pp->dict);
|
---|
799 | }
|
---|
800 |
|
---|
801 | static PyObject *
|
---|
802 | proxy_str(proxyobject *pp)
|
---|
803 | {
|
---|
804 | return PyObject_Str(pp->dict);
|
---|
805 | }
|
---|
806 |
|
---|
807 | static int
|
---|
808 | proxy_traverse(PyObject *self, visitproc visit, void *arg)
|
---|
809 | {
|
---|
810 | proxyobject *pp = (proxyobject *)self;
|
---|
811 | Py_VISIT(pp->dict);
|
---|
812 | return 0;
|
---|
813 | }
|
---|
814 |
|
---|
815 | static int
|
---|
816 | proxy_compare(proxyobject *v, PyObject *w)
|
---|
817 | {
|
---|
818 | return PyObject_Compare(v->dict, w);
|
---|
819 | }
|
---|
820 |
|
---|
821 | static PyObject *
|
---|
822 | proxy_richcompare(proxyobject *v, PyObject *w, int op)
|
---|
823 | {
|
---|
824 | return PyObject_RichCompare(v->dict, w, op);
|
---|
825 | }
|
---|
826 |
|
---|
827 | static PyTypeObject proxytype = {
|
---|
828 | PyObject_HEAD_INIT(&PyType_Type)
|
---|
829 | 0, /* ob_size */
|
---|
830 | "dictproxy", /* tp_name */
|
---|
831 | sizeof(proxyobject), /* tp_basicsize */
|
---|
832 | 0, /* tp_itemsize */
|
---|
833 | /* methods */
|
---|
834 | (destructor)proxy_dealloc, /* tp_dealloc */
|
---|
835 | 0, /* tp_print */
|
---|
836 | 0, /* tp_getattr */
|
---|
837 | 0, /* tp_setattr */
|
---|
838 | (cmpfunc)proxy_compare, /* tp_compare */
|
---|
839 | 0, /* tp_repr */
|
---|
840 | 0, /* tp_as_number */
|
---|
841 | &proxy_as_sequence, /* tp_as_sequence */
|
---|
842 | &proxy_as_mapping, /* tp_as_mapping */
|
---|
843 | 0, /* tp_hash */
|
---|
844 | 0, /* tp_call */
|
---|
845 | (reprfunc)proxy_str, /* tp_str */
|
---|
846 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
847 | 0, /* tp_setattro */
|
---|
848 | 0, /* tp_as_buffer */
|
---|
849 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
850 | 0, /* tp_doc */
|
---|
851 | proxy_traverse, /* tp_traverse */
|
---|
852 | 0, /* tp_clear */
|
---|
853 | (richcmpfunc)proxy_richcompare, /* tp_richcompare */
|
---|
854 | 0, /* tp_weaklistoffset */
|
---|
855 | (getiterfunc)proxy_getiter, /* tp_iter */
|
---|
856 | 0, /* tp_iternext */
|
---|
857 | proxy_methods, /* tp_methods */
|
---|
858 | 0, /* tp_members */
|
---|
859 | 0, /* tp_getset */
|
---|
860 | 0, /* tp_base */
|
---|
861 | 0, /* tp_dict */
|
---|
862 | 0, /* tp_descr_get */
|
---|
863 | 0, /* tp_descr_set */
|
---|
864 | };
|
---|
865 |
|
---|
866 | PyObject *
|
---|
867 | PyDictProxy_New(PyObject *dict)
|
---|
868 | {
|
---|
869 | proxyobject *pp;
|
---|
870 |
|
---|
871 | pp = PyObject_GC_New(proxyobject, &proxytype);
|
---|
872 | if (pp != NULL) {
|
---|
873 | Py_INCREF(dict);
|
---|
874 | pp->dict = dict;
|
---|
875 | _PyObject_GC_TRACK(pp);
|
---|
876 | }
|
---|
877 | return (PyObject *)pp;
|
---|
878 | }
|
---|
879 |
|
---|
880 |
|
---|
881 | /* --- Wrapper object for "slot" methods --- */
|
---|
882 |
|
---|
883 | /* This has no reason to be in this file except that adding new files is a
|
---|
884 | bit of a pain */
|
---|
885 |
|
---|
886 | typedef struct {
|
---|
887 | PyObject_HEAD
|
---|
888 | PyWrapperDescrObject *descr;
|
---|
889 | PyObject *self;
|
---|
890 | } wrapperobject;
|
---|
891 |
|
---|
892 | static void
|
---|
893 | wrapper_dealloc(wrapperobject *wp)
|
---|
894 | {
|
---|
895 | PyObject_GC_UnTrack(wp);
|
---|
896 | Py_TRASHCAN_SAFE_BEGIN(wp)
|
---|
897 | Py_XDECREF(wp->descr);
|
---|
898 | Py_XDECREF(wp->self);
|
---|
899 | PyObject_GC_Del(wp);
|
---|
900 | Py_TRASHCAN_SAFE_END(wp)
|
---|
901 | }
|
---|
902 |
|
---|
903 | static int
|
---|
904 | wrapper_compare(wrapperobject *a, wrapperobject *b)
|
---|
905 | {
|
---|
906 | if (a->descr == b->descr)
|
---|
907 | return PyObject_Compare(a->self, b->self);
|
---|
908 | else
|
---|
909 | return (a->descr < b->descr) ? -1 : 1;
|
---|
910 | }
|
---|
911 |
|
---|
912 | static long
|
---|
913 | wrapper_hash(wrapperobject *wp)
|
---|
914 | {
|
---|
915 | int x, y;
|
---|
916 | x = _Py_HashPointer(wp->descr);
|
---|
917 | if (x == -1)
|
---|
918 | return -1;
|
---|
919 | y = PyObject_Hash(wp->self);
|
---|
920 | if (y == -1)
|
---|
921 | return -1;
|
---|
922 | x = x ^ y;
|
---|
923 | if (x == -1)
|
---|
924 | x = -2;
|
---|
925 | return x;
|
---|
926 | }
|
---|
927 |
|
---|
928 | static PyObject *
|
---|
929 | wrapper_repr(wrapperobject *wp)
|
---|
930 | {
|
---|
931 | return PyString_FromFormat("<method-wrapper '%s' of %s object at %p>",
|
---|
932 | wp->descr->d_base->name,
|
---|
933 | wp->self->ob_type->tp_name,
|
---|
934 | wp->self);
|
---|
935 | }
|
---|
936 |
|
---|
937 | static PyMemberDef wrapper_members[] = {
|
---|
938 | {"__self__", T_OBJECT, offsetof(wrapperobject, self), READONLY},
|
---|
939 | {0}
|
---|
940 | };
|
---|
941 |
|
---|
942 | static PyObject *
|
---|
943 | wrapper_objclass(wrapperobject *wp)
|
---|
944 | {
|
---|
945 | PyObject *c = (PyObject *)wp->descr->d_type;
|
---|
946 |
|
---|
947 | Py_INCREF(c);
|
---|
948 | return c;
|
---|
949 | }
|
---|
950 |
|
---|
951 | static PyObject *
|
---|
952 | wrapper_name(wrapperobject *wp)
|
---|
953 | {
|
---|
954 | char *s = wp->descr->d_base->name;
|
---|
955 |
|
---|
956 | return PyString_FromString(s);
|
---|
957 | }
|
---|
958 |
|
---|
959 | static PyObject *
|
---|
960 | wrapper_doc(wrapperobject *wp)
|
---|
961 | {
|
---|
962 | char *s = wp->descr->d_base->doc;
|
---|
963 |
|
---|
964 | if (s == NULL) {
|
---|
965 | Py_INCREF(Py_None);
|
---|
966 | return Py_None;
|
---|
967 | }
|
---|
968 | else {
|
---|
969 | return PyString_FromString(s);
|
---|
970 | }
|
---|
971 | }
|
---|
972 |
|
---|
973 | static PyGetSetDef wrapper_getsets[] = {
|
---|
974 | {"__objclass__", (getter)wrapper_objclass},
|
---|
975 | {"__name__", (getter)wrapper_name},
|
---|
976 | {"__doc__", (getter)wrapper_doc},
|
---|
977 | {0}
|
---|
978 | };
|
---|
979 |
|
---|
980 | static PyObject *
|
---|
981 | wrapper_call(wrapperobject *wp, PyObject *args, PyObject *kwds)
|
---|
982 | {
|
---|
983 | wrapperfunc wrapper = wp->descr->d_base->wrapper;
|
---|
984 | PyObject *self = wp->self;
|
---|
985 |
|
---|
986 | if (wp->descr->d_base->flags & PyWrapperFlag_KEYWORDS) {
|
---|
987 | wrapperfunc_kwds wk = (wrapperfunc_kwds)wrapper;
|
---|
988 | return (*wk)(self, args, wp->descr->d_wrapped, kwds);
|
---|
989 | }
|
---|
990 |
|
---|
991 | if (kwds != NULL && (!PyDict_Check(kwds) || PyDict_Size(kwds) != 0)) {
|
---|
992 | PyErr_Format(PyExc_TypeError,
|
---|
993 | "wrapper %s doesn't take keyword arguments",
|
---|
994 | wp->descr->d_base->name);
|
---|
995 | return NULL;
|
---|
996 | }
|
---|
997 | return (*wrapper)(self, args, wp->descr->d_wrapped);
|
---|
998 | }
|
---|
999 |
|
---|
1000 | static int
|
---|
1001 | wrapper_traverse(PyObject *self, visitproc visit, void *arg)
|
---|
1002 | {
|
---|
1003 | wrapperobject *wp = (wrapperobject *)self;
|
---|
1004 | Py_VISIT(wp->descr);
|
---|
1005 | Py_VISIT(wp->self);
|
---|
1006 | return 0;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | static PyTypeObject wrappertype = {
|
---|
1010 | PyObject_HEAD_INIT(&PyType_Type)
|
---|
1011 | 0, /* ob_size */
|
---|
1012 | "method-wrapper", /* tp_name */
|
---|
1013 | sizeof(wrapperobject), /* tp_basicsize */
|
---|
1014 | 0, /* tp_itemsize */
|
---|
1015 | /* methods */
|
---|
1016 | (destructor)wrapper_dealloc, /* tp_dealloc */
|
---|
1017 | 0, /* tp_print */
|
---|
1018 | 0, /* tp_getattr */
|
---|
1019 | 0, /* tp_setattr */
|
---|
1020 | (cmpfunc)wrapper_compare, /* tp_compare */
|
---|
1021 | (reprfunc)wrapper_repr, /* tp_repr */
|
---|
1022 | 0, /* tp_as_number */
|
---|
1023 | 0, /* tp_as_sequence */
|
---|
1024 | 0, /* tp_as_mapping */
|
---|
1025 | (hashfunc)wrapper_hash, /* tp_hash */
|
---|
1026 | (ternaryfunc)wrapper_call, /* tp_call */
|
---|
1027 | 0, /* tp_str */
|
---|
1028 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
1029 | 0, /* tp_setattro */
|
---|
1030 | 0, /* tp_as_buffer */
|
---|
1031 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
1032 | 0, /* tp_doc */
|
---|
1033 | wrapper_traverse, /* tp_traverse */
|
---|
1034 | 0, /* tp_clear */
|
---|
1035 | 0, /* tp_richcompare */
|
---|
1036 | 0, /* tp_weaklistoffset */
|
---|
1037 | 0, /* tp_iter */
|
---|
1038 | 0, /* tp_iternext */
|
---|
1039 | 0, /* tp_methods */
|
---|
1040 | wrapper_members, /* tp_members */
|
---|
1041 | wrapper_getsets, /* tp_getset */
|
---|
1042 | 0, /* tp_base */
|
---|
1043 | 0, /* tp_dict */
|
---|
1044 | 0, /* tp_descr_get */
|
---|
1045 | 0, /* tp_descr_set */
|
---|
1046 | };
|
---|
1047 |
|
---|
1048 | PyObject *
|
---|
1049 | PyWrapper_New(PyObject *d, PyObject *self)
|
---|
1050 | {
|
---|
1051 | wrapperobject *wp;
|
---|
1052 | PyWrapperDescrObject *descr;
|
---|
1053 |
|
---|
1054 | assert(PyObject_TypeCheck(d, &PyWrapperDescr_Type));
|
---|
1055 | descr = (PyWrapperDescrObject *)d;
|
---|
1056 | assert(PyObject_IsInstance(self, (PyObject *)(descr->d_type)));
|
---|
1057 |
|
---|
1058 | wp = PyObject_GC_New(wrapperobject, &wrappertype);
|
---|
1059 | if (wp != NULL) {
|
---|
1060 | Py_INCREF(descr);
|
---|
1061 | wp->descr = descr;
|
---|
1062 | Py_INCREF(self);
|
---|
1063 | wp->self = self;
|
---|
1064 | _PyObject_GC_TRACK(wp);
|
---|
1065 | }
|
---|
1066 | return (PyObject *)wp;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 |
|
---|
1070 | /* A built-in 'property' type */
|
---|
1071 |
|
---|
1072 | /*
|
---|
1073 | class property(object):
|
---|
1074 |
|
---|
1075 | def __init__(self, fget=None, fset=None, fdel=None, doc=None):
|
---|
1076 | if doc is None and fget is not None and hasattr(fget, "__doc__"):
|
---|
1077 | doc = fget.__doc__
|
---|
1078 | self.__get = fget
|
---|
1079 | self.__set = fset
|
---|
1080 | self.__del = fdel
|
---|
1081 | self.__doc__ = doc
|
---|
1082 |
|
---|
1083 | def __get__(self, inst, type=None):
|
---|
1084 | if inst is None:
|
---|
1085 | return self
|
---|
1086 | if self.__get is None:
|
---|
1087 | raise AttributeError, "unreadable attribute"
|
---|
1088 | return self.__get(inst)
|
---|
1089 |
|
---|
1090 | def __set__(self, inst, value):
|
---|
1091 | if self.__set is None:
|
---|
1092 | raise AttributeError, "can't set attribute"
|
---|
1093 | return self.__set(inst, value)
|
---|
1094 |
|
---|
1095 | def __delete__(self, inst):
|
---|
1096 | if self.__del is None:
|
---|
1097 | raise AttributeError, "can't delete attribute"
|
---|
1098 | return self.__del(inst)
|
---|
1099 |
|
---|
1100 | */
|
---|
1101 |
|
---|
1102 | typedef struct {
|
---|
1103 | PyObject_HEAD
|
---|
1104 | PyObject *prop_get;
|
---|
1105 | PyObject *prop_set;
|
---|
1106 | PyObject *prop_del;
|
---|
1107 | PyObject *prop_doc;
|
---|
1108 | } propertyobject;
|
---|
1109 |
|
---|
1110 | static PyMemberDef property_members[] = {
|
---|
1111 | {"fget", T_OBJECT, offsetof(propertyobject, prop_get), READONLY},
|
---|
1112 | {"fset", T_OBJECT, offsetof(propertyobject, prop_set), READONLY},
|
---|
1113 | {"fdel", T_OBJECT, offsetof(propertyobject, prop_del), READONLY},
|
---|
1114 | {"__doc__", T_OBJECT, offsetof(propertyobject, prop_doc), READONLY},
|
---|
1115 | {0}
|
---|
1116 | };
|
---|
1117 |
|
---|
1118 |
|
---|
1119 | static void
|
---|
1120 | property_dealloc(PyObject *self)
|
---|
1121 | {
|
---|
1122 | propertyobject *gs = (propertyobject *)self;
|
---|
1123 |
|
---|
1124 | _PyObject_GC_UNTRACK(self);
|
---|
1125 | Py_XDECREF(gs->prop_get);
|
---|
1126 | Py_XDECREF(gs->prop_set);
|
---|
1127 | Py_XDECREF(gs->prop_del);
|
---|
1128 | Py_XDECREF(gs->prop_doc);
|
---|
1129 | self->ob_type->tp_free(self);
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | static PyObject *
|
---|
1133 | property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
|
---|
1134 | {
|
---|
1135 | propertyobject *gs = (propertyobject *)self;
|
---|
1136 |
|
---|
1137 | if (obj == NULL || obj == Py_None) {
|
---|
1138 | Py_INCREF(self);
|
---|
1139 | return self;
|
---|
1140 | }
|
---|
1141 | if (gs->prop_get == NULL) {
|
---|
1142 | PyErr_SetString(PyExc_AttributeError, "unreadable attribute");
|
---|
1143 | return NULL;
|
---|
1144 | }
|
---|
1145 | return PyObject_CallFunction(gs->prop_get, "(O)", obj);
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | static int
|
---|
1149 | property_descr_set(PyObject *self, PyObject *obj, PyObject *value)
|
---|
1150 | {
|
---|
1151 | propertyobject *gs = (propertyobject *)self;
|
---|
1152 | PyObject *func, *res;
|
---|
1153 |
|
---|
1154 | if (value == NULL)
|
---|
1155 | func = gs->prop_del;
|
---|
1156 | else
|
---|
1157 | func = gs->prop_set;
|
---|
1158 | if (func == NULL) {
|
---|
1159 | PyErr_SetString(PyExc_AttributeError,
|
---|
1160 | value == NULL ?
|
---|
1161 | "can't delete attribute" :
|
---|
1162 | "can't set attribute");
|
---|
1163 | return -1;
|
---|
1164 | }
|
---|
1165 | if (value == NULL)
|
---|
1166 | res = PyObject_CallFunction(func, "(O)", obj);
|
---|
1167 | else
|
---|
1168 | res = PyObject_CallFunction(func, "(OO)", obj, value);
|
---|
1169 | if (res == NULL)
|
---|
1170 | return -1;
|
---|
1171 | Py_DECREF(res);
|
---|
1172 | return 0;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | static int
|
---|
1176 | property_init(PyObject *self, PyObject *args, PyObject *kwds)
|
---|
1177 | {
|
---|
1178 | PyObject *get = NULL, *set = NULL, *del = NULL, *doc = NULL;
|
---|
1179 | static char *kwlist[] = {"fget", "fset", "fdel", "doc", 0};
|
---|
1180 | propertyobject *gs = (propertyobject *)self;
|
---|
1181 |
|
---|
1182 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOO:property",
|
---|
1183 | kwlist, &get, &set, &del, &doc))
|
---|
1184 | return -1;
|
---|
1185 |
|
---|
1186 | if (get == Py_None)
|
---|
1187 | get = NULL;
|
---|
1188 | if (set == Py_None)
|
---|
1189 | set = NULL;
|
---|
1190 | if (del == Py_None)
|
---|
1191 | del = NULL;
|
---|
1192 |
|
---|
1193 | Py_XINCREF(get);
|
---|
1194 | Py_XINCREF(set);
|
---|
1195 | Py_XINCREF(del);
|
---|
1196 | Py_XINCREF(doc);
|
---|
1197 |
|
---|
1198 | /* if no docstring given and the getter has one, use that one */
|
---|
1199 | if ((doc == NULL || doc == Py_None) && get != NULL) {
|
---|
1200 | PyObject *get_doc = PyObject_GetAttrString(get, "__doc__");
|
---|
1201 | if (get_doc != NULL) {
|
---|
1202 | Py_XDECREF(doc);
|
---|
1203 | doc = get_doc; /* get_doc already INCREF'd by GetAttr */
|
---|
1204 | } else {
|
---|
1205 | PyErr_Clear();
|
---|
1206 | }
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | gs->prop_get = get;
|
---|
1210 | gs->prop_set = set;
|
---|
1211 | gs->prop_del = del;
|
---|
1212 | gs->prop_doc = doc;
|
---|
1213 |
|
---|
1214 | return 0;
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | PyDoc_STRVAR(property_doc,
|
---|
1218 | "property(fget=None, fset=None, fdel=None, doc=None) -> property attribute\n"
|
---|
1219 | "\n"
|
---|
1220 | "fget is a function to be used for getting an attribute value, and likewise\n"
|
---|
1221 | "fset is a function for setting, and fdel a function for del'ing, an\n"
|
---|
1222 | "attribute. Typical use is to define a managed attribute x:\n"
|
---|
1223 | "class C(object):\n"
|
---|
1224 | " def getx(self): return self.__x\n"
|
---|
1225 | " def setx(self, value): self.__x = value\n"
|
---|
1226 | " def delx(self): del self.__x\n"
|
---|
1227 | " x = property(getx, setx, delx, \"I'm the 'x' property.\")");
|
---|
1228 |
|
---|
1229 | static int
|
---|
1230 | property_traverse(PyObject *self, visitproc visit, void *arg)
|
---|
1231 | {
|
---|
1232 | propertyobject *pp = (propertyobject *)self;
|
---|
1233 | Py_VISIT(pp->prop_get);
|
---|
1234 | Py_VISIT(pp->prop_set);
|
---|
1235 | Py_VISIT(pp->prop_del);
|
---|
1236 | Py_VISIT(pp->prop_doc);
|
---|
1237 | return 0;
|
---|
1238 | }
|
---|
1239 |
|
---|
1240 | PyTypeObject PyProperty_Type = {
|
---|
1241 | PyObject_HEAD_INIT(&PyType_Type)
|
---|
1242 | 0, /* ob_size */
|
---|
1243 | "property", /* tp_name */
|
---|
1244 | sizeof(propertyobject), /* tp_basicsize */
|
---|
1245 | 0, /* tp_itemsize */
|
---|
1246 | /* methods */
|
---|
1247 | property_dealloc, /* tp_dealloc */
|
---|
1248 | 0, /* tp_print */
|
---|
1249 | 0, /* tp_getattr */
|
---|
1250 | 0, /* tp_setattr */
|
---|
1251 | 0, /* tp_compare */
|
---|
1252 | 0, /* tp_repr */
|
---|
1253 | 0, /* tp_as_number */
|
---|
1254 | 0, /* tp_as_sequence */
|
---|
1255 | 0, /* tp_as_mapping */
|
---|
1256 | 0, /* tp_hash */
|
---|
1257 | 0, /* tp_call */
|
---|
1258 | 0, /* tp_str */
|
---|
1259 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
1260 | 0, /* tp_setattro */
|
---|
1261 | 0, /* tp_as_buffer */
|
---|
1262 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
|
---|
1263 | Py_TPFLAGS_BASETYPE, /* tp_flags */
|
---|
1264 | property_doc, /* tp_doc */
|
---|
1265 | property_traverse, /* tp_traverse */
|
---|
1266 | 0, /* tp_clear */
|
---|
1267 | 0, /* tp_richcompare */
|
---|
1268 | 0, /* tp_weaklistoffset */
|
---|
1269 | 0, /* tp_iter */
|
---|
1270 | 0, /* tp_iternext */
|
---|
1271 | 0, /* tp_methods */
|
---|
1272 | property_members, /* tp_members */
|
---|
1273 | 0, /* tp_getset */
|
---|
1274 | 0, /* tp_base */
|
---|
1275 | 0, /* tp_dict */
|
---|
1276 | property_descr_get, /* tp_descr_get */
|
---|
1277 | property_descr_set, /* tp_descr_set */
|
---|
1278 | 0, /* tp_dictoffset */
|
---|
1279 | property_init, /* tp_init */
|
---|
1280 | PyType_GenericAlloc, /* tp_alloc */
|
---|
1281 | PyType_GenericNew, /* tp_new */
|
---|
1282 | PyObject_GC_Del, /* tp_free */
|
---|
1283 | };
|
---|