Mercurial > cpython-lru_cache-weakref
annotate Objects/classobject.c @ 62449:53a152b400a7 legacy-trunk
must force gc here
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Tue, 22 Jun 2010 20:26:20 +0000 |
| parents | fd25030223b0 |
| children | b36cb4602e21 |
| rev | line source |
|---|---|
| 145 | 1 |
| 6 | 2 /* Class object implementation */ |
| 3 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
4 #include "Python.h" |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
5 #include "structmember.h" |
| 6 | 6 |
|
45189
a4c87c889648
Limit free list of method and builtin function objects to 256 entries each.
Christian Heimes <christian@cheimes.de>
parents:
45142
diff
changeset
|
7 /* Free list for method objects to safe malloc/free overhead |
|
a4c87c889648
Limit free list of method and builtin function objects to 256 entries each.
Christian Heimes <christian@cheimes.de>
parents:
45142
diff
changeset
|
8 * The im_self element is used to chain the elements. |
|
a4c87c889648
Limit free list of method and builtin function objects to 256 entries each.
Christian Heimes <christian@cheimes.de>
parents:
45142
diff
changeset
|
9 */ |
|
a4c87c889648
Limit free list of method and builtin function objects to 256 entries each.
Christian Heimes <christian@cheimes.de>
parents:
45142
diff
changeset
|
10 static PyMethodObject *free_list; |
|
a4c87c889648
Limit free list of method and builtin function objects to 256 entries each.
Christian Heimes <christian@cheimes.de>
parents:
45142
diff
changeset
|
11 static int numfree = 0; |
|
45191
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45189
diff
changeset
|
12 #ifndef PyMethod_MAXFREELIST |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45189
diff
changeset
|
13 #define PyMethod_MAXFREELIST 256 |
|
920a7633ae08
Unified naming convention for free lists and their limits. All free lists
Christian Heimes <christian@cheimes.de>
parents:
45189
diff
changeset
|
14 #endif |
|
45189
a4c87c889648
Limit free list of method and builtin function objects to 256 entries each.
Christian Heimes <christian@cheimes.de>
parents:
45142
diff
changeset
|
15 |
|
20501
733cb268ad2f
Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents:
20183
diff
changeset
|
16 #define TP_DESCR_GET(t) \ |
|
733cb268ad2f
Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents:
20183
diff
changeset
|
17 (PyType_HasFeature(t, Py_TPFLAGS_HAVE_CLASS) ? (t)->tp_descr_get : NULL) |
|
733cb268ad2f
Protect references to tp_descr_get and tp_dict with the appropriate test:
Guido van Rossum <guido@python.org>
parents:
20183
diff
changeset
|
18 |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
19 /* Forward */ |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13430
diff
changeset
|
20 static PyObject *class_lookup(PyClassObject *, PyObject *, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
21 PyClassObject **); |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13430
diff
changeset
|
22 static PyObject *instance_getattr1(PyInstanceObject *, PyObject *); |
|
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13430
diff
changeset
|
23 static PyObject *instance_getattr2(PyInstanceObject *, PyObject *); |
|
1861
d405e3e8ffd3
Yet another version (by me) of __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1860
diff
changeset
|
24 |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
25 static PyObject *getattrstr, *setattrstr, *delattrstr; |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
26 |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
27 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
28 PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
29 PyClass_New(PyObject *bases, PyObject *dict, PyObject *name) |
|
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
30 /* bases is NULL or tuple of classobjects! */ |
| 6 | 31 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
32 PyClassObject *op, *dummy; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
33 static PyObject *docstr, *modstr, *namestr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
34 if (docstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
35 docstr= PyString_InternFromString("__doc__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
36 if (docstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
37 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
38 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
39 if (modstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
40 modstr= PyString_InternFromString("__module__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
41 if (modstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
42 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
43 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
44 if (namestr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
45 namestr= PyString_InternFromString("__name__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
46 if (namestr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
47 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
48 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
49 if (name == NULL || !PyString_Check(name)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
50 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
51 "PyClass_New: name must be a string"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
52 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
53 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
54 if (dict == NULL || !PyDict_Check(dict)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
55 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
56 "PyClass_New: dict must be a dictionary"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
57 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
58 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
59 if (PyDict_GetItem(dict, docstr) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
60 if (PyDict_SetItem(dict, docstr, Py_None) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
61 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
62 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
63 if (PyDict_GetItem(dict, modstr) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
64 PyObject *globals = PyEval_GetGlobals(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
65 if (globals != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
66 PyObject *modname = PyDict_GetItem(globals, namestr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
67 if (modname != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
68 if (PyDict_SetItem(dict, modstr, modname) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
69 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
70 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
71 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
72 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
73 if (bases == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
74 bases = PyTuple_New(0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
75 if (bases == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
76 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
77 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
78 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
79 Py_ssize_t i, n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
80 PyObject *base; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
81 if (!PyTuple_Check(bases)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
82 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
83 "PyClass_New: bases must be a tuple"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
84 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
85 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
86 n = PyTuple_Size(bases); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
87 for (i = 0; i < n; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
88 base = PyTuple_GET_ITEM(bases, i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
89 if (!PyClass_Check(base)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
90 if (PyCallable_Check( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
91 (PyObject *) base->ob_type)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
92 return PyObject_CallFunctionObjArgs( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
93 (PyObject *) base->ob_type, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
94 name, bases, dict, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
95 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
96 "PyClass_New: base must be a class"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
97 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
98 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
99 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
100 Py_INCREF(bases); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
101 } |
|
39582
a745a2bd48af
Move initialization of interned strings to before allocating the
Neal Norwitz <nnorwitz@gmail.com>
parents:
39481
diff
changeset
|
102 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
103 if (getattrstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
104 getattrstr = PyString_InternFromString("__getattr__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
105 if (getattrstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
106 goto alloc_error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
107 setattrstr = PyString_InternFromString("__setattr__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
108 if (setattrstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
109 goto alloc_error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
110 delattrstr = PyString_InternFromString("__delattr__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
111 if (delattrstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
112 goto alloc_error; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
113 } |
|
39582
a745a2bd48af
Move initialization of interned strings to before allocating the
Neal Norwitz <nnorwitz@gmail.com>
parents:
39481
diff
changeset
|
114 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
115 op = PyObject_GC_New(PyClassObject, &PyClass_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
116 if (op == NULL) { |
|
39582
a745a2bd48af
Move initialization of interned strings to before allocating the
Neal Norwitz <nnorwitz@gmail.com>
parents:
39481
diff
changeset
|
117 alloc_error: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
118 Py_DECREF(bases); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
119 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
120 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
121 op->cl_bases = bases; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
122 Py_INCREF(dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
123 op->cl_dict = dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
124 Py_XINCREF(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
125 op->cl_name = name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
126 op->cl_weakreflist = NULL; |
|
39582
a745a2bd48af
Move initialization of interned strings to before allocating the
Neal Norwitz <nnorwitz@gmail.com>
parents:
39481
diff
changeset
|
127 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
128 op->cl_getattr = class_lookup(op, getattrstr, &dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
129 op->cl_setattr = class_lookup(op, setattrstr, &dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
130 op->cl_delattr = class_lookup(op, delattrstr, &dummy); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
131 Py_XINCREF(op->cl_getattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
132 Py_XINCREF(op->cl_setattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
133 Py_XINCREF(op->cl_delattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
134 _PyObject_GC_TRACK(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
135 return (PyObject *) op; |
| 6 | 136 } |
| 137 | |
|
19751
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
138 PyObject * |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
139 PyMethod_Function(PyObject *im) |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
140 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
141 if (!PyMethod_Check(im)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
142 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
143 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
144 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
145 return ((PyMethodObject *)im)->im_func; |
|
19751
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
146 } |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
147 |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
148 PyObject * |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
149 PyMethod_Self(PyObject *im) |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
150 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
151 if (!PyMethod_Check(im)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
152 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
153 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
154 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
155 return ((PyMethodObject *)im)->im_self; |
|
19751
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
156 } |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
157 |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
158 PyObject * |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
159 PyMethod_Class(PyObject *im) |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
160 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
161 if (!PyMethod_Check(im)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
162 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
163 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
164 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
165 return ((PyMethodObject *)im)->im_class; |
|
19751
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
166 } |
|
3d76dfc34c57
Add PyMethod_Function(), PyMethod_Self(), PyMethod_Class() back.
Guido van Rossum <guido@python.org>
parents:
19581
diff
changeset
|
167 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
168 PyDoc_STRVAR(class_doc, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
169 "classobj(name, bases, dict)\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
170 \n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
171 Create a class object. The name must be a string; the second argument\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
172 a tuple of classes, and the third a dictionary."); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
173 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
174 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
175 class_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
176 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
177 PyObject *name, *bases, *dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
178 static char *kwlist[] = {"name", "bases", "dict", 0}; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
179 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
180 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", kwlist, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
181 &name, &bases, &dict)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
182 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
183 return PyClass_New(bases, dict, name); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
184 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
185 |
| 6 | 186 /* Class methods */ |
| 187 | |
| 188 static void | |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
189 class_dealloc(PyClassObject *op) |
| 6 | 190 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
191 _PyObject_GC_UNTRACK(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
192 if (op->cl_weakreflist != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
193 PyObject_ClearWeakRefs((PyObject *) op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
194 Py_DECREF(op->cl_bases); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
195 Py_DECREF(op->cl_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
196 Py_XDECREF(op->cl_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
197 Py_XDECREF(op->cl_getattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
198 Py_XDECREF(op->cl_setattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
199 Py_XDECREF(op->cl_delattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
200 PyObject_GC_Del(op); |
| 6 | 201 } |
| 202 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
203 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
204 class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass) |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
205 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
206 Py_ssize_t i, n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
207 PyObject *value = PyDict_GetItem(cp->cl_dict, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
208 if (value != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
209 *pclass = cp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
210 return value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
211 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
212 n = PyTuple_Size(cp->cl_bases); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
213 for (i = 0; i < n; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
214 /* XXX What if one of the bases is not a class? */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
215 PyObject *v = class_lookup( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
216 (PyClassObject *) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
217 PyTuple_GetItem(cp->cl_bases, i), name, pclass); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
218 if (v != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
219 return v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
220 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
221 return NULL; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
222 } |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
223 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
224 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
225 class_getattr(register PyClassObject *op, PyObject *name) |
| 6 | 226 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
227 register PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
228 register char *sname = PyString_AsString(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
229 PyClassObject *klass; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
230 descrgetfunc f; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
231 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
232 if (sname[0] == '_' && sname[1] == '_') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
233 if (strcmp(sname, "__dict__") == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
234 if (PyEval_GetRestricted()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
235 PyErr_SetString(PyExc_RuntimeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
236 "class.__dict__ not accessible in restricted mode"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
237 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
238 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
239 Py_INCREF(op->cl_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
240 return op->cl_dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
241 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
242 if (strcmp(sname, "__bases__") == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
243 Py_INCREF(op->cl_bases); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
244 return op->cl_bases; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
245 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
246 if (strcmp(sname, "__name__") == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
247 if (op->cl_name == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
248 v = Py_None; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
249 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
250 v = op->cl_name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
251 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
252 return v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
253 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
254 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
255 v = class_lookup(op, name, &klass); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
256 if (v == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
257 PyErr_Format(PyExc_AttributeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
258 "class %.50s has no attribute '%.400s'", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
259 PyString_AS_STRING(op->cl_name), sname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
260 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
261 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
262 f = TP_DESCR_GET(v->ob_type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
263 if (f == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
264 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
265 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
266 v = f(v, (PyObject *)NULL, (PyObject *)op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
267 return v; |
| 6 | 268 } |
| 269 | |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
270 static void |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
271 set_slot(PyObject **slot, PyObject *v) |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
272 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
273 PyObject *temp = *slot; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
274 Py_XINCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
275 *slot = v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
276 Py_XDECREF(temp); |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
277 } |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
278 |
|
8649
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
279 static void |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
280 set_attr_slots(PyClassObject *c) |
|
8649
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
281 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
282 PyClassObject *dummy; |
|
8649
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
283 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
284 set_slot(&c->cl_getattr, class_lookup(c, getattrstr, &dummy)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
285 set_slot(&c->cl_setattr, class_lookup(c, setattrstr, &dummy)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
286 set_slot(&c->cl_delattr, class_lookup(c, delattrstr, &dummy)); |
|
8649
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
287 } |
|
9af95d7270df
Recompute the special getattr/setattr/delattr cache slots after
Guido van Rossum <guido@python.org>
parents:
8496
diff
changeset
|
288 |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
289 static char * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
290 set_dict(PyClassObject *c, PyObject *v) |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
291 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
292 if (v == NULL || !PyDict_Check(v)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
293 return "__dict__ must be a dictionary object"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
294 set_slot(&c->cl_dict, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
295 set_attr_slots(c); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
296 return ""; |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
297 } |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
298 |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
299 static char * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
300 set_bases(PyClassObject *c, PyObject *v) |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
301 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
302 Py_ssize_t i, n; |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
303 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
304 if (v == NULL || !PyTuple_Check(v)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
305 return "__bases__ must be a tuple object"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
306 n = PyTuple_Size(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
307 for (i = 0; i < n; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
308 PyObject *x = PyTuple_GET_ITEM(v, i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
309 if (!PyClass_Check(x)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
310 return "__bases__ items must be classes"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
311 if (PyClass_IsSubclass(x, (PyObject *)c)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
312 return "a __bases__ item causes an inheritance cycle"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
313 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
314 set_slot(&c->cl_bases, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
315 set_attr_slots(c); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
316 return ""; |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
317 } |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
318 |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
319 static char * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
320 set_name(PyClassObject *c, PyObject *v) |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
321 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
322 if (v == NULL || !PyString_Check(v)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
323 return "__name__ must be a string object"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
324 if (strlen(PyString_AS_STRING(v)) != (size_t)PyString_GET_SIZE(v)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
325 return "__name__ must not contain null bytes"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
326 set_slot(&c->cl_name, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
327 return ""; |
|
8464
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
328 } |
|
7160e529e9a1
Allow assignments to special class attributes -- with typechecks, and
Guido van Rossum <guido@python.org>
parents:
8438
diff
changeset
|
329 |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
330 static int |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
331 class_setattr(PyClassObject *op, PyObject *name, PyObject *v) |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
332 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
333 char *sname; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
334 if (PyEval_GetRestricted()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
335 PyErr_SetString(PyExc_RuntimeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
336 "classes are read-only in restricted mode"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
337 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
338 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
339 sname = PyString_AsString(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
340 if (sname[0] == '_' && sname[1] == '_') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
341 Py_ssize_t n = PyString_Size(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
342 if (sname[n-1] == '_' && sname[n-2] == '_') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
343 char *err = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
344 if (strcmp(sname, "__dict__") == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
345 err = set_dict(op, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
346 else if (strcmp(sname, "__bases__") == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
347 err = set_bases(op, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
348 else if (strcmp(sname, "__name__") == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
349 err = set_name(op, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
350 else if (strcmp(sname, "__getattr__") == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
351 set_slot(&op->cl_getattr, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
352 else if (strcmp(sname, "__setattr__") == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
353 set_slot(&op->cl_setattr, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
354 else if (strcmp(sname, "__delattr__") == 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
355 set_slot(&op->cl_delattr, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
356 /* For the last three, we fall through to update the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
357 dictionary as well. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
358 if (err != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
359 if (*err == '\0') |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
360 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
361 PyErr_SetString(PyExc_TypeError, err); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
362 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
363 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
364 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
365 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
366 if (v == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
367 int rv = PyDict_DelItem(op->cl_dict, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
368 if (rv < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
369 PyErr_Format(PyExc_AttributeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
370 "class %.50s has no attribute '%.400s'", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
371 PyString_AS_STRING(op->cl_name), sname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
372 return rv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
373 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
374 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
375 return PyDict_SetItem(op->cl_dict, name, v); |
|
386
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
376 } |
|
0598f4f25d04
Add several secret __*__ attributes
Guido van Rossum <guido@python.org>
parents:
224
diff
changeset
|
377 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
378 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
379 class_repr(PyClassObject *op) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
380 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
381 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
382 char *name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
383 if (op->cl_name == NULL || !PyString_Check(op->cl_name)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
384 name = "?"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
385 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
386 name = PyString_AsString(op->cl_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
387 if (mod == NULL || !PyString_Check(mod)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
388 return PyString_FromFormat("<class ?.%s at %p>", name, op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
389 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
390 return PyString_FromFormat("<class %s.%s at %p>", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
391 PyString_AsString(mod), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
392 name, op); |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
393 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
394 |
|
6502
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
395 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
396 class_str(PyClassObject *op) |
|
6502
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
397 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
398 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
399 PyObject *name = op->cl_name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
400 PyObject *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
401 Py_ssize_t m, n; |
|
6502
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
402 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
403 if (name == NULL || !PyString_Check(name)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
404 return class_repr(op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
405 if (mod == NULL || !PyString_Check(mod)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
406 Py_INCREF(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
407 return name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
408 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
409 m = PyString_GET_SIZE(mod); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
410 n = PyString_GET_SIZE(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
411 res = PyString_FromStringAndSize((char *)NULL, m+1+n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
412 if (res != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
413 char *s = PyString_AS_STRING(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
414 memcpy(s, PyString_AS_STRING(mod), m); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
415 s += m; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
416 *s++ = '.'; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
417 memcpy(s, PyString_AS_STRING(name), n); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
418 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
419 return res; |
|
6502
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
420 } |
|
2a2e5b731d1c
Write a str() function for class objects that returns
Guido van Rossum <guido@python.org>
parents:
6430
diff
changeset
|
421 |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
422 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
423 class_traverse(PyClassObject *o, visitproc visit, void *arg) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
424 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
425 Py_VISIT(o->cl_bases); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
426 Py_VISIT(o->cl_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
427 Py_VISIT(o->cl_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
428 Py_VISIT(o->cl_getattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
429 Py_VISIT(o->cl_setattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
430 Py_VISIT(o->cl_delattr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
431 return 0; |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
432 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
433 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
434 PyTypeObject PyClass_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
435 PyObject_HEAD_INIT(&PyType_Type) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
436 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
437 "classobj", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
438 sizeof(PyClassObject), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
439 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
440 (destructor)class_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
441 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
442 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
443 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
444 0, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
445 (reprfunc)class_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
446 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
447 0, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
448 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
449 0, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
450 PyInstance_New, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
451 (reprfunc)class_str, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
452 (getattrofunc)class_getattr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
453 (setattrofunc)class_setattr, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
454 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
455 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
456 class_doc, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
457 (traverseproc)class_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
458 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
459 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
460 offsetof(PyClassObject, cl_weakreflist), /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
461 0, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
462 0, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
463 0, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
464 0, /* tp_members */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
465 0, /* tp_getset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
466 0, /* tp_base */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
467 0, /* tp_dict */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
468 0, /* tp_descr_get */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
469 0, /* tp_descr_set */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
470 0, /* tp_dictoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
471 0, /* tp_init */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
472 0, /* tp_alloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
473 class_new, /* tp_new */ |
| 6 | 474 }; |
| 475 | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
476 int |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
477 PyClass_IsSubclass(PyObject *klass, PyObject *base) |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
478 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
479 Py_ssize_t i, n; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
480 PyClassObject *cp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
481 if (klass == base) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
482 return 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
483 if (PyTuple_Check(base)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
484 n = PyTuple_GET_SIZE(base); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
485 for (i = 0; i < n; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
486 if (PyClass_IsSubclass(klass, PyTuple_GET_ITEM(base, i))) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
487 return 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
488 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
489 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
490 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
491 if (klass == NULL || !PyClass_Check(klass)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
492 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
493 cp = (PyClassObject *)klass; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
494 n = PyTuple_Size(cp->cl_bases); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
495 for (i = 0; i < n; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
496 if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
497 return 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
498 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
499 return 0; |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
500 } |
| 6 | 501 |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
502 |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
503 /* Instance objects */ |
| 6 | 504 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
505 PyObject * |
|
16314
24e457ba1f92
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents:
16004
diff
changeset
|
506 PyInstance_NewRaw(PyObject *klass, PyObject *dict) |
|
24e457ba1f92
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents:
16004
diff
changeset
|
507 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
508 PyInstanceObject *inst; |
|
16314
24e457ba1f92
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents:
16004
diff
changeset
|
509 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
510 if (!PyClass_Check(klass)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
511 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
512 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
513 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
514 if (dict == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
515 dict = PyDict_New(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
516 if (dict == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
517 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
518 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
519 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
520 if (!PyDict_Check(dict)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
521 PyErr_BadInternalCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
522 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
523 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
524 Py_INCREF(dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
525 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
526 inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
527 if (inst == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
528 Py_DECREF(dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
529 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
530 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
531 inst->in_weakreflist = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
532 Py_INCREF(klass); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
533 inst->in_class = (PyClassObject *)klass; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
534 inst->in_dict = dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
535 _PyObject_GC_TRACK(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
536 return (PyObject *)inst; |
|
16314
24e457ba1f92
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents:
16004
diff
changeset
|
537 } |
|
24e457ba1f92
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents:
16004
diff
changeset
|
538 |
|
24e457ba1f92
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents:
16004
diff
changeset
|
539 PyObject * |
|
24e457ba1f92
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents:
16004
diff
changeset
|
540 PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw) |
| 6 | 541 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
542 register PyInstanceObject *inst; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
543 PyObject *init; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
544 static PyObject *initstr; |
|
16314
24e457ba1f92
Re-factored PyInstance_New() into PyInstance_New() and PyInstance_NewRaw().
Fred Drake <fdrake@acm.org>
parents:
16004
diff
changeset
|
545 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
546 if (initstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
547 initstr = PyString_InternFromString("__init__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
548 if (initstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
549 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
550 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
551 inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
552 if (inst == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
553 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
554 init = instance_getattr2(inst, initstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
555 if (init == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
556 if (PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
557 Py_DECREF(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
558 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
559 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
560 if ((arg != NULL && (!PyTuple_Check(arg) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
561 PyTuple_Size(arg) != 0)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
562 || (kw != NULL && (!PyDict_Check(kw) || |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
563 PyDict_Size(kw) != 0))) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
564 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
565 "this constructor takes no arguments"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
566 Py_DECREF(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
567 inst = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
568 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
569 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
570 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
571 PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
572 Py_DECREF(init); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
573 if (res == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
574 Py_DECREF(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
575 inst = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
576 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
577 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
578 if (res != Py_None) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
579 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
580 "__init__() should return None"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
581 Py_DECREF(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
582 inst = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
583 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
584 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
585 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
586 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
587 return (PyObject *)inst; |
| 6 | 588 } |
| 589 | |
|
174
20fee4e2eecd
Change ugly 'class member' to more normal 'instance'.
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
590 /* Instance methods */ |
| 6 | 591 |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
592 PyDoc_STRVAR(instance_doc, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
593 "instance(class[, dict])\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
594 \n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
595 Create an instance without calling its __init__() method.\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
596 The class must be a classic class.\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
597 If present, dict must be a dictionary or None."); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
598 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
599 static PyObject * |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
600 instance_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
601 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
602 PyObject *klass; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
603 PyObject *dict = Py_None; |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
604 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
605 if (!PyArg_ParseTuple(args, "O!|O:instance", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
606 &PyClass_Type, &klass, &dict)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
607 return NULL; |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
608 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
609 if (dict == Py_None) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
610 dict = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
611 else if (!PyDict_Check(dict)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
612 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
613 "instance() second arg must be dictionary or None"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
614 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
615 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
616 return PyInstance_NewRaw(klass, dict); |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
617 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
618 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
619 |
| 6 | 620 static void |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
621 instance_dealloc(register PyInstanceObject *inst) |
| 6 | 622 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
623 PyObject *error_type, *error_value, *error_traceback; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
624 PyObject *del; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
625 static PyObject *delstr; |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
626 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
627 _PyObject_GC_UNTRACK(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
628 if (inst->in_weakreflist != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
629 PyObject_ClearWeakRefs((PyObject *) inst); |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16356
diff
changeset
|
630 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
631 /* Temporarily resurrect the object. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
632 assert(inst->ob_type == &PyInstance_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
633 assert(inst->ob_refcnt == 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
634 inst->ob_refcnt = 1; |
|
14653
3d6fe64bcd21
Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents:
14605
diff
changeset
|
635 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
636 /* Save the current exception, if any. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
637 PyErr_Fetch(&error_type, &error_value, &error_traceback); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
638 /* Execute __del__ method, if any. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
639 if (delstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
640 delstr = PyString_InternFromString("__del__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
641 if (delstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
642 PyErr_WriteUnraisable((PyObject*)inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
643 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
644 if (delstr && (del = instance_getattr2(inst, delstr)) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
645 PyObject *res = PyEval_CallObject(del, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
646 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
647 PyErr_WriteUnraisable(del); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
648 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
649 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
650 Py_DECREF(del); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
651 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
652 /* Restore the saved exception. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
653 PyErr_Restore(error_type, error_value, error_traceback); |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
654 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
655 /* Undo the temporary resurrection; can't use DECREF here, it would |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
656 * cause a recursive call. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
657 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
658 assert(inst->ob_refcnt > 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
659 if (--inst->ob_refcnt == 0) { |
|
44751
58303ed72ba1
Fix an edge case whereby the __del__() method of a classic class could
Guido van Rossum <guido@python.org>
parents:
41634
diff
changeset
|
660 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
661 /* New weakrefs could be created during the finalizer call. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
662 If this occurs, clear them out without calling their |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
663 finalizers since they might rely on part of the object |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
664 being finalized that has already been destroyed. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
665 while (inst->in_weakreflist != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
666 _PyWeakref_ClearRef((PyWeakReference *) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
667 (inst->in_weakreflist)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
668 } |
|
44751
58303ed72ba1
Fix an edge case whereby the __del__() method of a classic class could
Guido van Rossum <guido@python.org>
parents:
41634
diff
changeset
|
669 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
670 Py_DECREF(inst->in_class); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
671 Py_XDECREF(inst->in_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
672 PyObject_GC_Del(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
673 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
674 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
675 Py_ssize_t refcnt = inst->ob_refcnt; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
676 /* __del__ resurrected it! Make it look like the original |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
677 * Py_DECREF never happened. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
678 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
679 _Py_NewReference((PyObject *)inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
680 inst->ob_refcnt = refcnt; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
681 _PyObject_GC_TRACK(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
682 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
683 * we need to undo that. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
684 _Py_DEC_REFTOTAL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
685 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
686 * object chain, so no more to do there. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
687 * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
688 * _Py_NewReference bumped tp_allocs: both of those need to be |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
689 * undone. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
690 */ |
|
3022
c5d852735e7f
Fixed calling of __del__ method with TRACE_REFS defined.
Sjoerd Mullender <sjoerd@acm.org>
parents:
2889
diff
changeset
|
691 #ifdef COUNT_ALLOCS |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
692 --inst->ob_type->tp_frees; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
693 --inst->ob_type->tp_allocs; |
|
14653
3d6fe64bcd21
Fix for SF bug 110688: Instance deallocation neglected to account for
Tim Peters <tim.peters@gmail.com>
parents:
14605
diff
changeset
|
694 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
695 } |
| 6 | 696 } |
| 697 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
698 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
699 instance_getattr1(register PyInstanceObject *inst, PyObject *name) |
| 6 | 700 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
701 register PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
702 register char *sname = PyString_AsString(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
703 if (sname[0] == '_' && sname[1] == '_') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
704 if (strcmp(sname, "__dict__") == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
705 if (PyEval_GetRestricted()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
706 PyErr_SetString(PyExc_RuntimeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
707 "instance.__dict__ not accessible in restricted mode"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
708 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
709 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
710 Py_INCREF(inst->in_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
711 return inst->in_dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
712 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
713 if (strcmp(sname, "__class__") == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
714 Py_INCREF(inst->in_class); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
715 return (PyObject *)inst->in_class; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
716 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
717 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
718 v = instance_getattr2(inst, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
719 if (v == NULL && !PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
720 PyErr_Format(PyExc_AttributeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
721 "%.50s instance has no attribute '%.400s'", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
722 PyString_AS_STRING(inst->in_class->cl_name), sname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
723 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
724 return v; |
|
12504
44e521a93648
potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12321
diff
changeset
|
725 } |
|
44e521a93648
potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12321
diff
changeset
|
726 |
|
44e521a93648
potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12321
diff
changeset
|
727 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
728 instance_getattr2(register PyInstanceObject *inst, PyObject *name) |
|
12504
44e521a93648
potentially useless optimization
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12321
diff
changeset
|
729 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
730 register PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
731 PyClassObject *klass; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
732 descrgetfunc f; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
733 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
734 v = PyDict_GetItem(inst->in_dict, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
735 if (v != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
736 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
737 return v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
738 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
739 v = class_lookup(inst->in_class, name, &klass); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
740 if (v != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
741 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
742 f = TP_DESCR_GET(v->ob_type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
743 if (f != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
744 PyObject *w = f(v, (PyObject *)inst, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
745 (PyObject *)(inst->in_class)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
746 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
747 v = w; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
748 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
749 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
750 return v; |
| 6 | 751 } |
| 752 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
753 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
754 instance_getattr(register PyInstanceObject *inst, PyObject *name) |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
755 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
756 register PyObject *func, *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
757 res = instance_getattr1(inst, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
758 if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
759 PyObject *args; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
760 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
761 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
762 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
763 args = PyTuple_Pack(2, inst, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
764 if (args == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
765 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
766 res = PyEval_CallObject(func, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
767 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
768 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
769 return res; |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
770 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
771 |
|
28519
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
772 /* See classobject.h comments: this only does dict lookups, and is always |
|
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
773 * safe to call. |
|
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
774 */ |
|
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
775 PyObject * |
|
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
776 _PyInstance_Lookup(PyObject *pinst, PyObject *name) |
|
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
777 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
778 PyObject *v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
779 PyClassObject *klass; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
780 PyInstanceObject *inst; /* pinst cast to the right type */ |
|
28519
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
781 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
782 assert(PyInstance_Check(pinst)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
783 inst = (PyInstanceObject *)pinst; |
|
28519
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
784 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
785 assert(PyString_Check(name)); |
|
28519
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
786 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
787 v = PyDict_GetItem(inst->in_dict, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
788 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
789 v = class_lookup(inst->in_class, name, &klass); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
790 return v; |
|
28519
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
791 } |
|
2a3095a415b1
New private API function _PyInstance_Lookup. gc will use this to figure
Tim Peters <tim.peters@gmail.com>
parents:
27817
diff
changeset
|
792 |
| 6 | 793 static int |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
794 instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v) |
| 6 | 795 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
796 if (v == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
797 int rv = PyDict_DelItem(inst->in_dict, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
798 if (rv < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
799 PyErr_Format(PyExc_AttributeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
800 "%.50s instance has no attribute '%.400s'", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
801 PyString_AS_STRING(inst->in_class->cl_name), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
802 PyString_AS_STRING(name)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
803 return rv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
804 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
805 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
806 return PyDict_SetItem(inst->in_dict, name, v); |
| 6 | 807 } |
| 808 | |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
809 static int |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
810 instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v) |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
811 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
812 PyObject *func, *args, *res, *tmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
813 char *sname = PyString_AsString(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
814 if (sname[0] == '_' && sname[1] == '_') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
815 Py_ssize_t n = PyString_Size(name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
816 if (sname[n-1] == '_' && sname[n-2] == '_') { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
817 if (strcmp(sname, "__dict__") == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
818 if (PyEval_GetRestricted()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
819 PyErr_SetString(PyExc_RuntimeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
820 "__dict__ not accessible in restricted mode"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
821 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
822 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
823 if (v == NULL || !PyDict_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
824 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
825 "__dict__ must be set to a dictionary"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
826 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
827 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
828 tmp = inst->in_dict; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
829 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
830 inst->in_dict = v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
831 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
832 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
833 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
834 if (strcmp(sname, "__class__") == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
835 if (PyEval_GetRestricted()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
836 PyErr_SetString(PyExc_RuntimeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
837 "__class__ not accessible in restricted mode"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
838 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
839 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
840 if (v == NULL || !PyClass_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
841 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
842 "__class__ must be set to a class"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
843 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
844 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
845 tmp = (PyObject *)(inst->in_class); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
846 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
847 inst->in_class = (PyClassObject *)v; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
848 Py_DECREF(tmp); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
849 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
850 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
851 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
852 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
853 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
854 func = inst->in_class->cl_delattr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
855 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
856 func = inst->in_class->cl_setattr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
857 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
858 return instance_setattr1(inst, name, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
859 if (v == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
860 args = PyTuple_Pack(2, inst, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
861 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
862 args = PyTuple_Pack(3, inst, name, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
863 if (args == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
864 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
865 res = PyEval_CallObject(func, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
866 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
867 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
868 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
869 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
870 return 0; |
|
1860
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
871 } |
|
78898b04a65b
Mods (really diffs to 2.29) by Michael Scharf for alternative __getattr__ etc.
Guido van Rossum <guido@python.org>
parents:
1781
diff
changeset
|
872 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
873 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
874 instance_repr(PyInstanceObject *inst) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
875 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
876 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
877 PyObject *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
878 static PyObject *reprstr; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
879 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
880 if (reprstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
881 reprstr = PyString_InternFromString("__repr__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
882 if (reprstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
883 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
884 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
885 func = instance_getattr(inst, reprstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
886 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
887 PyObject *classname, *mod; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
888 char *cname; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
889 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
890 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
891 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
892 classname = inst->in_class->cl_name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
893 mod = PyDict_GetItemString(inst->in_class->cl_dict, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
894 "__module__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
895 if (classname != NULL && PyString_Check(classname)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
896 cname = PyString_AsString(classname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
897 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
898 cname = "?"; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
899 if (mod == NULL || !PyString_Check(mod)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
900 return PyString_FromFormat("<?.%s instance at %p>", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
901 cname, inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
902 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
903 return PyString_FromFormat("<%s.%s instance at %p>", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
904 PyString_AsString(mod), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
905 cname, inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
906 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
907 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
908 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
909 return res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
910 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
911 |
|
17643
56c5445ec835
Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
912 static PyObject * |
|
56c5445ec835
Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
913 instance_str(PyInstanceObject *inst) |
|
56c5445ec835
Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
914 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
915 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
916 PyObject *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
917 static PyObject *strstr; |
|
17643
56c5445ec835
Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
918 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
919 if (strstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
920 strstr = PyString_InternFromString("__str__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
921 if (strstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
922 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
923 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
924 func = instance_getattr(inst, strstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
925 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
926 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
927 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
928 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
929 return instance_repr(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
930 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
931 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
932 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
933 return res; |
|
17643
56c5445ec835
Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
934 } |
|
56c5445ec835
Well darnit! The innocuous fix I made to PyObject_Print() caused
Guido van Rossum <guido@python.org>
parents:
17617
diff
changeset
|
935 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
936 static long |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
937 instance_hash(PyInstanceObject *inst) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
938 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
939 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
940 PyObject *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
941 long outcome; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
942 static PyObject *hashstr, *eqstr, *cmpstr; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
943 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
944 if (hashstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
945 hashstr = PyString_InternFromString("__hash__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
946 if (hashstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
947 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
948 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
949 func = instance_getattr(inst, hashstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
950 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
951 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
952 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
953 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
954 /* If there is no __eq__ and no __cmp__ method, we hash on the |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
955 address. If an __eq__ or __cmp__ method exists, there must |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
956 be a __hash__. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
957 if (eqstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
958 eqstr = PyString_InternFromString("__eq__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
959 if (eqstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
960 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
961 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
962 func = instance_getattr(inst, eqstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
963 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
964 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
965 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
966 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
967 if (cmpstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
968 cmpstr = PyString_InternFromString("__cmp__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
969 if (cmpstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
970 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
971 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
972 func = instance_getattr(inst, cmpstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
973 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
974 if (!PyErr_ExceptionMatches( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
975 PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
976 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
977 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
978 return _Py_HashPointer(inst); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
979 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
980 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
981 Py_XDECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
982 PyErr_SetString(PyExc_TypeError, "unhashable instance"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
983 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
984 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
985 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
986 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
987 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
988 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
989 if (PyInt_Check(res) || PyLong_Check(res)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
990 /* This already converts a -1 result to -2. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
991 outcome = res->ob_type->tp_hash(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
992 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
993 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
994 "__hash__() should return an int"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
995 outcome = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
996 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
997 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
998 return outcome; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
999 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1000 |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1001 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1002 instance_traverse(PyInstanceObject *o, visitproc visit, void *arg) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1003 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1004 Py_VISIT(o->in_class); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1005 Py_VISIT(o->in_dict); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1006 return 0; |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1007 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
1008 |
|
17617
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
1009 static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr; |
|
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
1010 static PyObject *iterstr, *nextstr; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1011 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1012 static Py_ssize_t |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1013 instance_length(PyInstanceObject *inst) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1014 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1015 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1016 PyObject *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1017 Py_ssize_t outcome; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1018 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1019 if (lenstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1020 lenstr = PyString_InternFromString("__len__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1021 if (lenstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1022 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1023 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1024 func = instance_getattr(inst, lenstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1025 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1026 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1027 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1028 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1029 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1030 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1031 if (PyInt_Check(res)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1032 outcome = PyInt_AsSsize_t(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1033 if (outcome == -1 && PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1034 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1035 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1036 } |
|
39464
48f1dbc57845
Fix a couple of bugs exposed by the new __index__ code. The 64-bit buildbots
Neal Norwitz <nnorwitz@gmail.com>
parents:
39463
diff
changeset
|
1037 #if SIZEOF_SIZE_T < SIZEOF_INT |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1038 /* Overflow check -- range of PyInt is more than C int */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1039 if (outcome != (int)outcome) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1040 PyErr_SetString(PyExc_OverflowError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1041 "__len__() should return 0 <= outcome < 2**31"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1042 outcome = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1043 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1044 else |
|
35599
9b994c67e909
A minor fix for 64-bit platforms: when __len__() returns Python int
Guido van Rossum <guido@python.org>
parents:
35111
diff
changeset
|
1045 #endif |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1046 if (outcome < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1047 PyErr_SetString(PyExc_ValueError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1048 "__len__() should return >= 0"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1049 outcome = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1050 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1051 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1052 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1053 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1054 "__len__() should return an int"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1055 outcome = -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1056 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1057 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1058 return outcome; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1059 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1060 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1061 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1062 instance_subscript(PyInstanceObject *inst, PyObject *key) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1063 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1064 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1065 PyObject *arg; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1066 PyObject *res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1067 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1068 if (getitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1069 getitemstr = PyString_InternFromString("__getitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1070 if (getitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1071 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1072 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1073 func = instance_getattr(inst, getitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1074 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1075 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1076 arg = PyTuple_Pack(1, key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1077 if (arg == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1078 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1079 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1080 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1081 res = PyEval_CallObject(func, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1082 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1083 Py_DECREF(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1084 return res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1085 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1086 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1087 static int |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1088 instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1089 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1090 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1091 PyObject *arg; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1092 PyObject *res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1093 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1094 if (value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1095 if (delitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1096 delitemstr = PyString_InternFromString("__delitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1097 if (delitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1098 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1099 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1100 func = instance_getattr(inst, delitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1101 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1102 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1103 if (setitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1104 setitemstr = PyString_InternFromString("__setitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1105 if (setitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1106 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1107 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1108 func = instance_getattr(inst, setitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1109 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1110 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1111 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1112 if (value == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1113 arg = PyTuple_Pack(1, key); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1114 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1115 arg = PyTuple_Pack(2, key, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1116 if (arg == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1117 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1118 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1119 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1120 res = PyEval_CallObject(func, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1121 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1122 Py_DECREF(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1123 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1124 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1125 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1126 return 0; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1127 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1128 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1129 static PyMappingMethods instance_as_mapping = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1130 (lenfunc)instance_length, /* mp_length */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1131 (binaryfunc)instance_subscript, /* mp_subscript */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1132 (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1133 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1134 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1135 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1136 instance_item(PyInstanceObject *inst, Py_ssize_t i) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1137 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1138 PyObject *func, *res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1139 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1140 if (getitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1141 getitemstr = PyString_InternFromString("__getitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1142 if (getitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1143 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1144 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1145 func = instance_getattr(inst, getitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1146 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1147 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1148 res = PyObject_CallFunction(func, "n", i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1149 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1150 return res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1151 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1152 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1153 static PyObject * |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1154 instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1155 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1156 PyObject *func, *arg, *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1157 static PyObject *getslicestr; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1158 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1159 if (getslicestr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1160 getslicestr = PyString_InternFromString("__getslice__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1161 if (getslicestr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1162 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1163 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1164 func = instance_getattr(inst, getslicestr); |
|
14037
91f847a783d2
Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents:
13678
diff
changeset
|
1165 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1166 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1167 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1168 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1169 PyErr_Clear(); |
|
14037
91f847a783d2
Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents:
13678
diff
changeset
|
1170 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1171 if (getitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1172 getitemstr = PyString_InternFromString("__getitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1173 if (getitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1174 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1175 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1176 func = instance_getattr(inst, getitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1177 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1178 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1179 arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1180 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1181 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1182 if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1183 "use __getitem__", 1) < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1184 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1185 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1186 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1187 arg = Py_BuildValue("(nn)", i, j); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1188 } |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1189 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1190 if (arg == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1191 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1192 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1193 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1194 res = PyEval_CallObject(func, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1195 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1196 Py_DECREF(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1197 return res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1198 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1199 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1200 static int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1201 instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1202 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1203 PyObject *func, *arg, *res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1204 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1205 if (item == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1206 if (delitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1207 delitemstr = PyString_InternFromString("__delitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1208 if (delitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1209 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1210 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1211 func = instance_getattr(inst, delitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1212 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1213 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1214 if (setitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1215 setitemstr = PyString_InternFromString("__setitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1216 if (setitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1217 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1218 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1219 func = instance_getattr(inst, setitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1220 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1221 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1222 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1223 if (item == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1224 arg = PyInt_FromSsize_t(i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1225 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1226 arg = Py_BuildValue("(nO)", i, item); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1227 if (arg == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1228 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1229 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1230 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1231 res = PyEval_CallObject(func, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1232 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1233 Py_DECREF(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1234 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1235 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1236 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1237 return 0; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1238 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1239 |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1240 static int |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1241 instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1242 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1243 PyObject *func, *arg, *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1244 static PyObject *setslicestr, *delslicestr; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1245 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1246 if (value == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1247 if (delslicestr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1248 delslicestr = |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1249 PyString_InternFromString("__delslice__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1250 if (delslicestr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1251 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1252 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1253 func = instance_getattr(inst, delslicestr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1254 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1255 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1256 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1257 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1258 if (delitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1259 delitemstr = |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1260 PyString_InternFromString("__delitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1261 if (delitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1262 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1263 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1264 func = instance_getattr(inst, delitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1265 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1266 return -1; |
|
14037
91f847a783d2
Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents:
13678
diff
changeset
|
1267 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1268 arg = Py_BuildValue("(N)", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1269 _PySlice_FromIndices(i, j)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1270 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1271 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1272 if (PyErr_WarnPy3k("in 3.x, __delslice__ has been " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1273 "removed; use __delitem__", 1) < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1274 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1275 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1276 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1277 arg = Py_BuildValue("(nn)", i, j); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1278 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1279 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1280 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1281 if (setslicestr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1282 setslicestr = |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1283 PyString_InternFromString("__setslice__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1284 if (setslicestr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1285 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1286 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1287 func = instance_getattr(inst, setslicestr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1288 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1289 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1290 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1291 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1292 if (setitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1293 setitemstr = |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1294 PyString_InternFromString("__setitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1295 if (setitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1296 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1297 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1298 func = instance_getattr(inst, setitemstr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1299 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1300 return -1; |
|
14037
91f847a783d2
Apply SF patch #101029: call __getitem__ with a proper slice object if there
Thomas Wouters <thomas@python.org>
parents:
13678
diff
changeset
|
1301 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1302 arg = Py_BuildValue("(NO)", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1303 _PySlice_FromIndices(i, j), value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1304 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1305 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1306 if (PyErr_WarnPy3k("in 3.x, __setslice__ has been " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1307 "removed; use __setitem__", 1) < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1308 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1309 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1310 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1311 arg = Py_BuildValue("(nnO)", i, j, value); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1312 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1313 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1314 if (arg == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1315 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1316 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1317 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1318 res = PyEval_CallObject(func, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1319 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1320 Py_DECREF(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1321 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1322 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1323 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1324 return 0; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1325 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1326 |
|
17692
1812476b7fe4
Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
1327 static int |
|
1812476b7fe4
Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
1328 instance_contains(PyInstanceObject *inst, PyObject *member) |
|
11869
32c59e2889c4
Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1329 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1330 static PyObject *__contains__; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1331 PyObject *func; |
|
17692
1812476b7fe4
Reimplement PySequence_Contains() and instance_contains(), so they work
Tim Peters <tim.peters@gmail.com>
parents:
17669
diff
changeset
|
1332 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1333 /* Try __contains__ first. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1334 * If that can't be done, try iterator-based searching. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1335 */ |
|
11869
32c59e2889c4
Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1336 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1337 if(__contains__ == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1338 __contains__ = PyString_InternFromString("__contains__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1339 if(__contains__ == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1340 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1341 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1342 func = instance_getattr(inst, __contains__); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1343 if (func) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1344 PyObject *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1345 int ret; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1346 PyObject *arg = PyTuple_Pack(1, member); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1347 if(arg == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1348 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1349 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1350 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1351 res = PyEval_CallObject(func, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1352 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1353 Py_DECREF(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1354 if(res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1355 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1356 ret = PyObject_IsTrue(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1357 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1358 return ret; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1359 } |
|
11869
32c59e2889c4
Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1360 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1361 /* Couldn't find __contains__. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1362 if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1363 Py_ssize_t rc; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1364 /* Assume the failure was simply due to that there is no |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1365 * __contains__ attribute, and try iterating instead. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1366 */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1367 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1368 rc = _PySequence_IterSearch((PyObject *)inst, member, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1369 PY_ITERSEARCH_CONTAINS); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1370 if (rc >= 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1371 return rc > 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1372 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1373 return -1; |
|
11869
32c59e2889c4
Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1374 } |
|
32c59e2889c4
Patch by Mozhe Zadka, for __contains__ (overloading 'in'). This adds
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1375 |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1376 static PySequenceMethods |
|
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1377 instance_as_sequence = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1378 (lenfunc)instance_length, /* sq_length */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1379 0, /* sq_concat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1380 0, /* sq_repeat */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1381 (ssizeargfunc)instance_item, /* sq_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1382 (ssizessizeargfunc)instance_slice, /* sq_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1383 (ssizeobjargproc)instance_ass_item, /* sq_ass_item */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1384 (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1385 (objobjproc)instance_contains, /* sq_contains */ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1386 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1387 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1388 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1389 generic_unary_op(PyInstanceObject *self, PyObject *methodname) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1390 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1391 PyObject *func, *res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1392 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1393 if ((func = instance_getattr(self, methodname)) == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1394 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1395 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1396 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1397 return res; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1398 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1399 |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1400 static PyObject * |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1401 generic_binary_op(PyObject *v, PyObject *w, char *opname) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1402 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1403 PyObject *result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1404 PyObject *args; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1405 PyObject *func = PyObject_GetAttrString(v, opname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1406 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1407 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1408 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1409 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1410 Py_INCREF(Py_NotImplemented); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1411 return Py_NotImplemented; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1412 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1413 args = PyTuple_Pack(1, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1414 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1415 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1416 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1417 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1418 result = PyEval_CallObject(func, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1419 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1420 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1421 return result; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1422 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1423 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1424 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1425 static PyObject *coerce_obj; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1426 |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1427 /* Try one half of a binary operator involving a class instance. */ |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1428 static PyObject * |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1429 half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1430 int swapped) |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1431 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1432 PyObject *args; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1433 PyObject *coercefunc; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1434 PyObject *coerced = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1435 PyObject *v1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1436 PyObject *result; |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1437 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1438 if (!PyInstance_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1439 Py_INCREF(Py_NotImplemented); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1440 return Py_NotImplemented; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1441 } |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1442 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1443 if (coerce_obj == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1444 coerce_obj = PyString_InternFromString("__coerce__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1445 if (coerce_obj == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1446 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1447 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1448 coercefunc = PyObject_GetAttr(v, coerce_obj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1449 if (coercefunc == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1450 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1451 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1452 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1453 return generic_binary_op(v, w, opname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1454 } |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1455 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1456 args = PyTuple_Pack(1, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1457 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1458 Py_DECREF(coercefunc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1459 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1460 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1461 coerced = PyEval_CallObject(coercefunc, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1462 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1463 Py_DECREF(coercefunc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1464 if (coerced == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1465 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1466 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1467 if (coerced == Py_None || coerced == Py_NotImplemented) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1468 Py_DECREF(coerced); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1469 return generic_binary_op(v, w, opname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1470 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1471 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1472 Py_DECREF(coerced); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1473 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1474 "coercion should return None or 2-tuple"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1475 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1476 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1477 v1 = PyTuple_GetItem(coerced, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1478 w = PyTuple_GetItem(coerced, 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1479 if (v1->ob_type == v->ob_type && PyInstance_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1480 /* prevent recursion if __coerce__ returns self as the first |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1481 * argument */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1482 result = generic_binary_op(v1, w, opname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1483 } else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1484 if (Py_EnterRecursiveCall(" after coercion")) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1485 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1486 if (swapped) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1487 result = (thisfunc)(w, v1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1488 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1489 result = (thisfunc)(v1, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1490 Py_LeaveRecursiveCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1491 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1492 Py_DECREF(coerced); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1493 return result; |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1494 } |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1495 |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1496 /* Implement a binary operator involving at least one class instance. */ |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1497 static PyObject * |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1498 do_binop(PyObject *v, PyObject *w, char *opname, char *ropname, |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1499 binaryfunc thisfunc) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1500 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1501 PyObject *result = half_binop(v, w, opname, thisfunc, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1502 if (result == Py_NotImplemented) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1503 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1504 result = half_binop(w, v, ropname, thisfunc, 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1505 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1506 return result; |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1507 } |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1508 |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1509 static PyObject * |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1510 do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname, |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1511 char *ropname, binaryfunc thisfunc) |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1512 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1513 PyObject *result = half_binop(v, w, iopname, thisfunc, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1514 if (result == Py_NotImplemented) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1515 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1516 result = do_binop(v, w, opname, ropname, thisfunc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1517 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1518 return result; |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1519 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1520 |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1521 static int |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1522 instance_coerce(PyObject **pv, PyObject **pw) |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1523 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1524 PyObject *v = *pv; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1525 PyObject *w = *pw; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1526 PyObject *coercefunc; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1527 PyObject *args; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1528 PyObject *coerced; |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1529 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1530 if (coerce_obj == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1531 coerce_obj = PyString_InternFromString("__coerce__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1532 if (coerce_obj == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1533 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1534 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1535 coercefunc = PyObject_GetAttr(v, coerce_obj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1536 if (coercefunc == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1537 /* No __coerce__ method */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1538 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1539 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1540 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1541 return 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1542 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1543 /* Has __coerce__ method: call it */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1544 args = PyTuple_Pack(1, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1545 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1546 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1547 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1548 coerced = PyEval_CallObject(coercefunc, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1549 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1550 Py_DECREF(coercefunc); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1551 if (coerced == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1552 /* __coerce__ call raised an exception */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1553 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1554 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1555 if (coerced == Py_None || coerced == Py_NotImplemented) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1556 /* __coerce__ says "I can't do it" */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1557 Py_DECREF(coerced); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1558 return 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1559 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1560 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1561 /* __coerce__ return value is malformed */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1562 Py_DECREF(coerced); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1563 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1564 "coercion should return None or 2-tuple"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1565 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1566 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1567 /* __coerce__ returned two new values */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1568 *pv = PyTuple_GetItem(coerced, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1569 *pw = PyTuple_GetItem(coerced, 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1570 Py_INCREF(*pv); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1571 Py_INCREF(*pw); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1572 Py_DECREF(coerced); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1573 return 0; |
|
2112
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1574 } |
|
2669ef14c205
implement coercions involving instances properly
Guido van Rossum <guido@python.org>
parents:
2105
diff
changeset
|
1575 |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1576 #define UNARY(funcname, methodname) \ |
|
13678
3177e2829167
ANSIfy functions that were hiding inside a macro.
Thomas Wouters <thomas@python.org>
parents:
13620
diff
changeset
|
1577 static PyObject *funcname(PyInstanceObject *self) { \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1578 static PyObject *o; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1579 if (o == NULL) { o = PyString_InternFromString(methodname); \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1580 if (o == NULL) return NULL; } \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1581 return generic_unary_op(self, o); \ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1582 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1583 |
|
41634
54179538a387
As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40301
diff
changeset
|
1584 /* unary function with a fallback */ |
|
54179538a387
As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40301
diff
changeset
|
1585 #define UNARY_FB(funcname, methodname, funcname_fb) \ |
|
54179538a387
As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40301
diff
changeset
|
1586 static PyObject *funcname(PyInstanceObject *self) { \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1587 static PyObject *o; \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1588 if (o == NULL) { o = PyString_InternFromString(methodname); \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1589 if (o == NULL) return NULL; } \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1590 if (PyObject_HasAttr((PyObject*)self, o)) \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1591 return generic_unary_op(self, o); \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1592 else \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1593 return funcname_fb(self); \ |
|
41634
54179538a387
As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40301
diff
changeset
|
1594 } |
|
54179538a387
As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40301
diff
changeset
|
1595 |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1596 #define BINARY(f, m, n) \ |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1597 static PyObject *f(PyObject *v, PyObject *w) { \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1598 return do_binop(v, w, "__" m "__", "__r" m "__", n); \ |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1599 } |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1600 |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1601 #define BINARY_INPLACE(f, m, n) \ |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1602 static PyObject *f(PyObject *v, PyObject *w) { \ |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1603 return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1604 "__r" m "__", n); \ |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1605 } |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1606 |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1607 UNARY(instance_neg, "__neg__") |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1608 UNARY(instance_pos, "__pos__") |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1609 UNARY(instance_abs, "__abs__") |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1610 |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1611 BINARY(instance_or, "or", PyNumber_Or) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1612 BINARY(instance_and, "and", PyNumber_And) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1613 BINARY(instance_xor, "xor", PyNumber_Xor) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1614 BINARY(instance_lshift, "lshift", PyNumber_Lshift) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1615 BINARY(instance_rshift, "rshift", PyNumber_Rshift) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1616 BINARY(instance_add, "add", PyNumber_Add) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1617 BINARY(instance_sub, "sub", PyNumber_Subtract) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1618 BINARY(instance_mul, "mul", PyNumber_Multiply) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1619 BINARY(instance_div, "div", PyNumber_Divide) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1620 BINARY(instance_mod, "mod", PyNumber_Remainder) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1621 BINARY(instance_divmod, "divmod", PyNumber_Divmod) |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18934
diff
changeset
|
1622 BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide) |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18934
diff
changeset
|
1623 BINARY(instance_truediv, "truediv", PyNumber_TrueDivide) |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1624 |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1625 BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1626 BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1627 BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1628 BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1629 BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1630 BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1631 BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1632 BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1633 BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1634 BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder) |
|
19051
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18934
diff
changeset
|
1635 BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide) |
|
f20727cc3d0f
Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents:
18934
diff
changeset
|
1636 BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide) |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1637 |
| 15943 | 1638 /* Try a 3-way comparison, returning an int; v is an instance. Return: |
| 1639 -2 for an exception; | |
| 1640 -1 if v < w; | |
| 1641 0 if v == w; | |
| 1642 1 if v > w; | |
| 1643 2 if this particular 3-way comparison is not implemented or undefined. | |
| 1644 */ | |
| 1645 static int | |
| 1646 half_cmp(PyObject *v, PyObject *w) | |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1647 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1648 static PyObject *cmp_obj; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1649 PyObject *args; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1650 PyObject *cmp_func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1651 PyObject *result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1652 long l; |
| 15943 | 1653 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1654 assert(PyInstance_Check(v)); |
| 15943 | 1655 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1656 if (cmp_obj == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1657 cmp_obj = PyString_InternFromString("__cmp__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1658 if (cmp_obj == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1659 return -2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1660 } |
| 15943 | 1661 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1662 cmp_func = PyObject_GetAttr(v, cmp_obj); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1663 if (cmp_func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1664 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1665 return -2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1666 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1667 return 2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1668 } |
| 15943 | 1669 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1670 args = PyTuple_Pack(1, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1671 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1672 Py_DECREF(cmp_func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1673 return -2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1674 } |
| 15943 | 1675 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1676 result = PyEval_CallObject(cmp_func, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1677 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1678 Py_DECREF(cmp_func); |
| 15943 | 1679 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1680 if (result == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1681 return -2; |
| 15943 | 1682 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1683 if (result == Py_NotImplemented) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1684 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1685 return 2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1686 } |
| 15943 | 1687 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1688 l = PyInt_AsLong(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1689 Py_DECREF(result); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1690 if (l == -1 && PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1691 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1692 "comparison did not return an int"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1693 return -2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1694 } |
| 15943 | 1695 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1696 return l < 0 ? -1 : l > 0 ? 1 : 0; |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1697 } |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1698 |
| 15943 | 1699 /* Try a 3-way comparison, returning an int; either v or w is an instance. |
| 1700 We first try a coercion. Return: | |
| 1701 -2 for an exception; | |
| 1702 -1 if v < w; | |
| 1703 0 if v == w; | |
| 1704 1 if v > w; | |
| 1705 2 if this particular 3-way comparison is not implemented or undefined. | |
| 1706 THIS IS ONLY CALLED FROM object.c! | |
| 1707 */ | |
| 1708 static int | |
| 1709 instance_compare(PyObject *v, PyObject *w) | |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1710 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1711 int c; |
| 15943 | 1712 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1713 c = PyNumber_CoerceEx(&v, &w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1714 if (c < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1715 return -2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1716 if (c == 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1717 /* If neither is now an instance, use regular comparison */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1718 if (!PyInstance_Check(v) && !PyInstance_Check(w)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1719 c = PyObject_Compare(v, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1720 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1721 Py_DECREF(w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1722 if (PyErr_Occurred()) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1723 return -2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1724 return c < 0 ? -1 : c > 0 ? 1 : 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1725 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1726 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1727 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1728 /* The coercion didn't do anything. |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1729 Treat this the same as returning v and w unchanged. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1730 Py_INCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1731 Py_INCREF(w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1732 } |
| 15943 | 1733 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1734 if (PyInstance_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1735 c = half_cmp(v, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1736 if (c <= 1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1737 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1738 Py_DECREF(w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1739 return c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1740 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1741 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1742 if (PyInstance_Check(w)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1743 c = half_cmp(w, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1744 if (c <= 1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1745 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1746 Py_DECREF(w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1747 if (c >= -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1748 c = -c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1749 return c; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1750 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1751 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1752 Py_DECREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1753 Py_DECREF(w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1754 return 2; |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1755 } |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1756 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1757 static int |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1758 instance_nonzero(PyInstanceObject *self) |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1759 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1760 PyObject *func, *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1761 long outcome; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1762 static PyObject *nonzerostr; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1763 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1764 if (nonzerostr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1765 nonzerostr = PyString_InternFromString("__nonzero__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1766 if (nonzerostr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1767 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1768 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1769 if ((func = instance_getattr(self, nonzerostr)) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1770 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1771 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1772 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1773 if (lenstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1774 lenstr = PyString_InternFromString("__len__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1775 if (lenstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1776 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1777 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1778 if ((func = instance_getattr(self, lenstr)) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1779 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1780 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1781 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1782 /* Fall back to the default behavior: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1783 all instances are nonzero */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1784 return 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1785 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1786 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1787 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1788 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1789 if (res == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1790 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1791 if (!PyInt_Check(res)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1792 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1793 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1794 "__nonzero__ should return an int"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1795 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1796 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1797 outcome = PyInt_AsLong(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1798 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1799 if (outcome < 0) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1800 PyErr_SetString(PyExc_ValueError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1801 "__nonzero__ should return >= 0"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1802 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1803 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1804 return outcome > 0; |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1805 } |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1806 |
|
39463
6cafdc81cfde
Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39410
diff
changeset
|
1807 static PyObject * |
|
36882
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
1808 instance_index(PyInstanceObject *self) |
|
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
1809 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1810 PyObject *func, *res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1811 static PyObject *indexstr = NULL; |
|
36882
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
1812 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1813 if (indexstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1814 indexstr = PyString_InternFromString("__index__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1815 if (indexstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1816 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1817 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1818 if ((func = instance_getattr(self, indexstr)) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1819 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1820 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1821 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1822 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1823 "object cannot be interpreted as an index"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1824 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1825 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1826 res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1827 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1828 return res; |
|
36882
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
1829 } |
|
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
1830 |
|
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36675
diff
changeset
|
1831 |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
1832 UNARY(instance_invert, "__invert__") |
|
45142
f03955faffb4
Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44751
diff
changeset
|
1833 UNARY(_instance_trunc, "__trunc__") |
|
f03955faffb4
Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44751
diff
changeset
|
1834 |
|
f03955faffb4
Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44751
diff
changeset
|
1835 static PyObject * |
|
f03955faffb4
Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44751
diff
changeset
|
1836 instance_int(PyInstanceObject *self) |
|
f03955faffb4
Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44751
diff
changeset
|
1837 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1838 PyObject *truncated; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1839 static PyObject *int_name; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1840 if (int_name == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1841 int_name = PyString_InternFromString("__int__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1842 if (int_name == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1843 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1844 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1845 if (PyObject_HasAttr((PyObject*)self, int_name)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1846 return generic_unary_op(self, int_name); |
|
45142
f03955faffb4
Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44751
diff
changeset
|
1847 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1848 truncated = _instance_trunc(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1849 /* __trunc__ is specified to return an Integral type, but |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1850 int() needs to return an int. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1851 return _PyNumber_ConvertIntegralToInt( |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1852 truncated, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1853 "__trunc__ returned non-Integral (type %.200s)"); |
|
45142
f03955faffb4
Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44751
diff
changeset
|
1854 } |
|
f03955faffb4
Make int() and long() fall back to __trunc__(). See issue 2002.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44751
diff
changeset
|
1855 |
|
41634
54179538a387
As per Armin Rigo's suggestion, remove special handing from intobject.c to deal with the peculiarities of classobject's implementation of the number protocol. The nb_long method of classobject now falls back to nb_int if there is no __long__ attribute present.
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
40301
diff
changeset
|
1856 UNARY_FB(instance_long, "__long__", instance_int) |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1857 UNARY(instance_float, "__float__") |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1858 UNARY(instance_oct, "__oct__") |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1859 UNARY(instance_hex, "__hex__") |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1031
diff
changeset
|
1860 |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1861 static PyObject * |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1862 bin_power(PyObject *v, PyObject *w) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1863 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1864 return PyNumber_Power(v, w, Py_None); |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1865 } |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1866 |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1867 /* This version is for ternary calls only (z != None) */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
1868 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
1869 instance_pow(PyObject *v, PyObject *w, PyObject *z) |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1870 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1871 if (z == Py_None) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1872 return do_binop(v, w, "__pow__", "__rpow__", bin_power); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1873 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1874 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1875 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1876 PyObject *args; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1877 PyObject *result; |
|
3987
6fbc5c665812
Optimizations by Sjoerd:
Guido van Rossum <guido@python.org>
parents:
3769
diff
changeset
|
1878 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1879 /* XXX Doesn't do coercions... */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1880 func = PyObject_GetAttrString(v, "__pow__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1881 if (func == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1882 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1883 args = PyTuple_Pack(2, w, z); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1884 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1885 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1886 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1887 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1888 result = PyEval_CallObject(func, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1889 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1890 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1891 return result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1892 } |
|
1908
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1893 } |
|
0437738279a8
* Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents:
1861
diff
changeset
|
1894 |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14049
diff
changeset
|
1895 static PyObject * |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1896 bin_inplace_power(PyObject *v, PyObject *w) |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14049
diff
changeset
|
1897 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1898 return PyNumber_InPlacePower(v, w, Py_None); |
|
14161
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14049
diff
changeset
|
1899 } |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14049
diff
changeset
|
1900 |
|
52fd496d3532
Support for the in-place operations introduced by augmented assignment. Only
Thomas Wouters <thomas@python.org>
parents:
14049
diff
changeset
|
1901 |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1902 static PyObject * |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1903 instance_ipow(PyObject *v, PyObject *w, PyObject *z) |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1904 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1905 if (z == Py_None) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1906 return do_binop_inplace(v, w, "__ipow__", "__pow__", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1907 "__rpow__", bin_inplace_power); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1908 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1909 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1910 /* XXX Doesn't do coercions... */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1911 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1912 PyObject *args; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1913 PyObject *result; |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1914 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1915 func = PyObject_GetAttrString(v, "__ipow__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1916 if (func == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1917 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1918 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1919 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1920 return instance_pow(v, w, z); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1921 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1922 args = PyTuple_Pack(2, w, z); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1923 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1924 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1925 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1926 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1927 result = PyEval_CallObject(func, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1928 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1929 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1930 return result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1931 } |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1932 } |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1933 |
|
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
1934 |
| 15943 | 1935 /* Map rich comparison operators to their __xx__ namesakes */ |
|
17783
1dacd0d60637
Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17692
diff
changeset
|
1936 #define NAME_OPS 6 |
|
1dacd0d60637
Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17692
diff
changeset
|
1937 static PyObject **name_op = NULL; |
|
1dacd0d60637
Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17692
diff
changeset
|
1938 |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
24121
diff
changeset
|
1939 static int |
|
17894
14da5314664d
init_name_op(): add (void) to the argument list to make it a valid
Guido van Rossum <guido@python.org>
parents:
17783
diff
changeset
|
1940 init_name_op(void) |
|
17783
1dacd0d60637
Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17692
diff
changeset
|
1941 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1942 int i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1943 char *_name_op[] = { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1944 "__lt__", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1945 "__le__", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1946 "__eq__", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1947 "__ne__", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1948 "__gt__", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1949 "__ge__", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1950 }; |
|
17783
1dacd0d60637
Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17692
diff
changeset
|
1951 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1952 name_op = (PyObject **)malloc(sizeof(PyObject *) * NAME_OPS); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1953 if (name_op == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1954 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1955 for (i = 0; i < NAME_OPS; ++i) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1956 name_op[i] = PyString_InternFromString(_name_op[i]); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1957 if (name_op[i] == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1958 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1959 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1960 return 0; |
|
17783
1dacd0d60637
Variant of SF patch 423181
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
17692
diff
changeset
|
1961 } |
| 15943 | 1962 |
| 1963 static PyObject * | |
| 1964 half_richcompare(PyObject *v, PyObject *w, int op) | |
| 1965 { | |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1966 PyObject *method; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1967 PyObject *args; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1968 PyObject *res; |
| 15943 | 1969 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1970 assert(PyInstance_Check(v)); |
| 15943 | 1971 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1972 if (name_op == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1973 if (init_name_op() < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1974 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1975 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1976 /* If the instance doesn't define an __getattr__ method, use |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1977 instance_getattr2 directly because it will not set an |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1978 exception on failure. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1979 if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1980 method = instance_getattr2((PyInstanceObject *)v, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1981 name_op[op]); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1982 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1983 method = PyObject_GetAttr(v, name_op[op]); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1984 if (method == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1985 if (PyErr_Occurred()) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1986 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1987 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1988 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1989 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1990 res = Py_NotImplemented; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1991 Py_INCREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1992 return res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1993 } |
| 15943 | 1994 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1995 args = PyTuple_Pack(1, w); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1996 if (args == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1997 Py_DECREF(method); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1998 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
1999 } |
| 15943 | 2000 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2001 res = PyEval_CallObject(method, args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2002 Py_DECREF(args); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2003 Py_DECREF(method); |
| 15943 | 2004 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2005 return res; |
| 15943 | 2006 } |
| 2007 | |
| 2008 static PyObject * | |
| 2009 instance_richcompare(PyObject *v, PyObject *w, int op) | |
| 2010 { | |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2011 PyObject *res; |
| 15943 | 2012 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2013 if (PyInstance_Check(v)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2014 res = half_richcompare(v, w, op); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2015 if (res != Py_NotImplemented) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2016 return res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2017 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2018 } |
| 15943 | 2019 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2020 if (PyInstance_Check(w)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2021 res = half_richcompare(w, v, _Py_SwappedOp[op]); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2022 if (res != Py_NotImplemented) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2023 return res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2024 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2025 } |
| 15943 | 2026 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2027 Py_INCREF(Py_NotImplemented); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2028 return Py_NotImplemented; |
| 15943 | 2029 } |
| 2030 | |
|
15722
3e862a51861f
Make instances a new style number type. See PEP 208 for details. Instance
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
15318
diff
changeset
|
2031 |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2032 /* Get the iterator */ |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2033 static PyObject * |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2034 instance_getiter(PyInstanceObject *self) |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2035 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2036 PyObject *func; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2037 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2038 if (iterstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2039 iterstr = PyString_InternFromString("__iter__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2040 if (iterstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2041 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2042 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2043 if (getitemstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2044 getitemstr = PyString_InternFromString("__getitem__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2045 if (getitemstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2046 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2047 } |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2048 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2049 if ((func = instance_getattr(self, iterstr)) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2050 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2051 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2052 if (res != NULL && !PyIter_Check(res)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2053 PyErr_Format(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2054 "__iter__ returned non-iterator " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2055 "of type '%.100s'", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2056 res->ob_type->tp_name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2057 Py_DECREF(res); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2058 res = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2059 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2060 return res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2061 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2062 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2063 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2064 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2065 if ((func = instance_getattr(self, getitemstr)) == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2066 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2067 "iteration over non-sequence"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2068 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2069 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2070 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2071 return PySeqIter_New((PyObject *)self); |
|
17617
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2072 } |
|
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2073 |
|
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2074 |
|
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2075 /* Call the iterator's next */ |
|
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2076 static PyObject * |
|
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2077 instance_iternext(PyInstanceObject *self) |
|
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2078 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2079 PyObject *func; |
|
17617
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2080 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2081 if (nextstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2082 nextstr = PyString_InternFromString("next"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2083 if (nextstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2084 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2085 } |
|
17617
aac65f50df6f
Mondo changes to the iterator stuff, without changing how Python code
Guido van Rossum <guido@python.org>
parents:
17597
diff
changeset
|
2086 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2087 if ((func = instance_getattr(self, nextstr)) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2088 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2089 Py_DECREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2090 if (res != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2091 return res; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2092 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2093 if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2094 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2095 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2096 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2097 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2098 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2099 PyErr_SetString(PyExc_TypeError, "instance has no next() method"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2100 return NULL; |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2101 } |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2102 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2103 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2104 instance_call(PyObject *func, PyObject *arg, PyObject *kw) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2105 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2106 PyObject *res, *call = PyObject_GetAttrString(func, "__call__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2107 if (call == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2108 PyInstanceObject *inst = (PyInstanceObject*) func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2109 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2110 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2111 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2112 PyErr_Format(PyExc_AttributeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2113 "%.200s instance has no __call__ method", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2114 PyString_AsString(inst->in_class->cl_name)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2115 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2116 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2117 /* We must check and increment the recursion depth here. Scenario: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2118 class A: |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2119 pass |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2120 A.__call__ = A() # that's right |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2121 a = A() # ok |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2122 a() # infinite recursion |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2123 This bounces between instance_call() and PyObject_Call() without |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2124 ever hitting eval_frame() (which has the main recursion check). */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2125 if (Py_EnterRecursiveCall(" in __call__")) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2126 res = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2127 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2128 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2129 res = PyObject_Call(call, arg, kw); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2130 Py_LeaveRecursiveCall(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2131 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2132 Py_DECREF(call); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2133 return res; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2134 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2135 |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17170
diff
changeset
|
2136 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
2137 static PyNumberMethods instance_as_number = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2138 instance_add, /* nb_add */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2139 instance_sub, /* nb_subtract */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2140 instance_mul, /* nb_multiply */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2141 instance_div, /* nb_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2142 instance_mod, /* nb_remainder */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2143 instance_divmod, /* nb_divmod */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2144 instance_pow, /* nb_power */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2145 (unaryfunc)instance_neg, /* nb_negative */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2146 (unaryfunc)instance_pos, /* nb_positive */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2147 (unaryfunc)instance_abs, /* nb_absolute */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2148 (inquiry)instance_nonzero, /* nb_nonzero */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2149 (unaryfunc)instance_invert, /* nb_invert */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2150 instance_lshift, /* nb_lshift */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2151 instance_rshift, /* nb_rshift */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2152 instance_and, /* nb_and */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2153 instance_xor, /* nb_xor */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2154 instance_or, /* nb_or */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2155 instance_coerce, /* nb_coerce */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2156 (unaryfunc)instance_int, /* nb_int */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2157 (unaryfunc)instance_long, /* nb_long */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2158 (unaryfunc)instance_float, /* nb_float */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2159 (unaryfunc)instance_oct, /* nb_oct */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2160 (unaryfunc)instance_hex, /* nb_hex */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2161 instance_iadd, /* nb_inplace_add */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2162 instance_isub, /* nb_inplace_subtract */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2163 instance_imul, /* nb_inplace_multiply */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2164 instance_idiv, /* nb_inplace_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2165 instance_imod, /* nb_inplace_remainder */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2166 instance_ipow, /* nb_inplace_power */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2167 instance_ilshift, /* nb_inplace_lshift */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2168 instance_irshift, /* nb_inplace_rshift */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2169 instance_iand, /* nb_inplace_and */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2170 instance_ixor, /* nb_inplace_xor */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2171 instance_ior, /* nb_inplace_or */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2172 instance_floordiv, /* nb_floor_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2173 instance_truediv, /* nb_true_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2174 instance_ifloordiv, /* nb_inplace_floor_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2175 instance_itruediv, /* nb_inplace_true_divide */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2176 (unaryfunc)instance_index, /* nb_index */ |
|
944
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
2177 }; |
|
222f04f55083
Changes so that user-defined classes can implement operations invoked
Guido van Rossum <guido@python.org>
parents:
791
diff
changeset
|
2178 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
2179 PyTypeObject PyInstance_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2180 PyObject_HEAD_INIT(&PyType_Type) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2181 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2182 "instance", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2183 sizeof(PyInstanceObject), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2184 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2185 (destructor)instance_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2186 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2187 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2188 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2189 instance_compare, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2190 (reprfunc)instance_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2191 &instance_as_number, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2192 &instance_as_sequence, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2193 &instance_as_mapping, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2194 (hashfunc)instance_hash, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2195 instance_call, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2196 (reprfunc)instance_str, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2197 (getattrofunc)instance_getattr, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2198 (setattrofunc)instance_setattr, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2199 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2200 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2201 instance_doc, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2202 (traverseproc)instance_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2203 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2204 instance_richcompare, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2205 offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2206 (getiterfunc)instance_getiter, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2207 (iternextfunc)instance_iternext, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2208 0, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2209 0, /* tp_members */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2210 0, /* tp_getset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2211 0, /* tp_base */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2212 0, /* tp_dict */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2213 0, /* tp_descr_get */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2214 0, /* tp_descr_set */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2215 0, /* tp_dictoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2216 0, /* tp_init */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2217 0, /* tp_alloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2218 instance_new, /* tp_new */ |
| 6 | 2219 }; |
| 2220 | |
| 2221 | |
|
1289
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
2222 /* Instance method objects are used for two purposes: |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
2223 (a) as bound instance methods (returned by instancename.methodname) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
2224 (b) as unbound methods (returned by ClassName.methodname) |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
2225 In case (b), im_self is NULL |
|
4d292c7857d4
Access checks now work, at least for instance data (not for methods
Guido van Rossum <guido@python.org>
parents:
1288
diff
changeset
|
2226 */ |
| 6 | 2227 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
2228 PyObject * |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
2229 PyMethod_New(PyObject *func, PyObject *self, PyObject *klass) |
| 6 | 2230 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2231 register PyMethodObject *im; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2232 im = free_list; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2233 if (im != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2234 free_list = (PyMethodObject *)(im->im_self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2235 PyObject_INIT(im, &PyMethod_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2236 numfree--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2237 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2238 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2239 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2240 if (im == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2241 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2242 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2243 im->im_weakreflist = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2244 Py_INCREF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2245 im->im_func = func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2246 Py_XINCREF(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2247 im->im_self = self; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2248 Py_XINCREF(klass); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2249 im->im_class = klass; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2250 _PyObject_GC_TRACK(im); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2251 return (PyObject *)im; |
| 6 | 2252 } |
| 2253 | |
|
19956
33669c8914b4
Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents:
19808
diff
changeset
|
2254 /* Descriptors for PyMethod attributes */ |
|
33669c8914b4
Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents:
19808
diff
changeset
|
2255 |
|
33669c8914b4
Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents:
19808
diff
changeset
|
2256 /* im_class, im_func and im_self are stored in the PyMethod object */ |
| 6 | 2257 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
2258 #define OFF(x) offsetof(PyMethodObject, x) |
| 86 | 2259 |
|
20012
3faabf54b8b8
Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents:
19966
diff
changeset
|
2260 static PyMemberDef instancemethod_memberlist[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2261 {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2262 "the class associated with a method"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2263 {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2264 "the function (or other callable) implementing a method"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2265 {"__func__", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2266 "the function (or other callable) implementing a method"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2267 {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2268 "the instance to which a method is bound; None for unbound methods"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2269 {"__self__", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2270 "the instance to which a method is bound; None for unbound methods"}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2271 {NULL} /* Sentinel */ |
| 86 | 2272 }; |
| 2273 | |
|
31003
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2274 /* Christian Tismer argued convincingly that method attributes should |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2275 (nearly) always override function attributes. |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2276 The one exception is __doc__; there's a default __doc__ which |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2277 should only be used for the class, not for instances */ |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2278 |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2279 static PyObject * |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2280 instancemethod_get_doc(PyMethodObject *im, void *context) |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2281 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2282 static PyObject *docstr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2283 if (docstr == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2284 docstr= PyString_InternFromString("__doc__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2285 if (docstr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2286 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2287 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2288 return PyObject_GetAttr(im->im_func, docstr); |
|
31003
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2289 } |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2290 |
|
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2291 static PyGetSetDef instancemethod_getset[] = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2292 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2293 {0} |
|
31003
278976b19dcc
- When method objects have an attribute that can be satisfied either
Guido van Rossum <guido@python.org>
parents:
30801
diff
changeset
|
2294 }; |
|
15894
1c6b0d3a58ea
Committing PEP 232, function attribute feature, approved by Guido.
Barry Warsaw <barry@python.org>
parents:
15722
diff
changeset
|
2295 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
2296 static PyObject * |
|
19956
33669c8914b4
Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents:
19808
diff
changeset
|
2297 instancemethod_getattro(PyObject *obj, PyObject *name) |
| 86 | 2298 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2299 PyMethodObject *im = (PyMethodObject *)obj; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2300 PyTypeObject *tp = obj->ob_type; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2301 PyObject *descr = NULL; |
|
19956
33669c8914b4
Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents:
19808
diff
changeset
|
2302 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2303 if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2304 if (tp->tp_dict == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2305 if (PyType_Ready(tp) < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2306 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2307 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2308 descr = _PyType_Lookup(tp, name); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2309 } |
|
19956
33669c8914b4
Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents:
19808
diff
changeset
|
2310 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2311 if (descr != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2312 descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2313 if (f != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2314 return f(descr, obj, (PyObject *)obj->ob_type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2315 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2316 Py_INCREF(descr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2317 return descr; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2318 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2319 } |
|
19956
33669c8914b4
Redo the PyMethod attributes using a dir()-friendly approach, creating
Guido van Rossum <guido@python.org>
parents:
19808
diff
changeset
|
2320 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2321 return PyObject_GetAttr(im->im_func, name); |
| 86 | 2322 } |
| 2323 | |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2324 PyDoc_STRVAR(instancemethod_doc, |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2325 "instancemethod(function, instance, class)\n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2326 \n\ |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2327 Create an instance method object."); |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2328 |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2329 static PyObject * |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2330 instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2331 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2332 PyObject *func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2333 PyObject *self; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2334 PyObject *classObj = NULL; |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2335 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2336 if (!_PyArg_NoKeywords("instancemethod", kw)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2337 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2338 if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2339 &func, &self, &classObj)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2340 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2341 if (!PyCallable_Check(func)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2342 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2343 "first argument must be callable"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2344 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2345 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2346 if (self == Py_None) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2347 self = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2348 if (self == NULL && classObj == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2349 PyErr_SetString(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2350 "unbound methods must have non-NULL im_class"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2351 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2352 } |
| 34850 | 2353 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2354 return PyMethod_New(func, self, classObj); |
|
23868
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2355 } |
|
8e6760bf1884
SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents:
23842
diff
changeset
|
2356 |
| 6 | 2357 static void |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2358 instancemethod_dealloc(register PyMethodObject *im) |
| 6 | 2359 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2360 _PyObject_GC_UNTRACK(im); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2361 if (im->im_weakreflist != NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2362 PyObject_ClearWeakRefs((PyObject *)im); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2363 Py_DECREF(im->im_func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2364 Py_XDECREF(im->im_self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2365 Py_XDECREF(im->im_class); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2366 if (numfree < PyMethod_MAXFREELIST) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2367 im->im_self = (PyObject *)free_list; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2368 free_list = im; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2369 numfree++; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2370 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2371 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2372 PyObject_GC_Del(im); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2373 } |
| 6 | 2374 } |
| 2375 | |
|
1021
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
2376 static int |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2377 instancemethod_compare(PyMethodObject *a, PyMethodObject *b) |
|
1021
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
2378 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2379 int cmp; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2380 cmp = PyObject_Compare(a->im_func, b->im_func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2381 if (cmp) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2382 return cmp; |
|
38659
5c58fda95aa5
(arre, arigo) SF bug #1350060
Armin Rigo <arigo@tunes.org>
parents:
38485
diff
changeset
|
2383 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2384 if (a->im_self == b->im_self) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2385 return 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2386 if (a->im_self == NULL || b->im_self == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2387 return (a->im_self < b->im_self) ? -1 : 1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2388 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2389 return PyObject_Compare(a->im_self, b->im_self); |
|
1021
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
2390 } |
|
0d7c82366704
Compare instance methods by comparing the object and the function.
Guido van Rossum <guido@python.org>
parents:
956
diff
changeset
|
2391 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
2392 static PyObject * |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2393 instancemethod_repr(PyMethodObject *a) |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
2394 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2395 PyObject *self = a->im_self; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2396 PyObject *func = a->im_func; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2397 PyObject *klass = a->im_class; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2398 PyObject *funcname = NULL, *klassname = NULL, *result = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2399 char *sfuncname = "?", *sklassname = "?"; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2400 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2401 funcname = PyObject_GetAttrString(func, "__name__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2402 if (funcname == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2403 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2404 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2405 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2406 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2407 else if (!PyString_Check(funcname)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2408 Py_DECREF(funcname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2409 funcname = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2410 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2411 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2412 sfuncname = PyString_AS_STRING(funcname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2413 if (klass == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2414 klassname = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2415 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2416 klassname = PyObject_GetAttrString(klass, "__name__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2417 if (klassname == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2418 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2419 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2420 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2421 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2422 else if (!PyString_Check(klassname)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2423 Py_DECREF(klassname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2424 klassname = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2425 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2426 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2427 sklassname = PyString_AS_STRING(klassname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2428 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2429 if (self == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2430 result = PyString_FromFormat("<unbound method %s.%s>", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2431 sklassname, sfuncname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2432 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2433 /* XXX Shouldn't use repr() here! */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2434 PyObject *selfrepr = PyObject_Repr(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2435 if (selfrepr == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2436 goto fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2437 if (!PyString_Check(selfrepr)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2438 Py_DECREF(selfrepr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2439 goto fail; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2440 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2441 result = PyString_FromFormat("<bound method %s.%s of %s>", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2442 sklassname, sfuncname, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2443 PyString_AS_STRING(selfrepr)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2444 Py_DECREF(selfrepr); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2445 } |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2446 fail: |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2447 Py_XDECREF(funcname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2448 Py_XDECREF(klassname); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2449 return result; |
|
1288
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
2450 } |
|
9fed5726a36d
Several changes in one:
Guido van Rossum <guido@python.org>
parents:
1272
diff
changeset
|
2451 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
2452 static long |
|
13449
0409a98cd582
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13447
diff
changeset
|
2453 instancemethod_hash(PyMethodObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
2454 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2455 long x, y; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2456 if (a->im_self == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2457 x = PyObject_Hash(Py_None); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2458 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2459 x = PyObject_Hash(a->im_self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2460 if (x == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2461 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2462 y = PyObject_Hash(a->im_func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2463 if (y == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2464 return -1; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2465 x = x ^ y; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2466 if (x == -1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2467 x = -2; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2468 return x; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
2469 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
2470 |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2471 static int |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2472 instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg) |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2473 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2474 Py_VISIT(im->im_func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2475 Py_VISIT(im->im_self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2476 Py_VISIT(im->im_class); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2477 return 0; |
|
12990
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2478 } |
|
e615596c5c47
Round 1 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
2479 |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24184
diff
changeset
|
2480 static void |
|
37589
aeee85b6a5dd
More C++-compliance. Note especially listobject.c - to get C++ to accept the
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37304
diff
changeset
|
2481 getclassname(PyObject *klass, char *buf, int bufsize) |
|
19480
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2482 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2483 PyObject *name; |
|
19480
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2484 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2485 assert(bufsize > 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2486 strcpy(buf, "?"); /* Default outcome */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2487 if (klass == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2488 return; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2489 name = PyObject_GetAttrString(klass, "__name__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2490 if (name == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2491 /* This function cannot return an exception */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2492 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2493 return; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2494 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2495 if (PyString_Check(name)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2496 strncpy(buf, PyString_AS_STRING(name), bufsize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2497 buf[bufsize-1] = '\0'; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2498 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2499 Py_DECREF(name); |
|
19480
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2500 } |
|
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2501 |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24184
diff
changeset
|
2502 static void |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24184
diff
changeset
|
2503 getinstclassname(PyObject *inst, char *buf, int bufsize) |
|
19480
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2504 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2505 PyObject *klass; |
|
19480
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2506 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2507 if (inst == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2508 assert(bufsize > 0 && (size_t)bufsize > strlen("nothing")); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2509 strcpy(buf, "nothing"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2510 return; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2511 } |
|
19480
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2512 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2513 klass = PyObject_GetAttrString(inst, "__class__"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2514 if (klass == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2515 /* This function cannot return an exception */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2516 PyErr_Clear(); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2517 klass = (PyObject *)(inst->ob_type); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2518 Py_INCREF(klass); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2519 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2520 getclassname(klass, buf, bufsize); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2521 Py_XDECREF(klass); |
|
19480
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2522 } |
|
984ffa124606
Improve the error message issued when an unbound method is called with
Guido van Rossum <guido@python.org>
parents:
19476
diff
changeset
|
2523 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2524 static PyObject * |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2525 instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2526 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2527 PyObject *self = PyMethod_GET_SELF(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2528 PyObject *klass = PyMethod_GET_CLASS(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2529 PyObject *result; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2530 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2531 func = PyMethod_GET_FUNCTION(func); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2532 if (self == NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2533 /* Unbound methods must be called with an instance of |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2534 the class (or a derived class) as first argument */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2535 int ok; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2536 if (PyTuple_Size(arg) >= 1) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2537 self = PyTuple_GET_ITEM(arg, 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2538 if (self == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2539 ok = 0; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2540 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2541 ok = PyObject_IsInstance(self, klass); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2542 if (ok < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2543 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2544 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2545 if (!ok) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2546 char clsbuf[256]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2547 char instbuf[256]; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2548 getclassname(klass, clsbuf, sizeof(clsbuf)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2549 getinstclassname(self, instbuf, sizeof(instbuf)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2550 PyErr_Format(PyExc_TypeError, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2551 "unbound method %s%s must be called with " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2552 "%s instance as first argument " |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2553 "(got %s%s instead)", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2554 PyEval_GetFuncName(func), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2555 PyEval_GetFuncDesc(func), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2556 clsbuf, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2557 instbuf, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2558 self == NULL ? "" : " instance"); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2559 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2560 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2561 Py_INCREF(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2562 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2563 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2564 Py_ssize_t argcount = PyTuple_Size(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2565 PyObject *newarg = PyTuple_New(argcount + 1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2566 int i; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2567 if (newarg == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2568 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2569 Py_INCREF(self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2570 PyTuple_SET_ITEM(newarg, 0, self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2571 for (i = 0; i < argcount; i++) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2572 PyObject *v = PyTuple_GET_ITEM(arg, i); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2573 Py_XINCREF(v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2574 PyTuple_SET_ITEM(newarg, i+1, v); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2575 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2576 arg = newarg; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2577 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2578 result = PyObject_Call((PyObject *)func, arg, kw); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2579 Py_DECREF(arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2580 return result; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2581 } |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
17894
diff
changeset
|
2582 |
|
19280
f10b68eceefc
PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents:
19051
diff
changeset
|
2583 static PyObject * |
|
27817
bf083e35185f
Refactor instancemethod_descr_get() to (a) be more clear, (b) be safe
Guido van Rossum <guido@python.org>
parents:
26818
diff
changeset
|
2584 instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
|
19280
f10b68eceefc
PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents:
19051
diff
changeset
|
2585 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2586 /* Don't rebind an already bound method, or an unbound method |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2587 of a class that's not a base class of cls. */ |
|
27817
bf083e35185f
Refactor instancemethod_descr_get() to (a) be more clear, (b) be safe
Guido van Rossum <guido@python.org>
parents:
26818
diff
changeset
|
2588 |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2589 if (PyMethod_GET_SELF(meth) != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2590 /* Already bound */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2591 Py_INCREF(meth); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2592 return meth; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2593 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2594 /* No, it is an unbound method */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2595 if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2596 /* Do subclass test. If it fails, return meth unchanged. */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2597 int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth)); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2598 if (ok < 0) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2599 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2600 if (!ok) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2601 Py_INCREF(meth); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2602 return meth; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2603 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2604 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2605 /* Bind it to obj */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2606 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls); |
|
19280
f10b68eceefc
PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents:
19051
diff
changeset
|
2607 } |
|
f10b68eceefc
PyMethod_Type: add a tp_descr_get slot function to ensure proper
Guido van Rossum <guido@python.org>
parents:
19051
diff
changeset
|
2608 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5008
diff
changeset
|
2609 PyTypeObject PyMethod_Type = { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2610 PyObject_HEAD_INIT(&PyType_Type) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2611 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2612 "instancemethod", |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2613 sizeof(PyMethodObject), |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2614 0, |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2615 (destructor)instancemethod_dealloc, /* tp_dealloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2616 0, /* tp_print */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2617 0, /* tp_getattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2618 0, /* tp_setattr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2619 (cmpfunc)instancemethod_compare, /* tp_compare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2620 (reprfunc)instancemethod_repr, /* tp_repr */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2621 0, /* tp_as_number */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2622 0, /* tp_as_sequence */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2623 0, /* tp_as_mapping */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2624 (hashfunc)instancemethod_hash, /* tp_hash */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2625 instancemethod_call, /* tp_call */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2626 0, /* tp_str */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2627 instancemethod_getattro, /* tp_getattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2628 PyObject_GenericSetAttr, /* tp_setattro */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2629 0, /* tp_as_buffer */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2630 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2631 instancemethod_doc, /* tp_doc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2632 (traverseproc)instancemethod_traverse, /* tp_traverse */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2633 0, /* tp_clear */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2634 0, /* tp_richcompare */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2635 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2636 0, /* tp_iter */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2637 0, /* tp_iternext */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2638 0, /* tp_methods */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2639 instancemethod_memberlist, /* tp_members */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2640 instancemethod_getset, /* tp_getset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2641 0, /* tp_base */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2642 0, /* tp_dict */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2643 instancemethod_descr_get, /* tp_descr_get */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2644 0, /* tp_descr_set */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2645 0, /* tp_dictoffset */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2646 0, /* tp_init */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2647 0, /* tp_alloc */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2648 instancemethod_new, /* tp_new */ |
| 6 | 2649 }; |
|
5932
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2650 |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2651 /* Clear out the free list */ |
|
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2652 |
|
45341
65ae68c7aca3
Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
2653 int |
|
65ae68c7aca3
Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
2654 PyMethod_ClearFreeList(void) |
|
5932
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2655 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2656 int freelist_size = numfree; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2657 |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2658 while (free_list) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2659 PyMethodObject *im = free_list; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2660 free_list = (PyMethodObject *)(im->im_self); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2661 PyObject_GC_Del(im); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2662 numfree--; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2663 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2664 assert(numfree == 0); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2665 return freelist_size; |
|
5932
40cfc905c776
Added separate free list for instance method objects, for a few
Guido van Rossum <guido@python.org>
parents:
5610
diff
changeset
|
2666 } |
|
45341
65ae68c7aca3
Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
2667 |
|
65ae68c7aca3
Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
2668 void |
|
65ae68c7aca3
Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
2669 PyMethod_Fini(void) |
|
65ae68c7aca3
Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
2670 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
60112
diff
changeset
|
2671 (void)PyMethod_ClearFreeList(); |
|
45341
65ae68c7aca3
Implemented Martin's suggestion to clear the free lists during the garbage collection of the highest generation.
Christian Heimes <christian@cheimes.de>
parents:
45191
diff
changeset
|
2672 } |
